[git commit] ash: optional sleep builtin

Denys Vlasenko vda.linux at googlemail.com
Sat Aug 27 17:56:21 UTC 2022


commit: https://git.busybox.net/busybox/commit/?id=58598eb7093561d914a6254697e137b815f1fdfc
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
sleepcmd                                               -      10     +10
builtintab                                           352     360      +8
.rodata                                           105264  105271      +7
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 25/0)               Total: 25 bytes

Signed-off-by: Shawn Landden <shawnlandden at tutanota.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/sleep.c |  1 +
 include/libbb.h   |  1 +
 libbb/duration.c  |  6 ++++++
 shell/ash.c       | 11 +++++++++++
 4 files changed, 19 insertions(+)

diff --git a/coreutils/sleep.c b/coreutils/sleep.c
index 2658e84df..442841210 100644
--- a/coreutils/sleep.c
+++ b/coreutils/sleep.c
@@ -37,6 +37,7 @@
 //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
 
 //kbuild:lib-$(CONFIG_SLEEP) += sleep.o
+//kbuild:lib-$(CONFIG_ASH_SLEEP) += sleep.o
 
 /* BB_AUDIT SUSv3 compliant */
 /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
diff --git a/include/libbb.h b/include/libbb.h
index abbc9ac59..19ed9ec09 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1510,6 +1510,7 @@ int ash_main(int argc, char** argv) IF_SHELL_ASH(MAIN_EXTERNALLY_VISIBLE);
 int hush_main(int argc, char** argv) IF_SHELL_HUSH(MAIN_EXTERNALLY_VISIBLE);
 /* If shell needs them, they exist even if not enabled as applets */
 int echo_main(int argc, char** argv) IF_ECHO(MAIN_EXTERNALLY_VISIBLE);
+int sleep_main(int argc, char **argv) IF_SLEEP(MAIN_EXTERNALLY_VISIBLE);
 int printf_main(int argc, char **argv) IF_PRINTF(MAIN_EXTERNALLY_VISIBLE);
 int test_main(int argc, char **argv)
 #if ENABLE_TEST || ENABLE_TEST1 || ENABLE_TEST2
diff --git a/libbb/duration.c b/libbb/duration.c
index a6a29ddae..793d02f42 100644
--- a/libbb/duration.c
+++ b/libbb/duration.c
@@ -76,6 +76,12 @@ void FAST_FUNC sleep_for_duration(duration_t duration)
 		ts.tv_sec = duration;
 		ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
 	}
+	/* NB: if ENABLE_ASH_SLEEP, we end up here if "sleep N"
+	 * is run in ash. ^C will still work, because ash's signal handler
+	 * does not return (it longjumps), the below loop
+	 * will not continue looping.
+	 * (This wouldn't work in hush)
+	 */
 	do {
 		errno = 0;
 		nanosleep(&ts, &ts);
diff --git a/shell/ash.c b/shell/ash.c
index 55c1034f5..326f8b2a9 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -134,6 +134,11 @@
 //config:	default y
 //config:	depends on SHELL_ASH
 //config:
+//config:config ASH_SLEEP
+//config:	bool "sleep builtin"
+//config:	default y
+//config:	depends on SHELL_ASH
+//config:
 //config:config ASH_HELP
 //config:	bool "help builtin"
 //config:	default y
@@ -10155,6 +10160,9 @@ static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc,
 #if ENABLE_ASH_TEST || BASH_TEST2
 static int FAST_FUNC testcmd(int argc, char **argv)   { return test_main(argc, argv); }
 #endif
+#if ENABLE_ASH_SLEEP
+static int FAST_FUNC sleepcmd(int argc, char **argv)  { return sleep_main(argc, argv); }
+#endif
 
 /* Keep these in proper order since it is searched via bsearch() */
 static const struct builtincmd builtintab[] = {
@@ -10217,6 +10225,9 @@ static const struct builtincmd builtintab[] = {
 	{ BUILTIN_SPEC_REG      "return"  , returncmd  },
 	{ BUILTIN_SPEC_REG      "set"     , setcmd     },
 	{ BUILTIN_SPEC_REG      "shift"   , shiftcmd   },
+#if ENABLE_ASH_SLEEP
+	{ BUILTIN_REGULAR       "sleep"   , sleepcmd   },
+#endif
 #if BASH_SOURCE
 	{ BUILTIN_SPEC_REG      "source"  , dotcmd     },
 #endif


More information about the busybox-cvs mailing list