Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 8 | */ |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 10 | |
Denys Vlasenko | 6a55b4e | 2020-11-29 12:40:25 +0100 | [diff] [blame] | 11 | /* void FAST_FUNC bb_do_delay(unsigned seconds) { ... } - no users yet */ |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | 87bd558 | 2020-11-29 11:32:48 +0100 | [diff] [blame] | 13 | #ifndef LOGIN_FAIL_DELAY |
| 14 | #define LOGIN_FAIL_DELAY 3 |
| 15 | #endif |
| 16 | void 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 Vlasenko | 12450db | 2009-10-27 09:54:34 +0100 | [diff] [blame] | 23 | do { |
Denys Vlasenko | 87bd558 | 2020-11-29 11:32:48 +0100 | [diff] [blame] | 24 | sleep(diff); |
| 25 | diff = end - time(NULL); |
| 26 | } while (diff > 0); |
| 27 | #else |
| 28 | sleep(LOGIN_FAIL_DELAY); |
| 29 | #endif |
Rob Landley | 84cb767 | 2006-01-06 20:59:09 +0000 | [diff] [blame] | 30 | } |
Denys Vlasenko | ec16c03 | 2020-11-29 11:37:34 +0100 | [diff] [blame] | 31 | |
| 32 | void FAST_FUNC sleep1(void) |
| 33 | { |
| 34 | sleep(1); |
| 35 | } |
| 36 | |
Denys Vlasenko | 6a55b4e | 2020-11-29 12:40:25 +0100 | [diff] [blame] | 37 | void FAST_FUNC msleep(unsigned ms) |
| 38 | { |
Denys Vlasenko | 030fe31 | 2020-12-11 16:48:47 +0100 | [diff] [blame] | 39 | #if 0 |
Denys Vlasenko | 6a55b4e | 2020-11-29 12:40:25 +0100 | [diff] [blame] | 40 | /* 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 Vlasenko | 030fe31 | 2020-12-11 16:48:47 +0100 | [diff] [blame] | 50 | #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 Vlasenko | 6a55b4e | 2020-11-29 12:40:25 +0100 | [diff] [blame] | 67 | } |