From ec16c030bd5527137f95a1c30e6a1f3c9054dcf7 Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Sun, 29 Nov 2020 11:37:34 +0100
Subject: libbb: introduce and use sleep1()

function                                             old     new   delta
sleep1                                                 -       9      +9
run_shutdown_and_kill_processes                       97      95      -2
restore_state_and_exit                               116     114      -2
reread_partition_table                                67      65      -2
flush_buffer_cache                                    80      78      -2
chat_main                                           1302    1300      -2
timeout_main                                         310     307      -3
telnet_main                                         1235    1232      -3
stop_handler                                          86      83      -3
process_action                                      1078    1075      -3
nbdclient_main                                      1185    1182      -3
init_main                                            789     786      -3
getty_main                                          1541    1538      -3
do_time                                              410     407      -3
runsv_main                                          1682    1677      -5
pause_and_low_level_reboot                            59      54      -5
inetd_main                                          1917    1911      -6
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/16 up/down: 9/-50)            Total: -41 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 coreutils/timeout.c     |  2 +-
 e2fsprogs/fsck.c        |  2 +-
 include/libbb.h         |  4 +---
 init/halt.c             |  4 ++--
 init/init.c             | 12 ++++++------
 libbb/bb_do_delay.c     |  6 ++++++
 loginutils/getty.c      |  2 +-
 miscutils/chat.c        |  2 +-
 miscutils/hdparm.c      |  4 ++--
 networking/ifplugd.c    |  2 +-
 networking/inetd.c      |  4 ++--
 networking/nbd-client.c |  2 +-
 networking/nc_bloaty.c  |  2 +-
 networking/slattach.c   |  2 +-
 networking/telnet.c     |  2 +-
 runit/runsv.c           |  4 ++--
 sysklogd/logread.c      |  2 +-
 util-linux/fdisk.c      |  2 +-
 util-linux/mdev.c       |  2 +-
 19 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/coreutils/timeout.c b/coreutils/timeout.c
index 8b7bc2eaa..2a628b71d 100644
--- a/coreutils/timeout.c
+++ b/coreutils/timeout.c
@@ -105,7 +105,7 @@ int timeout_main(int argc UNUSED_PARAM, char **argv)
  grandchild:
 		/* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
 		while (1) {
-			sleep(1);
+			sleep1();
 			if (--timeout <= 0)
 				break;
 			if (kill(parent, 0)) {
diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c
index fc53a9043..96c1e51e0 100644
--- a/e2fsprogs/fsck.c
+++ b/e2fsprogs/fsck.c
@@ -480,7 +480,7 @@ static int wait_one(int flags)
 			 * time to set up the signal handler
 			 */
 			if (inst2->start_time >= time(NULL) - 1)
-				sleep(1);
+				sleep1();
 			kill(inst2->pid, SIGUSR1);
 			inst2->flags |= FLAG_PROGRESS;
 			break;
diff --git a/include/libbb.h b/include/libbb.h
index 6b822016e..9fa0ce90d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1601,11 +1601,9 @@ char *bb_simplify_path(const char *path) FAST_FUNC;
 /* Returns ptr to NUL */
 char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
 
-#ifndef LOGIN_FAIL_DELAY
-#define LOGIN_FAIL_DELAY 3
-#endif
 void pause_after_failed_login(void) FAST_FUNC;
 void bb_do_delay(int seconds) FAST_FUNC;
+void sleep1(void) FAST_FUNC;
 void change_identity(const struct passwd *pw) FAST_FUNC;
 void run_shell(const char *shell, int loginshell, const char **args) NORETURN FAST_FUNC;
 
diff --git a/init/halt.c b/init/halt.c
index 785c38130..2070eaa4d 100644
--- a/init/halt.c
+++ b/init/halt.c
@@ -144,14 +144,14 @@ static int init_was_not_there(void)
 	 */
 #if 0
 	while (kill(1, 0) != 0 && --cnt >= 0)
-		sleep(1);
+		sleep1();
 #endif
 	/* ... so let's wait for some evidence a usual startup event,
 	 * mounting of /proc, happened. By that time init should be ready
 	 * for signals.
 	 */
 	while (access("/proc/meminfo", F_OK) != 0 && --cnt >= 0)
-		sleep(1);
+		sleep1();
 
 	/* Does it look like init wasn't there? */
 	return (cnt != initial - 1);
diff --git a/init/init.c b/init/init.c
index 28775a65c..efab5dcb4 100644
--- a/init/init.c
+++ b/init/init.c
@@ -736,7 +736,7 @@ static void pause_and_low_level_reboot(unsigned magic)
 	pid_t pid;
 
 	/* Allow time for last message to reach serial console, etc */
