From e4d925b8899e38437311d2c63d0d0d200e04b5a1 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 8 Apr 2016 00:20:36 +0200 Subject: sed: support "-f -" idiom Signed-off-by: Denys Vlasenko --- editors/sed.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index 4c7f75521..9c4c8e148 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -1507,12 +1507,12 @@ int sed_main(int argc UNUSED_PARAM, char **argv) while (opt_f) { // -f char *line; FILE *cmdfile; - cmdfile = xfopen_for_read(llist_pop(&opt_f)); + cmdfile = xfopen_stdin(llist_pop(&opt_f)); while ((line = xmalloc_fgetline(cmdfile)) != NULL) { add_cmd(line); free(line); } - fclose(cmdfile); + fclose_if_not_stdin(cmdfile); } /* if we didn't get a pattern from -e or -f, use argv[0] */ if (!(opt & 0x30)) { -- cgit v1.2.3-55-g6feb From 2b91958dff0b7bae83cf2c3f2db55bd248fe0956 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 8 Apr 2016 11:57:20 +0100 Subject: Rewrite iteration through applet names to save a few bytes function old new delta run_applet_and_exit 758 755 -3 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-3) Total: -3 bytes In standalone shell mode the saving increases to 17 bytes. Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 3 ++- libbb/lineedit.c | 7 +++---- shell/ash.c | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libbb/appletlib.c b/libbb/appletlib.c index de654f64c..b682e6b85 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -791,7 +791,8 @@ static int busybox_main(char **argv) full_write2_str(a); full_write2_str("\n"); i++; - a += strlen(a) + 1; + while (*a++ != '\0') + continue; } return 0; } diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 2ddb2b6e9..3e62f46b4 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -779,12 +779,11 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) if (type == FIND_EXE_ONLY) { const char *p = applet_names; - i = 0; - while (i < NUM_APPLETS) { + while (*p) { if (strncmp(pfind, p, pf_len) == 0) add_match(xstrdup(p)); - p += strlen(p) + 1; - i++; + while (*p++ != '\0') + continue; } } #endif diff --git a/shell/ash.c b/shell/ash.c index 5613e1f33..13eeab34b 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -12597,7 +12597,8 @@ helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) out1fmt("\n"); col = 0; } - a += strlen(a) + 1; + while (*a++ != '\0') + continue; } } # endif -- cgit v1.2.3-55-g6feb From 84ba50c32f7dbfccddd5c5ca34d48d97c3f72193 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 3 Apr 2016 22:43:14 +0100 Subject: ash: bash-compatible $'...' shouldn't expand in double quotes Bash doesn't expand its $'...' construct in double quotes: $ echo "$'a\tb'" $'a\tb' Change BusyBox ash to do the same. This also fixes a problem with here documents where BusyBox ash gave an incorrect result for: $ cat < '$' > EOF '$' Reported-by: Timo Teras Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/ash.c | 2 +- shell/ash_test/ash-heredoc/heredoc4.right | 1 + shell/ash_test/ash-heredoc/heredoc4.tests | 3 +++ shell/ash_test/ash-quoting/dollar_squote_bash1.right | 1 + shell/ash_test/ash-quoting/dollar_squote_bash1.tests | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 shell/ash_test/ash-heredoc/heredoc4.right create mode 100755 shell/ash_test/ash-heredoc/heredoc4.tests diff --git a/shell/ash.c b/shell/ash.c index 13eeab34b..da9c95045 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -11500,7 +11500,7 @@ parsesub: { || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) ) { #if ENABLE_ASH_BASH_COMPAT - if (c == '\'') + if (syntax != DQSYNTAX && c == '\'') bash_dollar_squote = 1; else #endif diff --git a/shell/ash_test/ash-heredoc/heredoc4.right b/shell/ash_test/ash-heredoc/heredoc4.right new file mode 100644 index 000000000..371b092e2 --- /dev/null +++ b/shell/ash_test/ash-heredoc/heredoc4.right @@ -0,0 +1 @@ +'$' diff --git a/shell/ash_test/ash-heredoc/heredoc4.tests b/shell/ash_test/ash-heredoc/heredoc4.tests new file mode 100755 index 000000000..642ddb324 --- /dev/null +++ b/shell/ash_test/ash-heredoc/heredoc4.tests @@ -0,0 +1,3 @@ +cat < Date: Sun, 17 Apr 2016 21:05:34 +0200 Subject: unzip: fix a case where we find wrong CDE. Closes 8821 function old new delta unzip_main 2472 2490 +18 Signed-off-by: Denys Vlasenko --- archival/unzip.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/archival/unzip.c b/archival/unzip.c index f41ab6f44..b0a6ca836 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -45,6 +45,12 @@ #include "libbb.h" #include "bb_archive.h" +#if 0 +# define dbg(...) bb_error_msg(__VA_ARGS__) +#else +# define dbg(...) ((void)0) +#endif + enum { #if BB_BIG_ENDIAN ZIP_FILEHEADER_MAGIC = 0x504b0304, @@ -193,15 +199,17 @@ static uint32_t find_cdf_offset(void) unsigned char *p; off_t end; unsigned char *buf = xzalloc(PEEK_FROM_END); + uint32_t found; end = xlseek(zip_fd, 0, SEEK_END); end -= PEEK_FROM_END; if (end < 0) end = 0; - xlseek(zip_fd, end, SEEK_SET); + dbg("Looking for cdf_offset starting from 0x%"OFF_FMT"x", end); + xlseek(zip_fd, end, SEEK_SET); full_read(zip_fd, buf, PEEK_FROM_END); - cde_header.formatted.cdf_offset = BAD_CDF_OFFSET; + found = BAD_CDF_OFFSET; p = buf; while (p <= buf + PEEK_FROM_END - CDE_HEADER_LEN - 4) { if (*p != 'P') { @@ -220,14 +228,25 @@ static uint32_t find_cdf_offset(void) /* * I've seen .ZIP files with seemingly valid CDEs * where cdf_offset points past EOF - ?? - * Ignore such CDEs: + * This check ignores such CDEs: */ - if (cde_header.formatted.cdf_offset < end + (p - buf)) - break; - cde_header.formatted.cdf_offset = BAD_CDF_OFFSET; + if (cde_header.formatted.cdf_offset < end + (p - buf)) { + found = cde_header.formatted.cdf_offset; + dbg("Possible cdf_offset:0x%x at 0x%"OFF_FMT"x", + (unsigned)found, end + (p-3 - buf)); + dbg(" cdf_offset+cdf_size:0x%x", + (unsigned)(found + SWAP_LE32(cde_header.formatted.cdf_size))); + /* + * We do not "break" here because only the last CDE is valid. + * I've seen a .zip archive which contained a .zip file, + * uncompressed, and taking the first CDE was using + * the CDE inside that file! + */ + } } free(buf); - return cde_header.formatted.cdf_offset; + dbg("Found cdf_offset:0x%x", (unsigned)found); + return found; }; static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr) @@ -240,9 +259,13 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr) cdf_offset = find_cdf_offset(); if (cdf_offset != BAD_CDF_OFFSET) { + dbg("Reading CDF at 0x%x", (unsigned)cdf_offset); xlseek(zip_fd, cdf_offset + 4, SEEK_SET); xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN); FIX_ENDIANNESS_CDF(*cdf_ptr); + dbg("file_name_length:%u", (unsigned)cdf_ptr->formatted.file_name_length); + dbg("extra_field_length:%u", (unsigned)cdf_ptr->formatted.extra_field_length); + dbg("file_comment_length:%u", (unsigned)cdf_ptr->formatted.file_comment_length); cdf_offset += 4 + CDF_HEADER_LEN + cdf_ptr->formatted.file_name_length + cdf_ptr->formatted.extra_field_length @@ -532,11 +555,14 @@ int unzip_main(int argc, char **argv) /* Check magic number */ xread(zip_fd, &magic, 4); /* Central directory? It's at the end, so exit */ - if (magic == ZIP_CDF_MAGIC) + if (magic == ZIP_CDF_MAGIC) { + dbg("got ZIP_CDF_MAGIC"); break; + } #if ENABLE_DESKTOP /* Data descriptor? It was a streaming file, go on */ if (magic == ZIP_DD_MAGIC) { + dbg("got ZIP_DD_MAGIC"); /* skip over duplicate crc32, cmpsize and ucmpsize */ unzip_skip(3 * 4); continue; @@ -544,6 +570,7 @@ int unzip_main(int argc, char **argv) #endif if (magic != ZIP_FILEHEADER_MAGIC) bb_error_msg_and_die("invalid zip magic %08X", (int)magic); + dbg("got ZIP_FILEHEADER_MAGIC"); /* Read the file header */ xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN); -- cgit v1.2.3-55-g6feb From bca4deee8393395f77630ad320c306d06ea186ed Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Apr 2016 01:14:05 +0200 Subject: unzip: fix percent overflow; show "stored" files properly Signed-off-by: Denys Vlasenko --- archival/unzip.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/archival/unzip.c b/archival/unzip.c index b0a6ca836..7bf51f466 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -263,15 +263,18 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr) xlseek(zip_fd, cdf_offset + 4, SEEK_SET); xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN); FIX_ENDIANNESS_CDF(*cdf_ptr); - dbg("file_name_length:%u", (unsigned)cdf_ptr->formatted.file_name_length); - dbg("extra_field_length:%u", (unsigned)cdf_ptr->formatted.extra_field_length); - dbg("file_comment_length:%u", (unsigned)cdf_ptr->formatted.file_comment_length); + dbg(" file_name_length:%u extra_field_length:%u file_comment_length:%u", + (unsigned)cdf_ptr->formatted.file_name_length, + (unsigned)cdf_ptr->formatted.extra_field_length, + (unsigned)cdf_ptr->formatted.file_comment_length + ); cdf_offset += 4 + CDF_HEADER_LEN + cdf_ptr->formatted.file_name_length + cdf_ptr->formatted.extra_field_length + cdf_ptr->formatted.file_comment_length; } + dbg("Returning file position to 0x%"OFF_FMT"x", org); xlseek(zip_fd, org, SEEK_SET); return cdf_offset; }; @@ -614,6 +617,11 @@ int unzip_main(int argc, char **argv) bb_error_msg_and_die("can't find file table"); } #endif + dbg("File cmpsize:0x%x extra_len:0x%x ucmpsize:0x%x", + (unsigned)zip_header.formatted.cmpsize, + (unsigned)zip_header.formatted.extra_len, + (unsigned)zip_header.formatted.ucmpsize + ); /* Read filename */ free(dst_fn); @@ -646,16 +654,31 @@ int unzip_main(int argc, char **argv) (dostime & 0x0000f800) >> 11, (dostime & 0x000007e0) >> 5, dst_fn); - total_usize += zip_header.formatted.ucmpsize; } else { unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize; + if ((int32_t)percents < 0) + percents = 0; /* happens if ucmpsize < cmpsize */ percents = percents * 100; if (zip_header.formatted.ucmpsize) percents /= zip_header.formatted.ucmpsize; // " Length Method Size Ratio Date Time CRC-32 Name\n" // "-------- ------ ------- ----- ---- ---- ------ ----" - printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n", + printf( "%8u %s" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n", (unsigned)zip_header.formatted.ucmpsize, + zip_header.formatted.method == 0 ? "Stored" : "Defl:N", /* Defl is method 8 */ +/* TODO: show other methods? + * 1 - Shrunk + * 2 - Reduced with compression factor 1 + * 3 - Reduced with compression factor 2 + * 4 - Reduced with compression factor 3 + * 5 - Reduced with compression factor 4 + * 6 - Imploded + * 7 - Reserved for Tokenizing compression algorithm + * 9 - Deflate64 + * 10 - PKWARE Data Compression Library Imploding + * 11 - Reserved by PKWARE + * 12 - BZIP2 + */ (unsigned)zip_header.formatted.cmpsize, (unsigned)percents, (dostime & 0x01e00000) >> 21, @@ -665,9 +688,9 @@ int unzip_main(int argc, char **argv) (dostime & 0x000007e0) >> 5, zip_header.formatted.crc32, dst_fn); - total_usize += zip_header.formatted.ucmpsize; total_size += zip_header.formatted.cmpsize; } + total_usize += zip_header.formatted.ucmpsize; i = 'n'; } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */ -- cgit v1.2.3-55-g6feb From 07bd9799217038391c8d299e6a2e031fef23c20b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Apr 2016 01:43:24 +0200 Subject: unzip: better match for "standard" unzip's output; string shrinkage function old new delta unzip_main 2490 2426 -64 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-64) Total: -64 bytes text data bss dec hex filename 924008 906 17160 942074 e5ffa busybox_old 923846 906 17160 941912 e5f58 busybox_unstripped Signed-off-by: Denys Vlasenko --- archival/unzip.c | 62 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/archival/unzip.c b/archival/unzip.c index 7bf51f466..a7532e0ff 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -514,11 +514,11 @@ int unzip_main(int argc, char **argv) printf("Archive: %s\n", src_fn); if (listing) { puts(verbose ? - " Length Method Size Ratio Date Time CRC-32 Name\n" - "-------- ------ ------- ----- ---- ---- ------ ----" + " Length Method Size Cmpr Date Time CRC-32 Name\n" + "-------- ------ ------- ---- ---------- ----- -------- ----" : - " Length Date Time Name\n" - " -------- ---- ---- ----" + " Length Date Time Name\n" + "--------- ---------- ----- ----" ); } } @@ -643,16 +643,20 @@ int unzip_main(int argc, char **argv) if (listing) { /* List entry */ unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16); - if (!verbose) { - // " Length Date Time Name\n" - // " -------- ---- ---- ----" - printf( "%9u %02u-%02u-%02u %02u:%02u %s\n", - (unsigned)zip_header.formatted.ucmpsize, + char dtbuf[sizeof("mm-dd-yyyy hh:mm")]; + sprintf(dtbuf, "%02u-%02u-%04u %02u:%02u", (dostime & 0x01e00000) >> 21, (dostime & 0x001f0000) >> 16, - (((dostime & 0xfe000000) >> 25) + 1980) % 100, + ((dostime & 0xfe000000) >> 25) + 1980, (dostime & 0x0000f800) >> 11, - (dostime & 0x000007e0) >> 5, + (dostime & 0x000007e0) >> 5 + ); + if (!verbose) { + // " Length Date Time Name\n" + // "--------- ---------- ----- ----" + printf( "%9u " "%s " "%s\n", + (unsigned)zip_header.formatted.ucmpsize, + dtbuf, dst_fn); } else { unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize; @@ -661,9 +665,9 @@ int unzip_main(int argc, char **argv) percents = percents * 100; if (zip_header.formatted.ucmpsize) percents /= zip_header.formatted.ucmpsize; - // " Length Method Size Ratio Date Time CRC-32 Name\n" - // "-------- ------ ------- ----- ---- ---- ------ ----" - printf( "%8u %s" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n", + // " Length Method Size Cmpr Date Time CRC-32 Name\n" + // "-------- ------ ------- ---- ---------- ----- -------- ----" + printf( "%8u %s" "%9u%4u%% " "%s " "%08x " "%s\n", (unsigned)zip_header.formatted.ucmpsize, zip_header.formatted.method == 0 ? "Stored" : "Defl:N", /* Defl is method 8 */ /* TODO: show other methods? @@ -681,11 +685,7 @@ int unzip_main(int argc, char **argv) */ (unsigned)zip_header.formatted.cmpsize, (unsigned)percents, - (dostime & 0x01e00000) >> 21, - (dostime & 0x001f0000) >> 16, - (((dostime & 0xfe000000) >> 25) + 1980) % 100, - (dostime & 0x0000f800) >> 11, - (dostime & 0x000007e0) >> 5, + dtbuf, zip_header.formatted.crc32, dst_fn); total_size += zip_header.formatted.cmpsize; @@ -793,21 +793,25 @@ int unzip_main(int argc, char **argv) if (listing && quiet <= 1) { if (!verbose) { - // " Length Date Time Name\n" - // " -------- ---- ---- ----" - printf( " -------- -------\n" - "%9lu" " %u files\n", - total_usize, total_entries); + // " Length Date Time Name\n" + // "--------- ---------- ----- ----" + printf( " --------%21s" "-------\n" + "%9lu%21s" "%u files\n", + "", + total_usize, "", total_entries); } else { unsigned long percents = total_usize - total_size; + if ((long)percents < 0) + percents = 0; /* happens if usize < size */ percents = percents * 100; if (total_usize) percents /= total_usize; - // " Length Method Size Ratio Date Time CRC-32 Name\n" - // "-------- ------ ------- ----- ---- ---- ------ ----" - printf( "-------- ------- --- -------\n" - "%8lu" "%17lu%4u%% %u files\n", - total_usize, total_size, (unsigned)percents, + // " Length Method Size Cmpr Date Time CRC-32 Name\n" + // "-------- ------ ------- ---- ---------- ----- -------- ----" + printf( "-------- ------- ----%28s" "----\n" + "%8lu" "%17lu%4u%%%28s" "%u files\n", + "", + total_usize, total_size, (unsigned)percents, "", total_entries); } } -- cgit v1.2.3-55-g6feb From 5598bdf0d3d46a865a4d23785e2d09e6db9be420 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 18 Apr 2016 02:34:29 +0200 Subject: unzip: shorter code for date/time generation function old new delta unzip_main 2426 2414 -12 Signed-off-by: Denys Vlasenko --- archival/unzip.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/archival/unzip.c b/archival/unzip.c index a7532e0ff..be32e60e2 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -642,14 +642,14 @@ int unzip_main(int argc, char **argv) } else { if (listing) { /* List entry */ - unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16); char dtbuf[sizeof("mm-dd-yyyy hh:mm")]; sprintf(dtbuf, "%02u-%02u-%04u %02u:%02u", - (dostime & 0x01e00000) >> 21, - (dostime & 0x001f0000) >> 16, - ((dostime & 0xfe000000) >> 25) + 1980, - (dostime & 0x0000f800) >> 11, - (dostime & 0x000007e0) >> 5 + (zip_header.formatted.moddate >> 5) & 0xf, // mm: 0x01e0 + (zip_header.formatted.moddate) & 0x1f, // dd: 0x001f + (zip_header.formatted.moddate >> 9) + 1980, // yy: 0xfe00 + (zip_header.formatted.modtime >> 11), // hh: 0xf800 + (zip_header.formatted.modtime >> 5) & 0x3f // mm: 0x07e0 + // seconds/2 are not shown, encoded in ----------- 0x001f ); if (!verbose) { // " Length Date Time Name\n" -- cgit v1.2.3-55-g6feb From e6a2f4cc5a47d3022bdf5ca2cacbaa5a8c5baf7a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 16:26:30 +0200 Subject: 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; -- cgit v1.2.3-55-g6feb From 47cfbf32fd66563f8c4e09ad6cced6abfbe2fad5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 18:18:48 +0200 Subject: *: add most of the required setup_common_bufsiz() calls Signed-off-by: Denys Vlasenko --- archival/cpio.c | 1 + archival/lzop.c | 2 +- archival/rpm.c | 2 +- console-tools/resize.c | 3 +++ coreutils/dd.c | 1 + coreutils/du.c | 2 +- coreutils/expr.c | 2 +- coreutils/ls.c | 1 + coreutils/od_bloaty.c | 1 + coreutils/tail.c | 2 +- debianutils/run_parts.c | 2 +- debianutils/start_stop_daemon.c | 1 + e2fsprogs/fsck.c | 1 + editors/sed.c | 1 + findutils/find.c | 1 + findutils/grep.c | 1 + findutils/xargs.c | 1 + init/bootchartd.c | 2 +- loginutils/login.c | 2 +- miscutils/crond.c | 1 + miscutils/dc.c | 1 + miscutils/hdparm.c | 1 + networking/arp.c | 1 + networking/arping.c | 1 + networking/ftpd.c | 1 + networking/ftpgetput.c | 1 + networking/ifupdown.c | 2 +- networking/inetd.c | 1 + networking/ping.c | 3 ++- networking/slattach.c | 2 +- networking/tc.c | 1 + networking/tcpudp.c | 1 + networking/telnet.c | 1 + networking/telnetd.c | 1 + networking/tftp.c | 1 + networking/udhcp/dhcprelay.c | 3 +++ networking/zcip.c | 2 +- procps/free.c | 2 +- procps/fuser.c | 1 + procps/ps.c | 2 +- procps/top.c | 1 + runit/runsv.c | 1 + runit/runsvdir.c | 2 +- runit/sv.c | 2 +- scripts/generate_BUFSIZ.sh | 2 ++ selinux/setfiles.c | 1 + sysklogd/logread.c | 1 + util-linux/mdev.c | 1 + util-linux/mkswap.c | 3 +++ util-linux/more.c | 2 +- util-linux/mount.c | 2 +- util-linux/swaponoff.c | 2 +- util-linux/uevent.c | 3 +++ 53 files changed, 63 insertions(+), 19 deletions(-) diff --git a/archival/cpio.c b/archival/cpio.c index a3036e1ab..3b1550720 100644 --- a/archival/cpio.c +++ b/archival/cpio.c @@ -174,6 +174,7 @@ struct globals { #define G (*(struct globals*)bb_common_bufsiz1) void BUG_cpio_globals_too_big(void); #define INIT_G() do { \ + setup_common_bufsiz(); \ G.owner_ugid.uid = -1L; \ G.owner_ugid.gid = -1L; \ } while (0) diff --git a/archival/lzop.c b/archival/lzop.c index 1371c9751..4afa21889 100644 --- a/archival/lzop.c +++ b/archival/lzop.c @@ -445,7 +445,7 @@ struct globals { chksum_t chksum_out; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) //#define G (*ptr_to_globals) //#define INIT_G() do { // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); diff --git a/archival/rpm.c b/archival/rpm.c index 079b7a95b..83160f975 100644 --- a/archival/rpm.c +++ b/archival/rpm.c @@ -95,7 +95,7 @@ struct globals { int tagcount; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void extract_cpio(int fd, const char *source_rpm) { diff --git a/console-tools/resize.c b/console-tools/resize.c index ed80aa082..a3342a195 100644 --- a/console-tools/resize.c +++ b/console-tools/resize.c @@ -19,6 +19,7 @@ #define ESC "\033" #define old_termios_p ((struct termios*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void onintr(int sig UNUSED_PARAM) @@ -34,6 +35,8 @@ int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) struct winsize w = { 0, 0, 0, 0 }; int ret; + INIT_G(); + /* We use _stderr_ in order to make resize usable * in shell backticks (those redirect stdout away from tty). * NB: other versions of resize open "/dev/tty" diff --git a/coreutils/dd.c b/coreutils/dd.c index a5b8882a0..4dc302926 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -111,6 +111,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ } while (0) diff --git a/coreutils/du.c b/coreutils/du.c index 3d6777670..1240bcbbc 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -87,7 +87,7 @@ struct globals { dev_t dir_dev; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void print(unsigned long long size, const char *filename) diff --git a/coreutils/expr.c b/coreutils/expr.c index 59a66d9c5..ce6b2d189 100644 --- a/coreutils/expr.c +++ b/coreutils/expr.c @@ -101,7 +101,7 @@ struct globals { char **args; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /* forward declarations */ static VALUE *eval(void); diff --git a/coreutils/ls.c b/coreutils/ls.c index e8c3e0490..344b4e61e 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -368,6 +368,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ IF_FEATURE_AUTOWIDTH(G_terminal_width = TERMINAL_WIDTH;) \ diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index 1e252caf0..c8a654165 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -217,6 +217,7 @@ enum { G_pseudo_offset = 0 }; #endif #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ G.bytes_per_block = 32; \ } while (0) diff --git a/coreutils/tail.c b/coreutils/tail.c index cdc9fb66a..39f87679e 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -56,7 +56,7 @@ struct globals { bool exitcode; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void tail_xprint_header(const char *fmt, const char *filename) { diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index a5e53576c..c671b9252 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c @@ -100,7 +100,7 @@ struct globals { #define names (G.names) #define cur (G.cur ) #define cmd (G.cmd ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 }; diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index 6b8d53b13..3625ffee8 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -200,6 +200,7 @@ struct globals { #define user_id (G.user_id ) #define signal_nr (G.signal_nr ) #define INIT_G() do { \ + setup_common_bufsiz(); \ user_id = -1; \ signal_nr = 15; \ } while (0) diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c index b534568c2..59514a1a6 100644 --- a/e2fsprogs/fsck.c +++ b/e2fsprogs/fsck.c @@ -172,6 +172,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/editors/sed.c b/editors/sed.c index 330190e78..ed48de17f 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -164,6 +164,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ G.sed_cmd_tail = &G.sed_cmd_head; \ } while (0) diff --git a/findutils/find.c b/findutils/find.c index 32d830337..d71c69782 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -424,6 +424,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ diff --git a/findutils/grep.c b/findutils/grep.c index a669ac80b..b072cd441 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -204,6 +204,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) #define max_matches (G.max_matches ) diff --git a/findutils/xargs.c b/findutils/xargs.c index bfbd94960..ae01a49be 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -103,6 +103,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \ diff --git a/init/bootchartd.c b/init/bootchartd.c index 5101b28ae..7f511e650 100644 --- a/init/bootchartd.c +++ b/init/bootchartd.c @@ -117,7 +117,7 @@ struct globals { char jiffy_line[COMMON_BUFSIZE]; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void dump_file(FILE *fp, const char *filename) { diff --git a/loginutils/login.c b/loginutils/login.c index ea7c5a23d..94b6c45db 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -140,7 +140,7 @@ struct globals { struct termios tty_attrs; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_NOLOGIN diff --git a/miscutils/crond.c b/miscutils/crond.c index 8536d43c5..f96c96ee7 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -143,6 +143,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.log_level = 8; \ G.crontab_dir_name = CRONTABS; \ } while (0) diff --git a/miscutils/dc.c b/miscutils/dc.c index 3fbb89f5b..4d92bc3d0 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -53,6 +53,7 @@ enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof( #define base (G.base ) #define stack (G.stack ) #define INIT_G() do { \ + setup_common_bufsiz(); \ base = 10; \ } while (0) diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 9e141de2f..b4c5876d4 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c @@ -432,6 +432,7 @@ struct globals { #define hwif_ctrl (G.hwif_ctrl ) #define hwif_irq (G.hwif_irq ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/arp.c b/networking/arp.c index 5f7818663..9381eb53a 100644 --- a/networking/arp.c +++ b/networking/arp.c @@ -76,6 +76,7 @@ struct globals { #define device (G.device ) #define hw_set (G.hw_set ) #define INIT_G() do { \ + setup_common_bufsiz(); \ device = ""; \ } while (0) diff --git a/networking/arping.c b/networking/arping.c index 52f5ba51f..6b0de4de2 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -77,6 +77,7 @@ struct globals { #define brd_recv (G.brd_recv ) #define req_recv (G.req_recv ) #define INIT_G() do { \ + setup_common_bufsiz(); \ count = -1; \ } while (0) diff --git a/networking/ftpd.c b/networking/ftpd.c index 8553a28f5..360d1e6be 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c @@ -126,6 +126,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* Moved to main */ \ /*strcpy(G.msg_ok + 4, MSG_OK );*/ \ /*strcpy(G.msg_err + 4, MSG_ERR);*/ \ diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 61bc45c4e..91fb4569a 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -71,6 +71,7 @@ enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; #define do_continue (G.do_continue ) #define buf (G.buf ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 399ff6b5d..25b04c9d7 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c @@ -131,7 +131,7 @@ struct globals { char *shell; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static const char keywords_up_down[] ALIGN1 = diff --git a/networking/inetd.c b/networking/inetd.c index aa35ffa2b..8d44b5198 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -350,6 +350,7 @@ enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) }; #define allsock (G.allsock ) #define line (G.line ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ rlim_ofile_cur = OPEN_MAX; \ global_queuelen = 128; \ diff --git a/networking/ping.c b/networking/ping.c index 761660979..cfe682646 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -188,7 +188,7 @@ struct globals { char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void noresp(int ign UNUSED_PARAM) { @@ -398,6 +398,7 @@ struct globals { #define pingaddr (G.pingaddr ) #define rcvd_tbl (G.rcvd_tbl ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ datalen = DEFDATALEN; \ timeout = MAXWAIT; \ diff --git a/networking/slattach.c b/networking/slattach.c index d9d8fe7b8..2d1305e32 100644 --- a/networking/slattach.c +++ b/networking/slattach.c @@ -39,7 +39,7 @@ struct globals { #define handle (G.handle ) #define saved_disc (G.saved_disc ) #define saved_state (G.saved_state ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /* diff --git a/networking/tc.c b/networking/tc.c index 1372ca081..d0bcbdeaa 100644 --- a/networking/tc.c +++ b/networking/tc.c @@ -71,6 +71,7 @@ struct globals { #define filter_prio (G.filter_prio) #define filter_proto (G.filter_proto) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/tcpudp.c b/networking/tcpudp.c index 624973042..31bc70459 100644 --- a/networking/tcpudp.c +++ b/networking/tcpudp.c @@ -101,6 +101,7 @@ struct globals { #define env_cur (G.env_cur ) #define env_var (G.env_var ) #define INIT_G() do { \ + setup_common_bufsiz(); \ cmax = 30; \ env_cur = &env_var[0]; \ } while (0) diff --git a/networking/telnet.c b/networking/telnet.c index 2946bc831..d2daf5c8c 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -111,6 +111,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/telnetd.c b/networking/telnetd.c index 13d5a8f64..13c36aa46 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -85,6 +85,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.loginpath = "/bin/login"; \ G.issuefile = "/etc/issue.net"; \ } while (0) diff --git a/networking/tftp.c b/networking/tftp.c index 8aeb79aca..e879c4674 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -131,6 +131,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/udhcp/dhcprelay.c b/networking/udhcp/dhcprelay.c index 1722a85de..f52a0cf88 100644 --- a/networking/udhcp/dhcprelay.c +++ b/networking/udhcp/dhcprelay.c @@ -34,6 +34,7 @@ struct xid_item { } FIX_ALIASING; #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client) { @@ -257,6 +258,8 @@ int dhcprelay_main(int argc, char **argv) int num_sockets, max_socket; uint32_t our_nip; + INIT_G(); + server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); server_addr.sin_port = htons(SERVER_PORT); diff --git a/networking/zcip.c b/networking/zcip.c index 79643458c..47f3216a0 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -92,7 +92,7 @@ struct globals { uint32_t localnet_ip; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /** diff --git a/procps/free.c b/procps/free.c index 9fde64b64..fca9a2242 100644 --- a/procps/free.c +++ b/procps/free.c @@ -37,7 +37,7 @@ struct globals { #endif } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static unsigned long long scale(unsigned long d) diff --git a/procps/fuser.c b/procps/fuser.c index 2cda0f9d7..6dac852ed 100644 --- a/procps/fuser.c +++ b/procps/fuser.c @@ -46,6 +46,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.mypid = getpid(); \ G.killsig = SIGKILL; \ } while (0) diff --git a/procps/ps.c b/procps/ps.c index 65d62e256..08dfce12e 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -153,7 +153,7 @@ struct globals { #define buffer (G.buffer ) #define terminal_width (G.terminal_width ) #define kernel_HZ (G.kernel_HZ ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_PS_TIME /* for ELF executables, notes are pushed before environment and args */ diff --git a/procps/top.c b/procps/top.c index 1c42b249c..640bcdc6d 100644 --- a/procps/top.c +++ b/procps/top.c @@ -202,6 +202,7 @@ enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; #define total_pcpu (G.total_pcpu ) #define line_buf (G.line_buf ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ BUILD_BUG_ON(LINE_BUF_SIZE <= 80); \ } while (0) diff --git a/runit/runsv.c b/runit/runsv.c index 8833f4c96..e0e31508a 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -115,6 +115,7 @@ struct globals { #define dir (G.dir ) #define svd (G.svd ) #define INIT_G() do { \ + setup_common_bufsiz(); \ pidchanged = 1; \ } while (0) diff --git a/runit/runsvdir.c b/runit/runsvdir.c index 49c8f5b48..2b7927542 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c @@ -93,7 +93,7 @@ struct globals { #define logpipe (G.logpipe ) #define pfd (G.pfd ) #define stamplog (G.stamplog ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void fatal2_cannot(const char *m1, const char *m2) { diff --git a/runit/sv.c b/runit/sv.c index e83a29781..2a256a6b4 100644 --- a/runit/sv.c +++ b/runit/sv.c @@ -207,7 +207,7 @@ struct globals { #define tstart (G.tstart ) #define tnow (G.tnow ) #define svstatus (G.svstatus ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define str_equal(s,t) (!strcmp((s), (t))) diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index afe9eee1e..bb0738641 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -111,4 +111,6 @@ fi if test $OLD != $REM; then echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." +else + echo "COMMON_BUFSIZE = $REM bytes" fi diff --git a/selinux/setfiles.c b/selinux/setfiles.c index 441345ae9..51a7e63bd 100644 --- a/selinux/setfiles.c +++ b/selinux/setfiles.c @@ -80,6 +80,7 @@ struct globals { #define G (*(struct globals*)bb_common_bufsiz1) void BUG_setfiles_globals_too_big(void); #define INIT_G() do { \ + setup_common_bufsiz(); \ if (sizeof(G) > COMMON_BUFSIZE) \ BUG_setfiles_globals_too_big(); \ /* memset(&G, 0, sizeof(G)); - already is */ \ diff --git a/sysklogd/logread.c b/sysklogd/logread.c index ebd7f8b2c..5b999730a 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -73,6 +73,7 @@ struct globals { #define SMrdn (G.SMrdn) #define shbuf (G.shbuf) #define INIT_G() do { \ + setup_common_bufsiz(); \ memcpy(SMrup, init_sem, sizeof(init_sem)); \ } while (0) diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 7473b1855..37514eb54 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -288,6 +288,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \ IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \ } while (0) diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index f9451792b..dcb53f008 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c @@ -76,6 +76,7 @@ struct swap_header_v1 { #define NWORDS 129 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) struct BUG_sizes { char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; @@ -93,6 +94,8 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv) off_t len; const char *label = ""; + INIT_G(); + opt_complementary = "-1"; /* at least one param */ /* TODO: -p PAGESZ, -U UUID */ getopt32(argv, "L:", &label); diff --git a/util-linux/more.c b/util-linux/more.c index 58be3ac3b..95cbdd994 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -33,10 +33,10 @@ struct globals { struct termios new_settings; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() ((void)0) #define initial_settings (G.initial_settings) #define new_settings (G.new_settings ) #define cin_fileno (G.cin_fileno ) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define setTermSettings(fd, argp) \ do { \ diff --git a/util-linux/mount.c b/util-linux/mount.c index e5c85feff..244f4fa27 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -457,7 +457,7 @@ enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_b #endif #define fslist (G.fslist ) #define getmntent_buf (G.getmntent_buf ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_MTAB_SUPPORT /* diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c index 43228a6ba..6713852e5 100644 --- a/util-linux/swaponoff.c +++ b/util-linux/swaponoff.c @@ -72,7 +72,7 @@ struct globals { #define save_g_flags() ((void)0) #define restore_g_flags() ((void)0) #endif -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define do_swapoff (applet_name[5] == 'f') diff --git a/util-linux/uevent.c b/util-linux/uevent.c index 58668fa5d..b98fe6160 100644 --- a/util-linux/uevent.c +++ b/util-linux/uevent.c @@ -31,6 +31,7 @@ #define BUFFER_SIZE 16*1024 #define env ((char **)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) enum { MAX_ENV = COMMON_BUFSIZE / sizeof(env[0]) - 1, }; @@ -46,6 +47,8 @@ int uevent_main(int argc UNUSED_PARAM, char **argv) struct sockaddr_nl sa; int fd; + INIT_G(); + argv++; // Subscribe for UEVENT kernel messages -- cgit v1.2.3-55-g6feb From 9de2e5a22213842da5b116723392de88de9ed419 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 18:38:51 +0200 Subject: *: hopefully all setup_common_bufsiz() are in place Signed-off-by: Denys Vlasenko --- archival/tar.c | 2 ++ console-tools/dumpkmap.c | 1 + coreutils/catv.c | 4 +++- coreutils/cksum.c | 6 +++--- coreutils/date.c | 6 +++--- coreutils/split.c | 2 ++ coreutils/stat.c | 7 +++---- coreutils/sum.c | 4 +++- coreutils/tee.c | 6 +++--- editors/diff.c | 1 + editors/ed.c | 8 ++++---- miscutils/chat.c | 3 ++- miscutils/conspy.c | 7 ++++--- miscutils/fbsplash.c | 7 ++++--- miscutils/inotifyd.c | 6 +++--- miscutils/less.c | 7 ++++--- miscutils/microcom.c | 6 +++--- networking/httpd.c | 1 + networking/isrv_identd.c | 7 ++++--- networking/libiproute/ipaddress.c | 5 ++++- networking/libiproute/ipneigh.c | 3 +++ networking/libiproute/iproute.c | 3 +++ networking/nc.c | 3 ++- procps/nmeter.c | 9 ++++----- runit/svlogd.c | 4 ++-- sysklogd/klogd.c | 7 ++++--- sysklogd/logger.c | 2 ++ util-linux/script.c | 10 +++++----- util-linux/umount.c | 1 + 29 files changed, 83 insertions(+), 55 deletions(-) diff --git a/archival/tar.c b/archival/tar.c index caf4363de..346a9404e 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -162,6 +162,7 @@ #define block_buf bb_common_bufsiz1 +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_TAR_CREATE @@ -964,6 +965,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv) #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM llist_t *excludes = NULL; #endif + INIT_G(); /* Initialise default values */ tar_handle = init_handle(); diff --git a/console-tools/dumpkmap.c b/console-tools/dumpkmap.c index 6412dffc8..b6fd466dc 100644 --- a/console-tools/dumpkmap.c +++ b/console-tools/dumpkmap.c @@ -38,6 +38,7 @@ int dumpkmap_main(int argc UNUSED_PARAM, char **argv) struct kbentry ke; int i, j, fd; #define flags bb_common_bufsiz1 + setup_common_bufsiz(); /* When user accidentally runs "dumpkmap FILE" * instead of "dumpkmap >FILE", we'd dump binary stuff to tty. diff --git a/coreutils/catv.c b/coreutils/catv.c index 801d2451d..0e71368a5 100644 --- a/coreutils/catv.c +++ b/coreutils/catv.c @@ -49,6 +49,9 @@ int catv_main(int argc UNUSED_PARAM, char **argv) /* Read from stdin if there's nothing else to do. */ if (!argv[0]) *--argv = (char*)"-"; + +#define read_buf bb_common_bufsiz1 + setup_common_bufsiz(); do { fd = open_or_warn_stdin(*argv); if (fd < 0) { @@ -58,7 +61,6 @@ int catv_main(int argc UNUSED_PARAM, char **argv) for (;;) { int i, res; -#define read_buf bb_common_bufsiz1 res = read(fd, read_buf, COMMON_BUFSIZE); if (res < 0) retval = EXIT_FAILURE; diff --git a/coreutils/cksum.c b/coreutils/cksum.c index d8351e7c6..8a8a39f68 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c @@ -33,6 +33,7 @@ int cksum_main(int argc UNUSED_PARAM, char **argv) argv++; #endif + setup_common_bufsiz(); do { int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); @@ -43,9 +44,8 @@ int cksum_main(int argc UNUSED_PARAM, char **argv) crc = 0; length = 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) { +#define read_buf bb_common_bufsiz1 + while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 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 59b4b8f2a..ff3214d85 100644 --- a/coreutils/date.c +++ b/coreutils/date.c @@ -368,8 +368,8 @@ int date_main(int argc UNUSED_PARAM, char **argv) } #endif -#define date_buf bb_common_bufsiz1 -#define sizeof_date_buf COMMON_BUFSIZE +#define date_buf bb_common_bufsiz1 + setup_common_bufsiz(); if (*fmt_dt2str == '\0') { /* With no format string, just print a blank line */ date_buf[0] = '\0'; @@ -379,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, COMMON_BUFSIZE, fmt_dt2str, &tm_time); } puts(date_buf); diff --git a/coreutils/split.c b/coreutils/split.c index b2da74e27..e67c3de66 100644 --- a/coreutils/split.c +++ b/coreutils/split.c @@ -79,6 +79,8 @@ int split_main(int argc UNUSED_PARAM, char **argv) ssize_t bytes_read, to_write; char *src; + setup_common_bufsiz(); + opt_complementary = "?2:a+"; /* max 2 args; -a N */ opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len); diff --git a/coreutils/stat.c b/coreutils/stat.c index 78df9c948..ddcfcf2d7 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -158,10 +158,9 @@ 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 sizeof_buf COMMON_BUFSIZE - - strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof_buf, &t), ".000000000"); +#define buf bb_common_bufsiz1 + setup_common_bufsiz(); + strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000"); return buf; #undef buf } diff --git a/coreutils/sum.c b/coreutils/sum.c index cc6677221..ec9ed2a11 100644 --- a/coreutils/sum.c +++ b/coreutils/sum.c @@ -31,12 +31,14 @@ enum { SUM_BSD, PRINT_NAME, SUM_SYSV }; /* Return 1 if successful. */ static unsigned sum_file(const char *file, unsigned type) { -#define buf bb_common_bufsiz1 unsigned long long total_bytes = 0; int fd, r; /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ unsigned s = 0; +#define buf bb_common_bufsiz1 + setup_common_bufsiz(); + fd = open_or_warn_stdin(file); if (fd == -1) return 0; diff --git a/coreutils/tee.c b/coreutils/tee.c index a0e177cbc..a68e9446f 100644 --- a/coreutils/tee.c +++ b/coreutils/tee.c @@ -37,8 +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 sizeof_buf COMMON_BUFSIZE +# define buf bb_common_bufsiz1 + setup_common_bufsiz(); #else int c; #endif @@ -81,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, COMMON_BUFSIZE)) > 0) { fp = files; do fwrite(buf, 1, c, *fp); diff --git a/editors/diff.c b/editors/diff.c index 3c8e9074a..ff269360f 100644 --- a/editors/diff.c +++ b/editors/diff.c @@ -749,6 +749,7 @@ static int diffreg(char *file[2]) fp[i] = fdopen(fd, "r"); } + setup_common_bufsiz(); while (1) { const size_t sz = COMMON_BUFSIZE / 2; char *const buf0 = bb_common_bufsiz1; diff --git a/editors/ed.c b/editors/ed.c index 8da7b1dd5..c028b78cb 100644 --- a/editors/ed.c +++ b/editors/ed.c @@ -33,12 +33,11 @@ typedef struct LINE { } LINE; -#define searchString bb_common_bufsiz1 -#define sizeof_searchString COMMON_BUFSIZE +#define searchString bb_common_bufsiz1 enum { - USERSIZE = sizeof_searchString > 1024 ? 1024 - : sizeof_searchString - 1, /* max line length typed in by user */ + USERSIZE = COMMON_BUFSIZE > 1024 ? 1024 + : COMMON_BUFSIZE - 1, /* max line length typed in by user */ INITBUF_SIZE = 1024, /* initial buffer size */ }; @@ -68,6 +67,7 @@ struct globals { #define lines (G.lines ) #define marks (G.marks ) #define INIT_G() do { \ + setup_common_bufsiz(); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ } while (0) diff --git a/miscutils/chat.c b/miscutils/chat.c index 25850dd20..6b429f2a6 100644 --- a/miscutils/chat.c +++ b/miscutils/chat.c @@ -286,9 +286,10 @@ int chat_main(int argc UNUSED_PARAM, char **argv) && poll(&pfd, 1, timeout) > 0 && (pfd.revents & POLLIN) ) { -#define buf bb_common_bufsiz1 llist_t *l; ssize_t delta; +#define buf bb_common_bufsiz1 + setup_common_bufsiz(); // read next char from device if (safe_read(STDIN_FILENO, buf+buf_len, 1) > 0) { diff --git a/miscutils/conspy.c b/miscutils/conspy.c index 0d96a5f9a..f6468c116 100644 --- a/miscutils/conspy.c +++ b/miscutils/conspy.c @@ -364,8 +364,6 @@ 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 sizeof_keybuf COMMON_BUFSIZE struct termios termbuf; unsigned opts; unsigned ttynum; @@ -384,6 +382,9 @@ int conspy_main(int argc UNUSED_PARAM, char **argv) applet_long_options = getopt_longopts; #endif +#define keybuf bb_common_bufsiz1 + setup_common_bufsiz(); + INIT_G(); strcpy(G.vcsa_name, DEV_VCSA); @@ -515,7 +516,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, COMMON_BUFSIZE - G.key_count); if (bytes_read < 0) goto abort; diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c index b26ad2c15..3ddf8a242 100644 --- a/miscutils/fbsplash.c +++ b/miscutils/fbsplash.c @@ -373,12 +373,13 @@ 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 sizeof_concat_buf COMMON_BUFSIZE +#define concat_buf bb_common_bufsiz1 + setup_common_bufsiz(); + 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 + COMMON_BUFSIZE - read_ptr; if (rem < 2 || fgets(read_ptr, rem, theme_file) == NULL ) { diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c index 1d28e8f99..52db08ada 100644 --- a/miscutils/inotifyd.c +++ b/miscutils/inotifyd.c @@ -162,10 +162,10 @@ int inotifyd_main(int argc, char **argv) // read out all pending events // (NB: len must be int, not ssize_t or long!) +#define eventbuf bb_common_bufsiz1 + setup_common_bufsiz(); xioctl(pfd.fd, FIONREAD, &len); -#define eventbuf bb_common_bufsiz1 -#define sizeof_eventbuf COMMON_BUFSIZE - ie = buf = (len <= sizeof_eventbuf) ? eventbuf : xmalloc(len); + ie = buf = (len <= COMMON_BUFSIZE) ? 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 94ecf1686..d7076dbbc 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -440,8 +440,6 @@ static int at_end(void) */ static void read_lines(void) { -#define readbuf bb_common_bufsiz1 -#define sizeof_readbuf COMMON_BUFSIZE char *current_line, *p; int w = width; char last_terminated = terminated; @@ -451,6 +449,9 @@ static void read_lines(void) unsigned old_max_fline = max_fline; #endif +#define readbuf bb_common_bufsiz1 + setup_common_bufsiz(); + /* (careful: max_fline can be -1) */ if (max_fline + 1 > MAXLINES) return; @@ -482,7 +483,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, COMMON_BUFSIZE); if (errno != EAGAIN) break; t = time(NULL); diff --git a/miscutils/microcom.c b/miscutils/microcom.c index 5eb2e6743..d9e8f9187 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c @@ -156,11 +156,11 @@ int microcom_main(int argc UNUSED_PARAM, char **argv) skip_write: ; } if (pfd[0].revents) { -#define iobuf bb_common_bufsiz1 -#define sizeof_iobuf COMMON_BUFSIZE ssize_t len; +#define iobuf bb_common_bufsiz1 + setup_common_bufsiz(); // read from device -> write to stdout - len = safe_read(sfd, iobuf, sizeof_iobuf); + len = safe_read(sfd, iobuf, COMMON_BUFSIZE); if (len > 0) full_write(STDOUT_FILENO, iobuf, len); else { diff --git a/networking/httpd.c b/networking/httpd.c index ef90770ac..abe83a458 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -370,6 +370,7 @@ enum { # define content_gzip 0 #endif #define INIT_G() do { \ + setup_common_bufsiz(); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ IF_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \ IF_FEATURE_HTTPD_RANGES(range_start = -1;) \ diff --git a/networking/isrv_identd.c b/networking/isrv_identd.c index f63ed8ee4..8a15926e5 100644 --- a/networking/isrv_identd.c +++ b/networking/isrv_identd.c @@ -29,8 +29,7 @@ typedef struct identd_buf_t { char buf[64 - sizeof(int)]; } identd_buf_t; -#define bogouser bb_common_bufsiz1 -#define sizeof_bogouser COMMON_BUFSIZE +#define bogouser bb_common_bufsiz1 static int new_peer(isrv_state_t *state, int fd) { @@ -117,10 +116,12 @@ int fakeidentd_main(int argc UNUSED_PARAM, char **argv) unsigned opt; int fd; + setup_common_bufsiz(); + opt = getopt32(argv, "fiwb:", &bind_address); strcpy(bogouser, "nobody"); if (argv[optind]) - strncpy(bogouser, argv[optind], sizeof_bogouser - 1); + strncpy(bogouser, argv[optind], COMMON_BUFSIZE - 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 2c0f514c7..d9e099607 100644 --- a/networking/libiproute/ipaddress.c +++ b/networking/libiproute/ipaddress.c @@ -41,7 +41,7 @@ struct filter_t { typedef struct filter_t filter_t; #define G_filter (*(filter_t*)bb_common_bufsiz1) - +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void print_link_flags(unsigned flags, unsigned mdown) { @@ -745,6 +745,9 @@ int FAST_FUNC do_ipaddr(char **argv) /* 0 1 2 3 4 5 6 7 8 */ "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0"; int cmd = 2; + + INIT_G(); + if (*argv) { cmd = index_in_substrings(commands, *argv); if (cmd < 0) diff --git a/networking/libiproute/ipneigh.c b/networking/libiproute/ipneigh.c index 151d3d109..d2028b7b6 100644 --- a/networking/libiproute/ipneigh.c +++ b/networking/libiproute/ipneigh.c @@ -42,6 +42,7 @@ struct filter_t { typedef struct filter_t filter_t; #define G_filter (*(filter_t*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static int flush_update(void) { @@ -339,6 +340,8 @@ int FAST_FUNC do_ipneigh(char **argv) /*0-1*/ "show\0" "flush\0"; int command_num; + INIT_G(); + if (!*argv) return ipneigh_list_or_flush(argv, 0); diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c index 34d4f4758..e674e9a0d 100644 --- a/networking/libiproute/iproute.c +++ b/networking/libiproute/iproute.c @@ -45,6 +45,7 @@ struct filter_t { typedef struct filter_t filter_t; #define G_filter (*(filter_t*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static int flush_update(void) { @@ -903,6 +904,8 @@ int FAST_FUNC do_iproute(char **argv) unsigned flags = 0; int cmd = RTM_NEWROUTE; + INIT_G(); + if (!*argv) return iproute_list_or_flush(argv, 0); diff --git a/networking/nc.c b/networking/nc.c index 50edee450..13a9b48a8 100644 --- a/networking/nc.c +++ b/networking/nc.c @@ -239,6 +239,8 @@ int nc_main(int argc, char **argv) FD_SET(cfd, &readfds); FD_SET(STDIN_FILENO, &readfds); +#define iobuf bb_common_bufsiz1 + setup_common_bufsiz(); for (;;) { int fd; int ofd; @@ -249,7 +251,6 @@ int nc_main(int argc, char **argv) if (select(cfd + 1, &testfds, NULL, NULL, NULL) < 0) bb_perror_msg_and_die("select"); -#define iobuf bb_common_bufsiz1 fd = STDIN_FILENO; while (1) { if (FD_ISSET(fd, &testfds)) { diff --git a/procps/nmeter.c b/procps/nmeter.c index efa3d553d..3eac2d3b2 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c @@ -109,16 +109,15 @@ struct globals { #define proc_meminfo (G.proc_meminfo ) #define proc_diskstats (G.proc_diskstats ) #define proc_sys_fs_filenr (G.proc_sys_fs_filenr) +#define outbuf bb_common_bufsiz1 #define INIT_G() do { \ + setup_common_bufsiz(); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ cur_outbuf = outbuf; \ G.final_char = '\n'; \ G.deltanz = G.delta = 1000000; \ } while (0) -#define outbuf bb_common_bufsiz1 -#define sizeof_outbuf COMMON_BUFSIZE - static inline void reset_outbuf(void) { cur_outbuf = outbuf; @@ -141,7 +140,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 + COMMON_BUFSIZE - p; while (*s && --sz >= 0) *p++ = *s++; cur_outbuf = p; @@ -149,7 +148,7 @@ static void put(const char *s) static void put_c(char c) { - if (cur_outbuf < outbuf + sizeof_outbuf) + if (cur_outbuf < outbuf + COMMON_BUFSIZE) *cur_outbuf++ = c; } diff --git a/runit/svlogd.c b/runit/svlogd.c index 09efdb695..7cae81cb2 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c @@ -234,7 +234,9 @@ struct globals { #define blocked_sigset (G.blocked_sigset) #define fl_flag_0 (G.fl_flag_0 ) #define dirn (G.dirn ) +#define line bb_common_bufsiz1 #define INIT_G() do { \ + setup_common_bufsiz(); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ linemax = 1000; \ /*buflen = 1024;*/ \ @@ -242,8 +244,6 @@ struct globals { replace = ""; \ } while (0) -#define line bb_common_bufsiz1 - #define FATAL "fatal: " #define WARNING "warning: " diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 845c49a5e..4db72110d 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -146,10 +146,9 @@ static void klogd_close(void) #endif -#define log_buffer bb_common_bufsiz1 -#define sizeof_log_buffer COMMON_BUFSIZE +#define log_buffer bb_common_bufsiz1 enum { - KLOGD_LOGBUF_SIZE = sizeof_log_buffer, + KLOGD_LOGBUF_SIZE = COMMON_BUFSIZE, OPT_LEVEL = (1 << 0), OPT_FOREGROUND = (1 << 1), }; @@ -175,6 +174,8 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) int opt; int used; + setup_common_bufsiz(); + opt = getopt32(argv, "c:n", &opt_c); if (opt & OPT_LEVEL) { /* Valid levels are between 1 and 8 */ diff --git a/sysklogd/logger.c b/sysklogd/logger.c index b3ca85703..f9eafeb25 100644 --- a/sysklogd/logger.c +++ b/sysklogd/logger.c @@ -99,6 +99,8 @@ int logger_main(int argc UNUSED_PARAM, char **argv) int opt; int i = 0; + setup_common_bufsiz(); + /* Fill out the name string early (may be overwritten later) */ str_t = uid2uname_utoa(geteuid()); diff --git a/util-linux/script.c b/util-linux/script.c index 6195161bc..86475c1f1 100644 --- a/util-linux/script.c +++ b/util-linux/script.c @@ -109,12 +109,12 @@ int script_main(int argc UNUSED_PARAM, char **argv) if (child_pid) { /* parent */ -#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; smallint fd_count = 2; +#define buf bb_common_bufsiz1 + setup_common_bufsiz(); outfd = xopen(fname, mode); pfd[0].fd = pty; @@ -136,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, COMMON_BUFSIZE); if (count <= 0 && errno != EAGAIN) { /* err/eof from pty: exit */ goto restore; @@ -159,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, COMMON_BUFSIZE); if (count <= 0) { /* err/eof from stdin: don't read stdin anymore */ pfd[1].revents = 0; @@ -178,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, COMMON_BUFSIZE)) > 0) { full_write(STDOUT_FILENO, buf, count); full_write(outfd, buf, count); } diff --git a/util-linux/umount.c b/util-linux/umount.c index be0300394..91da69674 100644 --- a/util-linux/umount.c +++ b/util-linux/umount.c @@ -103,6 +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 { + setup_common_bufsiz(); while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) { /* Match fstype if passed */ if (!match_fstype(&me, fstype)) -- cgit v1.2.3-55-g6feb From df70a43af281f7e653b1c95ff17201669fdcfca3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 18:54:36 +0200 Subject: udhcp: add setup_common_bufsiz() as needed Signed-off-by: Denys Vlasenko --- networking/udhcp/d6_dhcpc.c | 2 ++ networking/udhcp/dhcpc.c | 2 ++ networking/udhcp/dhcpd.c | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index 422254d62..c77669a31 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c @@ -930,6 +930,8 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) int retval; fd_set rfds; + setup_common_bufsiz(); + /* Default options */ IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;) IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;) diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 660b943ce..8f5a03f2e 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -1268,6 +1268,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) int retval; fd_set rfds; + setup_common_bufsiz(); + /* Default options */ IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 2671ea3e2..e93a9f1da 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -310,10 +310,10 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) unsigned arpping_ms; IF_FEATURE_UDHCP_PORT(char *str_P;) -#if ENABLE_FEATURE_UDHCP_PORT - SERVER_PORT = 67; - CLIENT_PORT = 68; -#endif + setup_common_bufsiz(); + + IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) + IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 opt_complementary = "vv"; -- cgit v1.2.3-55-g6feb From f56fb5eb1120a92bdfb6d0ce64b3430b42a2efa0 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 21:03:51 +0200 Subject: libbb: make "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" work Signed-off-by: Denys Vlasenko --- libbb/common_bufsiz.c | 6 +++--- scripts/generate_BUFSIZ.sh | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c index c16c361c9..26faafcbb 100644 --- a/libbb/common_bufsiz.c +++ b/libbb/common_bufsiz.c @@ -55,14 +55,14 @@ char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); # ifndef setup_common_bufsiz /* - * It is not a "((void)0)" macro. It means we have to provide this function. + * It is not defined as a dummy macro. + * It means we have to provide this function. */ char* bb_common_bufsiz1; -char* setup_common_bufsiz(void) +void setup_common_bufsiz(void) { if (!bb_common_bufsiz1) bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); - return bb_common_bufsiz1; } # else # ifndef bb_common_bufsiz1 diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index bb0738641..d54142597 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -87,7 +87,8 @@ if test $REM -lt 1024; then echo "Rerun make to build a binary which doesn't use it!" exit 1 fi - exit 0 + echo "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" + exit 0 fi # _end[] has REM bytes for bb_common_bufsiz1[] @@ -102,7 +103,7 @@ 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: +# Check that code did not grow too much and thus _end[] did not shrink: if test $OLD -gt $REM; then echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!" echo "Rerun make!" -- cgit v1.2.3-55-g6feb From 93e1aaa1c7e5ed6d2704262700ec28837bdfc9b7 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 21:47:45 +0200 Subject: libbb: constify *bb_common_bufsiz1 (if it is compiled to be a pointer) This lets gcc optimize much better: text data bss dec hex filename 922846 910 13056 936812 e4b6c busybox_unstripped.nonconst 920255 910 13056 934221 e414d busybox_unstripped Signed-off-by: Denys Vlasenko --- libbb/common_bufsiz.c | 4 ++-- scripts/generate_BUFSIZ.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c index 26faafcbb..1a3585169 100644 --- a/libbb/common_bufsiz.c +++ b/libbb/common_bufsiz.c @@ -58,11 +58,11 @@ char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); * It is not defined as a dummy macro. * It means we have to provide this function. */ -char* bb_common_bufsiz1; +char *const bb_common_bufsiz1 __attribute__ ((section (".data"))); void setup_common_bufsiz(void) { if (!bb_common_bufsiz1) - bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); + *(char**)&bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); } # else # ifndef bb_common_bufsiz1 diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index d54142597..1914fa0f5 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -77,7 +77,7 @@ if test $REM -lt 1024; then # users will need to malloc it. { echo "enum { COMMON_BUFSIZE = 1024 };" - echo "extern char *bb_common_bufsiz1;" + echo "extern char *const bb_common_bufsiz1;" echo "void setup_common_bufsiz(void);" } | regenerate "$common_bufsiz_h" # Check that we aren't left with a buggy binary: -- cgit v1.2.3-55-g6feb From d7b502c05911e1cf1d4fc31be71f3abccd0927a5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 23:52:35 +0200 Subject: build system: fix generate_BUFSIZ.sh to not alternate 1k and malloc builds Signed-off-by: Denys Vlasenko --- Makefile | 2 +- scripts/generate_BUFSIZ.sh | 180 +++++++++++++++++++++++++++------------------ 2 files changed, 109 insertions(+), 73 deletions(-) diff --git a/Makefile b/Makefile index 75a33c1fb..d5950230d 100644 --- a/Makefile +++ b/Makefile @@ -611,7 +611,7 @@ quiet_cmd_busybox__ ?= LINK $@ "$(core-y)" \ "$(libs-y)" \ "$(LDLIBS)" \ - && $(srctree)/scripts/generate_BUFSIZ.sh include/common_bufsiz.h + && $(srctree)/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h # Generate System.map quiet_cmd_sysmap = SYSMAP diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 1914fa0f5..8aa0174a6 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -7,11 +7,16 @@ debug=false +postcompile=false +test x"$1" = x"--post" && { postcompile=true; shift; } + common_bufsiz_h=$1 test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" +exitcmd="exit 0" + regenerate() { cat >"$1.$$" test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return @@ -19,99 +24,130 @@ regenerate() { } generate_std_and_exit() { - $debug && echo "Default: bb_common_bufsiz1[] in bss" + $debug && echo "Configuring: 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 + echo "std" >"$common_bufsiz_h.method" + $exitcmd } -# 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[]" +generate_big_and_exit() { + $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1" { - echo "enum { COMMON_BUFSIZE = 1024 };" + echo "enum { COMMON_BUFSIZE = $1 };" 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 + echo "$2" >"$common_bufsiz_h.method" + $exitcmd } -# 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. +generate_1k_and_exit() { + generate_big_and_exit 1024 "1k" +} + + +generate_malloc_and_exit() { + $debug && echo "Configuring: bb_common_bufsiz1[] is malloced" { echo "enum { COMMON_BUFSIZE = 1024 };" echo "extern char *const 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 + echo "malloc" >"$common_bufsiz_h.method" + $exitcmd +} + +# User does not want any funky stuff? +test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit + +# The script is run two times: before compilation, when it needs to +# (re)generate $common_bufsiz_h, and directly after successful build, +# when it needs to assess whether the build is ok to use at all (not buggy), +# and (re)generate $common_bufsiz_h for a future build. + +if $postcompile; then + # Postcompile needs to create/delete OK/FAIL files + + test -f busybox_unstripped || exit 1 + test -f "$common_bufsiz_h.method" || exit 1 + + # How the build was done? + method=`cat -- "$common_bufsiz_h.method"` + + # 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 " + echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0" + echo "char page_size[PAGE_SIZE];" + echo "#endif" + } >page_size_$$.c + $CC -c "page_size_$$.c" || exit 1 + PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` + rm "page_size_$$.c" "page_size_$$.o" + test x"$PAGE_SIZE" = x"" && exit 1 + $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" + PAGE_SIZE=$((0x$PAGE_SIZE)) + test $PAGE_SIZE -lt 512 && exit 1 + + # How much space between _end[] and next page? + PAGE_MASK=$((PAGE_SIZE-1)) + COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) + echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" + + if test x"$method" = x"1k"; then + if test $COMMON_BUFSIZE -lt 1024; then + # _end[] has no enough space for bb_common_bufsiz1[] + rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null + { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" + echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" + echo "Rerun make to build a binary which doesn't use it!" + rm busybox_unstripped + exitcmd="exit 1" + else + rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null + echo $COMMON_BUFSIZE >"$common_bufsiz_h.1k.OK" + test $COMMON_BUFSIZE -gt $((1024+32)) && echo "Rerun make to use larger COMMON_BUFSIZE" + fi fi - echo "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" - 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 shrink: -if test $OLD -gt $REM; then - echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!" - echo "Rerun make!" - exit 1 -fi +# Based on past success/fail of 1k build, decide next build type -if test $OLD != $REM; then - echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." -else - echo "COMMON_BUFSIZE = $REM bytes" +if test -f "$common_bufsiz_h.1k.OK"; then + # Previous build succeeded fitting 1k into _end[]. + # Try bigger COMMON_BUFSIZE if possible. + COMMON_BUFSIZE=`cat -- "$common_bufsiz_h.1k.OK"` + # Round down a bit + COMMON_BUFSIZE=$(( (COMMON_BUFSIZE-32) & 0xfffffe0 )) + COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) + test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit + generate_big_and_exit $COMMON_BUFSIZE "big" +fi +if test -f "$common_bufsiz_h.1k.FAIL"; then + # Previous build FAILED to fit 1k into _end[]. + # Was it with same .config? + oldcfg=`cat -- "$common_bufsiz_h.1k.FAIL"` + curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config` + # If yes, then build a "malloced" version + if test x"$oldcfg" = x"$curcfg"; then + echo "Will not try 1k build, it failed before. Touch .config to override" + generate_malloc_and_exit + fi + # else: try 1k version + echo "New .config, will try 1k build" + rm -- "$common_bufsiz_h.1k.FAIL" + generate_1k_and_exit fi +# There was no 1k build yet. Try it. +generate_1k_and_exit -- cgit v1.2.3-55-g6feb From 7ff24bd5fb37c58d9e41743a910df147357dda61 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Apr 2016 00:24:53 +0200 Subject: generate_BUFSIZ.sh: catch BUFSIZE < 1024 also on "big" builds Signed-off-by: Denys Vlasenko --- scripts/generate_BUFSIZ.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 8aa0174a6..750fedbef 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -105,14 +105,14 @@ if $postcompile; then COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" - if test x"$method" = x"1k"; then + if test x"$method" != x"malloc"; then if test $COMMON_BUFSIZE -lt 1024; then # _end[] has no enough space for bb_common_bufsiz1[] rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" echo "Rerun make to build a binary which doesn't use it!" - rm busybox_unstripped + rm busybox_unstripped busybox exitcmd="exit 1" else rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null -- cgit v1.2.3-55-g6feb From 663d1da1e68b15397c00d6a094f78c2cf08358ea Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Apr 2016 02:00:04 +0200 Subject: scripts/trylink: document DATA_SEGMENT_ALIGN() hack Signed-off-by: Denys Vlasenko --- scripts/trylink | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/trylink b/scripts/trylink index 15435f009..129570a60 100755 --- a/scripts/trylink +++ b/scripts/trylink @@ -209,6 +209,16 @@ else # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) # This will eliminate most of the padding (~3kb). # Hmm, "ld --sort-section alignment" should do it too. + # + # There is a ld hack which is meant to decrease disk usage + # at the cost of more RAM usage (??!!) in standard ld script: + # /* Adjust the address for the data segment. We want to adjust up to + # the same address within the page on the next page up. */ + # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); + # Replace it with: + # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); + # to unconditionally align .data to the next page boundary, + # instead of "next page, plus current offset in this page" try $CC $CFLAGS $LDFLAGS \ -o $EXE \ $SORT_COMMON \ -- cgit v1.2.3-55-g6feb From 3e134ebf6afb5552b3619f98f6a2ffa01a07eebb Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Apr 2016 18:09:21 +0200 Subject: *: slap on a few ALIGN1/2s where appropriate The result of looking at "grep -F -B2 '*fill*' busybox_unstripped.map" text data bss dec hex filename 829901 4086 1904 835891 cc133 busybox_before 829665 4086 1904 835655 cc047 busybox Signed-off-by: Denys Vlasenko --- archival/libarchive/common.c | 2 +- archival/lzop.c | 2 +- archival/unzip.c | 2 +- coreutils/stty.c | 6 +++--- e2fsprogs/e2fs_lib.c | 4 ++-- editors/sed.c | 2 +- editors/vi.c | 2 +- init/bootchartd.c | 2 +- libbb/mode_string.c | 4 ++-- libbb/pw_encrypt.c | 2 +- libbb/u_signal_names.c | 2 +- miscutils/adjtimex.c | 6 +++--- miscutils/eject.c | 2 +- miscutils/ionice.c | 2 +- miscutils/setserial.c | 8 ++++---- networking/libiproute/ipneigh.c | 2 +- networking/libiproute/ll_proto.c | 2 +- networking/libiproute/ll_types.c | 4 ++-- networking/telnetd.ctrlSQ.patch | 4 ++-- networking/udhcp/dhcpc.c | 2 +- networking/wget.c | 6 +++--- procps/top.c | 6 +++--- shell/ash.c | 6 +++--- shell/shell_common.c | 2 +- util-linux/fatattr.c | 2 +- util-linux/mount.c | 6 +++--- util-linux/nsenter.c | 2 +- util-linux/unshare.c | 2 +- util-linux/volume_id/bcache.c | 2 +- util-linux/volume_id/luks.c | 2 +- 30 files changed, 49 insertions(+), 49 deletions(-) diff --git a/archival/libarchive/common.c b/archival/libarchive/common.c index dd69d2222..389cb7856 100644 --- a/archival/libarchive/common.c +++ b/archival/libarchive/common.c @@ -6,4 +6,4 @@ #include "libbb.h" #include "bb_archive.h" -const char cpio_TRAILER[] = "TRAILER!!!"; +const char cpio_TRAILER[] ALIGN1 = "TRAILER!!!"; diff --git a/archival/lzop.c b/archival/lzop.c index 4afa21889..202de4d03 100644 --- a/archival/lzop.c +++ b/archival/lzop.c @@ -896,7 +896,7 @@ static NOINLINE int lzo_decompress(const header_t *h) * chksum_out * The rest is identical. */ -static const unsigned char lzop_magic[9] = { +static const unsigned char lzop_magic[9] ALIGN1 = { 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; diff --git a/archival/unzip.c b/archival/unzip.c index be32e60e2..c540485ac 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -487,7 +487,7 @@ int unzip_main(int argc, char **argv) if (overwrite == O_PROMPT) overwrite = O_NEVER; } else { - static const char extn[][5] = { ".zip", ".ZIP" }; + static const char extn[][5] ALIGN1 = { ".zip", ".ZIP" }; char *ext = src_fn + strlen(src_fn); int src_fd; diff --git a/coreutils/stty.c b/coreutils/stty.c index 0e32fc898..52967ea8f 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -318,7 +318,7 @@ enum { #define MI_ENTRY(N,T,F,B,M) N "\0" /* Mode names given on command line */ -static const char mode_name[] = +static const char mode_name[] ALIGN1 = MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 ) MI_ENTRY("parity", combination, REV | OMIT, 0, 0 ) MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 ) @@ -681,7 +681,7 @@ enum { #define CI_ENTRY(n,s,o) n "\0" /* Name given on command line */ -static const char control_name[] = +static const char control_name[] ALIGN1 = CI_ENTRY("intr", CINTR, VINTR ) CI_ENTRY("quit", CQUIT, VQUIT ) CI_ENTRY("erase", CERASE, VERASE ) @@ -723,7 +723,7 @@ static const char control_name[] = #undef CI_ENTRY #define CI_ENTRY(n,s,o) { s, o }, -static const struct control_info control_info[] = { +static const struct control_info control_info[] ALIGN2 = { /* This should be verbatim cut-n-paste copy of the above CI_ENTRYs */ CI_ENTRY("intr", CINTR, VINTR ) CI_ENTRY("quit", CQUIT, VQUIT ) diff --git a/e2fsprogs/e2fs_lib.c b/e2fsprogs/e2fs_lib.c index a6aec9484..6ce655be3 100644 --- a/e2fsprogs/e2fs_lib.c +++ b/e2fsprogs/e2fs_lib.c @@ -149,14 +149,14 @@ const uint32_t e2attr_flags_value[] = { EXT2_TOPDIR_FL }; -const char e2attr_flags_sname[] = +const char e2attr_flags_sname[] ALIGN1 = #ifdef ENABLE_COMPRESSION "BZXE" #endif "I" "suSDiadAcjtT"; -static const char e2attr_flags_lname[] = +static const char e2attr_flags_lname[] ALIGN1 = #ifdef ENABLE_COMPRESSION "Compressed_File" "\0" "Compressed_Dirty_File" "\0" diff --git a/editors/sed.c b/editors/sed.c index ed48de17f..6bce25b2c 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -473,7 +473,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr) */ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr) { - static const char cmd_letters[] = "saicrw:btTydDgGhHlnNpPqx={}"; + static const char cmd_letters[] ALIGN1 = "saicrw:btTydDgGhHlnNpPqx={}"; enum { IDX_s = 0, IDX_a, diff --git a/editors/vi.c b/editors/vi.c index f355712ab..974f9978b 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -251,7 +251,7 @@ enum { // cmds modifying text[] // vda: removed "aAiIs" as they switch us into insert mode // and remembering input for replay after them makes no sense -static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~"; +static const char modifying_cmds[] ALIGN1 = "cCdDJoOpPrRxX<>~"; #endif enum { diff --git a/init/bootchartd.c b/init/bootchartd.c index 7f511e650..92aaade0f 100644 --- a/init/bootchartd.c +++ b/init/bootchartd.c @@ -194,7 +194,7 @@ static char *make_tempdir(void) * Since we unmount it at once, we can mount it anywhere. * Try a few locations which are likely ti exist. */ - static const char dirs[] = "/mnt\0""/tmp\0""/boot\0""/proc\0"; + static const char dirs[] ALIGN1 = "/mnt\0""/tmp\0""/boot\0""/proc\0"; const char *try_dir = dirs; while (mount("none", try_dir, "tmpfs", MS_SILENT, "size=16m") != 0) { try_dir += strlen(try_dir) + 1; diff --git a/libbb/mode_string.c b/libbb/mode_string.c index f1afe7d61..934eb6dc7 100644 --- a/libbb/mode_string.c +++ b/libbb/mode_string.c @@ -87,9 +87,9 @@ const char* FAST_FUNC bb_mode_string(mode_t mode) /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C', * and 'B' types don't appear to be available on linux. So I removed them. */ -static const char type_chars[16] = "?pc?d?b?-?l?s???"; +static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???"; /********************************** 0123456789abcdef */ -static const char mode_chars[7] = "rwxSTst"; +static const char mode_chars[7] ALIGN1 = "rwxSTst"; const char* FAST_FUNC bb_mode_string(mode_t mode) { diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c index dbc15e5fc..4cdc2de76 100644 --- a/libbb/pw_encrypt.c +++ b/libbb/pw_encrypt.c @@ -9,7 +9,7 @@ #include "libbb.h" -/* static const uint8_t ascii64[] = +/* static const uint8_t ascii64[] ALIGN1 = * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; */ diff --git a/libbb/u_signal_names.c b/libbb/u_signal_names.c index 8c78f5e20..b49714f2a 100644 --- a/libbb/u_signal_names.c +++ b/libbb/u_signal_names.c @@ -19,7 +19,7 @@ /* Believe it or not, but some arches have more than 32 SIGs! * HPPA: SIGSTKFLT == 36. */ -static const char signals[][7] = { +static const char signals[][7] ALIGN1 = { // SUSv3 says kill must support these, and specifies the numerical values, // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, diff --git a/miscutils/adjtimex.c b/miscutils/adjtimex.c index 534364a69..058aa9a5c 100644 --- a/miscutils/adjtimex.c +++ b/miscutils/adjtimex.c @@ -29,7 +29,7 @@ # include #endif -static const uint16_t statlist_bit[] = { +static const uint16_t statlist_bit[] ALIGN2 = { STA_PLL, STA_PPSFREQ, STA_PPSTIME, @@ -45,7 +45,7 @@ static const uint16_t statlist_bit[] = { STA_CLOCKERR, 0 }; -static const char statlist_name[] = +static const char statlist_name[] ALIGN1 = "PLL" "\0" "PPSFREQ" "\0" "PPSTIME" "\0" @@ -61,7 +61,7 @@ static const char statlist_name[] = "CLOCKERR" ; -static const char ret_code_descript[] = +static const char ret_code_descript[] ALIGN1 = "clock synchronized" "\0" "insert leap second" "\0" "delete leap second" "\0" diff --git a/miscutils/eject.c b/miscutils/eject.c index e33d79127..16ae250ff 100644 --- a/miscutils/eject.c +++ b/miscutils/eject.c @@ -40,7 +40,7 @@ #if ENABLE_FEATURE_EJECT_SCSI static void eject_scsi(const char *dev) { - static const char sg_commands[3][6] = { + static const char sg_commands[3][6] ALIGN1 = { { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 }, { START_STOP, 0, 0, 0, 1, 0 }, { START_STOP, 0, 0, 0, 2, 0 } diff --git a/miscutils/ionice.c b/miscutils/ionice.c index bd300605f..0c14256ab 100644 --- a/miscutils/ionice.c +++ b/miscutils/ionice.c @@ -41,7 +41,7 @@ enum { IOPRIO_CLASS_IDLE }; -static const char to_prio[] = "none\0realtime\0best-effort\0idle"; +static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle"; #define IOPRIO_CLASS_SHIFT 13 diff --git a/miscutils/setserial.c b/miscutils/setserial.c index dfed3306e..8b5c4a9c7 100644 --- a/miscutils/setserial.c +++ b/miscutils/setserial.c @@ -257,7 +257,7 @@ enum print_mode #define CTL_CLOSE (1 << 3) #define CTL_NODIE (1 << 4) -static const char serial_types[] = +static const char serial_types[] ALIGN1 = "unknown\0" /* 0 */ "8250\0" /* 1 */ "16450\0" /* 2 */ @@ -288,7 +288,7 @@ static const char serial_types[] = # define MAX_SERIAL_TYPE 13 #endif -static const char commands[] = +static const char commands[] ALIGN1 = "spd_normal\0" "spd_hi\0" "spd_vhi\0" @@ -404,8 +404,8 @@ static const uint16_t setbits[CMD_FLAG_LAST + 1] = ASYNC_LOW_LATENCY }; -static const char STR_INFINITE[] = "infinite"; -static const char STR_NONE[] = "none"; +#define STR_INFINITE "infinite" +#define STR_NONE "none" static const char *uart_type(int type) { diff --git a/networking/libiproute/ipneigh.c b/networking/libiproute/ipneigh.c index d2028b7b6..2a1c20e20 100644 --- a/networking/libiproute/ipneigh.c +++ b/networking/libiproute/ipneigh.c @@ -62,7 +62,7 @@ static unsigned nud_state_a2n(char *arg) "stale\0" "incomplete\0" "delay\0" "probe\0" "failed\0" ; - static uint8_t nuds[] = { + static uint8_t nuds[] ALIGN1 = { NUD_PERMANENT,NUD_REACHABLE, NUD_NOARP,NUD_NONE, NUD_STALE, NUD_INCOMPLETE,NUD_DELAY,NUD_PROBE, NUD_FAILED diff --git a/networking/libiproute/ll_proto.c b/networking/libiproute/ll_proto.c index da2b53cbf..4c32ae574 100644 --- a/networking/libiproute/ll_proto.c +++ b/networking/libiproute/ll_proto.c @@ -84,7 +84,7 @@ ETH_P_IP /* Keep declarations above and below in sync! */ -static const char llproto_names[] = +static const char llproto_names[] ALIGN1 = #define __PF(f,n) #n "\0" __PF(LOOP,loop) __PF(PUP,pup) diff --git a/networking/libiproute/ll_types.c b/networking/libiproute/ll_types.c index bb42e269e..62ee0cc54 100644 --- a/networking/libiproute/ll_types.c +++ b/networking/libiproute/ll_types.c @@ -16,7 +16,7 @@ const char* FAST_FUNC ll_type_n2a(int type, char *buf) { - static const char arphrd_name[] = + static const char arphrd_name[] ALIGN1 = /* 0, */ "generic" "\0" /* ARPHRD_LOOPBACK, */ "loopback" "\0" /* ARPHRD_ETHER, */ "ether" "\0" @@ -105,7 +105,7 @@ const char* FAST_FUNC ll_type_n2a(int type, char *buf) /* Keep these arrays in sync! */ - static const uint16_t arphrd_type[] = { + static const uint16_t arphrd_type[] ALIGN2 = { 0, /* "generic" "\0" */ ARPHRD_LOOPBACK, /* "loopback" "\0" */ ARPHRD_ETHER, /* "ether" "\0" */ diff --git a/networking/telnetd.ctrlSQ.patch b/networking/telnetd.ctrlSQ.patch index 7060e1c6e..bc26d2279 100644 --- a/networking/telnetd.ctrlSQ.patch +++ b/networking/telnetd.ctrlSQ.patch @@ -94,9 +94,9 @@ exceptional conditions. #endif +#ifdef TIOCPKT + int control; -+ static const char lflow_on[] = ++ static const char lflow_on[] ALIGN1 = + {IAC, SB, TELOPT_LFLOW, LFLOW_ON, IAC, SE}; -+ static const char lflow_off[] = ++ static const char lflow_off[] ALIGN1 = + {IAC, SB, TELOPT_LFLOW, LFLOW_OFF, IAC, SE}; +# define RESERVED sizeof(lflow_on) +#else diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 8f5a03f2e..fc7b6216d 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -109,7 +109,7 @@ enum { /*** Script execution code ***/ /* get a rough idea of how long an option will be (rounding up...) */ -static const uint8_t len_of_option_as_string[] = { +static const uint8_t len_of_option_as_string[] ALIGN1 = { [OPTION_IP ] = sizeof("255.255.255.255 "), [OPTION_IP_PAIR ] = sizeof("255.255.255.255 ") * 2, [OPTION_STATIC_ROUTES ] = sizeof("255.255.255.255/32 255.255.255.255 "), diff --git a/networking/wget.c b/networking/wget.c index 5c12423c7..28c12540b 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -146,10 +146,10 @@ struct host_info { char *host; int port; }; -static const char P_FTP[] = "ftp"; -static const char P_HTTP[] = "http"; +static const char P_FTP[] ALIGN1 = "ftp"; +static const char P_HTTP[] ALIGN1 = "http"; #if ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_SSL_HELPER -static const char P_HTTPS[] = "https"; +static const char P_HTTPS[] ALIGN1 = "https"; #endif #if ENABLE_FEATURE_WGET_LONG_OPTIONS diff --git a/procps/top.c b/procps/top.c index 640bcdc6d..73cd285f0 100644 --- a/procps/top.c +++ b/procps/top.c @@ -265,9 +265,9 @@ static int mult_lvl_cmp(void* a, void* b) static NOINLINE int read_cpu_jiffy(FILE *fp, jiffy_counts_t *p_jif) { #if !ENABLE_FEATURE_TOP_SMP_CPU - static const char fmt[] = "cpu %llu %llu %llu %llu %llu %llu %llu %llu"; + static const char fmt[] ALIGN1 = "cpu %llu %llu %llu %llu %llu %llu %llu %llu"; #else - static const char fmt[] = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu"; + static const char fmt[] ALIGN1 = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu"; #endif int ret; @@ -519,7 +519,7 @@ enum { static void parse_meminfo(unsigned long meminfo[MI_MAX]) { - static const char fields[] = + static const char fields[] ALIGN1 = "MemTotal\0" "MemFree\0" "MemShared\0" diff --git a/shell/ash.c b/shell/ash.c index da9c95045..faa45a8dc 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2750,7 +2750,7 @@ pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) #else # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) #endif -static const uint16_t S_I_T[] = { +static const uint16_t S_I_T[] ALIGN2 = { #if ENABLE_ASH_ALIAS SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ #endif @@ -2852,7 +2852,7 @@ SIT(int c, int syntax) #else /* !USE_SIT_FUNCTION */ -static const uint8_t syntax_index_table[] = { +static const uint8_t syntax_index_table[] ALIGN1 = { /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ /* 0 */ CWORD_CWORD_CWORD_CWORD, /* 1 */ CWORD_CWORD_CWORD_CWORD, @@ -7977,7 +7977,7 @@ static char *funcstring; /* block to allocate strings from */ #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ #define EV_BACKCMD 04 /* command executing within back quotes */ -static const uint8_t nodesize[N_NUMBER] = { +static const uint8_t nodesize[N_NUMBER] ALIGN1 = { [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), diff --git a/shell/shell_common.c b/shell/shell_common.c index 8c9607c8c..14eeaafcc 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -328,7 +328,7 @@ enum { }; /* "-": treat args as parameters of option with ASCII code 1 */ -static const char ulimit_opt_string[] = "-HSa" +static const char ulimit_opt_string[] ALIGN1 = "-HSa" #ifdef RLIMIT_FSIZE "f::" #endif diff --git a/util-linux/fatattr.c b/util-linux/fatattr.c index 5d933874a..6dca24a73 100644 --- a/util-linux/fatattr.c +++ b/util-linux/fatattr.c @@ -42,7 +42,7 @@ * Extra space at the end is a hack to print space separator in file listing. * Let's hope no one ever passes space as an option char :) */ -static const char bit_to_char[] = "rhsvda67 "; +static const char bit_to_char[] ALIGN1 = "rhsvda67 "; static inline unsigned long get_flag(char c) { diff --git a/util-linux/mount.c b/util-linux/mount.c index 244f4fa27..c76f6ef61 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -377,7 +377,7 @@ static const int32_t mount_options[] = { /* "remount" */ MS_REMOUNT // action flag }; -static const char mount_option_str[] = +static const char mount_option_str[] ALIGN1 = IF_FEATURE_MOUNT_LOOP( "loop\0" ) @@ -1003,7 +1003,7 @@ enum { # define EDQUOT ENOSPC #endif /* Convert each NFSERR_BLAH into EBLAH */ -static const uint8_t nfs_err_stat[] = { +static const uint8_t nfs_err_stat[] ALIGN1 = { 1, 2, 5, 6, 13, 17, 19, 20, 21, 22, 27, 28, 30, 63, 66, 69, 70, 71 @@ -1016,7 +1016,7 @@ typedef uint8_t nfs_err_type; #else typedef uint16_t nfs_err_type; #endif -static const nfs_err_type nfs_err_errnum[] = { +static const nfs_err_type nfs_err_errnum[] ALIGN2 = { EPERM , ENOENT , EIO , ENXIO , EACCES, EEXIST, ENODEV, ENOTDIR , EISDIR , EINVAL, EFBIG , ENOSPC, EROFS , ENAMETOOLONG, ENOTEMPTY, EDQUOT, ESTALE, EREMOTE diff --git a/util-linux/nsenter.c b/util-linux/nsenter.c index b08b3dae7..6834292da 100644 --- a/util-linux/nsenter.c +++ b/util-linux/nsenter.c @@ -128,7 +128,7 @@ static const struct namespace_descr ns_list[] = { /* * Upstream nsenter doesn't support the short option for --preserve-credentials */ -static const char opt_str[] = "U::i::u::n::p::m::""t+S+G+r::w::F"; +static const char opt_str[] ALIGN1 = "U::i::u::n::p::m::""t+S+G+r::w::F"; #if ENABLE_FEATURE_NSENTER_LONG_OPTS static const char nsenter_longopts[] ALIGN1 = diff --git a/util-linux/unshare.c b/util-linux/unshare.c index d05cfdb6c..fa7086add 100644 --- a/util-linux/unshare.c +++ b/util-linux/unshare.c @@ -137,7 +137,7 @@ static const struct namespace_descr ns_list[] = { * we are forced to use "fake" letters for them. * '+': stop at first non-option. */ -static const char opt_str[] = "+muinpU""fr""\xfd::""\xfe:""\xff:"; +static const char opt_str[] ALIGN1 = "+muinpU""fr""\xfd::""\xfe:""\xff:"; static const char unshare_longopts[] ALIGN1 = "mount\0" Optional_argument "\xf0" "uts\0" Optional_argument "\xf1" diff --git a/util-linux/volume_id/bcache.c b/util-linux/volume_id/bcache.c index 648e44de5..fd40eb081 100644 --- a/util-linux/volume_id/bcache.c +++ b/util-linux/volume_id/bcache.c @@ -24,7 +24,7 @@ #define SB_LABEL_SIZE 32 #define SB_JOURNAL_BUCKETS 256U -static const char bcache_magic[] = { +static const char bcache_magic[] ALIGN1 = { 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 }; diff --git a/util-linux/volume_id/luks.c b/util-linux/volume_id/luks.c index 42bf87659..21cb26f51 100644 --- a/util-linux/volume_id/luks.c +++ b/util-linux/volume_id/luks.c @@ -40,7 +40,7 @@ #define LUKS_SALTSIZE 32 #define LUKS_NUMKEYS 8 -static const uint8_t LUKS_MAGIC[] = { 'L','U','K','S', 0xba, 0xbe }; +static const uint8_t LUKS_MAGIC[] ALIGN1 = { 'L','U','K','S', 0xba, 0xbe }; struct luks_phdr { uint8_t magic[LUKS_MAGIC_L]; -- cgit v1.2.3-55-g6feb From 4c8fa34417fd2ccdda6a8ea508a3f1e7fb1d4ceb Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 24 Apr 2016 14:13:35 +0200 Subject: generate_BUFSIZ.sh: yet another tweak Signed-off-by: Denys Vlasenko --- include/.gitignore | 1 + scripts/generate_BUFSIZ.sh | 54 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/include/.gitignore b/include/.gitignore index 9d9b6c499..75afff9ca 100644 --- a/include/.gitignore +++ b/include/.gitignore @@ -8,3 +8,4 @@ /NUM_APPLETS.h /usage_compressed.h /usage.h +/common_bufsiz.h* diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 750fedbef..844261906 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -6,6 +6,7 @@ . ./.config || exit 1 debug=false +#debug=true postcompile=false test x"$1" = x"--post" && { postcompile=true; shift; } @@ -62,6 +63,11 @@ generate_malloc_and_exit() { $exitcmd } +round_down_COMMON_BUFSIZE() { + COMMON_BUFSIZE=$(( ($1-32) & 0xfffffe0 )) + COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) +} + # User does not want any funky stuff? test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit @@ -102,22 +108,43 @@ if $postcompile; then # How much space between _end[] and next page? PAGE_MASK=$((PAGE_SIZE-1)) - COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) - echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" + TAIL_SIZE=$(( (-END) & PAGE_MASK )) + $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes" - if test x"$method" != x"malloc"; then - if test $COMMON_BUFSIZE -lt 1024; then + if test x"$method" = x"1k" || test x"$method" = x"big"; then + if test $TAIL_SIZE -lt 1024; then # _end[] has no enough space for bb_common_bufsiz1[] + echo "Warning! Space in _end[] is too small ($TAIL_SIZE bytes)!" + echo "Rerun make to build a binary which doesn't use it!" rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" - echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" - echo "Rerun make to build a binary which doesn't use it!" - rm busybox_unstripped busybox + rm busybox_unstripped busybox 2>/dev/null +# Note: here we can do either a "malloc" or "std" build. +# "malloc" gives a bit bigger code: +# text bss filename +# 804355 5385 busybox.std +# 804618 4361 busybox.malloc +# but may have a smaller .bss (not guaranteed!). Use "pmap -x" to verify. exitcmd="exit 1" + generate_malloc_and_exit else + PREV_SIZE=1024 + test x"$method" = x"big" && PREV_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` + round_down_COMMON_BUFSIZE $PREV_SIZE + PREV_BUFSIZE=$COMMON_BUFSIZE + rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null - echo $COMMON_BUFSIZE >"$common_bufsiz_h.1k.OK" - test $COMMON_BUFSIZE -gt $((1024+32)) && echo "Rerun make to use larger COMMON_BUFSIZE" + echo $TAIL_SIZE >"$common_bufsiz_h.1k.OK" + round_down_COMMON_BUFSIZE $TAIL_SIZE + # emit message only if COMMON_BUFSIZE is indeed larger + test $COMMON_BUFSIZE -gt $PREV_BUFSIZE \ + && echo "Rerun make to use larger COMMON_BUFSIZE ($COMMON_BUFSIZE)" +#TODO: test $PREV_BUFSIZE -lt $TAIL_SIZE && PANIC!!! +#Code size with COMMON_BUFSIZE > 1024 may be bigger than code with COMMON_BUFSIZE = 1024! +#(currently we just hope "-32 and round down to 32" saves us) + + test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit + generate_big_and_exit $COMMON_BUFSIZE "big" fi fi fi @@ -127,13 +154,12 @@ fi if test -f "$common_bufsiz_h.1k.OK"; then # Previous build succeeded fitting 1k into _end[]. # Try bigger COMMON_BUFSIZE if possible. - COMMON_BUFSIZE=`cat -- "$common_bufsiz_h.1k.OK"` - # Round down a bit - COMMON_BUFSIZE=$(( (COMMON_BUFSIZE-32) & 0xfffffe0 )) - COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) + TAIL_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` + round_down_COMMON_BUFSIZE $TAIL_SIZE test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit generate_big_and_exit $COMMON_BUFSIZE "big" fi + if test -f "$common_bufsiz_h.1k.FAIL"; then # Previous build FAILED to fit 1k into _end[]. # Was it with same .config? @@ -142,6 +168,7 @@ if test -f "$common_bufsiz_h.1k.FAIL"; then # If yes, then build a "malloced" version if test x"$oldcfg" = x"$curcfg"; then echo "Will not try 1k build, it failed before. Touch .config to override" +# Note: here we can do either a "malloc" or "std" build. generate_malloc_and_exit fi # else: try 1k version @@ -149,5 +176,6 @@ if test -f "$common_bufsiz_h.1k.FAIL"; then rm -- "$common_bufsiz_h.1k.FAIL" generate_1k_and_exit fi + # There was no 1k build yet. Try it. generate_1k_and_exit -- cgit v1.2.3-55-g6feb From cbdff15bb78ba9d83be7f6b5087ee665715999b0 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 24 Apr 2016 16:18:03 +0200 Subject: sed: understand \n,\r and \t in i and a commands. Closes 8871 Signed-off-by: Denys Vlasenko --- editors/sed.c | 38 +++++++++++++++++++++++++------------- testsuite/sed.tests | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index 6bce25b2c..7f18fd0c4 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -218,23 +218,33 @@ static void cleanup_outname(void) /* strcpy, replacing "\from" with 'to'. If to is NUL, replacing "\any" with 'any' */ -static void parse_escapes(char *dest, const char *string, int len, char from, char to) +static unsigned parse_escapes(char *dest, const char *string, int len, char from, char to) { + char *d = dest; int i = 0; + if (len == -1) + len = strlen(string); + while (i < len) { if (string[i] == '\\') { if (!to || string[i+1] == from) { - *dest++ = to ? to : string[i+1]; + if ((*d = to ? to : string[i+1]) == '\0') + return d - dest; i += 2; + d++; continue; } - *dest++ = string[i++]; + i++; /* skip backslash in string[] */ + *d++ = '\\'; + /* fall through: copy next char verbatim */ } - /* TODO: is it safe wrt a string with trailing '\\' ? */ - *dest++ = string[i++]; + if ((*d = string[i++]) == '\0') + return d - dest; + d++; } - *dest = '\0'; + *d = '\0'; + return d - dest; } static char *copy_parsing_escapes(const char *string, int len) @@ -245,9 +255,8 @@ static char *copy_parsing_escapes(const char *string, int len) /* sed recognizes \n */ /* GNU sed also recognizes \t and \r */ for (s = "\nn\tt\rr"; *s; s += 2) { - parse_escapes(dest, string, len, s[1], s[0]); + len = parse_escapes(dest, string, len, s[1], s[0]); string = dest; - len = strlen(dest); } return dest; } @@ -516,6 +525,8 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr) } /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ else if (idx <= IDX_c) { /* a,i,c */ + unsigned len; + if (idx < IDX_c) { /* a,i */ if (sed_cmd->end_line || sed_cmd->end_match) bb_error_msg_and_die("command '%c' uses only one address", sed_cmd->cmd); @@ -529,10 +540,11 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr) break; cmdstr++; } - sed_cmd->string = xstrdup(cmdstr); + len = strlen(cmdstr); + sed_cmd->string = copy_parsing_escapes(cmdstr, len); + cmdstr += len; /* "\anychar" -> "anychar" */ - parse_escapes(sed_cmd->string, sed_cmd->string, strlen(cmdstr), '\0', '\0'); - cmdstr += strlen(cmdstr); + parse_escapes(sed_cmd->string, sed_cmd->string, -1, '\0', '\0'); } /* handle file cmds: (r)ead */ else if (idx <= IDX_w) { /* r,w */ @@ -564,8 +576,8 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr) cmdstr += parse_regex_delim(cmdstr, &match, &replace)+1; /* \n already parsed, but \delimiter needs unescaping. */ - parse_escapes(match, match, strlen(match), i, i); - parse_escapes(replace, replace, strlen(replace), i, i); + parse_escapes(match, match, -1, i, i); + parse_escapes(replace, replace, -1, i, i); sed_cmd->string = xzalloc((strlen(match) + 1) * 2); for (i = 0; match[i] && replace[i]; i++) { diff --git a/testsuite/sed.tests b/testsuite/sed.tests index 5d2356b64..c4b6fa278 100755 --- a/testsuite/sed.tests +++ b/testsuite/sed.tests @@ -275,6 +275,24 @@ testing "sed a cmd ended by double backslash" \ | two \\ ' +testing "sed a cmd understands \\n,\\t,\\r" \ + "sed '/1/a\\\\t\\rzero\\none\\\\ntwo\\\\\\nthree'" \ +"\ +line1 +\t\rzero +one\\\\ntwo\\ +three +" "" "line1\n" + +testing "sed i cmd understands \\n,\\t,\\r" \ + "sed '/1/i\\\\t\\rzero\\none\\\\ntwo\\\\\\nthree'" \ +"\ +\t\rzero +one\\\\ntwo\\ +three +line1 +" "" "line1\n" + # first three lines are deleted; 4th line is matched and printed by "2,3" and by "4" ranges testing "sed with N skipping lines past ranges on next cmds" \ "sed -n '1{N;N;d};1p;2,3p;3p;4p'" \ -- cgit v1.2.3-55-g6feb From 4ab372d49a6e82b0bf097dedb96d26330c5f2d5f Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sun, 24 Apr 2016 17:39:02 +0200 Subject: ip: fix problem on mips64 n64 big endian musl systems Use designated initializers for struct msghdr. The struct layout is non-portable and musl libc does not match what busybox expects. Signed-off-by: Szabolcs Nagy Tested-by: Waldemar Brodkorb Signed-off-by: Denys Vlasenko --- networking/libiproute/libnetlink.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/networking/libiproute/libnetlink.c b/networking/libiproute/libnetlink.c index c7533a4a7..cbb5daf95 100644 --- a/networking/libiproute/libnetlink.c +++ b/networking/libiproute/libnetlink.c @@ -71,11 +71,15 @@ int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, in struct nlmsghdr nlh; struct sockaddr_nl nladdr; struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } }; + /* Use designated initializers, struct layout is non-portable */ struct msghdr msg = { - (void*)&nladdr, sizeof(nladdr), - iov, 2, - NULL, 0, - 0 + .msg_name = (void*)&nladdr, + .msg_namelen = sizeof(nladdr), + .msg_iov = iov, + .msg_iovlen = 2, + .msg_control = NULL, + .msg_controllen = 0, + .msg_flags = 0 }; memset(&nladdr, 0, sizeof(nladdr)); @@ -104,12 +108,15 @@ static int rtnl_dump_filter(struct rtnl_handle *rth, while (1) { int status; struct nlmsghdr *h; - + /* Use designated initializers, struct layout is non-portable */ struct msghdr msg = { - (void*)&nladdr, sizeof(nladdr), - &iov, 1, - NULL, 0, - 0 + .msg_name = (void*)&nladdr, + .msg_namelen = sizeof(nladdr), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = NULL, + .msg_controllen = 0, + .msg_flags = 0 }; status = recvmsg(rth->fd, &msg, 0); @@ -211,11 +218,15 @@ int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, struct sockaddr_nl nladdr; struct iovec iov = { (void*)n, n->nlmsg_len }; char *buf = xmalloc(8*1024); /* avoid big stack buffer */ + /* Use designated initializers, struct layout is non-portable */ struct msghdr msg = { - (void*)&nladdr, sizeof(nladdr), - &iov, 1, - NULL, 0, - 0 + .msg_name = (void*)&nladdr, + .msg_namelen = sizeof(nladdr), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = NULL, + .msg_controllen = 0, + .msg_flags = 0 }; memset(&nladdr, 0, sizeof(nladdr)); -- cgit v1.2.3-55-g6feb From e1d426fd65c00a6d01a10d85edf8a294ae8a2d2b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 24 Apr 2016 18:19:49 +0200 Subject: flock: fix -c; improve error handling of fork+exec function old new delta flock_main 254 334 +80 Signed-off-by: Denys Vlasenko --- util-linux/flock.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/util-linux/flock.c b/util-linux/flock.c index 05a747f72..539a835b7 100644 --- a/util-linux/flock.c +++ b/util-linux/flock.c @@ -57,7 +57,6 @@ int flock_main(int argc UNUSED_PARAM, char **argv) /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32: * we use "+" in order to support "flock -opt FILE PROG -with-opts", * we need to remove -c by hand. - * TODO: in upstream, -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */ if (argv[0] && argv[0][0] == '-' @@ -66,6 +65,9 @@ int flock_main(int argc UNUSED_PARAM, char **argv) ) ) { argv++; + if (argv[1]) + bb_error_msg_and_die("-c takes only one argument"); + opt |= OPT_c; } if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) { @@ -90,8 +92,21 @@ int flock_main(int argc UNUSED_PARAM, char **argv) bb_perror_nomsg_and_die(); } - if (argv[0]) + if (argv[0]) { + if (!(opt & OPT_c)) { + int rc = spawn_and_wait(argv); + if (rc < 0) + bb_simple_perror_msg(argv[0]); + return rc; + } + /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */ + argv -= 2; + argv[0] = (char*)get_shell_name(); + argv[1] = (char*)"-c"; + /* argv[2] = "PROG ARGS"; */ + /* argv[3] = NULL; */ return spawn_and_wait(argv); + } return EXIT_SUCCESS; } -- cgit v1.2.3-55-g6feb From 2fbc3123a2d94a85317b2269c724939db7e18fbf Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 24 Apr 2016 18:21:32 +0200 Subject: flock: merge spawn_and_wait() code patchs for -c and sans-c uses function old new delta flock_main 334 319 -15 Signed-off-by: Denys Vlasenko --- util-linux/flock.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/util-linux/flock.c b/util-linux/flock.c index 539a835b7..1f7ade7c4 100644 --- a/util-linux/flock.c +++ b/util-linux/flock.c @@ -93,19 +93,19 @@ int flock_main(int argc UNUSED_PARAM, char **argv) } if (argv[0]) { - if (!(opt & OPT_c)) { - int rc = spawn_and_wait(argv); - if (rc < 0) - bb_simple_perror_msg(argv[0]); - return rc; + int rc; + if (opt & OPT_c) { + /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */ + argv -= 2; + argv[0] = (char*)get_shell_name(); + argv[1] = (char*)"-c"; + /* argv[2] = "PROG ARGS"; */ + /* argv[3] = NULL; */ } - /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */ - argv -= 2; - argv[0] = (char*)get_shell_name(); - argv[1] = (char*)"-c"; - /* argv[2] = "PROG ARGS"; */ - /* argv[3] = NULL; */ - return spawn_and_wait(argv); + rc = spawn_and_wait(argv); + if (rc < 0) + bb_simple_perror_msg(argv[0]); + return rc; } return EXIT_SUCCESS; -- cgit v1.2.3-55-g6feb From f2559e5c2b7bd2c5fa0dd8e88d0a931da92a23af Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 6 May 2016 18:25:56 +0200 Subject: sed: fix append command to match GNU sed 4.2.1 This closes one testcase failure Signed-off-by: Denys Vlasenko --- editors/sed.c | 29 +++++++++++++++++++---------- testsuite/sed.tests | 6 ++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index 7f18fd0c4..f37c37d88 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -956,13 +956,22 @@ static void puts_maybe_newline(char *s, FILE *file, char *last_puts_char, char l *last_puts_char = lpc; } -static void flush_append(char *last_puts_char, char last_gets_char) +static void flush_append(char *last_puts_char) { char *data; /* Output appended lines. */ - while ((data = (char *)llist_pop(&G.append_head))) { - puts_maybe_newline(data, G.nonstdout, last_puts_char, last_gets_char); + while ((data = (char *)llist_pop(&G.append_head)) != NULL) { + /* Append command does not respect "nonterminated-ness" + * of last line. Try this: + * $ echo -n "woot" | sed -e '/woot/a woo' - + * woot + * woo + * (both lines are terminated with \n) + * Therefore we do not propagate "last_gets_char" here, + * pass '\n' instead: + */ + puts_maybe_newline(data, G.nonstdout, last_puts_char, '\n'); free(data); } } @@ -970,13 +979,13 @@ static void flush_append(char *last_puts_char, char last_gets_char) /* Get next line of input from G.input_file_list, flushing append buffer and * noting if we ran out of files without a newline on the last line we read. */ -static char *get_next_line(char *gets_char, char *last_puts_char, char last_gets_char) +static char *get_next_line(char *gets_char, char *last_puts_char) { char *temp = NULL; int len; char gc; - flush_append(last_puts_char, last_gets_char); + flush_append(last_puts_char); /* will be returned if last line in the file * doesn't end with either '\n' or '\0' */ @@ -1054,7 +1063,7 @@ static void process_files(void) int substituted; /* Prime the pump */ - next_line = get_next_line(&next_gets_char, &last_puts_char, '\n' /*last_gets_char*/); + next_line = get_next_line(&next_gets_char, &last_puts_char); /* Go through every line in each file */ again: @@ -1068,7 +1077,7 @@ static void process_files(void) /* Read one line in advance so we can act on the last line, * the '$' address */ - next_line = get_next_line(&next_gets_char, &last_puts_char, last_gets_char); + next_line = get_next_line(&next_gets_char, &last_puts_char); linenum++; /* For every line, go through all the commands */ @@ -1295,7 +1304,7 @@ static void process_files(void) free(pattern_space); pattern_space = next_line; last_gets_char = next_gets_char; - next_line = get_next_line(&next_gets_char, &last_puts_char, last_gets_char); + next_line = get_next_line(&next_gets_char, &last_puts_char); substituted = 0; linenum++; break; @@ -1331,7 +1340,7 @@ static void process_files(void) pattern_space[len] = '\n'; strcpy(pattern_space + len+1, next_line); last_gets_char = next_gets_char; - next_line = get_next_line(&next_gets_char, &last_puts_char, last_gets_char); + next_line = get_next_line(&next_gets_char, &last_puts_char); linenum++; break; } @@ -1435,7 +1444,7 @@ static void process_files(void) /* Delete and such jump here. */ discard_line: - flush_append(&last_puts_char, last_gets_char); + flush_append(&last_puts_char /*,last_gets_char*/); free(pattern_space); goto again; diff --git a/testsuite/sed.tests b/testsuite/sed.tests index c4b6fa278..a71f8b1f0 100755 --- a/testsuite/sed.tests +++ b/testsuite/sed.tests @@ -135,10 +135,12 @@ testing "sed empty file plus cat" "sed -e 's/nohit//' input -" "one\ntwo" \ "" "one\ntwo" testing "sed cat plus empty file" "sed -e 's/nohit//' input -" "one\ntwo" \ "one\ntwo" "" -test x"$SKIP_KNOWN_BUGS" = x"" && { testing "sed append autoinserts newline" "sed -e '/woot/a woo' -" \ "woot\nwoo\n" "" "woot" -} +testing "sed append autoinserts newline 2" "sed -e '/oot/a woo' - input" \ + "woot\nwoo\nboot\nwoo\n" "boot" "woot" +testing "sed append autoinserts newline 3" "sed -e '/oot/a woo' -i input && cat input" \ + "boot\nwoo\n" "boot" "" testing "sed insert doesn't autoinsert newline" "sed -e '/woot/i woo' -" \ "woo\nwoot" "" "woot" testing "sed print autoinsert newlines" "sed -e 'p' -" "one\none" "" "one" -- cgit v1.2.3-55-g6feb