Skip to content

Commit

Permalink
Applications should use the clock_gettime() function instead of the o…
Browse files Browse the repository at this point in the history
…bsolescent gettimeofday() function.
  • Loading branch information
michaelortmann committed Jul 4, 2024
1 parent 5c34bd6 commit 9563384
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ AX_TYPE_SOCKLEN_T
AX_CREATE_STDINT_H([eggint.h])

# Checks for functions and their arguments.
AC_CHECK_FUNCS([dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf])
AC_CHECK_FUNCS([clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf])
AC_FUNC_SELECT_ARGTYPES
EGG_FUNC_B64_NTOP
AC_FUNC_MMAP
Expand Down
10 changes: 8 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,17 +923,23 @@ static void mainloop(int toplevel)
static void init_random(void) {
unsigned int seed;
#ifdef HAVE_GETRANDOM
if (getrandom(&seed, sizeof(seed), 0) != sizeof(seed)) {
if (getrandom(&seed, sizeof seed, 0) != (sizeof seed)) {
if (errno != ENOSYS) {
fatal("ERROR: getrandom()\n", 0);
} else {
/* getrandom() is available in header but syscall is not!
* This can happen with glibc>=2.25 and linux<3.17
*/
#endif
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
seed = ((uint64_t) tp.tv_sec * tp.tv_nsec) ^ getpid();
#else
struct timeval tp;
gettimeofday(&tp, NULL);
seed = (((int64_t) tp.tv_sec * tp.tv_usec)) ^ getpid();
seed = ((uint64_t) tp.tv_sec * tp.tv_usec) ^ getpid();
#endif
#ifdef HAVE_GETRANDOM
}
}
Expand Down

0 comments on commit 9563384

Please sign in to comment.