-	sleep(1);
+	sleep1();
 
 	/* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
 	 * in linux/kernel/sys.c, which can cause the machine to panic when
@@ -751,7 +751,7 @@ static void pause_and_low_level_reboot(unsigned magic)
 	 * we would eternally sleep here - not what we want.
 	 */
 	waitpid(pid, NULL, 0);
-	sleep(1); /* paranoia */
+	sleep1(); /* paranoia */
 	_exit(EXIT_SUCCESS);
 }
 
@@ -768,12 +768,12 @@ static void run_shutdown_and_kill_processes(void)
 	kill(-1, SIGTERM);
 	message(L_CONSOLE, "Sent SIG%s to all processes", "TERM");
 	sync();
-	sleep(1);
+	sleep1();
 
 	kill(-1, SIGKILL);
 	message(L_CONSOLE, "Sent SIG%s to all processes", "KILL");
 	sync();
-	/*sleep(1); - callers take care about making a pause */
+	/*sleep1(); - callers take care about making a pause */
 }
 
 /* Signal handling by init:
@@ -904,7 +904,7 @@ static void stop_handler(int sig UNUSED_PARAM)
 		wpid = wait_any_nohang(NULL);
 		mark_terminated(wpid);
 		if (wpid <= 0) /* no processes exited? sleep a bit */
-			sleep(1);
+			sleep1();
 	}
 
 	signal(SIGCONT, SIG_DFL);
@@ -1209,7 +1209,7 @@ int init_main(int argc UNUSED_PARAM, char **argv)
 		}
 
 		/* Don't consume all CPU time - sleep a bit */
-		sleep(1);
+		sleep1();
 	} /* while (1) */
 }
 
diff --git a/libbb/bb_do_delay.c b/libbb/bb_do_delay.c
index 29343305d..3a86dc2ae 100644
--- a/libbb/bb_do_delay.c
+++ b/libbb/bb_do_delay.c
@@ -28,3 +28,9 @@ void FAST_FUNC pause_after_failed_login(void)
 	sleep(LOGIN_FAIL_DELAY);
 #endif
 }
+
+void FAST_FUNC sleep1(void)
+{
+	sleep(1);
+}
+
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 7393a3d1c..99af72f68 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -434,7 +434,7 @@ static void auto_baud(void)
 	 * Wait for a while, then read everything the modem has said so far and
 	 * try to extract the speed of the dial-in call.
 	 */
