blob: 9a84fa24bfe6c019c6e5c6b188fce1fd6b39d10d [file] [log] [blame]
Rob Landley84cb7672006-01-06 20:59:09 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Busybox utility routines.
4 *
5 * Copyright (C) 2005 by Tito Ragusa <tito-wolit@tiscali.it>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Rob Landley84cb7672006-01-06 20:59:09 +00008 */
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00009#include "libbb.h"
Rob Landley84cb7672006-01-06 20:59:09 +000010
Denys Vlasenko6a55b4e2020-11-29 12:40:25 +010011/* void FAST_FUNC bb_do_delay(unsigned seconds) { ... } - no users yet */
Rob Landley84cb7672006-01-06 20:59:09 +000012
Denys Vlasenko87bd5582020-11-29 11:32:48 +010013#ifndef LOGIN_FAIL_DELAY
14#define LOGIN_FAIL_DELAY 3
15#endif
16void FAST_FUNC pause_after_failed_login(void)
17{
18#if 0 /* over-engineered madness */
19 time_t end, diff;
20
21 end = time(NULL) + LOGIN_FAIL_DELAY;
22 diff = LOGIN_FAIL_DELAY;
Denys Vlasenko12450db2009-10-27 09:54:34 +010023 do {
Denys Vlasenko87bd5582020-11-29 11:32:48 +010024 sleep(diff);
25 diff = end - time(NULL);
26 } while (diff > 0);
27#else
28 sleep(LOGIN_FAIL_DELAY);
29#endif
Rob Landley84cb7672006-01-06 20:59:09 +000030}
Denys Vlasenkoec16c032020-11-29 11:37:34 +010031
32void FAST_FUNC sleep1(void)
33{
34 sleep(1);
35}
36
Denys Vlasenko6a55b4e2020-11-29 12:40:25 +010037void FAST_FUNC msleep(unsigned ms)
38{
Denys Vlasenko030fe312020-12-11 16:48:47 +010039#if 0
Denys Vlasenko6a55b4e2020-11-29 12:40:25 +010040 /* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000
41 * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967
42 * (sleep of ~71.5 minutes)
43 * Let's play safe and loop:
44 */
45 while (ms > 500) {
46 usleep(500000);
47 ms -= 500;
48 }
49 usleep(ms * 1000);
Denys Vlasenko030fe312020-12-11 16:48:47 +010050#else
51//usleep is often implemented as a call to nanosleep.
52//Simply do the same to implement msleep.
53//it's marginally larger, but wakes your CPU less often:
54//function old new delta
55//msleep 45 52 +7
56 struct timespec ts;
57 ts.tv_sec = ms / 1000;
58 ts.tv_nsec = (ms % 1000) * 1000000;
59 /*
60 * If a signal has non-default handler, nanosleep returns early.
61 * Our version of msleep doesn't return early
62 * if interrupted by such signals:
63 */
64 while (nanosleep(&ts, &ts) != 0)
65 continue;
66#endif
Denys Vlasenko6a55b4e2020-11-29 12:40:25 +010067}