From ca003385f1e32646c249dfcd3717afbe7535dcec Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 14 Jul 2016 20:58:39 +0200 Subject: cp: make verbose cp show symlink copies too Signed-off-by: Denys Vlasenko --- libbb/copy_file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 23bcf2e82..7801b58c7 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -375,7 +375,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) } /* _Not_ jumping to preserve_mode_ugid_time: * symlinks don't have those */ - return 0; + goto verb_and_exit; } if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) @@ -410,6 +410,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) bb_perror_msg("can't preserve %s of '%s'", "permissions", dest); } + verb_and_exit: if (flags & FILEUTILS_VERBOSE) { printf("'%s' -> '%s'\n", source, dest); } -- cgit v1.2.3-55-g6feb From 0fb0045aa9261be1dda49dfdfb95cbc585402a8b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Jul 2016 18:48:38 +0200 Subject: config: disentangle PREFER_APPLETS from SH_STANDALONE and SH_NOFORK On user request. I thought enabling/disabling them all together is more consistent. Evidently, some people do want them to be separately selectable. Signed-off-by: Denys Vlasenko --- applets/applet_tables.c | 4 +++- include/busybox.h | 4 +++- libbb/vfork_daemon_rexec.c | 5 +++-- shell/Config.src | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) (limited to 'libbb') diff --git a/applets/applet_tables.c b/applets/applet_tables.c index 843f2ec08..8401a1549 100644 --- a/applets/applet_tables.c +++ b/applets/applet_tables.c @@ -143,7 +143,9 @@ int main(int argc, char **argv) printf("};\n"); printf("#endif\n\n"); -#if ENABLE_FEATURE_PREFER_APPLETS +#if ENABLE_FEATURE_PREFER_APPLETS \ + || ENABLE_FEATURE_SH_STANDALONE \ + || ENABLE_FEATURE_SH_NOFORK printf("const uint8_t applet_flags[] ALIGN1 = {\n"); i = 0; while (i < NUM_APPLETS) { diff --git a/include/busybox.h b/include/busybox.h index 737627bd0..6a003d544 100644 --- a/include/busybox.h +++ b/include/busybox.h @@ -19,7 +19,9 @@ extern const uint8_t applet_flags[] ALIGN1; extern const uint8_t applet_suid[] ALIGN1; extern const uint8_t applet_install_loc[] ALIGN1; -#if ENABLE_FEATURE_PREFER_APPLETS +#if ENABLE_FEATURE_PREFER_APPLETS \ + || ENABLE_FEATURE_SH_STANDALONE \ + || ENABLE_FEATURE_SH_NOFORK # define APPLET_IS_NOFORK(i) (applet_flags[(i)/4] & (1 << (2 * ((i)%4)))) # define APPLET_IS_NOEXEC(i) (applet_flags[(i)/4] & (1 << ((2 * ((i)%4))+1))) #else diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index 1adb5b3c4..c192829b5 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c @@ -68,7 +68,8 @@ pid_t FAST_FUNC xspawn(char **argv) return pid; } -#if ENABLE_FEATURE_PREFER_APPLETS +#if ENABLE_FEATURE_PREFER_APPLETS \ + || ENABLE_FEATURE_SH_NOFORK static jmp_buf die_jmp; static void jump(void) { @@ -174,7 +175,7 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv) return rc & 0xff; /* don't confuse people with "exitcodes" >255 */ } -#endif /* FEATURE_PREFER_APPLETS */ +#endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */ int FAST_FUNC spawn_and_wait(char **argv) { diff --git a/shell/Config.src b/shell/Config.src index b31e62dda..e4df35973 100644 --- a/shell/Config.src +++ b/shell/Config.src @@ -88,7 +88,7 @@ config FEATURE_SH_EXTRA_QUIET config FEATURE_SH_STANDALONE bool "Standalone shell" default n - depends on (HUSH || ASH) && FEATURE_PREFER_APPLETS + depends on (HUSH || ASH) help This option causes busybox shells to use busybox applets in preference to executables in the PATH whenever possible. For @@ -121,7 +121,7 @@ config FEATURE_SH_STANDALONE config FEATURE_SH_NOFORK bool "Run 'nofork' applets directly" default n - depends on (HUSH || ASH) && FEATURE_PREFER_APPLETS + depends on (HUSH || ASH) help This option causes busybox shells to not execute typical fork/exec/wait sequence, but call _main directly, -- cgit v1.2.3-55-g6feb From 98c50f93fe494be5547188813c367299db80dbc5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 13 Aug 2016 23:23:48 +0200 Subject: cp: fix -i for POSIX mode. Closes 9106 Signed-off-by: Denys Vlasenko --- libbb/copy_file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 7801b58c7..23c0f8320 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -296,11 +296,16 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) if (!S_ISREG(source_stat.st_mode)) new_mode = 0666; - // POSIX way is a security problem versus (sym)link attacks - if (!ENABLE_FEATURE_NON_POSIX_CP) { - dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode); - } else { /* safe way: */ + if (ENABLE_FEATURE_NON_POSIX_CP || (flags & FILEUTILS_INTERACTIVE)) { + /* + * O_CREAT|O_EXCL: require that file did not exist before creation + */ dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode); + } else { /* POSIX, and not "cp -i" */ + /* + * O_CREAT|O_TRUNC: create, or truncate (security problem versus (sym)link attacks) + */ + dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode); } if (dst_fd == -1) { ovr = ask_and_unlink(dest, flags); -- cgit v1.2.3-55-g6feb From 7e6f9316a8bf3030da6c283e43aa0709d0cef259 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Sun, 14 Aug 2016 23:30:29 +0200 Subject: lineedit: trivial codeshrink for vi-mode Introduce and use BB_isalnum_or_underscore(). function old new delta BB_isalnum_or_underscore - 43 +43 vi_word_motion 162 150 -12 vi_end_motion 163 145 -18 vi_back_motion 198 179 -19 BB_isalnum 39 - -39 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 0/3 up/down: 43/-88) Total: -45 bytes Signed-off-by: Natanael Copa Signed-off-by: Denys Vlasenko --- libbb/lineedit.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'libbb') diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 3e62f46b4..3bfff0084 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -81,7 +81,9 @@ # define CHAR_T wchar_t static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); } # if ENABLE_FEATURE_EDITING_VI -static bool BB_isalnum(CHAR_T c) { return ((unsigned)c < 256 && isalnum(c)); } +static bool BB_isalnum_or_underscore(CHAR_T c) { + return ((unsigned)c < 256 && isalnum(c)) || c == '_'; +} # endif static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); } # undef isspace @@ -96,7 +98,11 @@ static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); } # define BB_NUL '\0' # define CHAR_T char # define BB_isspace(c) isspace(c) -# define BB_isalnum(c) isalnum(c) +# if ENABLE_FEATURE_EDITING_VI +static bool BB_isalnum_or_underscore(CHAR_T c) { + return ((unsigned)c < 256 && isalnum(c)) || c == '_'; +} +# endif # define BB_ispunct(c) ispunct(c) #endif #if ENABLE_UNICODE_PRESERVE_BROKEN @@ -1586,9 +1592,9 @@ vi_word_motion(int eat) { CHAR_T *command = command_ps; - if (BB_isalnum(command[cursor]) || command[cursor] == '_') { + if (BB_isalnum_or_underscore(command[cursor])) { while (cursor < command_len - && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_') + && (BB_isalnum_or_underscore(command[cursor+1])) ) { input_forward(); } @@ -1630,9 +1636,9 @@ vi_end_motion(void) input_forward(); if (cursor >= command_len-1) return; - if (BB_isalnum(command[cursor]) || command[cursor] == '_') { + if (BB_isalnum_or_underscore(command[cursor])) { while (cursor < command_len-1 - && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_') + && (BB_isalnum_or_underscore(command[cursor+1])) ) { input_forward(); } @@ -1665,9 +1671,9 @@ vi_back_motion(void) input_backward(1); if (cursor <= 0) return; - if (BB_isalnum(command[cursor]) || command[cursor] == '_') { + if (BB_isalnum_or_underscore(command[cursor])) { while (cursor > 0 - && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_') + && (BB_isalnum_or_underscore(command[cursor-1])) ) { input_backward(1); } -- cgit v1.2.3-55-g6feb From 215b0ca6e4fe466c6942d21a1bba62d97f2d5e5d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 19 Aug 2016 18:23:56 +0200 Subject: hush: fix a bug in FEATURE_SH_STANDALONE=y config. Closes 9186 Run this in a "sh SCRIPT": sha256sum /dev/null echo END sha256sum is a NOEXEC applet. It runs in a forked child. Then child exit()s. By this time, entire script is read, and buffered in a FILE object from fopen("SCRIPT"). But fgetc() did not consume entire input. exit() lseeks back by -9 bytes, from to 'e' in 'echo'. (this may be libc-specific). This change of fd position *is shared with the parent*! Now parent can read more, and it thinks there is another "echo END". End result: two "echo END"s are run. Fix this by _exit()ing instead. Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 4 +++- shell/hush.c | 23 ++++++++++++++++++++--- shell/hush_test/run-all | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index c341817e2..f760af2cb 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -877,7 +877,9 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv) } if (ENABLE_FEATURE_SUID) check_suid(applet_no); - exit(applet_main[applet_no](argc, argv)); + xfunc_error_retval = applet_main[applet_no](argc, argv); + /* Note: applet_main() may also not return (die on a xfunc or such) */ + xfunc_die(); } # endif /* NUM_APPLETS > 0 */ diff --git a/shell/hush.c b/shell/hush.c index ab192e2cd..be5c98a20 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -1580,11 +1580,11 @@ static void hush_exit(int exitcode) } #endif -#if ENABLE_HUSH_JOB fflush_all(); +#if ENABLE_HUSH_JOB sigexit(- (exitcode & 0xff)); #else - exit(exitcode); + _exit(exitcode); #endif } @@ -6466,7 +6466,23 @@ static void dump_cmd_in_x_mode(char **argv) * Never returns. * Don't exit() here. If you don't exec, use _exit instead. * The at_exit handlers apparently confuse the calling process, - * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ + * in particular stdin handling. Not sure why? -- because of vfork! (vda) + * Also, it was observed that on exit(), fgetc'ed buffered data + * gets "unwound" by some libcs, via lseek(fd, -NUM, SEEK_CUR). + * With the net effect that even after fork(), not vfork(), + * exit() in NOEXECed applet in "sh SCRIPT": + * noexec_applet_here + * echo END_OF_SCRIPT + * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". + * This makes "echo END_OF_SCRIPT" executed twice. exexit() is the fix. + */ +#if ENABLE_FEATURE_SH_STANDALONE +static void exexit(void) +{ + fflush_all(); + _exit(xfunc_error_retval); +} +#endif static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) NORETURN; @@ -6547,6 +6563,7 @@ static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, # if BB_MMU /* see above why on NOMMU it is not allowed */ if (APPLET_IS_NOEXEC(a)) { debug_printf_exec("running applet '%s'\n", argv[0]); + die_func = exexit; run_applet_no_and_exit(a, argv); } # endif diff --git a/shell/hush_test/run-all b/shell/hush_test/run-all index 64a7abc47..837b3f7da 100755 --- a/shell/hush_test/run-all +++ b/shell/hush_test/run-all @@ -64,11 +64,12 @@ do_test() echo -n "$1/$x:" ( "$THIS_SH" "./$x" >"$name.xx" 2>&1 + r=$? # filter C library differences sed -i \ -e "/: invalid option /s:'::g" \ "$name.xx" - test $? -eq 77 && rm -f "../$1-$x.fail" && exit 77 + test $r -eq 77 && rm -f "../$1-$x.fail" && exit 77 diff -u "$name.xx" "$name.right" >"../$1-$x.fail" && rm -f "$name.xx" "../$1-$x.fail" ) case $? in -- cgit v1.2.3-55-g6feb From d3d7f085ebf2898b62d4bb75566122c65be96454 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 26 Aug 2016 20:14:31 +0200 Subject: hexdump: fix numerous bugs in handling of backslashes Was: t=48\\ t=45\\ t=4c\\ t=4c\\ t=4f\\ t=0a\\ Now: =48=\n =45=\n =4c=\n =4c=\n =4f=\n =0a=\n Signed-off-by: Denys Vlasenko --- libbb/dump.c | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) (limited to 'libbb') diff --git a/libbb/dump.c b/libbb/dump.c index 566881a78..154be5d80 100644 --- a/libbb/dump.c +++ b/libbb/dump.c @@ -157,7 +157,7 @@ static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs) /* * figure out the byte count for each conversion; * rewrite the format as necessary, set up blank- - * pbb_dump_adding for end of data. + * padding for end of data. */ if (*p1 == 'c') { pr->flags = F_CHAR; @@ -466,14 +466,14 @@ static void bpad(PR *pr) } static const char conv_str[] ALIGN1 = - "\0\\0\0" - "\007\\a\0" /* \a */ - "\b\\b\0" - "\f\\b\0" - "\n\\n\0" - "\r\\r\0" - "\t\\t\0" - "\v\\v\0" + "\0" "\\""0""\0" + "\007""\\""a""\0" /* \a */ + "\b" "\\""b""\0" + "\f" "\\""f""\0" + "\n" "\\""n""\0" + "\r" "\\""r""\0" + "\t" "\\""t""\0" + "\v" "\\""v""\0" ; @@ -485,7 +485,7 @@ static void conv_c(PR *pr, unsigned char *p) do { if (*p == *str) { ++str; - goto strpr; + goto strpr; /* map e.g. '\n' to "\\n" */ } str += 4; } while (*str); @@ -702,8 +702,6 @@ int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv) void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt) { const char *p; - char *p1; - char *p2; FS *tfs; FU *tfu, **nextfupp; const char *savep; @@ -779,29 +777,42 @@ void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt) } } tfu->fmt = xstrndup(savep, p - savep); -/* escape(tfu->fmt); */ - - p1 = tfu->fmt; /* alphabetic escape sequences have to be done in place */ + strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt); + /* unknown mappings are not changed: "\z" -> '\\' 'z' */ + /* trailing backslash, if any, is preserved */ +#if 0 + char *p1; + char *p2; + p1 = tfu->fmt; for (p2 = p1;; ++p1, ++p2) { - if (*p1 == '\0') { - *p2 = *p1; + *p2 = *p1; + if (*p1 == '\0') break; - } + if (*p1 == '\\') { - const char *cs = conv_str + 4; - ++p1; + const char *cs; + + p1++; *p2 = *p1; + if (*p1 == '\0') { + /* "...\" trailing backslash. Eaten. */ + break; + } + cs = conv_str + 4; /* skip NUL element */ do { + /* map e.g. "\n" -> '\n' */ if (*p1 == cs[2]) { *p2 = cs[0]; break; } cs += 4; } while (*cs); + /* unknown mappings remove bkslash: "\z" -> 'z' */ } } +#endif p++; } -- cgit v1.2.3-55-g6feb From 71a090f1871f165ebf3c31f733b36aafca71a6b4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 29 Aug 2016 14:05:25 +0200 Subject: sha3: fix to conform to final SHA3 padding standard, add -a BITS option function old new delta hash_file 331 396 +65 md5_sha1_sum_main 485 538 +53 packed_usage 30423 30464 +41 sha3_begin 17 31 +14 sha3_hash 101 110 +9 sha3_end 41 49 +8 Signed-off-by: Denys Vlasenko --- coreutils/Config.src | 43 ----------------- coreutils/Kbuild.src | 5 -- coreutils/md5_sha1_sum.c | 122 ++++++++++++++++++++++++++++++++++++++++------- include/applets.src.h | 5 -- include/libbb.h | 1 + libbb/hash_md5_sha.c | 55 +++++++++++++-------- testsuite/sha3sum.tests | 2 +- 7 files changed, 143 insertions(+), 90 deletions(-) (limited to 'libbb') diff --git a/coreutils/Config.src b/coreutils/Config.src index 619c2efe8..b9dde1209 100644 --- a/coreutils/Config.src +++ b/coreutils/Config.src @@ -328,12 +328,6 @@ config FEATURE_LS_COLOR_IS_DEFAULT configurable, and the output may not be legible on many output screens. -config MD5SUM - bool "md5sum" - default y - help - md5sum is used to print or check MD5 checksums. - config MKDIR bool "mkdir" default y @@ -458,30 +452,6 @@ config SEQ help print a sequence of numbers -config SHA1SUM - bool "sha1sum" - default y - help - Compute and check SHA1 message digest - -config SHA256SUM - bool "sha256sum" - default y - help - Compute and check SHA256 message digest - -config SHA512SUM - bool "sha512sum" - default y - help - Compute and check SHA512 message digest - -config SHA3SUM - bool "sha3sum" - default y - help - Compute and check SHA3 (512-bit) message digest - config SLEEP bool "sleep" default y @@ -731,17 +701,4 @@ config FEATURE_HUMAN_READABLE help Allow df, du, and ls to have human readable output. -comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum" - depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM - -config FEATURE_MD5_SHA1_SUM_CHECK - bool "Enable -c, -s and -w options" - default y - depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM - help - Enabling the -c options allows files to be checked - against pre-calculated hash values. - - -s and -w are useful options when verifying checksums. - endmenu diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src index 4ec075ac6..5a64fee35 100644 --- a/coreutils/Kbuild.src +++ b/coreutils/Kbuild.src @@ -41,7 +41,6 @@ lib-$(CONFIG_LN) += ln.o lib-$(CONFIG_LOGNAME) += logname.o lib-$(CONFIG_LS) += ls.o lib-$(CONFIG_FTPD) += ls.o -lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o lib-$(CONFIG_MKDIR) += mkdir.o lib-$(CONFIG_MKFIFO) += mkfifo.o lib-$(CONFIG_MKNOD) += mknod.o @@ -58,10 +57,6 @@ lib-$(CONFIG_REALPATH) += realpath.o lib-$(CONFIG_RM) += rm.o lib-$(CONFIG_RMDIR) += rmdir.o lib-$(CONFIG_SEQ) += seq.o -lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o -lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o -lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o -lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o lib-$(CONFIG_SLEEP) += sleep.o lib-$(CONFIG_SPLIT) += split.o lib-$(CONFIG_SORT) += sort.o diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c index c0e816ba6..5e017b191 100644 --- a/coreutils/md5_sha1_sum.c +++ b/coreutils/md5_sha1_sum.c @@ -5,6 +5,60 @@ * * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ +//config:config MD5SUM +//config: bool "md5sum" +//config: default y +//config: help +//config: md5sum is used to print or check MD5 checksums. +//config: +//config:config SHA1SUM +//config: bool "sha1sum" +//config: default y +//config: help +//config: Compute and check SHA1 message digest +//config: +//config:config SHA256SUM +//config: bool "sha256sum" +//config: default y +//config: help +//config: Compute and check SHA256 message digest +//config: +//config:config SHA512SUM +//config: bool "sha512sum" +//config: default y +//config: help +//config: Compute and check SHA512 message digest +//config: +//config:config SHA3SUM +//config: bool "sha3sum" +//config: default y +//config: help +//config: Compute and check SHA3 (512-bit) message digest +//config: +//config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum" +//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM +//config: +//config:config FEATURE_MD5_SHA1_SUM_CHECK +//config: bool "Enable -c, -s and -w options" +//config: default y +//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM +//config: help +//config: Enabling the -c options allows files to be checked +//config: against pre-calculated hash values. +//config: +//config: -s and -w are useful options when verifying checksums. + +//applet:IF_MD5SUM(APPLET_NOEXEC(md5sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, md5sum)) +//applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum)) +//applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum)) +//applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum)) +//applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum)) + +//kbuild:lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o +//kbuild:lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o +//kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o +//kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o +//kbuild:lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o //usage:#define md5sum_trivial_usage //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..." @@ -57,13 +111,14 @@ //usage: ) //usage: //usage:#define sha3sum_trivial_usage -//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..." +//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[-a BITS] [FILE]..." //usage:#define sha3sum_full_usage "\n\n" -//usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3-512 checksums" +//usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3 checksums" //usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n" //usage: "\n -c Check sums against list in FILEs" //usage: "\n -s Don't output anything, status code shows success" //usage: "\n -w Warn about improperly formatted checksum lines" +//usage: "\n -a BITS 224 (default), 256, 384, 512" //usage: ) //FIXME: GNU coreutils 8.25 has no -s option, it has only these two long opts: @@ -97,7 +152,10 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value, return (unsigned char *)hex_value; } -static uint8_t *hash_file(const char *filename) +#if !ENABLE_SHA3SUM +# define hash_file(f,w) hash_file(f) +#endif +static uint8_t *hash_file(const char *filename, unsigned sha3_width) { int src_fd, hash_len, count; union _ctx_ { @@ -125,27 +183,47 @@ static uint8_t *hash_file(const char *filename) update = (void*)md5_hash; final = (void*)md5_end; hash_len = 16; - } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) { + } + else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) { sha1_begin(&context.sha1); update = (void*)sha1_hash; final = (void*)sha1_end; hash_len = 20; - } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) { + } + else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) { sha256_begin(&context.sha256); update = (void*)sha256_hash; final = (void*)sha256_end; hash_len = 32; - } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) { + } + else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) { sha512_begin(&context.sha512); update = (void*)sha512_hash; final = (void*)sha512_end; hash_len = 64; - } else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) { + } +#if ENABLE_SHA3SUM + else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) { sha3_begin(&context.sha3); update = (void*)sha3_hash; final = (void*)sha3_end; - hash_len = 64; - } else { + /* + * Should support 224, 256, 384, 512. + * We allow any value which does not blow the algorithm up. + */ + if (sha3_width >= 1600/2 /* input block can't be <= 0 */ + || sha3_width == 0 /* hash len can't be 0 */ + || (sha3_width & 0x1f) /* should be multiple of 32 */ + /* (because input uses up to 8 byte wide word XORs. 32/4=8) */ + ) { + bb_error_msg_and_die("bad -a%u", sha3_width); + } + sha3_width /= 4; + context.sha3.input_block_bytes = 1600/8 - sha3_width; + hash_len = sha3_width/2; + } +#endif + else { xfunc_die(); /* can't reach this */ } @@ -176,18 +254,30 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) { int return_value = EXIT_SUCCESS; unsigned flags; +#if ENABLE_SHA3SUM + unsigned sha3_width = 224; +#endif if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { /* -s and -w require -c */ opt_complementary = "s?c:w?c"; /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ - flags = getopt32(argv, "scwbt"); - argv += optind; - //argc -= optind; +#if ENABLE_SHA3SUM + if (applet_name[3] == HASH_SHA3) + flags = getopt32(argv, "scwbta:+", &sha3_width); + else +#endif + flags = getopt32(argv, "scwbt"); } else { - argv += 1; - //argc -= 1; +#if ENABLE_SHA3SUM + if (applet_name[3] == HASH_SHA3) + getopt32(argv, "a:+", &sha3_width); + else +#endif + getopt32(argv, ""); } + argv += optind; + //argc -= optind; if (!*argv) *--argv = (char*)"-"; @@ -222,7 +312,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) *filename_ptr = '\0'; filename_ptr += 2; - hash_value = hash_file(filename_ptr); + hash_value = hash_file(filename_ptr, sha3_width); if (hash_value && (strcmp((char*)hash_value, line) == 0)) { if (!(flags & FLAG_SILENT)) @@ -251,7 +341,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) } fclose_if_not_stdin(pre_computed_stream); } else { - uint8_t *hash_value = hash_file(*argv); + uint8_t *hash_value = hash_file(*argv, sha3_width); if (hash_value == NULL) { return_value = EXIT_FAILURE; } else { diff --git a/include/applets.src.h b/include/applets.src.h index 6e1b02fc3..248d539c4 100644 --- a/include/applets.src.h +++ b/include/applets.src.h @@ -205,7 +205,6 @@ IF_MAKEDEVS(APPLET(makedevs, BB_DIR_SBIN, BB_SUID_DROP)) IF_MAKEMIME(APPLET(makemime, BB_DIR_BIN, BB_SUID_DROP)) IF_MAN(APPLET(man, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP)) -IF_MD5SUM(APPLET_NOEXEC(md5sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, md5sum)) IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir)) IF_MKFS_VFAT(APPLET_ODDNAME(mkdosfs, mkfs_vfat, BB_DIR_SBIN, BB_SUID_DROP, mkfs_vfat)) @@ -280,10 +279,6 @@ IF_SETKEYCODES(APPLET(setkeycodes, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_SETLOGCONS(APPLET(setlogcons, BB_DIR_USR_SBIN, BB_SUID_DROP)) IF_SETSEBOOL(APPLET(setsebool, BB_DIR_USR_SBIN, BB_SUID_DROP)) IF_SETSID(APPLET(setsid, BB_DIR_USR_BIN, BB_SUID_DROP)) -IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum)) -IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum)) -IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum)) -IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum)) IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP)) IF_SLATTACH(APPLET(slattach, BB_DIR_SBIN, BB_SUID_DROP)) /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells: */ diff --git a/include/libbb.h b/include/libbb.h index b2e4299dd..bf356d727 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1737,6 +1737,7 @@ typedef struct sha512_ctx_t { typedef struct sha3_ctx_t { uint64_t state[25]; unsigned bytes_queued; + unsigned input_block_bytes; } sha3_ctx_t; void md5_begin(md5_ctx_t *ctx) FAST_FUNC; void md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index d08c6b2f7..7e7d8da2f 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -941,10 +941,6 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) # define OPTIMIZE_SHA3_FOR_32 1 #endif -enum { - SHA3_IBLK_BYTES = 72, /* 576 bits / 8 */ -}; - #if OPTIMIZE_SHA3_FOR_32 /* This splits every 64-bit word into a pair of 32-bit words, * even bits go into first word, odd bits go to second one. @@ -1352,6 +1348,8 @@ static void sha3_process_block72(uint64_t *state) void FAST_FUNC sha3_begin(sha3_ctx_t *ctx) { memset(ctx, 0, sizeof(*ctx)); + /* SHA3-512, user can override */ + ctx->input_block_bytes = (1600 - 512*2) / 8; /* 72 bytes */ } void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) @@ -1361,7 +1359,7 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) unsigned bufpos = ctx->bytes_queued; while (1) { - unsigned remaining = SHA3_IBLK_BYTES - bufpos; + unsigned remaining = ctx->input_block_bytes - bufpos; if (remaining > len) remaining = len; len -= remaining; @@ -1373,38 +1371,41 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) remaining--; } /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */ - bufpos -= SHA3_IBLK_BYTES; + bufpos -= ctx->input_block_bytes; if (bufpos != 0) break; /* Buffer is filled up, process it */ sha3_process_block72(ctx->state); /*bufpos = 0; - already is */ } - ctx->bytes_queued = bufpos + SHA3_IBLK_BYTES; + ctx->bytes_queued = bufpos + ctx->input_block_bytes; #else /* +50 bytes code size, but a bit faster because of long-sized XORs */ const uint8_t *data = buffer; unsigned bufpos = ctx->bytes_queued; + unsigned iblk_bytes = ctx->input_block_bytes; /* If already data in queue, continue queuing first */ - while (len != 0 && bufpos != 0) { - uint8_t *buf = (uint8_t*)ctx->state; - buf[bufpos] ^= *data++; - len--; - bufpos++; - if (bufpos == SHA3_IBLK_BYTES) { - bufpos = 0; - goto do_block; + if (bufpos != 0) { + while (len != 0) { + uint8_t *buf = (uint8_t*)ctx->state; + buf[bufpos] ^= *data++; + len--; + bufpos++; + if (bufpos == iblk_bytes) { + bufpos = 0; + goto do_block; + } } } /* Absorb complete blocks */ - while (len >= SHA3_IBLK_BYTES) { + while (len >= iblk_bytes) { /* XOR data onto beginning of state[]. * We try to be efficient - operate one word at a time, not byte. * Careful wrt unaligned access: can't just use "*(long*)data"! */ - unsigned count = SHA3_IBLK_BYTES / sizeof(long); + unsigned count = iblk_bytes / sizeof(long); long *buf = (long*)ctx->state; do { long v; @@ -1412,7 +1413,7 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) *buf++ ^= v; data += sizeof(long); } while (--count); - len -= SHA3_IBLK_BYTES; + len -= iblk_bytes; do_block: sha3_process_block72(ctx->state); } @@ -1433,8 +1434,22 @@ void FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf) { /* Padding */ uint8_t *buf = (uint8_t*)ctx->state; - buf[ctx->bytes_queued] ^= 1; - buf[SHA3_IBLK_BYTES - 1] ^= 0x80; + /* + * Keccak block padding is: add 1 bit after last bit of input, + * then add zero bits until the end of block, and add the last 1 bit + * (the last bit in the block) - the "10*1" pattern. + * SHA3 standard appends additional two bits, 01, before that padding: + * + * SHA3-224(M) = KECCAK[448](M||01, 224) + * SHA3-256(M) = KECCAK[512](M||01, 256) + * SHA3-384(M) = KECCAK[768](M||01, 384) + * SHA3-512(M) = KECCAK[1024](M||01, 512) + * (M is the input, || is bit concatenation) + * + * The 6 below contains 01 "SHA3" bits and the first 1 "Keccak" bit: + */ + buf[ctx->bytes_queued] ^= 6; /* bit pattern 00000110 */ + buf[ctx->input_block_bytes - 1] ^= 0x80; sha3_process_block72(ctx->state); diff --git a/testsuite/sha3sum.tests b/testsuite/sha3sum.tests index 82fada633..2cd8e3bf2 100755 --- a/testsuite/sha3sum.tests +++ b/testsuite/sha3sum.tests @@ -1,3 +1,3 @@ #!/bin/sh -. ./md5sum.tests sha3sum c29d77bc548fa2b20a04c861400a5360879c52156e2a54a3415b99a9a3123e1d5f36714a24eca8c1f05a8e2d8ba859c930d41141f64a255c6794436fc99c486a +. ./md5sum.tests sha3sum 11659f09370139f8ef384f4a6260947fafa6e4fcd87a1ef3f35410e9 -- cgit v1.2.3-55-g6feb From 76787a7e025fb4a8b83e29b2fbfdca8d5bd492b9 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 1 Sep 2016 11:44:13 +0200 Subject: libbb/speed_table.c: survive B115200 and B230400 not fitting into 16 bits Seen on OSX. While at it, expand baud table with B500000..B4000000 Signed-off-by: Denys Vlasenko --- libbb/speed_table.c | 66 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) (limited to 'libbb') diff --git a/libbb/speed_table.c b/libbb/speed_table.c index 174d531b2..3870d4f93 100644 --- a/libbb/speed_table.c +++ b/libbb/speed_table.c @@ -10,7 +10,16 @@ #include "libbb.h" struct speed_map { -#if defined __FreeBSD__ +#if defined __FreeBSD__ \ + || (defined B115200 && B115200 > 0xffff) \ + || (defined B230400 && B230400 > 0xffff) \ + || (defined B460800 && B460800 > 0xffff) \ + || (defined B921600 && B921600 > 0xffff) \ + || (defined B1152000 && B1152000 > 0xffff) \ + || (defined B1000000 && B1000000 > 0xffff) \ + || (defined B2000000 && B2000000 > 0xffff) \ + || (defined B3000000 && B3000000 > 0xffff) \ + || (defined B4000000 && B4000000 > 0xffff) /* On FreeBSD, B constants don't fit into a short */ unsigned speed; #else @@ -19,6 +28,7 @@ struct speed_map { unsigned short value; }; +/* On Linux, Bxx constants are 0..15 (up to B38400) and 0x1001..0x100f */ static const struct speed_map speeds[] = { {B0, 0}, {B50, 50}, @@ -37,28 +47,62 @@ static const struct speed_map speeds[] = { #ifdef B19200 {B19200, 19200}, #elif defined(EXTA) - {EXTA, 19200}, + {EXTA, 19200}, #endif +/* 19200 = 0x4b00 */ +/* 38400 = 0x9600, this value would use bit#15 is not "/200" encoded: */ #ifdef B38400 - {B38400, 38400/256 + 0x8000U}, + {B38400, 38400/200 + 0x8000u}, #elif defined(EXTB) - {EXTB, 38400/256 + 0x8000U}, + {EXTB, 38400/200 + 0x8000u}, #endif #ifdef B57600 - {B57600, 57600/256 + 0x8000U}, + {B57600, 57600/200 + 0x8000u}, #endif #ifdef B115200 - {B115200, 115200/256 + 0x8000U}, + {B115200, 115200/200 + 0x8000u}, #endif #ifdef B230400 - {B230400, 230400/256 + 0x8000U}, + {B230400, 230400/200 + 0x8000u}, #endif #ifdef B460800 - {B460800, 460800/256 + 0x8000U}, + {B460800, 460800/200 + 0x8000u}, +#endif +#ifdef B576000 + {B576000, 576000/200 + 0x8000u}, #endif #ifdef B921600 - {B921600, 921600/256 + 0x8000U}, + {B921600, 921600/200 + 0x8000u}, +#endif +#ifdef B1152000 + {B1152000, 1152000/200 + 0x8000u}, +#endif + +#ifdef B500000 + {B500000, 500000/200 + 0x8000u}, +#endif +#ifdef B1000000 + {B1000000, 1000000/200 + 0x8000u}, +#endif +#ifdef B1500000 + {B1500000, 1500000/200 + 0x8000u}, +#endif +#ifdef B2000000 + {B2000000, 2000000/200 + 0x8000u}, +#endif +#ifdef B2500000 + {B2500000, 2500000/200 + 0x8000u}, +#endif +#ifdef B3000000 + {B3000000, 3000000/200 + 0x8000u}, +#endif +#ifdef B3500000 + {B3500000, 3500000/200 + 0x8000u}, +#endif +#ifdef B4000000 + {B4000000, 4000000/200 + 0x8000u}, #endif +/* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */ }; enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; @@ -69,8 +113,8 @@ unsigned FAST_FUNC tty_baud_to_value(speed_t speed) do { if (speed == speeds[i].speed) { - if (speeds[i].value & 0x8000U) { - return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256; + if (speeds[i].value & 0x8000u) { + return ((unsigned long) (speeds[i].value) & 0x7fffU) * 200; } return speeds[i].value; } -- cgit v1.2.3-55-g6feb From 9fd61be191caf78138a50e9d6a465b39318f91c3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 5 Sep 2016 15:20:10 +0200 Subject: libbb/xwrite: print errno on "short write" errors Lauri Kasanen: :: Over at TinyCore, we receive a huge number of questions of the type "I :: got "short write", what does it mean?". Mostly for the rpi port and when :: using bb wget. Signed-off-by: Denys Vlasenko --- libbb/xfuncs_printf.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index e9222f690..1b11caf6b 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -235,8 +235,16 @@ void FAST_FUNC xwrite(int fd, const void *buf, size_t count) { if (count) { ssize_t size = full_write(fd, buf, count); - if ((size_t)size != count) - bb_error_msg_and_die("short write"); + if ((size_t)size != count) { + /* + * Two cases: write error immediately; + * or some writes succeeded, then we hit an error. + * In either case, errno is set. + */ + bb_perror_msg_and_die( + size >= 0 ? "short write" : "write error" + ); + } } } void FAST_FUNC xwrite_str(int fd, const char *str) -- cgit v1.2.3-55-g6feb From 7cef4817d6d6d61a1166ed7dfc13537b95c65970 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 15 Sep 2016 13:20:51 +0200 Subject: libbb:/send_to_from: do not require that "to" should have the same AF. Closes 9146 Signed-off-by: Denys Vlasenko --- libbb/udp_io.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/udp_io.c b/libbb/udp_io.c index a32af9bd2..6e3ef484e 100644 --- a/libbb/udp_io.c +++ b/libbb/udp_io.c @@ -70,7 +70,13 @@ send_to_from(int fd, void *buf, size_t len, int flags, msg.msg_flags = flags; cmsgptr = CMSG_FIRSTHDR(&msg); - if (to->sa_family == AF_INET && from->sa_family == AF_INET) { + /* + * Users report that to->sa_family can be AF_INET6 too, + * if "to" was acquired by recv_from_to(). IOW: recv_from_to() + * was seen showing IPv6 "from" even when the destination + * of received packet (our local address) was IPv4. + */ + if (/* to->sa_family == AF_INET && */ from->sa_family == AF_INET) { struct in_pktinfo *pktptr; cmsgptr->cmsg_level = IPPROTO_IP; cmsgptr->cmsg_type = IP_PKTINFO; @@ -86,7 +92,7 @@ send_to_from(int fd, void *buf, size_t len, int flags, pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr; } # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) - else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) { + else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) { struct in6_pktinfo *pktptr; cmsgptr->cmsg_level = IPPROTO_IPV6; cmsgptr->cmsg_type = IPV6_PKTINFO; -- cgit v1.2.3-55-g6feb From 65ba7113e3da696f8e2ebf48658b1f71967d030b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 24 Sep 2016 23:50:43 +0200 Subject: libbb: handle \S in /etc/issue Signed-off-by: Denys Vlasenko --- libbb/login.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'libbb') diff --git a/libbb/login.c b/libbb/login.c index 8f080b775..5a7acfcf0 100644 --- a/libbb/login.c +++ b/libbb/login.c @@ -45,6 +45,45 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) if (c == '\\' || c == '%') { c = fgetc(fp); switch (c) { +//From getty manpage (* - supported by us) +//======================================== +//4 or 4{interface} +// Insert the IPv4 address of the network interface (example: \4{eth0}). +// If the interface argument is not specified, then select the first +// fully configured (UP, non-LOOPBACK, RUNNING) interface. +//6 or 6{interface} -- The same as \4 but for IPv6. +//b -- Insert the baudrate of the current line. +//*d -- Insert the current date. +//*t -- Insert the current time. +//e or e{name} +// Translate the human-readable name to an escape sequence and insert it +// (for example: \e{red}Alert text.\e{reset}). If the name argument +// is not specified, then insert \033. The currently supported names are: +// black, blink, blue, bold, brown, cyan, darkgray, gray, green, halfbright, +// lightblue, lightcyan, lightgray, lightgreen, lightmagenta, lightred, +// magenta, red, reset, reverse, and yellow. Unknown names are ignored. +//*s +// Insert the system name (the name of the operating system - `uname -s`) +//*S or S{VARIABLE} +// Insert the VARIABLE data from /etc/os-release. +// If the VARIABLE argument is not specified, use PRETTY_NAME. +// If PRETTY_NAME is not in /etc/os-release, \S is the same as \s. +//*l -- Insert the name of the current tty line. +//*m -- Insert the architecture identifier of the machine: `uname -m`. +//*n -- Insert the nodename of the machine: `uname -n`. +//*o -- Insert the NIS domainname of the machine. Same as `hostname -d'. +//*O -- Insert the DNS domainname of the machine. +//*r -- Insert the release number of the OS: `uname -r`. +//u -- Insert the number of current users logged in. +//U -- Insert the string "1 user" or "N users" (current users logged in). +//*v -- Insert the version of the OS, e.g. the build-date etc: `uname -v`. +//We also implement: +//*D -- same as \O "DNS domainname" +//*h -- same as \n "nodename" + + case 'S': + /* minimal implementation, not reading /etc/os-release */ + /*FALLTHROUGH*/ case 's': outbuf = uts.sysname; break; @@ -65,6 +104,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) #if defined(__linux__) case 'D': case 'o': + case 'O': outbuf = uts.domainname; break; #endif -- cgit v1.2.3-55-g6feb From 4537f83d522086bec8aaa2077f889d8c1c389862 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 26 Sep 2016 08:52:52 +0200 Subject: typo fix in comment Signed-off-by: Denys Vlasenko --- libbb/speed_table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/speed_table.c b/libbb/speed_table.c index 3870d4f93..13dc9c73a 100644 --- a/libbb/speed_table.c +++ b/libbb/speed_table.c @@ -50,7 +50,7 @@ static const struct speed_map speeds[] = { {EXTA, 19200}, #endif /* 19200 = 0x4b00 */ -/* 38400 = 0x9600, this value would use bit#15 is not "/200" encoded: */ +/* 38400 = 0x9600, this value would use bit#15 if not "/200" encoded: */ #ifdef B38400 {B38400, 38400/200 + 0x8000u}, #elif defined(EXTB) -- cgit v1.2.3-55-g6feb From 525209ac9465f37a9ba292f1ff138dd80768e869 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 26 Sep 2016 14:37:12 +0200 Subject: libbb/speed_table.c: expand comments Signed-off-by: Denys Vlasenko --- libbb/speed_table.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/speed_table.c b/libbb/speed_table.c index 13dc9c73a..11ced01d0 100644 --- a/libbb/speed_table.c +++ b/libbb/speed_table.c @@ -103,8 +103,23 @@ static const struct speed_map speeds[] = { {B4000000, 4000000/200 + 0x8000u}, #endif /* 4000000/200 = 0x4e20, bit#15 still does not interfere with the value */ +/* (can use /800 if higher speeds would appear, /1600 won't work for B500000) */ }; +/* + * TODO: maybe we can just bite the bullet, ditch the table and use termios2 + * Linux API (supports arbitrary baud rates, no Bxxxx mess needed)? Example: + * + * #include + * #include + * struct termios2 t; + * ioctl(fd, TCGETS2, &t); + * t.c_ospeed = t.c_ispeed = 543210; + * t.c_cflag &= ~CBAUD; + * t.c_cflag |= BOTHER; + * ioctl(fd, TCSETS2, &t); + */ + enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; unsigned FAST_FUNC tty_baud_to_value(speed_t speed) @@ -114,7 +129,7 @@ unsigned FAST_FUNC tty_baud_to_value(speed_t speed) do { if (speed == speeds[i].speed) { if (speeds[i].value & 0x8000u) { - return ((unsigned long) (speeds[i].value) & 0x7fffU) * 200; + return ((unsigned)(speeds[i].value) & 0x7fffU) * 200; } return speeds[i].value; } -- cgit v1.2.3-55-g6feb From 3c18e3051dfdb5591afc18562e9d9da987a5945e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 26 Sep 2016 19:53:04 +0200 Subject: libbb: make xmalloc_sockaddr2dotted use NI_NUMERICSCOPE Gives "mount -t cifs //fe80::6a05:caff:fe3e:dbf5%eth0/test test" a chance to work: mount must pass "ip=numeric_IPv6%numeric_iface_id" in the omunt option string. Currently, it does not. Signed-off-by: Denys Vlasenko --- libbb/xconnect.c | 7 +++++-- util-linux/mount.c | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 6e78e6363..3a0dc2653 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -496,12 +496,15 @@ char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) { return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT); } +#ifndef NI_NUMERICSCOPE +# define NI_NUMERICSCOPE 0 +#endif char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa) { - return sockaddr2str(sa, NI_NUMERICHOST); + return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE); } char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) { - return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT); + return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE | IGNORE_PORT); } diff --git a/util-linux/mount.c b/util-linux/mount.c index 13590ceb4..c3e91e2a6 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -1975,6 +1975,12 @@ static int singlemount(struct mntent *mp, int ignore_busy) dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); if (ENABLE_FEATURE_CLEAN_UP) free(lsa); ip = xasprintf("ip=%s", dotted); +// Note: IPv6 scoped addresses ("name%iface", see RFC 4007) should be +// handled by libc in getnameinfo() (inside xmalloc_sockaddr2dotted_noport()). +// Currently, glibc does not support that (has no NI_NUMERICSCOPE), +// musl apparently does. This results in "ip=numericIPv6%iface_name" +// (instead of _numeric_ iface_id) with glibc. +// This probalby should be fixed in glibc, not here. if (ENABLE_FEATURE_CLEAN_UP) free(dotted); parse_mount_options(ip, &filteropts); if (ENABLE_FEATURE_CLEAN_UP) free(ip); -- cgit v1.2.3-55-g6feb From 7f0ebbc69ed14b2f35e8bc62b03612b94e270955 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 3 Oct 2016 17:42:53 +0200 Subject: hush: add commented-out debug printouts in "memleak" built-in Allocation addresses of malloc() are jittery, thought I had a mem leak in hush, but it was malloc variability. Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 8 ++++++++ shell/hush.c | 9 +++++++++ 2 files changed, 17 insertions(+) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index f760af2cb..2d01a3ae7 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -942,6 +942,14 @@ int main(int argc UNUSED_PARAM, char **argv) */ mallopt(M_MMAP_THRESHOLD, 32 * 1024 - 256); #endif +#if 0 /*def M_TOP_PAD*/ + /* When the program break is increased, then M_TOP_PAD bytes are added + * to the sbrk(2) request. When the heap is trimmed because of free(3), + * this much free space is preserved at the top of the heap. + * glibc default seems to be way too big: 128k, but need to verify. + */ + mallopt(M_TOP_PAD, 8 * 1024); +#endif #if !BB_MMU /* NOMMU re-exec trick sets high-order bit in first byte of name */ diff --git a/shell/hush.c b/shell/hush.c index 668b1f2b7..9b51f389e 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -9284,6 +9284,15 @@ static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) if (l < (unsigned long)p) l = (unsigned long)p; free(p); + +# if 0 /* debug */ + { + struct mallinfo mi = mallinfo(); + printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, + mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); + } +# endif + if (!G.memleak_value) G.memleak_value = l; -- cgit v1.2.3-55-g6feb