Skip to content

Commit

Permalink
Use the clock_gettime() instead of obsolescent gettimeofday()
Browse files Browse the repository at this point in the history
Patch by: michaelortmann
  • Loading branch information
michaelortmann authored Jul 29, 2024
1 parent 951b6ba commit cd32a73
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 memset_s snprintf strlcpy vsnprintf])
AC_CHECK_FUNCS([clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton 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 @@ -919,17 +919,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 cd32a73

Please sign in to comment.