-	sleep(1);
+	sleep1();
 	nread = safe_read(STDIN_FILENO, G.line_buf, sizeof(G.line_buf) - 1);
 	if (nread > 0) {
 		int speed;
diff --git a/miscutils/chat.c b/miscutils/chat.c
index a04565063..a7faaf284 100644
--- a/miscutils/chat.c
+++ b/miscutils/chat.c
@@ -473,7 +473,7 @@ int chat_main(int argc UNUSED_PARAM, char **argv)
 					if ('\\' == c) {
 						c = *++buf;
 						if ('d' == c) {
-							sleep(1);
+							sleep1();
 							len--;
 							continue;
 						}
diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c
index d25a2466e..431a0ad96 100644
--- a/miscutils/hdparm.c
+++ b/miscutils/hdparm.c
@@ -1436,7 +1436,7 @@ static void flush_buffer_cache(/*int fd*/ void)
 	fsync(fd);				/* flush buffers */
 	ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
 #ifdef HDIO_DRIVE_CMD
-	sleep(1);
+	sleep1();
 	if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) {	/* await completion */
 		if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
 			bb_simple_perror_msg("HDIO_DRIVE_CMD");
@@ -1511,7 +1511,7 @@ static void do_time(int cache /*,int fd*/)
 	 * NB: *small* delay. User is expected to have a clue and to not run
 	 * heavy io in parallel with measurements. */
 	sync();
-	sleep(1);
+	sleep1();
 	if (cache) { /* Time cache */
 		seek_to_zero();
 		read_big_block(buf);
diff --git a/networking/ifplugd.c b/networking/ifplugd.c
index 0d17b7d8c..60916eae6 100644
--- a/networking/ifplugd.c
+++ b/networking/ifplugd.c
@@ -735,7 +735,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
 					delay_time += G.delay_down;
 #if 0  /* if you are back in 1970... */
 				if (delay_time == 0) {
-					sleep(1);
+					sleep1();
 					delay_time = 1;
 				}
 #endif
diff --git a/networking/inetd.c b/networking/inetd.c
index 3cd2b11f0..44b1ac700 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1305,7 +1305,7 @@ int inetd_main(int argc UNUSED_PARAM, char **argv)
 		if (ready_fd_cnt < 0) {
 			if (errno != EINTR) {
 				bb_simple_perror_msg("select");
-				sleep(1);
+				sleep1();
 			}
 			continue;
 		}
@@ -1406,7 +1406,7 @@ int inetd_main(int argc UNUSED_PARAM, char **argv)
 
 				if (pid < 0) { /* fork error */
 					bb_simple_perror_msg("vfork"+1);
-					sleep(1);
+					sleep1();
 					restore_sigmask(&omask);
 					maybe_close(new_udp_fd);
 					maybe_close(accepted_fd);
diff --git a/networking/nbd-client.c b/networking/nbd-client.c
index 3db3b46f9..755b42ccd 100644
--- a/networking/nbd-client.c
+++ b/networking/nbd-client.c
@@ -270,7 +270,7 @@ int nbdclient_main(int argc, char **argv)
 						close(fd);
 					break;
 				}
-				sleep(1);
+				sleep1();
 			}
 			open(device, O_RDONLY);
 			return 0;
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
index 88eda6b28..25b95246f 100644
--- a/networking/nc_bloaty.c
+++ b/networking/nc_bloaty.c
@@ -196,7 +196,7 @@ enum {
 /* Debug: squirt whatever message and sleep a bit so we can see it go by. */
 /* Beware: writes to stdOUT... */
 #if 0
-#define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush_all(); sleep(1); } while (0)
+#define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush_all(); sleep1(); } while (0)
 #else
 #define Debug(...) do { } while (0)
 #endif
diff --git a/networking/slattach.c b/networking/slattach.c
index 51fbc1f49..6d2a252fc 100644
--- a/networking/slattach.c
+++ b/networking/slattach.c
@@ -76,7 +76,7 @@ static void restore_state_and_exit(int exitcode)
 	cfsetispeed(&state, B0);
 	cfsetospeed(&state, B0);
 	exitcode |= tcsetattr_serial_or_warn(&state);
-	sleep(1);
+	sleep1();
 
 	/* Restore line status */
 	if (tcsetattr_serial_or_warn(&G.saved_state))
diff --git a/networking/telnet.c b/networking/telnet.c
index 9fc85050b..19a414b30 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -673,7 +673,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
 			if (bb_got_signal)
 				con_escape();
 			else
-				sleep(1);
+				sleep1();
 			continue;
 		}
 
diff --git a/runit/runsv.c b/runit/runsv.c
index 36d85101e..7e22862cd 100644
--- a/runit/runsv.c
+++ b/runit/runsv.c
@@ -651,7 +651,7 @@ int runsv_main(int argc UNUSED_PARAM, char **argv)
 				gettimeofday_ns(&svd[0].start);
 				update_status(&svd[0]);
 				if (LESS(svd[0].start.tv_sec, deadline))
-					sleep(1);
+					sleep1();
 			}
 			if (haslog) {
 				if (child == svd[1].pid) {
@@ -664,7 +664,7 @@ int runsv_main(int argc UNUSED_PARAM, char **argv)
 					gettimeofday_ns(&svd[1].start);
 					update_status(&svd[1]);
 					if (LESS(svd[1].start.tv_sec, deadline))
-						sleep(1);
+						sleep1();
 				}
 			}
 		} /* for (;;) */
diff --git a/sysklogd/logread.c b/sysklogd/logread.c
index 1e1f1347f..b52dc9cac 100644
--- a/sysklogd/logread.c
+++ b/sysklogd/logread.c
@@ -180,7 +180,7 @@ int logread_main(int argc UNUSED_PARAM, char **argv)
 			if (cur == shbuf_tail) {
 				sem_up(log_semid);
 				fflush_all();
-				sleep(1); /* TODO: replace me with a sleep_on */
+				sleep1(); /* TODO: replace me with a sleep_on */
 				continue;
 			}
 		}
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 0fb2e3e17..0df3ebf7d 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -2667,7 +2667,7 @@ reread_partition_table(int leave)
 	/* Users with slow external USB disks on a 320MHz ARM system (year 2011)
 	 * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
 	 */
-	sleep(1);
+	sleep1();
 	i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
 			"WARNING: rereading partition table "
 			"failed, kernel still uses old table");
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 9a8c14456..ebf83d1a3 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -920,7 +920,7 @@ static void load_firmware(const char *firmware, const char *sysfs_path)
 		loading_fd = open("loading", O_WRONLY);
 		if (loading_fd >= 0)
 			goto loading;
-		sleep(1);
+		sleep1();
 	}
 	goto out;
 
-- 
cgit v1.2.3