libbb: make bb_common_bufsiz1 1 kbyte, add capability to use bss tail for it
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 21 Apr 2016 14:26:30 +0000 (16:26 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 21 Apr 2016 15:39:11 +0000 (17:39 +0200)
The config item is FEATURE_USE_BSS_TAIL. When it is off (default):

function                                             old     new   delta
read_config                                          210     228     +18
doCommands                                          2279    2294     +15
ipneigh_list_or_flush                                763     772      +9
ipaddr_list_or_flush                                1256    1261      +5
display_process_list                                1301    1306      +5
conspy_main                                         1378    1383      +5
do_lzo_compress                                      352     355      +3
do_lzo_decompress                                    565     567      +2
push                                                  46      44      -2
inetd_main                                          2136    2134      -2
uevent_main                                          421     418      -3
addLines                                              97      92      -5
bb_common_bufsiz1                                   8193    1024   -7169
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/5 up/down: 62/-7181)        Total: -7119 bytes
   text    data     bss     dec     hex filename
 829850    4086    9080  843016   cdd08 busybox_old
 829901    4086    1904  835891   cc133 busybox_unstripped

FEATURE_USE_BSS_TAIL=y:

read_config                                          210     228     +18
doCommands                                          2279    2294     +15
ipneigh_list_or_flush                                763     772      +9
ipaddr_list_or_flush                                1256    1261      +5
display_process_list                                1301    1306      +5
conspy_main                                         1378    1383      +5
do_lzo_compress                                      352     355      +3
do_lzo_decompress                                    565     567      +2
inetd_main                                          2136    2134      -2
bb_common_bufsiz1                                   8193       -   -8193
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 8/1 up/down: 62/-8195)        Total: -8133 bytes
   text    data     bss     dec     hex filename
 829850    4086    9080  843016   cdd08 busybox_old
 829911    4086     880  834877   cbd3d busybox_unstripped

FIXME: setup_common_bufsiz() calls are missing.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
91 files changed:
Makefile
archival/cpio.c
archival/lzop.c
archival/rpm.c
archival/tar.c
console-tools/dumpkmap.c
console-tools/resize.c
coreutils/catv.c
coreutils/cksum.c
coreutils/date.c
coreutils/dd.c
coreutils/du.c
coreutils/expr.c
coreutils/ls.c
coreutils/od_bloaty.c
coreutils/split.c
coreutils/stat.c
coreutils/stty.c
coreutils/sum.c
coreutils/tail.c
coreutils/tee.c
debianutils/run_parts.c
debianutils/start_stop_daemon.c
e2fsprogs/fsck.c
editors/diff.c
editors/ed.c
editors/sed.c
findutils/find.c
findutils/grep.c
findutils/xargs.c
include/libbb.h
init/bootchartd.c
libbb/common_bufsiz.c [new file with mode: 0644]
libbb/messages.c
loginutils/login.c
miscutils/chat.c
miscutils/conspy.c
miscutils/crond.c
miscutils/dc.c
miscutils/fbsplash.c
miscutils/hdparm.c
miscutils/i2c_tools.c
miscutils/inotifyd.c
miscutils/less.c
miscutils/microcom.c
networking/arp.c
networking/arping.c
networking/ftpd.c
networking/ftpgetput.c
networking/httpd.c
networking/ifupdown.c
networking/inetd.c
networking/isrv_identd.c
networking/libiproute/ipaddress.c
networking/libiproute/ipneigh.c
networking/libiproute/iproute.c
networking/nc.c
networking/ping.c
networking/slattach.c
networking/tc.c
networking/tcpudp.c
networking/telnet.c
networking/telnetd.c
networking/tftp.c
networking/udhcp/common.h
networking/udhcp/dhcpd.h
networking/udhcp/dhcprelay.c
networking/udhcp/files.c
networking/zcip.c
procps/free.c
procps/fuser.c
procps/nmeter.c
procps/ps.c
procps/top.c
runit/runsv.c
runit/runsvdir.c
runit/sv.c
runit/svlogd.c
scripts/generate_BUFSIZ.sh [new file with mode: 0755]
selinux/setfiles.c
sysklogd/klogd.c
sysklogd/logread.c
sysklogd/syslogd_and_logger.c
util-linux/mdev.c
util-linux/mkswap.c
util-linux/more.c
util-linux/mount.c
util-linux/script.c
util-linux/swaponoff.c
util-linux/uevent.c
util-linux/umount.c

index dab807805457dcb9b2c532ea658c093449c09f12..75a33c1fb51a66bc411f38e2baa2955f2f8ec12f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -610,7 +610,8 @@ quiet_cmd_busybox__ ?= LINK    $@
       "$(LDFLAGS) $(EXTRA_LDFLAGS)" \
       "$(core-y)" \
       "$(libs-y)" \
-      "$(LDLIBS)"
+      "$(LDLIBS)" \
+      && $(srctree)/scripts/generate_BUFSIZ.sh include/common_bufsiz.h
 
 # Generate System.map
 quiet_cmd_sysmap = SYSMAP
@@ -844,12 +845,15 @@ export CPPFLAGS_busybox.lds += -P -C -U$(ARCH)
 #      Split autoconf.h into include/linux/config/*
 quiet_cmd_gen_bbconfigopts = GEN     include/bbconfigopts.h
       cmd_gen_bbconfigopts = $(srctree)/scripts/mkconfigs include/bbconfigopts.h include/bbconfigopts_bz2.h
+quiet_cmd_gen_common_bufsiz = GEN     include/common_bufsiz.h
+      cmd_gen_common_bufsiz = $(srctree)/scripts/generate_BUFSIZ.sh include/common_bufsiz.h
 quiet_cmd_split_autoconf   = SPLIT   include/autoconf.h -> include/config/*
       cmd_split_autoconf   = scripts/basic/split-include include/autoconf.h include/config
 #bbox# piggybacked generation of few .h files
 include/config/MARKER: scripts/basic/split-include include/autoconf.h
        $(call cmd,split_autoconf)
        $(call cmd,gen_bbconfigopts)
+       $(call cmd,gen_common_bufsiz)
        @touch $@
 
 # Generate some files
@@ -965,6 +969,7 @@ CLEAN_FILES +=      busybox busybox_unstripped* busybox.links \
 MRPROPER_DIRS  += include/config include2
 MRPROPER_FILES += .config .config.old include/asm .version .old_version \
                  include/NUM_APPLETS.h \
+                 include/common_bufsiz.h \
                  include/autoconf.h \
                  include/bbconfigopts.h \
                  include/bbconfigopts_bz2.h \
index 82b3fe5ed99668118b6c9781a110cb6ebf961478..a3036e1ab3c2dbbc0e2790a4279dd0b31f862b63 100644 (file)
@@ -11,6 +11,7 @@
  * Only supports new ASCII and CRC formats
  */
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "bb_archive.h"
 
 //config:config CPIO
