From 4c20d9f2b0223874e2b5ac1235d5f33fdd02589b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 3 Aug 2018 18:17:12 +0200 Subject: extend fractional duration support to "top -d N.N" and "timeout" function old new delta parse_duration_str - 168 +168 sleep_for_duration - 157 +157 top_main 885 928 +43 timeout_main 269 312 +43 handle_input 571 614 +43 duration_suffixes - 40 +40 sfx 40 - -40 sleep_main 364 79 -285 ------------------------------------------------------------------------------ (add/remove: 4/1 grow/shrink: 3/1 up/down: 494/-325) Total: 169 bytes Signed-off-by: Denys Vlasenko --- libbb/duration.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 libbb/duration.c (limited to 'libbb') diff --git a/libbb/duration.c b/libbb/duration.c new file mode 100644 index 000000000..765a1e9fe --- /dev/null +++ b/libbb/duration.c @@ -0,0 +1,76 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 2018 Denys Vlasenko + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ +//config:config FLOAT_DURATION +//config: bool "Enable fractional duration arguments" +//config: default y +//config: help +//config: Allow sleep N.NNN, top -d N.NNN etc. + +//kbuild:lib-$(CONFIG_SLEEP) += duration.o +//kbuild:lib-$(CONFIG_TOP) += duration.o +//kbuild:lib-$(CONFIG_TIMEOUT) += duration.o + +#include "libbb.h" + +static const struct suffix_mult duration_suffixes[] = { + { "s", 1 }, + { "m", 60 }, + { "h", 60*60 }, + { "d", 24*60*60 }, + { "", 0 } +}; + +#if ENABLE_FLOAT_DURATION +duration_t FAST_FUNC parse_duration_str(char *str) +{ + duration_t duration; + + if (strchr(str, '.')) { + double d; + char *pp; + int len = strspn(str, "0123456789."); + char sv = str[len]; + str[len] = '\0'; + errno = 0; + d = strtod(str, &pp); + if (errno || *pp) + bb_show_usage(); + str += len; + *str-- = sv; + sv = *str; + *str = '1'; + duration = d * xatoul_sfx(str, duration_suffixes); + *str = sv; + } else { + duration = xatoul_sfx(str, duration_suffixes); + } + + return duration; +} +void FAST_FUNC sleep_for_duration(duration_t duration) +{ + struct timespec ts; + + ts.tv_sec = MAXINT(typeof(ts.tv_sec)); + ts.tv_nsec = 0; + if (duration >= 0 && duration < ts.tv_sec) { + ts.tv_sec = duration; + ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; + } + do { + errno = 0; + nanosleep(&ts, &ts); + } while (errno == EINTR); +} +#else +duration_t FAST_FUNC parse_duration_str(char *str) +{ + return xatou_range_sfx(*argv, 0, UINT_MAX, duration_suffixes); +} +#endif -- cgit v1.2.3-55-g6feb From 277e00ed12264f7ae1d17f2cbc8be40d3dc0655d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 3 Aug 2018 18:51:46 +0200 Subject: Complie libbb/duration.c if ping[6] is selected Signed-off-by: Denys Vlasenko --- libbb/duration.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'libbb') diff --git a/libbb/duration.c b/libbb/duration.c index 765a1e9fe..2f272771f 100644 --- a/libbb/duration.c +++ b/libbb/duration.c @@ -15,6 +15,8 @@ //kbuild:lib-$(CONFIG_SLEEP) += duration.o //kbuild:lib-$(CONFIG_TOP) += duration.o //kbuild:lib-$(CONFIG_TIMEOUT) += duration.o +//kbuild:lib-$(CONFIG_PING) += duration.o +//kbuild:lib-$(CONFIG_PING6) += duration.o #include "libbb.h" -- cgit v1.2.3-55-g6feb From 6791140123ebe7535f525f1a893a8536219122c4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 26 Aug 2018 16:32:16 +0200 Subject: fix !CONFIG_FLOAT_DURATION build Signed-off-by: Denys Vlasenko --- libbb/duration.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/duration.c b/libbb/duration.c index 2f272771f..5acd0dba3 100644 --- a/libbb/duration.c +++ b/libbb/duration.c @@ -73,6 +73,6 @@ void FAST_FUNC sleep_for_duration(duration_t duration) #else duration_t FAST_FUNC parse_duration_str(char *str) { - return xatou_range_sfx(*argv, 0, UINT_MAX, duration_suffixes); + return xatou_range_sfx(str, 0, UINT_MAX, duration_suffixes); } #endif -- cgit v1.2.3-55-g6feb From 0d598ab9f03dbf320f7b81c05e4a94cb303dfbc7 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 2 Sep 2018 18:35:29 +0200 Subject: Revert "libbb: remove unnecessary variable in xmalloc_fgets" The variable is in fact necessary. commit 2da9724b56169f00bd7fb6b9a11c9409a7620981 Author: Quentin Rameau Date: Sun Apr 1 17:05:35 2018 +0200 libbb: remove unnecessary variable in xmalloc_fgets Signed-off-by: Denys Vlasenko --- libbb/get_line_from_file.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index f3d6c6203..49ef093c2 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -47,7 +47,9 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) /* Get line, including trailing \n if any */ char* FAST_FUNC xmalloc_fgets(FILE *file) { - return bb_get_chunk_from_file(file, NULL); + int i; + + return bb_get_chunk_from_file(file, &i); } /* Get line. Remove trailing \n */ char* FAST_FUNC xmalloc_fgetline(FILE *file) -- cgit v1.2.3-55-g6feb From 22a99516206b33b7ae124d426319bab03d5c8309 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 2 Sep 2018 18:48:09 +0200 Subject: libbb: in xmalloc_fgets(), use size_t for bb_get_chunk_from_file() Signed-off-by: Denys Vlasenko --- libbb/get_line_from_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 49ef093c2..903ff1fb6 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -47,7 +47,7 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) /* Get line, including trailing \n if any */ char* FAST_FUNC xmalloc_fgets(FILE *file) { - int i; + size_t i; return bb_get_chunk_from_file(file, &i); } -- cgit v1.2.3-55-g6feb From 3060992ec94722b4f8f3711a1884270c81a6e5f5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 3 Sep 2018 10:25:29 +0200 Subject: libbb: fix use-after-free in copy_file 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 98bd4fe72..2d6557cd4 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -388,14 +388,15 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) char *lpath = xmalloc_readlink_or_warn(source); if (lpath) { int r = symlink(lpath, dest); - free(lpath); if (r < 0) { /* shared message */ bb_perror_msg("can't create %slink '%s' to '%s'", "sym", dest, lpath ); + free(lpath); return -1; } + free(lpath); if (flags & FILEUTILS_PRESERVE_STATUS) if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) bb_perror_msg("can't preserve %s of '%s'", "ownership", dest); -- cgit v1.2.3-55-g6feb From 28d91d754e423fd0df584bbfa9b903eacac21224 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 3 Sep 2018 10:36:51 +0200 Subject: libbb: fix potential NULL pointer use function old new delta unicode_conv_to_printable2 193 216 +23 Signed-off-by: Denys Vlasenko --- libbb/unicode.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'libbb') diff --git a/libbb/unicode.c b/libbb/unicode.c index 9c4da50d3..d378175a4 100644 --- a/libbb/unicode.c +++ b/libbb/unicode.c @@ -1121,6 +1121,8 @@ static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char dst[dst_len++] = ' '; } } + if (!dst) /* for example, if input was "" */ + dst = xzalloc(1); dst[dst_len] = '\0'; if (stats) { stats->byte_count = dst_len; -- cgit v1.2.3-55-g6feb From 05b18065ab9c375f6185b65a3631d4c6cc1a4be9 Mon Sep 17 00:00:00 2001 From: Chen Yu Date: Tue, 4 Sep 2018 15:26:22 +0800 Subject: remove_file: don't call rmdir if remove_file return failure When deleting a directory, the directory should not be removed if the file in the subdirectory fails to be deleted. Background information: When I tested the kernel using LTP (linux-test-project).I found the mv command have some issue. The LTP test case use the mv command to move the directory t1 in the cgroup file system to the /tmp directory. becase files in the cgroup file system are not allowed to be removed. so the mv reported "Permission denied", but I used the ls command to view the results and found that the directory t1 had been removed from the cgroup file system. For the same test case, I used the mv tool in the GNU coreutils, and the directory t1 will not be removed. the following testcase use busybox mv: / # mount -t cgroup -o cpu cgroup /cpu / # cd /cpu /cpu # mkdir -p t1 /cpu # ls cgroup.clone_children cpu.cfs_period_us cpu.stat t1 cgroup.procs cpu.cfs_quota_us notify_on_release tasks cgroup.sane_behavior cpu.shares release_agent /cpu # mv t1 /tmp mv: can't remove 't1/cgroup.procs': Operation not permitted mv: can't remove 't1/cpu.cfs_period_us': Operation not permitted mv: can't remove 't1/cpu.stat': Operation not permitted mv: can't remove 't1/cpu.shares': Operation not permitted mv: can't remove 't1/cpu.cfs_quota_us': Operation not permitted mv: can't remove 't1/tasks': Operation not permitted mv: can't remove 't1/notify_on_release': Operation not permitted mv: can't remove 't1/cgroup.clone_children': Operation not permitted /cpu # ls cgroup.clone_children cpu.cfs_period_us cpu.stat cgroup.procs cpu.cfs_quota_us notify_on_release tasks cgroup.sane_behavior cpu.shares release_agent /cpu # This patch fixed it, don't call rmdir if remove_file return failure, and under certain file systems, the mv could work normally. Signed-off-by: Chen Yu Signed-off-by: Denys Vlasenko --- libbb/remove_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/remove_file.c b/libbb/remove_file.c index 86c9a5c56..cea5d47e6 100644 --- a/libbb/remove_file.c +++ b/libbb/remove_file.c @@ -73,7 +73,7 @@ int FAST_FUNC remove_file(const char *path, int flags) return status; } - if (rmdir(path) < 0) { + if (status == 0 && rmdir(path) < 0) { bb_perror_msg("can't remove '%s'", path); return -1; } -- cgit v1.2.3-55-g6feb