Bug 14328 - usleep() is obsolete, use nanosleep()
Summary: usleep() is obsolete, use nanosleep()
Alias: None
Product: rsync
Classification: Unclassified
Component: core (show other bugs)
Version: 3.1.3
Hardware: All All
: P5 normal (vote)
Target Milestone: ---
Assignee: Wayne Davison
QA Contact: Rsync QA Contact
Depends on:
Reported: 2020-03-27 15:47 UTC by roland
Modified: 2020-04-05 18:51 UTC (History)
0 users

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description roland 2020-03-27 15:47:07 UTC
by chance i came across 


and indeed


is telling:

4.3BSD, POSIX.1-2001.  POSIX.1-2001 declares this function obsolete;
       use nanosleep(2) instead.  POSIX.1-2008 removes the specification of

recent rsync 3.1.3 is still using usleep:

# grep -r usleep *
config.h.in:/* Define to 1 if you have the `usleep' function. */
configure.ac:    initgroups utimensat posix_fallocate attropen setvbuf usleep)
configure.sh:    initgroups utimensat posix_fallocate attropen setvbuf usleep
OLDNEWS:    - Use usleep() for our msleep() function if it is available.
util2.c:	usleep(t*1000);

for my curiosity, that went into rsync @2014, which is not too long ago:


http://man7.org/linux/man-pages/man2/nanosleep.2.html is telling:

    Compared to sleep(3) and usleep(3), nanosleep() has the following
       advantages: it provides a higher resolution for specifying the sleep
       interval; POSIX.1 explicitly specifies that it does not interact with
       signals; and it makes the task of resuming a sleep that has been
       interrupted by a signal handler easier.

also mind this one, as usleep return value is NOT being checked at the moment :


2, pay attention

Be sure to pay attention to the return value when using these functions. Sometimes the sleep function is interrupted by the system, and the result is not as expected.
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) {}

so - usleep is being used to define msleep() and that being used several times in rsync source

i'm not sure if this could be a source of bugs, but i remember there are some "rsync sits around doing nothing after larger transfers" and perhaps there is a relation to this....

# grep -r msleep *
cleanup.c:			msleep(100);
clientserver.c:		msleep(400);
io.c:			msleep(20);
main.c:		msleep(20);
main.c:			msleep(20);
OLDNEWS:    - Use usleep() for our msleep() function if it is available.
OLDNEWS:    - Fix an issue in the msleep() function if time jumps backwards.
options.c:	msleep(20);
proto.h:int msleep(int t);
rsync.c:	msleep(400);
util2.c:int msleep(int t)
Comment 1 Wayne Davison 2020-04-05 17:07:36 UTC
Thanks for the suggestion. I've made nanosleep() the preferred means of implementing msleep().
Comment 2 roland 2020-04-05 18:51:37 UTC
thanks, too!