Skip to content

Commit 9b6e490

Browse files
riptlripatel-fd
authored andcommitted
util: use clock_nanosleep on Linux
Enshrines a glibc behavior to always use clock_nanosleep(2) instead of nanosleep(2) when doing fd_log_sleep. This fixes potential sandbox incompatibilities with other libcs.
1 parent 52a46e4 commit 9b6e490

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/util/log/fd_log.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,14 @@ fd_log_sleep( long dt ) {
489489
struct timespec rem[1];
490490
req->tv_sec = (time_t)( ((ulong)ns_dt) / ((ulong)1e9) ); /* in [0,2^31-1] */
491491
req->tv_nsec = (long) ( ((ulong)ns_dt) % ((ulong)1e9) ); /* in [0,1e9) */
492-
if( FD_UNLIKELY( nanosleep( req, rem ) ) && FD_LIKELY( errno==EINTR ) ) dt += ((long)1e9)*((long)rem->tv_sec) + rem->tv_nsec;
492+
int sleep_res;
493+
#if defined(__linux__)
494+
/* Always use clock_nanosleep on Linux to be predictable */
495+
sleep_res = clock_nanosleep( CLOCK_REALTIME, 0, req, rem );
496+
#else
497+
sleep_res = nanosleep( req, rem );
498+
#endif
499+
if( FD_UNLIKELY( sleep_res ) && FD_LIKELY( errno==EINTR ) ) dt += ((long)1e9)*((long)rem->tv_sec) + rem->tv_nsec;
493500
return dt;
494501
}
495502

0 commit comments

Comments
 (0)