From e6a2f4cc5a47d3022bdf5ca2cacbaa5a8c5baf7a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 16:26:30 +0200 Subject: [PATCH] libbb: make bb_common_bufsiz1 1 kbyte, add capability to use bss tail for it 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 --- Makefile | 7 +- archival/cpio.c | 3 +- archival/lzop.c | 3 +- archival/rpm.c | 3 +- archival/tar.c | 1 + console-tools/dumpkmap.c | 1 + console-tools/resize.c | 3 +- coreutils/catv.c | 1 + coreutils/cksum.c | 6 +- coreutils/date.c | 6 +- coreutils/dd.c | 3 +- coreutils/du.c | 3 +- coreutils/expr.c | 3 +- coreutils/ls.c | 3 +- coreutils/od_bloaty.c | 3 +- coreutils/split.c | 1 + coreutils/stat.c | 6 +- coreutils/stty.c | 3 +- coreutils/sum.c | 3 +- coreutils/tail.c | 3 +- coreutils/tee.c | 6 +- debianutils/run_parts.c | 3 +- debianutils/start_stop_daemon.c | 3 +- e2fsprogs/fsck.c | 3 +- editors/diff.c | 1 + editors/ed.c | 8 ++- editors/sed.c | 3 +- findutils/find.c | 3 +- findutils/grep.c | 3 +- findutils/xargs.c | 3 +- include/libbb.h | 4 -- init/bootchartd.c | 3 +- libbb/common_bufsiz.c | 74 +++++++++++++++++++ libbb/messages.c | 5 -- loginutils/login.c | 3 +- miscutils/chat.c | 1 + miscutils/conspy.c | 6 +- miscutils/crond.c | 3 +- miscutils/dc.c | 3 +- miscutils/fbsplash.c | 6 +- miscutils/hdparm.c | 3 +- miscutils/i2c_tools.c | 1 + miscutils/inotifyd.c | 6 +- miscutils/less.c | 6 +- miscutils/microcom.c | 6 +- networking/arp.c | 3 +- networking/arping.c | 3 +- networking/ftpd.c | 3 +- networking/ftpgetput.c | 3 +- networking/httpd.c | 10 +-- networking/ifupdown.c | 3 +- networking/inetd.c | 3 +- networking/isrv_identd.c | 6 +- networking/libiproute/ipaddress.c | 3 +- networking/libiproute/ipneigh.c | 3 +- networking/libiproute/iproute.c | 3 +- networking/nc.c | 3 +- networking/ping.c | 5 +- networking/slattach.c | 3 +- networking/tc.c | 3 +- networking/tcpudp.c | 3 +- networking/telnet.c | 3 +- networking/telnetd.c | 3 +- networking/tftp.c | 6 +- networking/udhcp/common.h | 1 + networking/udhcp/dhcpd.h | 2 +- networking/udhcp/dhcprelay.c | 2 +- networking/udhcp/files.c | 46 ++++++------ networking/zcip.c | 3 +- procps/free.c | 3 +- procps/fuser.c | 3 +- procps/nmeter.c | 9 +-- procps/ps.c | 3 +- procps/top.c | 3 +- runit/runsv.c | 3 +- runit/runsvdir.c | 3 +- runit/sv.c | 3 +- runit/svlogd.c | 5 +- scripts/generate_BUFSIZ.sh | 114 ++++++++++++++++++++++++++++++ selinux/setfiles.c | 2 +- sysklogd/klogd.c | 6 +- sysklogd/logread.c | 3 +- sysklogd/syslogd_and_logger.c | 1 + util-linux/mdev.c | 3 +- util-linux/mkswap.c | 1 + util-linux/more.c | 1 + util-linux/mount.c | 3 +- util-linux/script.c | 10 +-- util-linux/swaponoff.c | 3 +- util-linux/uevent.c | 3 +- util-linux/umount.c | 3 +- 91 files changed, 414 insertions(+), 131 deletions(-) create mode 100644 libbb/common_bufsiz.c create mode 100755 scripts/generate_BUFSIZ.sh diff --git a/Makefile b/Makefile index dab807805..75a33c1fb 100644 --- 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 \ diff --git a/archival/cpio.c b/archival/cpio.c index 82b3fe5ed..a3036e1ab 100644 --- a/archival/cpio.c +++ b/archival/cpio.c @@ -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; \ diff --git a/archival/lzop.c b/archival/lzop.c index a5fc01941..1371c9751 100644 --- a/archival/lzop.c +++ b/archival/lzop.c @@ -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 { diff --git a/archival/rpm.c b/archival/rpm.c index 105394481..079b7a95b 100644 --- a/archival/rpm.c +++ b/archival/rpm.c @@ -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) diff --git a/archival/tar.c b/archival/tar.c index 23ac00e86..caf4363de 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -146,6 +146,7 @@ #include #include "libbb.h" +#include "common_bufsiz.h" #include "bb_archive.h" /* FIXME: Stop using this non-standard feature */ #ifndef FNM_LEADING_DIR diff --git a/console-tools/dumpkmap.c b/console-tools/dumpkmap.c index bf8d690da..6412dffc8 100644 --- a/console-tools/dumpkmap.c +++ b/console-tools/dumpkmap.c @@ -18,6 +18,7 @@ //usage: "$ dumpkmap > keymap\n" #include "libbb.h" +#include "common_bufsiz.h" /* From */ struct kbentry { diff --git a/console-tools/resize.c b/console-tools/resize.c index 4b0d63a03..ed80aa082 100644 --- a/console-tools/resize.c +++ b/console-tools/resize.c @@ -14,10 +14,11 @@ //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) diff --git a/coreutils/catv.c b/coreutils/catv.c index 6bb73ba63..801d2451d 100644 --- a/coreutils/catv.c +++ b/coreutils/catv.c @@ -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) diff --git a/coreutils/cksum.c b/coreutils/cksum.c index ac0b0c319..d8351e7c6 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c @@ -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); } diff --git a/coreutils/date.c b/coreutils/date.c index 7965775fe..59b4b8f2a 100644 --- a/coreutils/date.c +++ b/coreutils/date.c @@ -138,6 +138,7 @@ //usage: "Wed Apr 12 18:52:41 MDT 2000\n" #include "libbb.h" +#include "common_bufsiz.h" #if ENABLE_FEATURE_DATE_NANO # include #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); diff --git a/coreutils/dd.c b/coreutils/dd.c index 0c0ea07b9..a5b8882a0 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -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)); \ diff --git a/coreutils/du.c b/coreutils/du.c index 1889c16bb..3d6777670 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -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) diff --git a/coreutils/expr.c b/coreutils/expr.c index c986f9327..59a66d9c5 100644 --- a/coreutils/expr.c +++ b/coreutils/expr.c @@ -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 */ diff --git a/coreutils/ls.c b/coreutils/ls.c index 20bd61860..e8c3e0490 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -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)); \ diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index f47f84b54..1e252caf0 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -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; \ diff --git a/coreutils/split.c b/coreutils/split.c index 1e1673efb..b2da74e27 100644 --- a/coreutils/split.c +++ b/coreutils/split.c @@ -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[] = { diff --git a/coreutils/stat.c b/coreutils/stat.c index 1a490fef7..78df9c948 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -103,6 +103,7 @@ //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 } diff --git a/coreutils/stty.c b/coreutils/stty.c index b63b0b91a..0e32fc898 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -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; \ diff --git a/coreutils/sum.c b/coreutils/sum.c index deb068e10..cc6677221 100644 --- a/coreutils/sum.c +++ b/coreutils/sum.c @@ -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); diff --git a/coreutils/tail.c b/coreutils/tail.c index e352ab627..cdc9fb66a 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -49,12 +49,13 @@ //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) diff --git a/coreutils/tee.c b/coreutils/tee.c index 48cc0508f..a0e177cbc 100644 --- a/coreutils/tee.c +++ b/coreutils/tee.c @@ -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); diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index 13617c6e1..a5e53576c 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c @@ -89,13 +89,14 @@ //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 ) diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index d7c730f45..6b8d53b13 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -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 ) diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c index 987d97528..b534568c2 100644 --- a/e2fsprogs/fsck.c +++ b/e2fsprogs/fsck.c @@ -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) diff --git a/editors/diff.c b/editors/diff.c index a892cfdf2..3c8e9074a 100644 --- a/editors/diff.c +++ b/editors/diff.c @@ -125,6 +125,7 @@ //usage: "\n -w Ignore all whitespace" #include "libbb.h" +#include "common_bufsiz.h" #if 0 # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__) diff --git a/editors/ed.c b/editors/ed.c index a4c419099..8da7b1dd5 100644 --- a/editors/ed.c +++ b/editors/ed.c @@ -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 */ }; diff --git a/editors/sed.c b/editors/sed.c index 9c4c8e148..330190e78 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -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; \ diff --git a/findutils/find.c b/findutils/find.c index a0d4853de..32d830337 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -342,6 +342,7 @@ #include #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 */ \ diff --git a/findutils/grep.c b/findutils/grep.c index dece90c58..a669ac80b 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -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) diff --git a/findutils/xargs.c b/findutils/xargs.c index 69f83b128..bfbd94960 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -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 = "{}";) \ diff --git a/include/libbb.h b/include/libbb.h index 111dd66e0..fd40ef74c 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -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; diff --git a/init/bootchartd.c b/init/bootchartd.c index c7388c99e..5101b28ae 100644 --- a/init/bootchartd.c +++ b/init/bootchartd.c @@ -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 @@ -115,7 +116,7 @@ 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 index 000000000..c16c361c9 --- /dev/null +++ b/libbb/common_bufsiz.c @@ -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 diff --git a/libbb/messages.c b/libbb/messages.c index 23e440bcd..cb0836de8 100644 --- a/libbb/messages.c +++ b/libbb/messages.c @@ -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)); diff --git a/loginutils/login.c b/loginutils/login.c index 4ebc18502..ea7c5a23d 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -62,6 +62,7 @@ //usage: "\n -p Preserve environment" #include "libbb.h" +#include "common_bufsiz.h" #include #include @@ -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) diff --git a/miscutils/chat.c b/miscutils/chat.c index bd2abc24a..25850dd20 100644 --- a/miscutils/chat.c +++ b/miscutils/chat.c @@ -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 diff --git a/miscutils/conspy.c b/miscutils/conspy.c index 1a46a4340..0d96a5f9a 100644 --- a/miscutils/conspy.c +++ b/miscutils/conspy.c @@ -42,6 +42,7 @@ //usage: "\n -y LINE Starting line" #include "libbb.h" +#include "common_bufsiz.h" #include #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; diff --git a/miscutils/crond.c b/miscutils/crond.c index eb327f855..8536d43c5 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -60,6 +60,7 @@ //usage: "\n -c DIR Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs" #include "libbb.h" +#include "common_bufsiz.h" #include /* 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; \ diff --git a/miscutils/dc.c b/miscutils/dc.c index 9c74172ba..3fbb89f5b 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -4,6 +4,7 @@ */ #include "libbb.h" +#include "common_bufsiz.h" #include //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 ) diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c index 9557c41db..b26ad2c15 100644 --- a/miscutils/fbsplash.c +++ b/miscutils/fbsplash.c @@ -34,6 +34,7 @@ //usage: "\n commands: 'NN' (% for progress bar) or 'exit'" #include "libbb.h" +#include "common_bufsiz.h" #include /* 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 ) { diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 8e201ac35..9e141de2f 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c @@ -63,6 +63,7 @@ //usage: "\n -z Reread partition table" #include "libbb.h" +#include "common_bufsiz.h" /* must be _after_ libbb.h: */ #include #include @@ -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 ) diff --git a/miscutils/i2c_tools.c b/miscutils/i2c_tools.c index aa1c7c5cc..57bb72ae7 100644 --- a/miscutils/i2c_tools.c +++ b/miscutils/i2c_tools.c @@ -60,6 +60,7 @@ */ #include "libbb.h" +#include "common_bufsiz.h" #include #include diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c index 908d657fd..1d28e8f99 100644 --- a/miscutils/inotifyd.c +++ b/miscutils/inotifyd.c @@ -56,6 +56,7 @@ //usage: "\nWhen x event happens for all FILEs, inotifyd exits." #include "libbb.h" +#include "common_bufsiz.h" #include 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) { diff --git a/miscutils/less.c b/miscutils/less.c index ccdb15fdc..94ecf1686 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -127,6 +127,7 @@ #include /* 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); diff --git a/miscutils/microcom.c b/miscutils/microcom.c index 5e29a1acd..5eb2e6743 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c @@ -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 { diff --git a/networking/arp.c b/networking/arp.c index 0099aa534..5f7818663 100644 --- a/networking/arp.c +++ b/networking/arp.c @@ -32,6 +32,7 @@ //usage: "\n -H HWTYPE Hardware address type" #include "libbb.h" +#include "common_bufsiz.h" #include "inet_common.h" #include @@ -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 ) diff --git a/networking/arping.c b/networking/arping.c index ef205e5e6..52f5ba51f 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -28,6 +28,7 @@ #include #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 ) diff --git a/networking/ftpd.c b/networking/ftpd.c index 8345ae67c..8553a28f5 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c @@ -29,6 +29,7 @@ //usage: "\n DIR Change root to this directory" #include "libbb.h" +#include "common_bufsiz.h" #include #include @@ -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 );*/ \ diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index b398bc874..61bc45c4e 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -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 ) diff --git a/networking/httpd.c b/networking/httpd.c index ed15fd883..ef90770ac 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -125,6 +125,7 @@ //usage: "\n -d STRING URL decode STRING" #include "libbb.h" +#include "common_bufsiz.h" #if ENABLE_PAM /* PAM may include . 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; diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 2c6db926f..399ff6b5d 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c @@ -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 #include @@ -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) diff --git a/networking/inetd.c b/networking/inetd.c index 4f6673b12..aa35ffa2b 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -170,6 +170,7 @@ #include #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 ) diff --git a/networking/isrv_identd.c b/networking/isrv_identd.c index 252c8aba9..f63ed8ee4 100644 --- a/networking/isrv_identd.c +++ b/networking/isrv_identd.c @@ -18,6 +18,7 @@ //usage: "\n STRING Ident answer string (default: nobody)" #include "libbb.h" +#include "common_bufsiz.h" #include #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)) diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c index 5c975d8c5..2c0f514c7 100644 --- a/networking/libiproute/ipaddress.c +++ b/networking/libiproute/ipaddress.c @@ -13,6 +13,7 @@ #include #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) diff --git a/networking/libiproute/ipneigh.c b/networking/libiproute/ipneigh.c index 179505c2d..151d3d109 100644 --- a/networking/libiproute/ipneigh.c +++ b/networking/libiproute/ipneigh.c @@ -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 @@ -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) { diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c index 82827488f..34d4f4758 100644 --- a/networking/libiproute/iproute.c +++ b/networking/libiproute/iproute.c @@ -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) { diff --git a/networking/nc.c b/networking/nc.c index 2f9e17466..50edee450 100644 --- a/networking/nc.c +++ b/networking/nc.c @@ -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); diff --git a/networking/ping.c b/networking/ping.c index 0eb1ae799..761660979 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -28,6 +28,7 @@ #include #include #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 ) diff --git a/networking/slattach.c b/networking/slattach.c index 14e0c1941..d9d8fe7b8 100644 --- a/networking/slattach.c +++ b/networking/slattach.c @@ -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 ) diff --git a/networking/tc.c b/networking/tc.c index c84c18a67..1372ca081 100644 --- a/networking/tc.c +++ b/networking/tc.c @@ -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) diff --git a/networking/tcpudp.c b/networking/tcpudp.c index 3df6a98d8..624973042 100644 --- a/networking/tcpudp.c +++ b/networking/tcpudp.c @@ -67,6 +67,7 @@ //usage: "\n -v Verbose" #include "libbb.h" +#include "common_bufsiz.h" /* Wants 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) diff --git a/networking/telnet.c b/networking/telnet.c index 944cf1bd6..2946bc831 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -39,6 +39,7 @@ #include #include #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) diff --git a/networking/telnetd.c b/networking/telnetd.c index 25d05fe7a..13d5a8f64 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -44,6 +44,7 @@ #define DEBUG 0 #include "libbb.h" +#include "common_bufsiz.h" #include #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"; \ diff --git a/networking/tftp.c b/networking/tftp.c index ad9308e52..8aeb79aca 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -51,6 +51,7 @@ //usage: "\n -l Log to syslog (inetd mode requires this)" #include "libbb.h" +#include "common_bufsiz.h" #include #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;) diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 496ab11a1..a526494d7 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h @@ -9,6 +9,7 @@ #define UDHCP_COMMON_H 1 #include "libbb.h" +#include "common_bufsiz.h" #include #include diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h index 183e7e24c..9dd5bef9e 100644 --- a/networking/udhcp/dhcpd.h +++ b/networking/udhcp/dhcpd.h @@ -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 diff --git a/networking/udhcp/dhcprelay.c b/networking/udhcp/dhcprelay.c index f82ac05b4..1722a85de 100644 --- a/networking/udhcp/dhcprelay.c +++ b/networking/udhcp/dhcprelay.c @@ -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) { diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 7b57c6258..b22425352 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -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; } diff --git a/networking/zcip.c b/networking/zcip.c index c93082619..79643458c 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -40,6 +40,7 @@ //usage: "\nexits only on I/O errors (link down etc)" #include "libbb.h" +#include "common_bufsiz.h" #include #include #include @@ -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) diff --git a/procps/free.c b/procps/free.c index 0d023f740..9fde64b64 100644 --- a/procps/free.c +++ b/procps/free.c @@ -22,6 +22,7 @@ //usage: "Total: 386144 257128 129016\n" #include "libbb.h" +#include "common_bufsiz.h" #ifdef __linux__ # include #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) diff --git a/procps/fuser.c b/procps/fuser.c index 05b52abb1..2cda0f9d7 100644 --- a/procps/fuser.c +++ b/procps/fuser.c @@ -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; \ diff --git a/procps/nmeter.c b/procps/nmeter.c index 33de3790f..efa3d553d 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c @@ -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; } diff --git a/procps/ps.c b/procps/ps.c index fbafa68a9..65d62e256 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -62,6 +62,7 @@ //usage: " 2990 andersen andersen R ps\n" #include "libbb.h" +#include "common_bufsiz.h" #ifdef __linux__ # include #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 ) diff --git a/procps/top.c b/procps/top.c index ddf794d7d..1c42b249c 100644 --- a/procps/top.c +++ b/procps/top.c @@ -105,6 +105,7 @@ //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 ) diff --git a/runit/runsv.c b/runit/runsv.c index 4b18d12d5..8833f4c96 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -45,6 +45,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #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 ) diff --git a/runit/runsvdir.c b/runit/runsvdir.c index b3d9e7390..49c8f5b48 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c @@ -57,6 +57,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #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 ) diff --git a/runit/sv.c b/runit/sv.c index de8a0d8a4..e83a29781 100644 --- a/runit/sv.c +++ b/runit/sv.c @@ -189,6 +189,7 @@ Exit Codes #include #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 ) diff --git a/runit/svlogd.c b/runit/svlogd.c index dbe8df65c..09efdb695 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c @@ -155,6 +155,7 @@ log message, you can use a pattern like this instead #include #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 index 000000000..afe9eee1e --- /dev/null +++ b/scripts/generate_BUFSIZ.sh @@ -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 +#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 diff --git a/selinux/setfiles.c b/selinux/setfiles.c index de99dfe44..441345ae9 100644 --- a/selinux/setfiles.c +++ b/selinux/setfiles.c @@ -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) \ diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 03d65b37f..845c49a5e 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -58,6 +58,7 @@ //usage: "\n -n Run in foreground" #include "libbb.h" +#include "common_bufsiz.h" #include @@ -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), }; diff --git a/sysklogd/logread.c b/sysklogd/logread.c index 781a603b2..ebd7f8b2c 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -42,6 +42,7 @@ //usage: "\n -F Same as -f, but dump buffer first" #include "libbb.h" +#include "common_bufsiz.h" #include #include #include @@ -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) diff --git a/sysklogd/syslogd_and_logger.c b/sysklogd/syslogd_and_logger.c index 0964f239c..6458a9332 100644 --- a/sysklogd/syslogd_and_logger.c +++ b/sysklogd/syslogd_and_logger.c @@ -8,6 +8,7 @@ */ #include "libbb.h" +#include "common_bufsiz.h" #define SYSLOG_NAMES #define SYSLOG_NAMES_CONST #include diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 37fa56827..7473b1855 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -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;) \ diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index b5d2c49b6..f9451792b 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c @@ -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) diff --git a/util-linux/more.c b/util-linux/more.c index 359571397..58be3ac3b 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -23,6 +23,7 @@ //usage: "$ dmesg | more\n" #include "libbb.h" +#include "common_bufsiz.h" /* Support for FEATURE_USE_TERMIOS */ diff --git a/util-linux/mount.c b/util-linux/mount.c index c428f5827..e5c85feff 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -223,6 +223,7 @@ #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 ) diff --git a/util-linux/script.c b/util-linux/script.c index abcd73bff..6195161bc 100644 --- a/util-linux/script.c +++ b/util-linux/script.c @@ -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); } diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c index c29dd3071..43228a6ba 100644 --- a/util-linux/swaponoff.c +++ b/util-linux/swaponoff.c @@ -28,6 +28,7 @@ //usage: "\n -a Stop swapping on all swap devices" #include "libbb.h" +#include "common_bufsiz.h" #include #ifndef __BIONIC__ # include @@ -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 diff --git a/util-linux/uevent.c b/util-linux/uevent.c index 514a9e934..58668fa5d 100644 --- a/util-linux/uevent.c +++ b/util-linux/uevent.c @@ -25,11 +25,12 @@ //usage: "\n"" # uevent mdev & mdev -s" #include "libbb.h" +#include "common_bufsiz.h" #include #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, }; diff --git a/util-linux/umount.c b/util-linux/umount.c index 30bef1686..be0300394 100644 --- a/util-linux/umount.c +++ b/util-linux/umount.c @@ -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;