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>
This commit is contained in:
Denys Vlasenko 2020-11-29 11:37:34 +01:00
parent 87bd558f3f
commit ec16c030bd
19 changed files with 33 additions and 29 deletions

View File

@ -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)) {

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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) */
}

View File

@ -28,3 +28,9 @@ void FAST_FUNC pause_after_failed_login(void)
sleep(LOGIN_FAIL_DELAY);
#endif
}
void FAST_FUNC sleep1(void)
{
sleep(1);
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -270,7 +270,7 @@ int nbdclient_main(int argc, char **argv)
close(fd);
break;
}
sleep(1);
sleep1();
}
open(device, O_RDONLY);
return 0;

View File

@ -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

View File

@ -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))

View File

@ -673,7 +673,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
if (bb_got_signal)
con_escape();
else
sleep(1);
sleep1();
continue;
}

View File

@ -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 (;;) */

View File

@ -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;
}
}

View File

@ -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");

View File

@ -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;