@@ -170,7 +171,7 @@ enum {
 struct globals {
        struct bb_uidgid_t owner_ugid;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 void BUG_cpio_globals_too_big(void);
 #define INIT_G() do { \
        G.owner_ugid.uid = -1L; \
index a5fc01941f56f2ac9babbc65cb81cda8d0847dd2..1371c97519f7abb15fc1981d6b58df9c9316abfa 100644 (file)
@@ -71,6 +71,7 @@
 //usage:     "\n       -F      Don't store or verify checksum"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "bb_archive.h"
 #include "liblzo_interface.h"
 
@@ -443,7 +444,7 @@ struct globals {
        chksum_t chksum_in;
        chksum_t chksum_out;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 //#define G (*ptr_to_globals)
 //#define INIT_G() do {
index 105394481d276315bc1fcbc76b8348ce34db559f..079b7a95b7ded2845441220eaab2908751739e0e 100644 (file)
@@ -29,6 +29,7 @@
 //usage:     "\n       -qpc    List config files"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "bb_archive.h"
 #include "rpm.h"
 
@@ -93,7 +94,7 @@ struct globals {
        rpm_index **mytags;
        int tagcount;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 static void extract_cpio(int fd, const char *source_rpm)
index 23ac00e866f97da6efb2c3cfcac611cd477efb00..caf4363dea69f63cae561d5623c5890f65160061 100644 (file)
 
 #include <fnmatch.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "bb_archive.h"
 /* FIXME: Stop using this non-standard feature */
 #ifndef FNM_LEADING_DIR
index bf8d690daa87da922dede1d01965b5a29154cdf8..6412dffc888bedd87c402c276f30ed59232dea01 100644 (file)
@@ -18,6 +18,7 @@
 //usage:       "$ dumpkmap > keymap\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* From <linux/kd.h> */
 struct kbentry {
index 4b0d63a03c8086a802e8d889aeeeb38b549f601f..ed80aa082c87ada9708bad936711b70c2eda56b1 100644 (file)
 //usage:       "Resize the screen"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #define ESC "\033"
 
-#define old_termios_p ((struct termios*)&bb_common_bufsiz1)
+#define old_termios_p ((struct termios*)bb_common_bufsiz1)
 
 static void
 onintr(int sig UNUSED_PARAM)
index 6bb73ba63d5e8341113fafbddf5a7d0b8b13761b..801d2451d4ff410df2a1484803a04e6651352c96 100644 (file)
@@ -19,6 +19,7 @@
 //usage:     "\n       -v      Don't use ^x or M-x escapes"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #define CATV_OPT_e (1<<0)
 #define CATV_OPT_t (1<<1)
index ac0b0c319ec1a9b39fee2db7b76a608ba6aebde9..d8351e7c6022bf9a87dfc7523985c87c0f6c5d3d 100644 (file)
@@ -13,6 +13,7 @@
 //usage:       "Calculate the CRC32 checksums of FILES"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* This is a NOEXEC applet. Be very careful! */
 
@@ -42,8 +43,9 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
                crc = 0;
                length = 0;
 
-#define read_buf bb_common_bufsiz1
-               while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
+#define        read_buf bb_common_bufsiz1
+#define sizeof_read_buf COMMON_BUFSIZE
+               while ((bytes_read = safe_read(fd, read_buf, sizeof_read_buf)) > 0) {
                        length += bytes_read;
                        crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
                }
index 7965775fefc8f4cccfa3355d05728600fc9bb4b9..59b4b8f2a1172bb450da08a36e85f390a8b96200 100644 (file)
 //usage:       "Wed Apr 12 18:52:41 MDT 2000\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #if ENABLE_FEATURE_DATE_NANO
 # include <sys/syscall.h>
 #endif
@@ -367,7 +368,8 @@ int date_main(int argc UNUSED_PARAM, char **argv)
        }
 #endif
 
-#define date_buf bb_common_bufsiz1
+#define        date_buf bb_common_bufsiz1
+#define sizeof_date_buf COMMON_BUFSIZE
        if (*fmt_dt2str == '\0') {
                /* With no format string, just print a blank line */
                date_buf[0] = '\0';
@@ -377,7 +379,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
                        fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
                }
                /* Generate output string */
-               strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time);
+               strftime(date_buf, sizeof_date_buf, fmt_dt2str, &tm_time);
        }
        puts(date_buf);
 
index 0c0ea07b95bc5bcc0c943c1a1962cc80a5fbc1d0..a5b8882a0fb11caf194f407fe589f9661b2349bd 100644 (file)
@@ -91,6 +91,7 @@
 //usage:       "4+0 records out\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* This is a NOEXEC applet. Be very careful! */
 
@@ -108,7 +109,7 @@ struct globals {
 #endif
        int flags;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        /* we have to zero it out because of NOEXEC */ \
        memset(&G, 0, sizeof(G)); \
index 1889c16bb13c0861bd82b6e46b428610a1245128..3d6777670ac84d4d7a924a16a21fa464ec7beb9a 100644 (file)
@@ -58,6 +58,7 @@
 //usage:       "2417    .\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 enum {
        OPT_a_files_too    = (1 << 0),
@@ -85,7 +86,7 @@ struct globals {
        int du_depth;
        dev_t dir_dev;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 
index c986f93274f7e57e4070b135958c10e97df744ce..59a66d9c517da0a6ca98b4540df2c7179db4c773 100644 (file)
@@ -61,6 +61,7 @@
 //usage:       "of characters matched or 0."
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 #if ENABLE_EXPR_MATH_SUPPORT_64
@@ -99,7 +100,7 @@ typedef struct valinfo VALUE;
 struct globals {
        char **args;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 /* forward declarations */
index 20bd6186053b41aabb075e3a3dda26eb90559104..e8c3e0490ef1f767e10b3341052606825634fd46 100644 (file)
@@ -93,6 +93,7 @@
 //usage:       )
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "unicode.h"
 
 
@@ -365,7 +366,7 @@ struct globals {
        time_t current_time_t;
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        /* we have to zero it out because of NOEXEC */ \
        memset(&G, 0, sizeof(G)); \
index f47f84b545afaf0c9d742769cc9ea1f0ec44c459..1e252caf097241b6745a8c466a36a9525df56f8b 100644 (file)
@@ -20,6 +20,7 @@
 
 
 /* #include "libbb.h" - done in od.c */
+#include "common_bufsiz.h"
 #define assert(a) ((void)0)
 
 
@@ -214,7 +215,7 @@ struct globals {
 #if !ENABLE_LONG_OPTS
 enum { G_pseudo_offset = 0 };
 #endif
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
        G.bytes_per_block = 32; \
index 1e1673efbe2c4b9fa51611e13c25752488ad90c4..b2da74e2789dd3cff5629fd85b93450715642bc6 100644 (file)
@@ -22,6 +22,7 @@
 //usage:       "$ cat TODO | split -a 2 -l 2 TODO_\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #if ENABLE_FEATURE_SPLIT_FANCY
 static const struct suffix_mult split_suffixes[] = {
index 1a490fef74b7276181a2e3d3320f7858b422c735..78df9c94840fe4ab2e0004205a8eb68972775e58 100644 (file)
 //usage:       )
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 enum {
        OPT_TERSE       = (1 << 0),
@@ -157,9 +158,10 @@ static const char *human_time(time_t t)
        /* coreutils 6.3 compat: */
 
        /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
-#define buf bb_common_bufsiz1
+#define        buf bb_common_bufsiz1
+#define sizeof_buf COMMON_BUFSIZE
 
-       strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000");
+       strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof_buf, &t), ".000000000");
        return buf;
 #undef buf
 }
index b63b0b91af7bce562f8d47b753129234583e9937..0e32fc8985b9da6d9f687d7f9bb1946b243246ed 100644 (file)
@@ -32,6 +32,7 @@
 //usage:     "\n       [SETTING]       See manpage"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #ifndef _POSIX_VDISABLE
 # define _POSIX_VDISABLE ((unsigned char) 0)
@@ -775,7 +776,7 @@ struct globals {
        unsigned current_col;
        char buf[10];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        G.device_name = bb_msg_standard_input; \
        G.max_col = 80; \
index deb068e1024e7501d0db6e5e130cb2402ecf4221..cc6677221a2068f20416b2144a90e4d291d2a8da 100644 (file)
@@ -21,6 +21,7 @@
 //usage:     "\n       -s      Use System V sum algorithm (512byte blocks)"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
 
@@ -41,7 +42,7 @@ static unsigned sum_file(const char *file, unsigned type)
                return 0;
 
        while (1) {
-               size_t bytes_read = safe_read(fd, buf, BUFSIZ);
+               size_t bytes_read = safe_read(fd, buf, COMMON_BUFSIZE);
 
                if ((ssize_t)bytes_read <= 0) {
                        r = (fd && close(fd) != 0);
index e352ab627b2e86b9e45c6ae59b1b17e4b0dd6ff1..cdc9fb66a6bf5bd05497543860887ef597181c6e 100644 (file)
 //usage:       "nameserver 10.0.0.1\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 struct globals {
        bool from_top;
        bool exitcode;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 static void tail_xprint_header(const char *fmt, const char *filename)
index 48cc0508fced0d498a6b09fadcd54fa14adf4836..a0e177cbcff9f413016e2e1a4806b3cf4ea86d90 100644 (file)
@@ -23,6 +23,7 @@
 //usage:       "Hello\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int tee_main(int argc, char **argv)
@@ -36,7 +37,8 @@ int tee_main(int argc, char **argv)
 //TODO: make unconditional
 #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
        ssize_t c;
-# define buf bb_common_bufsiz1
+# define        buf bb_common_bufsiz1
+# define sizeof_buf COMMON_BUFSIZE
 #else
        int c;
 #endif
@@ -79,7 +81,7 @@ int tee_main(int argc, char **argv)
        /* names[0] will be filled later */
 
 #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
-       while ((c = safe_read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
+       while ((c = safe_read(STDIN_FILENO, buf, sizeof_buf)) > 0) {
                fp = files;
                do
                        fwrite(buf, 1, c, *fp);
index 13617c6e16195022443a43211e38e4946926a5ca..a5e53576cb7363cfaab59df78e1eea038984370a 100644 (file)
 //usage:       "+ shutdown -h +4m"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 struct globals {
        char **names;
        int    cur;
        char  *cmd[2 /* using 1 provokes compiler warning */];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define names (G.names)
 #define cur   (G.cur  )
 #define cmd   (G.cmd  )
index d7c730f453500b7948333d26811c84a5633503ee..6b8d53b134e6b21c226ec146ddb22af619ae9be5 100644 (file)
@@ -153,6 +153,7 @@ Misc options:
 /* Override ENABLE_FEATURE_PIDFILE */
 #define WANT_PIDFILE 1
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 struct pid_list {
        struct pid_list *next;
@@ -191,7 +192,7 @@ struct globals {
        int user_id;
        smallint signal_nr;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define userspec          (G.userspec            )
 #define cmdname           (G.cmdname             )
 #define execname          (G.execname            )
index 987d9752846d50f7ed824119f8b0ddd8b01bb802..b534568c2ffd27beefffb9461b8b45985860f8bc 100644 (file)
@@ -60,6 +60,7 @@
 //usage:     "\n       -t TYPE List of filesystem types to check"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* "progress indicator" code is somewhat buggy and ext[23] specific.
  * We should be filesystem agnostic. IOW: there should be a well-defined
@@ -169,7 +170,7 @@ struct globals {
        struct fs_info *filesys_last;
        struct fsck_instance *instance_list;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
index a892cfdf223615e381de247d9bceb41c4aef3694..3c8e9074a649d3e7993a10f6460064c2214d50d0 100644 (file)
 //usage:     "\n       -w      Ignore all whitespace"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #if 0
 # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
index a4c41909937ba732e8d2a3508d8d7423c9a7f7d4..8da7b1dd5b05cc762803794ed7468265bca766f8 100644 (file)
@@ -23,6 +23,7 @@
 //usage:#define ed_full_usage ""
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 typedef struct LINE {
        struct LINE *next;
@@ -32,11 +33,12 @@ typedef struct LINE {
 } LINE;
 
 
-#define searchString bb_common_bufsiz1
+#define        searchString bb_common_bufsiz1
+#define sizeof_searchString COMMON_BUFSIZE
 
 enum {
-       USERSIZE = sizeof(searchString) > 1024 ? 1024
-                : sizeof(searchString) - 1, /* max line length typed in by user */
+       USERSIZE = sizeof_searchString > 1024 ? 1024
+                : sizeof_searchString - 1, /* max line length typed in by user */
        INITBUF_SIZE = 1024, /* initial buffer size */
 };
 
index 9c4c8e1486f78a525b0162c1edf9c2de931318a5..330190e78db64cdaa3e482f5bb59e2615d41f766 100644 (file)
@@ -86,6 +86,7 @@
 //usage:       "bar\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 #if 0
@@ -161,7 +162,7 @@ struct globals {
                int len;    /* Space allocated */
        } pipeline;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
        G.sed_cmd_tail = &G.sed_cmd_head; \
index a0d4853defb730e86f384210239b871a15eb0864..32d830337f1d4040e27e181dbe71954aee07f5c7 100644 (file)
 
 #include <fnmatch.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #if ENABLE_FEATURE_FIND_REGEX
 # include "xregex.h"
 #endif
@@ -421,7 +422,7 @@ struct globals {
        recurse_flags_t recurse_flags;
        IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
        /* we have to zero it out because of NOEXEC */ \
index dece90c580234deca1adf71b7f9a92f6a76addde..a669ac80bb714af852d03651a7e90df107969fe6 100644 (file)
@@ -58,6 +58,7 @@
 //config:        Print the specified number of context lines (-C).
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 
@@ -201,7 +202,7 @@ struct globals {
        llist_t *pattern_head;   /* growable list of patterns to match */
        const char *cur_file;    /* the current file we are reading */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
index 69f83b128969b1f5bbf9a207bccc327682d982ec..bfbd949606b5a1202135e6efe0d22584d432b129 100644 (file)
@@ -66,6 +66,7 @@
 //kbuild:lib-$(CONFIG_XARGS) += xargs.o
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* This is a NOEXEC applet. Be very careful! */
 
@@ -100,7 +101,7 @@ struct globals {
        const char *eof_str;
        int idx;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
        IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
index 111dd66e0dd5186307e01fc1f653b3fd26c82061..fd40ef74c04de7d349fd87cef9509c40e562c6c6 100644 (file)
@@ -1827,10 +1827,6 @@ extern const char bb_PATH_root_path[] ALIGN1; /* "PATH=/sbin:/usr/sbin:/bin:/usr
 extern const int const_int_0;
 //extern const int const_int_1;
 
-
-/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
-enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
-extern char bb_common_bufsiz1[COMMON_BUFSIZE];
 /* This struct is deliberately not defined. */
 /* See docs/keep_data_small.txt */
 struct globals;
index c7388c99e3d82bbfdb3ac36473362bc5b6a2bf9f..5101b28aeba57f697158a801d12dae44bfb64a51 100644 (file)
@@ -47,6 +47,7 @@
 //config:        and /etc/bootchartd.conf files.
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 /* After libbb.h, since it needs sys/types.h on some systems */
 #include <sys/utsname.h>
 
 struct globals {
        char jiffy_line[COMMON_BUFSIZE];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 static void dump_file(FILE *fp, const char *filename)
diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c
new file mode 100644 (file)
index 0000000..c16c361
--- /dev/null
@@ -0,0 +1,74 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 2016 Denys Vlasenko
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+//config:config FEATURE_USE_BSS_TAIL
+//config:      bool "Use the end of BSS page"
+//config:      default n
+//config:      help
+//config:        Attempt to reclaim a small unused part of BSS.
+//config:
+//config:        Executables have the following parts:
+//config:        = read-only executable code and constants, also known as "text"
+//config:        = read-write data
+//config:        = non-initialized (zeroed on demand) data, also known as "bss"
+//config:
+//config:        At link time, "text" is padded to a full page. At runtime, all "text"
+//config:        pages are mapped RO and executable.
+//config:        "Data" starts on the next page boundary, but is not padded
+//config:        to a full page at the end. "Bss" starts wherever "data" ends.
+//config:        At runtime, "data" pages are mapped RW and they are file-backed
+//config:        (this includes a small portion of "bss" which may live in the last
+//config:        partial page of "data").
+//config:        Pages which are fully in "bss" are mapped to anonymous memory.
+//config:
+//config:        "Bss" end is usually not page-aligned. There is an unused space
+//config:        in the last page. Linker marks its start with the "_end" symbol.
+//config:
+//config:        This option will attempt to use that space for bb_common_bufsiz1[]
+//config:        array. If it fits after _end, it will be used, and COMMON_BUFSIZE
+//config:        will be enlarged from its guaranteed minimum size of 1 kbyte.
+//config:        This may require recompilation a second time, since value of _end
+//config:        is known only after final link.
+//config:
+//config:        If you are getting a build error like this:
+//config:              appletlib.c:(.text.main+0xd): undefined reference to '_end'
+//config:        disable this option.
+
+//kbuild:lib-y += common_bufsiz.o
+
+#include "libbb.h"
+#include "common_bufsiz.h"
+
+#if !ENABLE_FEATURE_USE_BSS_TAIL
+
+/* We use it for "global" data via *(struct global*)bb_common_bufsiz1.
+ * Since gcc insists on aligning struct global's members, it would be a pity
+ * (and an alignment fault on some CPUs) to mess it up. */
+char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
+
+#else
+
+# ifndef setup_common_bufsiz
+/*
+ * It is not a "((void)0)" macro. It means we have to provide this function.
+ */
+char* bb_common_bufsiz1;
+char* setup_common_bufsiz(void)
+{
+       if (!bb_common_bufsiz1)
+               bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE);
+       return bb_common_bufsiz1;
+}
+# else
+#  ifndef bb_common_bufsiz1
+   /* bb_common_bufsiz1[] is not aliased to _end[] */
+char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
+#  endif
+# endif
+
+#endif
index 23e440bcdf0ef81e81524a41714fce1499b29014..cb0836de88d10d8d1225ca29a8339a856efe4447 100644 (file)
@@ -59,8 +59,3 @@ const char bb_path_wtmp_file[] ALIGN1 =
 #  error unknown path to wtmp file
 # endif
 #endif
-
-/* We use it for "global" data via *(struct global*)&bb_common_bufsiz1.
- * Since gcc insists on aligning struct global's members, it would be a pity
- * (and an alignment fault on some CPUs) to mess it up. */
-char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
index 4ebc185027f41662eabcb4767ad26d0e1e6cd44b..ea7c5a23d4535b61fe2fe4f1814fb7a0fea0b4cd 100644 (file)
@@ -62,6 +62,7 @@
 //usage:     "\n       -p      Preserve environment"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 #include <sys/resource.h>
 
@@ -138,7 +139,7 @@ enum {
 struct globals {
        struct termios tty_attrs;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 
index bd2abc24a687f3443b36d06b893f45940b27dd8d..25850dd20dcf6b752f641f8f54993cb8b224b836 100644 (file)
@@ -17,6 +17,7 @@
 //usage:       "chat '' ATZ OK ATD123456 CONNECT '' ogin: pppuser word: ppppass '~'"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 // default timeout: 45 sec
 #define DEFAULT_CHAT_TIMEOUT 45*1000
index 1a46a4340a2b65e9b5bef62e8f3c6f734587d9e9..0d96a5f9a4037f800f71da0f7f9799859c974dfc 100644 (file)
@@ -42,6 +42,7 @@
 //usage:     "\n       -y LINE Starting line"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <sys/kd.h>
 
 #define ESC "\033"
@@ -363,7 +364,8 @@ int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int conspy_main(int argc UNUSED_PARAM, char **argv)
 {
        char tty_name[sizeof(DEV_TTY "NN")];
-#define keybuf bb_common_bufsiz1
+#define        keybuf bb_common_bufsiz1
+#define sizeof_keybuf COMMON_BUFSIZE
        struct termios termbuf;
        unsigned opts;
        unsigned ttynum;
@@ -513,7 +515,7 @@ int conspy_main(int argc UNUSED_PARAM, char **argv)
                default:
                        // Read the keys pressed
                        k = keybuf + G.key_count;
-                       bytes_read = read(G.kbd_fd, k, sizeof(keybuf) - G.key_count);
+                       bytes_read = read(G.kbd_fd, k, sizeof_keybuf - G.key_count);
                        if (bytes_read < 0)
                                goto abort;
 
index eb327f8555692855d3b02f80c7110514a61fd7c7..8536d43c592cff3bd4022b9a1bf01ae3c01a43de 100644 (file)
@@ -60,6 +60,7 @@
 //usage:     "\n       -c DIR  Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 
 /* glibc frees previous setenv'ed value when we do next setenv()
@@ -140,7 +141,7 @@ struct globals {
        char *env_var_logname;
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        G.log_level = 8; \
        G.crontab_dir_name = CRONTABS; \
index 9c74172ba4f41dafef23b562ae527e28008bb2b4..3fbb89f5b88960819fba39a144cfa7cad4c13bbe 100644 (file)
@@ -4,6 +4,7 @@
  */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <math.h>
 
 //usage:#define dc_trivial_usage
@@ -47,7 +48,7 @@ struct globals {
        double stack[1];
 } FIX_ALIASING;
 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define pointer   (G.pointer   )
 #define base      (G.base      )
 #define stack     (G.stack     )
index 9557c41dba6525fc10bfb733e90c5787f17fc502..b26ad2c1566a6ae789d10afbaebe22bb7af1319b 100644 (file)
@@ -34,6 +34,7 @@
 //usage:     "\n                       commands: 'NN' (% for progress bar) or 'exit'"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <linux/fb.h>
 
 /* If you want logging messages on /tmp/fbsplash.log... */
@@ -372,11 +373,12 @@ static void fb_drawimage(void)
         * - A raster of Width * Height pixels in triplets of rgb
         *   in pure binary by 1 or 2 bytes. (we support only 1 byte)
         */
-#define concat_buf bb_common_bufsiz1
+#define        concat_buf bb_common_bufsiz1
+#define sizeof_concat_buf COMMON_BUFSIZE
        read_ptr = concat_buf;
        while (1) {
                int w, h, max_color_val;
-               int rem = concat_buf + sizeof(concat_buf) - read_ptr;
+               int rem = concat_buf + sizeof_concat_buf - read_ptr;
                if (rem < 2
                 || fgets(read_ptr, rem, theme_file) == NULL
                ) {
index 8e201ac358573372ddc47ebe49a5cd92274a23b1..9e141de2f110be75262ffd369d0aaf03c3c3ad5c 100644 (file)
@@ -63,6 +63,7 @@
 //usage:     "\n       -z      Reread partition table"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 /* must be _after_ libbb.h: */
 #include <linux/hdreg.h>
 #include <sys/mount.h>
@@ -367,7 +368,7 @@ struct globals {
        unsigned char flushcache[4] = { WIN_FLUSHCACHE, 0, 0, 0 };
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define get_identity       (G.get_identity           )
 #define get_geom           (G.get_geom               )
 #define do_flush           (G.do_flush               )
index aa1c7c5ccdd95a51c03e4c75da15186797d1b30d..57bb72ae7e99288734b13891d2ff2adbd0416126 100644 (file)
@@ -60,6 +60,7 @@
  */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #include <linux/i2c.h>
 #include <linux/i2c-dev.h>
index 908d657fd2f01b11163e77154971beda81c5dcfb..1d28e8f992d576ed3f8b2f134e17d767c39d2333 100644 (file)
@@ -56,6 +56,7 @@
 //usage:     "\nWhen x event happens for all FILEs, inotifyd exits."
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <sys/inotify.h>
 
 static const char mask_names[] ALIGN1 =
@@ -162,8 +163,9 @@ int inotifyd_main(int argc, char **argv)
                // read out all pending events
                // (NB: len must be int, not ssize_t or long!)
                xioctl(pfd.fd, FIONREAD, &len);
-#define eventbuf bb_common_bufsiz1
-               ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
+#define        eventbuf bb_common_bufsiz1
+#define sizeof_eventbuf COMMON_BUFSIZE
+               ie = buf = (len <= sizeof_eventbuf) ? eventbuf : xmalloc(len);
                len = full_read(pfd.fd, buf, len);
                // process events. N.B. events may vary in length
                while (len > 0) {
index ccdb15fdcaf4066f611afcb5f9104826f3cbb75a..94ecf1686c0e70b1a0e7f704f18c8f2552dd3fe3 100644 (file)
 #include <sched.h>  /* sched_yield() */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #if ENABLE_FEATURE_LESS_REGEXP
 #include "xregex.h"
 #endif
@@ -439,7 +440,8 @@ static int at_end(void)
  */
 static void read_lines(void)
 {
-#define readbuf bb_common_bufsiz1
+#define        readbuf bb_common_bufsiz1
+#define sizeof_readbuf COMMON_BUFSIZE
        char *current_line, *p;
        int w = width;
        char last_terminated = terminated;
@@ -480,7 +482,7 @@ static void read_lines(void)
                                        time_t t;
 
                                        errno = 0;
-                                       eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
+                                       eof_error = safe_read(STDIN_FILENO, readbuf, sizeof_readbuf);
                                        if (errno != EAGAIN)
                                                break;
                                        t = time(NULL);
index 5e29a1acdd483ba51e4382807c24685f8aa1b577..5eb2e6743e07ffa912181fcb54c8f1046aad4f86 100644 (file)
@@ -19,6 +19,7 @@
 //usage:     "\n       -X      Disable special meaning of NUL and Ctrl-X from stdin"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 // set raw tty mode
 static void xget1(int fd, struct termios *t, struct termios *oldt)
@@ -155,10 +156,11 @@ int microcom_main(int argc UNUSED_PARAM, char **argv)
 skip_write: ;
                }
                if (pfd[0].revents) {
-#define iobuf bb_common_bufsiz1
+#define        iobuf bb_common_bufsiz1
+#define sizeof_iobuf COMMON_BUFSIZE
                        ssize_t len;
                        // read from device -> write to stdout
-                       len = safe_read(sfd, iobuf, sizeof(iobuf));
+                       len = safe_read(sfd, iobuf, sizeof_iobuf);
                        if (len > 0)
                                full_write(STDOUT_FILENO, iobuf, len);
                        else {
index 0099aa534b7bce794a08060ac369dcf701625471..5f7818663a96a866298f35eb2f8628e2a0bb0f51 100644 (file)
@@ -32,6 +32,7 @@
 //usage:       "\n     -H HWTYPE       Hardware address type"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "inet_common.h"
 
 #include <arpa/inet.h>
@@ -69,7 +70,7 @@ struct globals {
        const char *device;      /* current device */
        smallint hw_set;         /* flag if hw-type was set (-H) */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define ap         (G.ap        )
 #define hw         (G.hw        )
 #define device     (G.device    )
index ef205e5e64b678010a891574b1ba1c854bb13dfb..52f5ba51fbd6c991f85578a67c066d14251bb1c3 100644 (file)
@@ -28,6 +28,7 @@
 #include <netpacket/packet.h>
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* We don't expect to see 1000+ seconds delay, unsigned is enough */
 #define MONOTONIC_US() ((unsigned)monotonic_us())
@@ -60,7 +61,7 @@ struct globals {
        unsigned brd_recv;
        unsigned req_recv;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define src        (G.src       )
 #define dst        (G.dst       )
 #define me         (G.me        )
index 8345ae67c112ad3c4eb874d2e0af8541a73dc6e0..8553a28f59d54983cec81e1658e54a5d464adb64 100644 (file)
@@ -29,6 +29,7 @@
 //usage:     "\n       DIR     Change root to this directory"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 #include <netinet/tcp.h>
 
@@ -123,7 +124,7 @@ struct globals {
        char msg_ok [(sizeof("NNN " MSG_OK ) + 3) & 0xfffc];
        char msg_err[(sizeof("NNN " MSG_ERR) + 3) & 0xfffc];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        /* Moved to main */ \
        /*strcpy(G.msg_ok  + 4, MSG_OK );*/ \
index b398bc8742f79f534ef219052994571bf5390e30..61bc45c4e3c919b7aa336aa6628f8537639f9162 100644 (file)
@@ -50,6 +50,7 @@
 //usage:       )
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 struct globals {
        const char *user;
@@ -60,7 +61,7 @@ struct globals {
        int do_continue;
        char buf[4]; /* actually [BUFSZ] */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
 #define user           (G.user          )
 #define password       (G.password      )
index ed15fd8834ed9fff43679ff6ddc2e1310080771b..ef90770acfcba4b20cfe3c79e4274c81635c8088 100644 (file)
 //usage:     "\n       -d STRING       URL decode STRING"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #if ENABLE_PAM
 /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
 # undef setlocale
@@ -307,7 +308,8 @@ struct globals {
        Htaccess *script_i;     /* config script interpreters */
 #endif
        char *iobuf;            /* [IOBUF_SIZE] */
-#define hdr_buf bb_common_bufsiz1
+#define        hdr_buf bb_common_bufsiz1
+#define sizeof_hdr_buf COMMON_BUFSIZE
        char *hdr_ptr;
        int hdr_cnt;
 #if ENABLE_FEATURE_HTTPD_ERROR_PAGES
@@ -1066,7 +1068,7 @@ static int get_line(void)
        alarm(HEADER_READ_TIMEOUT);
        while (1) {
                if (hdr_cnt <= 0) {
-                       hdr_cnt = safe_read(STDIN_FILENO, hdr_buf, sizeof(hdr_buf));
+                       hdr_cnt = safe_read(STDIN_FILENO, hdr_buf, sizeof_hdr_buf);
                        if (hdr_cnt <= 0)
                                break;
                        hdr_ptr = hdr_buf;
@@ -1191,9 +1193,9 @@ static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post
                        /* We expect data, prev data portion is eaten by CGI
                         * and there *is* data to read from the peer
                         * (POSTDATA) */
-                       //count = post_len > (int)sizeof(hdr_buf) ? (int)sizeof(hdr_buf) : post_len;
+                       //count = post_len > (int)sizeof_hdr_buf ? (int)sizeof_hdr_buf : post_len;
                        //count = safe_read(STDIN_FILENO, hdr_buf, count);
-                       count = safe_read(STDIN_FILENO, hdr_buf, sizeof(hdr_buf));
+                       count = safe_read(STDIN_FILENO, hdr_buf, sizeof_hdr_buf);
                        if (count > 0) {
                                hdr_cnt = count;
                                hdr_ptr = hdr_buf;
index 2c6db926f1ac9c9fc06fbccff756faaea3186e54..399ff6b5d080d694408e15447211f24119b7fe5c 100644 (file)
@@ -44,6 +44,7 @@
 //usage:     "\n       -f      Force de/configuration"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 /* After libbb.h, since it needs sys/types.h on some systems */
 #include <sys/utsname.h>
 #include <fnmatch.h>
@@ -129,7 +130,7 @@ struct globals {
        const char *startup_PATH;
        char *shell;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 
index 4f6673b1271b06fb8a3cdb3b21bc6de4bcb36610..aa35ffa2b879414634bb145a246ac10168b68ac5 100644 (file)
 #include <sys/un.h>
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #if ENABLE_FEATURE_INETD_RPC
 # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
@@ -327,7 +328,7 @@ struct globals {
        /* Used in next_line(), and as scratch read buffer */
        char line[256];          /* _at least_ 256, see LINE_SIZE */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) };
 #define rlim_ofile_cur  (G.rlim_ofile_cur )
 #define rlim_ofile      (G.rlim_ofile     )
index 252c8aba9b7ab879392e9499834ecbc197f18249..f63ed8ee4012264ce9e8485d4a4c1b4acb016579 100644 (file)
@@ -18,6 +18,7 @@
 //usage:     "\n       STRING  Ident answer string (default: nobody)"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 #include "isrv.h"
 
@@ -28,7 +29,8 @@ typedef struct identd_buf_t {
        char buf[64 - sizeof(int)];
 } identd_buf_t;
 
-#define bogouser bb_common_bufsiz1
+#define        bogouser bb_common_bufsiz1
+#define sizeof_bogouser COMMON_BUFSIZE
 
 static int new_peer(isrv_state_t *state, int fd)
 {
@@ -118,7 +120,7 @@ int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
        opt = getopt32(argv, "fiwb:", &bind_address);
        strcpy(bogouser, "nobody");
        if (argv[optind])
-               strncpy(bogouser, argv[optind], sizeof(bogouser) - 1);
+               strncpy(bogouser, argv[optind], sizeof_bogouser - 1);
 
        /* Daemonize if no -f and no -i and no -w */
        if (!(opt & OPT_fiw))
index 5c975d8c5198eb2669c1eaebf510d7a10565f48b..2c0f514c7d9bcdd3433059b1eec157a382a96672 100644 (file)
@@ -13,6 +13,7 @@
 #include <net/if_arp.h>
 
 #include "ip_common.h"  /* #include "libbb.h" is inside */
+#include "common_bufsiz.h"
 #include "rt_names.h"
 #include "utils.h"
 
@@ -39,7 +40,7 @@ struct filter_t {
 } FIX_ALIASING;
 typedef struct filter_t filter_t;
 
-#define G_filter (*(filter_t*)&bb_common_bufsiz1)
+#define G_filter (*(filter_t*)bb_common_bufsiz1)
 
 
 static void print_link_flags(unsigned flags, unsigned mdown)
index 179505c2de41f328090826761fe149a69aefe235..151d3d109bcc83378ff128bb1a99ca3586e89a69 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "ip_common.h"  /* #include "libbb.h" is inside */
+#include "common_bufsiz.h"
 #include "rt_names.h"
 #include "utils.h"
 #include <linux/neighbour.h>
@@ -40,7 +41,7 @@ struct filter_t {
 } FIX_ALIASING;
 typedef struct filter_t filter_t;
 
-#define G_filter (*(filter_t*)&bb_common_bufsiz1)
+#define G_filter (*(filter_t*)bb_common_bufsiz1)
 
 static int flush_update(void)
 {
index 82827488f547eb07d2c47ba2fafb5a760fcfdc0b..34d4f4758a40b3b4c2e51b6c06a021cae0f677b9 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include "ip_common.h"  /* #include "libbb.h" is inside */
+#include "common_bufsiz.h"
 #include "rt_names.h"
 #include "utils.h"
 
@@ -43,7 +44,7 @@ struct filter_t {
 } FIX_ALIASING;
 typedef struct filter_t filter_t;
 
-#define G_filter (*(filter_t*)&bb_common_bufsiz1)
+#define G_filter (*(filter_t*)bb_common_bufsiz1)
 
 static int flush_update(void)
 {
index 2f9e174667cfd376d438cc341a1cab76c8de69b9..50edee450d977aea24dd8418eed3f656b1de960d 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 //config:config NC
 //config:      bool "nc"
@@ -252,7 +253,7 @@ int nc_main(int argc, char **argv)
                fd = STDIN_FILENO;
                while (1) {
                        if (FD_ISSET(fd, &testfds)) {
-                               nread = safe_read(fd, iobuf, sizeof(iobuf));
+                               nread = safe_read(fd, iobuf, COMMON_BUFSIZE);
                                if (fd == cfd) {
                                        if (nread < 1)
                                                exit(EXIT_SUCCESS);
index 0eb1ae79956c99c67c394eab500784e54239ebf9..761660979ff05a6b12734ca33ec989c844057e6c 100644 (file)
@@ -28,6 +28,7 @@
 #include <net/if.h>
 #include <netinet/ip_icmp.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #ifdef __BIONIC__
 /* should be in netinet/ip_icmp.h */
@@ -186,7 +187,7 @@ struct globals {
        char *hostname;
        char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 static void noresp(int ign UNUSED_PARAM)
@@ -378,7 +379,7 @@ struct globals {
        } pingaddr;
        unsigned char rcvd_tbl[MAX_DUP_CHK / 8];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define if_index     (G.if_index    )
 #define source_lsa   (G.source_lsa  )
 #define str_I        (G.str_I       )
index 14e0c1941361ef66b811913271a30d386f21c929..d9d8fe7b8c060e116106b683b45a5e11079b5742 100644 (file)
@@ -27,6 +27,7 @@
 //usage:     "\n       -F      Disable RTS/CTS flow control"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "libiproute/utils.h" /* invarg_1_to_2() */
 
 struct globals {
@@ -34,7 +35,7 @@ struct globals {
        int saved_disc;
        struct termios saved_state;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define handle       (G.handle      )
 #define saved_disc   (G.saved_disc  )
 #define saved_state  (G.saved_state )
index c84c18a67516016e5c1de701006705ed16595db0..1372ca0810c042300479d173ccaa1e5467e7fa74 100644 (file)
@@ -29,6 +29,7 @@
 //usage:       "filter show [ dev STRING ] [ root | parent CLASSID ]"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #include "libiproute/utils.h"
 #include "libiproute/ip_common.h"
@@ -63,7 +64,7 @@ struct globals {
        uint32_t filter_prio;
        uint32_t filter_proto;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define filter_ifindex (G.filter_ifindex)
 #define filter_qdisc (G.filter_qdisc)
 #define filter_parent (G.filter_parent)
index 3df6a98d8cd377e23979a8be8f1d8cfc0850b59f..6249730424764104605f6d6122a7a3f1ad79e7cd 100644 (file)
@@ -67,6 +67,7 @@
 //usage:     "\n       -v              Verbose"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* Wants <limits.h> etc, thus included after libbb.h: */
 #ifdef __linux__
@@ -91,7 +92,7 @@ struct globals {
        char **env_cur;
        char *env_var[1]; /* actually bigger */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define verbose      (G.verbose     )
 #define max_per_host (G.max_per_host)
 #define cur_per_host (G.cur_per_host)
index 944cf1bd62aab4456fda9a97014d8394bb1548f6..2946bc831e38d852b78834a4f24fbff9b35088f9 100644 (file)
@@ -39,6 +39,7 @@
 #include <arpa/telnet.h>
 #include <netinet/in.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #ifdef __BIONIC__
 /* should be in arpa/telnet.h */
@@ -108,7 +109,7 @@ struct globals {
        struct termios termios_def;
        struct termios termios_raw;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
index 25d05fe7aa3d177c7376d4f5683288d5eb12841a..13d5a8f649cc6575253e711328bdd991b5fe5514 100644 (file)
@@ -44,6 +44,7 @@
 #define DEBUG 0
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 
 #if DEBUG
@@ -82,7 +83,7 @@ struct globals {
        const char *issuefile;
        int maxfd;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        G.loginpath = "/bin/login"; \
        G.issuefile = "/etc/issue.net"; \
index ad9308e524cf0c2fea93ee14b03be47300109d06..8aeb79aca8b4a171ab1a8c70e130eec7f4ffc669 100644 (file)
@@ -51,6 +51,7 @@
 //usage:     "\n       -l      Log to syslog (inetd mode requires this)"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 
 #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
@@ -128,7 +129,7 @@ struct globals {
        bb_progress_t pmt;
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
@@ -757,7 +758,8 @@ int tftpd_main(int argc UNUSED_PARAM, char **argv)
 {
        len_and_sockaddr *our_lsa;
        len_and_sockaddr *peer_lsa;
-       char *local_file, *mode, *user_opt;
+       char *mode, *user_opt;
+       char *local_file = local_file;
        const char *error_msg;
        int opt, result, opcode;
        IF_FEATURE_TFTP_BLOCKSIZE(int blksize = TFTP_BLKSIZE_DEFAULT;)
index 496ab11a1aef73f97fc6fc68b02544b024045dfe..a526494d7c5df0e75b27d3a975e393b485c88744 100644 (file)
@@ -9,6 +9,7 @@
 #define UDHCP_COMMON_H 1
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <netinet/udp.h>
 #include <netinet/ip.h>
 
index 183e7e24ca43dc597ce3991add0c4e0d086d0a9e..9dd5bef9eb8f83941fa071266d549dc29e6bfeb5 100644 (file)
@@ -57,7 +57,7 @@ struct server_config_t {
        struct static_lease *static_leases; /* List of ip/mac pairs to assign static leases */
 } FIX_ALIASING;
 
-#define server_config (*(struct server_config_t*)&bb_common_bufsiz1)
+#define server_config (*(struct server_config_t*)bb_common_bufsiz1)
 /* client_config sits in 2nd half of bb_common_bufsiz1 */
 
 #if ENABLE_FEATURE_UDHCP_PORT
index f82ac05b430221701ae49bc2ca4bd7c04ab920a0..1722a85deb080ce1343c9db9e95732e34ce06df7 100644 (file)
@@ -33,7 +33,7 @@ struct xid_item {
        struct xid_item *next;
 } FIX_ALIASING;
 
-#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
+#define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
 
 static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
 {
index 7b57c62584b0acd442dc6a11d8f26aea93dbf4ff..b22425352dfa99b1c3946baf01b6e17e9d10b02f 100644 (file)
@@ -57,33 +57,35 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
 struct config_keyword {
        const char *keyword;
        int (*handler)(const char *line, void *var) FAST_FUNC;
-       void *var;
+       unsigned ofs;
        const char *def;
 };
 
+#define OFS(field) offsetof(struct server_config_t, field)
+
 static const struct config_keyword keywords[] = {
        /* keyword        handler           variable address               default */
-       {"start"        , udhcp_str2nip   , &server_config.start_ip     , "192.168.0.20"},
-       {"end"          , udhcp_str2nip   , &server_config.end_ip       , "192.168.0.254"},
-       {"interface"    , read_str        , &server_config.interface    , "eth0"},
+       {"start"        , udhcp_str2nip   , OFS(start_ip     ), "192.168.0.20"},
+       {"end"          , udhcp_str2nip   , OFS(end_ip       ), "192.168.0.254"},
+       {"interface"    , read_str        , OFS(interface    ), "eth0"},
        /* Avoid "max_leases value not sane" warning by setting default
         * to default_end_ip - default_start_ip + 1: */
-       {"max_leases"   , read_u32        , &server_config.max_leases   , "235"},
-       {"auto_time"    , read_u32        , &server_config.auto_time    , "7200"},
-       {"decline_time" , read_u32        , &server_config.decline_time , "3600"},
-       {"conflict_time", read_u32        , &server_config.conflict_time, "3600"},
-       {"offer_time"   , read_u32        , &server_config.offer_time   , "60"},
-       {"min_lease"    , read_u32        , &server_config.min_lease_sec, "60"},
-       {"lease_file"   , read_str        , &server_config.lease_file   , LEASES_FILE},
-       {"pidfile"      , read_str        , &server_config.pidfile      , "/var/run/udhcpd.pid"},
-       {"siaddr"       , udhcp_str2nip   , &server_config.siaddr_nip   , "0.0.0.0"},
+       {"max_leases"   , read_u32        , OFS(max_leases   ), "235"},
+       {"auto_time"    , read_u32        , OFS(auto_time    ), "7200"},
+       {"decline_time" , read_u32        , OFS(decline_time ), "3600"},
+       {"conflict_time", read_u32        , OFS(conflict_time), "3600"},
+       {"offer_time"   , read_u32        , OFS(offer_time   ), "60"},
+       {"min_lease"    , read_u32        , OFS(min_lease_sec), "60"},
+       {"lease_file"   , read_str        , OFS(lease_file   ), LEASES_FILE},
+       {"pidfile"      , read_str        , OFS(pidfile      ), "/var/run/udhcpd.pid"},
+       {"siaddr"       , udhcp_str2nip   , OFS(siaddr_nip   ), "0.0.0.0"},
        /* keywords with no defaults must be last! */
-       {"option"       , udhcp_str2optset, &server_config.options      , ""},
-       {"opt"          , udhcp_str2optset, &server_config.options      , ""},
-       {"notify_file"  , read_str        , &server_config.notify_file  , NULL},
-       {"sname"        , read_str        , &server_config.sname        , NULL},
-       {"boot_file"    , read_str        , &server_config.boot_file    , NULL},
-       {"static_lease" , read_staticlease, &server_config.static_leases, ""},
+       {"option"       , udhcp_str2optset, OFS(options      ), ""},
+       {"opt"          , udhcp_str2optset, OFS(options      ), ""},
+       {"notify_file"  , read_str        , OFS(notify_file  ), NULL},
+       {"sname"        , read_str        , OFS(sname        ), NULL},
+       {"boot_file"    , read_str        , OFS(boot_file    ), NULL},
+       {"static_lease" , read_staticlease, OFS(static_leases), ""},
 };
 enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
 
@@ -95,17 +97,17 @@ void FAST_FUNC read_config(const char *file)
        char *token[2];
 
        for (i = 0; i < KWS_WITH_DEFAULTS; i++)
-               keywords[i].handler(keywords[i].def, keywords[i].var);
+               keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs);
 
        parser = config_open(file);
        while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
                for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
                        if (strcasecmp(token[0], k->keyword) == 0) {
-                               if (!k->handler(token[1], k->var)) {
+                               if (!k->handler(token[1], (char*)&server_config + k->ofs)) {
                                        bb_error_msg("can't parse line %u in %s",
                                                        parser->lineno, file);
                                        /* reset back to the default value */
-                                       k->handler(k->def, k->var);
+                                       k->handler(k->def, (char*)&server_config + k->ofs);
                                }
                                break;
                        }
index c930826194a3644fe0f383d994ee7c81be10f582..79643458c60aa5c057d5eed9df75babf5f9bf0f1 100644 (file)
@@ -40,6 +40,7 @@
 //usage:     "\nexits only on I/O errors (link down etc)"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <netinet/ether.h>
 #include <net/if.h>
 #include <net/if_arp.h>
@@ -90,7 +91,7 @@ struct globals {
        struct ether_addr our_ethaddr;
        uint32_t localnet_ip;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 
index 0d023f7401871f7a9be6f162bd2320b79e3b3dae..9fde64b64b714e7d0c2d95033beaa0e51a9bf672 100644 (file)
@@ -22,6 +22,7 @@
 //usage:       "Total:       386144       257128       129016\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #ifdef __linux__
 # include <sys/sysinfo.h>
 #endif
@@ -35,7 +36,7 @@ struct globals {
 # define G_unit_steps 10
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
 
index 05b52abb1c30edcce351694b1fe6a53c21283983..2cda0f9d7914f53aef1fa7ad8649cb9d15ea9ea0 100644 (file)
@@ -18,6 +18,7 @@
 //usage:     "\n       -SIGNAL Signal to send (default: KILL)"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #define MAX_LINE 255
 
@@ -43,7 +44,7 @@ struct globals {
        smallint kill_failed;
        int killsig;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        G.mypid = getpid(); \
        G.killsig = SIGKILL; \
index 33de3790f83ec91c33ca91864b3ce60f3ffda2c1..efa3d553d71134988daccf5af023a9d1029a25ab 100644 (file)
@@ -53,6 +53,7 @@
 //  totalswap=134209536, freeswap=134209536, procs=157})
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 typedef unsigned long long ullong;
 
@@ -115,8 +116,8 @@ struct globals {
        G.deltanz = G.delta = 1000000; \
 } while (0)
 
-// We depend on this being a char[], not char* - we take sizeof() of it
-#define outbuf bb_common_bufsiz1
+#define        outbuf bb_common_bufsiz1
+#define sizeof_outbuf COMMON_BUFSIZE
 
 static inline void reset_outbuf(void)
 {
@@ -140,7 +141,7 @@ static void print_outbuf(void)
 static void put(const char *s)
 {
        char *p = cur_outbuf;
-       int sz = outbuf + sizeof(outbuf) - p;
+       int sz = outbuf + sizeof_outbuf - p;
        while (*s && --sz >= 0)
                *p++ = *s++;
        cur_outbuf = p;
@@ -148,7 +149,7 @@ static void put(const char *s)
 
 static void put_c(char c)
 {
-       if (cur_outbuf < outbuf + sizeof(outbuf))
+       if (cur_outbuf < outbuf + sizeof_outbuf)
                *cur_outbuf++ = c;
 }
 
index fbafa68a92d1023668f9d9e070d3b34e5e921415..65d62e256af9432d3a239969c48a02446ac1d9c4 100644 (file)
@@ -62,6 +62,7 @@
 //usage:       " 2990 andersen andersen R ps\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #ifdef __linux__
 # include <sys/sysinfo.h>
 #endif
@@ -144,7 +145,7 @@ struct globals {
        unsigned long seconds_since_boot;
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define out                (G.out               )
 #define out_cnt            (G.out_cnt           )
 #define print_header       (G.print_header      )
index ddf794d7d3b99f4e6f56ba6f08e4de381c2cbb72..1c42b249c37ee350f7cd2a600560606da245e046 100644 (file)
 //config:        Enable 's' in top (gives lots of memory info).
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 
 typedef struct top_status_t {
@@ -183,7 +184,7 @@ struct globals {
        char line_buf[80];
 }; //FIX_ALIASING; - large code growth
 enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define top              (G.top               )
 #define ntop             (G.ntop              )
 #define sort_field       (G.sort_field        )
index 4b18d12d5700d914fdd57e0b692e92d748ad31b8..8833f4c96c22239caa52360c3a35becc504a71eb 100644 (file)
@@ -45,6 +45,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <sys/file.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "runit_lib.h"
 
 #if ENABLE_MONOTONIC_SYSCALL
@@ -105,7 +106,7 @@ struct globals {
        char *dir;
        struct svdir svd[2];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define haslog       (G.haslog      )
 #define sigterm      (G.sigterm     )
 #define pidchanged   (G.pidchanged  )
index b3d9e739057ac18d09003dc146a3fd3bed88ffb3..49c8f5b48e20e27316090159b1bbd66233f36d4b 100644 (file)
@@ -57,6 +57,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <sys/file.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "runit_lib.h"
 
 #define MAXSERVICES 1000
@@ -84,7 +85,7 @@ struct globals {
        unsigned stamplog;
 #endif
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define sv          (G.sv          )
 #define svdir       (G.svdir       )
 #define svnum       (G.svnum       )
index de8a0d8a46625c20e6b7cd94e996b570899741da..e83a297811d85b6d6f0341146673f38b16afd0f0 100644 (file)
@@ -189,6 +189,7 @@ Exit Codes
 
 #include <sys/file.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "runit_lib.h"
 
 struct globals {
@@ -199,7 +200,7 @@ struct globals {
        uint64_t tstart, tnow;
        svstatus_t svstatus;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define acts         (G.acts        )
 #define service      (G.service     )
 #define rc           (G.rc          )
index dbe8df65c1258c7a4c3ad508483842f443d2e59f..09efdb69563bc113d224f1ed9b1d93db73ebcc25 100644 (file)
@@ -155,6 +155,7 @@ log message, you can use a pattern like this instead
 
 #include <sys/file.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "runit_lib.h"
 
 #define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
@@ -1045,9 +1046,9 @@ int svlogd_main(int argc, char **argv)
        }
        if (opt & 2) if (!repl) repl = '_'; // -R
        if (opt & 4) { // -l
-               linemax = xatou_range(l, 0, BUFSIZ-26);
+               linemax = xatou_range(l, 0, COMMON_BUFSIZE-26);
                if (linemax == 0)
-                       linemax = BUFSIZ-26;
+                       linemax = COMMON_BUFSIZE-26;
                if (linemax < 256)
                        linemax = 256;
        }
diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh
new file mode 100755 (executable)
index 0000000..afe9eee
--- /dev/null
@@ -0,0 +1,114 @@
+#!/bin/sh
+# Called from top-level directory a-la
+#
+# scripts/generate_BUFSIZ.sh include/common_bufsiz.h
+
+. ./.config || exit 1
+
+debug=false
+
+common_bufsiz_h=$1
+
+test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
+test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
+
+regenerate() {
+       cat >"$1.$$"
+       test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
+       mv "$1.$$" "$1"
+}
+
+generate_std_and_exit() {
+       $debug && echo "Default: bb_common_bufsiz1[] in bss"
+       {
+       echo "enum { COMMON_BUFSIZE = 1024 };"
+       echo "extern char bb_common_bufsiz1[];"
+       echo "#define setup_common_bufsiz() ((void)0)"
+       } | regenerate "$common_bufsiz_h"
+       exit 0
+}
+
+# User does not want any funky stuff?
+test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit
+
+test -f busybox_unstripped || {
+       # We did not try anything yet
+       $debug && echo "Will try to fit bb_common_bufsiz1[] into _end[]"
+       {
+       echo "enum { COMMON_BUFSIZE = 1024 };"
+       echo "extern char _end[]; /* linker-provided label */"
+       echo "#define bb_common_bufsiz1 _end"
+       echo "#define setup_common_bufsiz() ((void)0)"
+       } | regenerate "$common_bufsiz_h"
+       echo 1024 >"$common_bufsiz_h.BUFSIZE"
+       exit 0
+}
+
+# Get _end address
+END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1`
+test x"$END" = x"" && generate_std_and_exit
+$debug && echo "END:0x$END $((0x$END))"
+END=$((0x$END))
+
+# Get PAGE_SIZE
+echo "\
+#include <sys/user.h>
+#if defined(PAGE_SIZE) && PAGE_SIZE > 0
+char page_size[PAGE_SIZE];
+#else
+char page_size[1];
+#endif
+" >page_size_$$.c
+$CC -c "page_size_$$.c" || generate_std_and_exit
+PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1`
+rm "page_size_$$.c" "page_size_$$.o"
+test x"$PAGE_SIZE" = x"" && generate_std_and_exit
+$debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))"
+PAGE_SIZE=$((0x$PAGE_SIZE))
+test $PAGE_SIZE -lt 1024 && generate_std_and_exit
+
+# How much space between _end[] and next page?
+PAGE_MASK=$((PAGE_SIZE-1))
+REM=$(( (-END) & PAGE_MASK ))
+$debug && echo "REM:$REM"
+
+if test $REM -lt 1024; then
+       # _end[] has no enough space for bb_common_bufsiz1[],
+       # users will need to malloc it.
+       {
+       echo "enum { COMMON_BUFSIZE = 1024 };"
+       echo "extern char *bb_common_bufsiz1;"
+       echo "void setup_common_bufsiz(void);"
+       } | regenerate "$common_bufsiz_h"
+       # Check that we aren't left with a buggy binary:
+       if test -f "$common_bufsiz_h.BUFSIZE"; then
+               rm "$common_bufsiz_h.BUFSIZE"
+               echo "Warning! Space in _end[] is too small ($REM bytes)!"
+               echo "Rerun make to build a binary which doesn't use it!"
+               exit 1
+       fi
+        exit 0
+fi
+
+# _end[] has REM bytes for bb_common_bufsiz1[]
+OLD=1024
+test -f "$common_bufsiz_h.BUFSIZE" && OLD=`cat "$common_bufsiz_h.BUFSIZE"`
+$debug && echo "OLD:$OLD"
+{
+echo "enum { COMMON_BUFSIZE = $REM };"
+echo "extern char _end[]; /* linker-provided label */"
+echo "#define bb_common_bufsiz1 _end"
+echo "#define setup_common_bufsiz() ((void)0)"
+} | regenerate "$common_bufsiz_h"
+echo $REM >"$common_bufsiz_h.BUFSIZE"
+
+# Check that code did not grow too much and thus _end[] did not shink:
+if test $OLD -gt $REM; then
+       echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!"
+       echo "Rerun make!"
+       exit 1
+fi
+
+if test $OLD != $REM; then
+       echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE."
+fi
index de99dfe442de487b83ada72072ec98debc67e80a..441345ae94b9eba8a423959c1260a1770b249e1b 100644 (file)
@@ -77,7 +77,7 @@ struct globals {
        int nerr;
        struct edir excludeArray[MAX_EXCLUDES];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 void BUG_setfiles_globals_too_big(void);
 #define INIT_G() do { \
        if (sizeof(G) > COMMON_BUFSIZE) \
index 03d65b37fb542862aca6199eb290b703034fdc14..845c49a5ea6844db01608011b60e8780a57ef57f 100644 (file)
@@ -58,6 +58,7 @@
 //usage:     "\n       -n      Run in foreground"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 
 
@@ -145,9 +146,10 @@ static void klogd_close(void)
 
 #endif
 
-#define log_buffer bb_common_bufsiz1
+#define        log_buffer bb_common_bufsiz1
+#define sizeof_log_buffer COMMON_BUFSIZE
 enum {
-       KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
+       KLOGD_LOGBUF_SIZE = sizeof_log_buffer,
        OPT_LEVEL      = (1 << 0),
        OPT_FOREGROUND = (1 << 1),
 };
index 781a603b2afea45ed830b2fd6e8fc8f23c36870c..ebd7f8b2c9dc658b25eade1ebeda2d3ee8652877 100644 (file)
@@ -42,6 +42,7 @@
 //usage:     "\n       -F      Same as -f, but dump buffer first"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <sys/ipc.h>
 #include <sys/sem.h>
 #include <sys/shm.h>
@@ -67,7 +68,7 @@ struct globals {
        struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
        struct shbuf_ds *shbuf;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define SMrup (G.SMrup)
 #define SMrdn (G.SMrdn)
 #define shbuf (G.shbuf)
index 0964f239cbec8d4c64853c0d8cc932a04d36e259..6458a9332ce1f7ab76c8e9e258706686900c9bbf 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #define SYSLOG_NAMES
 #define SYSLOG_NAMES_CONST
 #include <syslog.h>
index 37fa56827520bc95da40cb1dc9eb2e40ee0b3050..7473b1855655c0487ece61172d2b12f65d4526e2 100644 (file)
@@ -97,6 +97,7 @@
 //usage:       "If /dev/mdev.log file exists, debug log will be appended to it."
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
@@ -285,7 +286,7 @@ struct globals {
        struct rule cur_rule;
        char timestr[sizeof("HH:MM:SS.123456")];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
        IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
        IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
index b5d2c49b6b0e9fd8c1f50e5639b7f36b3c5adaff..f9451792ba39329cad7aea08fe9fb490a16ecf5e 100644 (file)
@@ -13,6 +13,7 @@
 //usage:     "\n       -L LBL  Label"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #if ENABLE_SELINUX
 static void mkswap_selinux_setcontext(int fd, const char *path)
index 35957139711f20b89b3c5b793b66f5ccc9a1af0f..58be3ac3be65ae9a5a82447582990d951a480a33 100644 (file)
@@ -23,6 +23,7 @@
 //usage:       "$ dmesg | more\n"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 /* Support for FEATURE_USE_TERMIOS */
 
index c428f58274d9f6356dd38fef94e480caa24ec338..e5c85feff2ce6635270bbc533ce90edc12d4be0d 100644 (file)
 #define BB_MS_INVERTED_VALUE (1u << 31)
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #if ENABLE_FEATURE_MOUNT_LABEL
 # include "volume_id.h"
 #else
@@ -447,7 +448,7 @@ struct globals {
        char getmntent_buf[1];
 } FIX_ALIASING;
 enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define nfs_mount_version (G.nfs_mount_version)
 #if ENABLE_FEATURE_MOUNT_VERBOSE
 #define verbose           (G.verbose          )
index abcd73bff5c0fc477c26a33c385491f8a6c8effc..6195161bcc8b6ca60909a52b753868b307bbe766 100644 (file)
@@ -23,6 +23,7 @@
 //usage:       )
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int script_main(int argc UNUSED_PARAM, char **argv)
@@ -108,7 +109,8 @@ int script_main(int argc UNUSED_PARAM, char **argv)
 
        if (child_pid) {
                /* parent */
-#define buf bb_common_bufsiz1
+#define        buf bb_common_bufsiz1
+#define sizeof_buf COMMON_BUFSIZE
                struct pollfd pfd[2];
                int outfd, count, loop;
                double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
@@ -134,7 +136,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                        }
                        if (pfd[0].revents) {
                                errno = 0;
-                               count = safe_read(pty, buf, sizeof(buf));
+                               count = safe_read(pty, buf, sizeof_buf);
                                if (count <= 0 && errno != EAGAIN) {
                                        /* err/eof from pty: exit */
                                        goto restore;
@@ -157,7 +159,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                                }
                        }
                        if (pfd[1].revents) {
-                               count = safe_read(STDIN_FILENO, buf, sizeof(buf));
+                               count = safe_read(STDIN_FILENO, buf, sizeof_buf);
                                if (count <= 0) {
                                        /* err/eof from stdin: don't read stdin anymore */
                                        pfd[1].revents = 0;
@@ -176,7 +178,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                 * (util-linux's script doesn't do this. buggy :) */
                loop = 999;
                /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
-               while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
+               while (--loop && (count = safe_read(pty, buf, sizeof_buf)) > 0) {
                        full_write(STDOUT_FILENO, buf, count);
                        full_write(outfd, buf, count);
                }
index c29dd30712737da213433e64c14372e8862808e3..43228a6bad66f696bc4f30331915aa4517d723a1 100644 (file)
@@ -28,6 +28,7 @@
 //usage:     "\n       -a      Stop swapping on all swap devices"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <mntent.h>
 #ifndef __BIONIC__
 # include <sys/swap.h>
@@ -62,7 +63,7 @@
 struct globals {
        int flags;
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define g_flags (G.flags)
 #define save_g_flags()    int save_g_flags = g_flags
 #define restore_g_flags() g_flags = save_g_flags
index 514a9e934a943204d7df8d99d84def0d26d5be89..58668fa5d5898de663dbd1789284d3533bf09448 100644 (file)
 //usage:   "\n""       # uevent mdev & mdev -s"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <linux/netlink.h>
 
 #define BUFFER_SIZE 16*1024
 
-#define env ((char **)&bb_common_bufsiz1)
+#define env ((char **)bb_common_bufsiz1)
 enum {
        MAX_ENV = COMMON_BUFSIZE / sizeof(env[0]) - 1,
 };
index 30bef1686a6223488ab8a68026a23bfe78e16957..be0300394dd8b7fb667e8865fa6e83738aacc9ab 100644 (file)
@@ -34,6 +34,7 @@
 # define MNT_DETACH 0x00000002
 #endif
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #if defined(__dietlibc__)
 // TODO: This does not belong here.
@@ -102,7 +103,7 @@ int umount_main(int argc UNUSED_PARAM, char **argv)
                if (opt & OPT_ALL)
                        bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
        } else {
-               while (getmntent_r(fp, &me, bb_common_bufsiz1, sizeof(bb_common_bufsiz1))) {
+               while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) {
                        /* Match fstype if passed */
                        if (!match_fstype(&me, fstype))
                                continue;