Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random_* - Better compatibility on Linux distros #1550

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions ext/standard/random.c
Expand Up @@ -31,7 +31,7 @@
# include "win32/winutil.h"
#endif
#ifdef __linux__
# include <linux/random.h>
# include <sys/syscall.h>
#endif

#ifdef ZTS
Expand Down Expand Up @@ -107,7 +107,7 @@ static int php_random_bytes(void *bytes, size_t size)
amount_to_read

*/
n = getrandom(bytes + read_bytes, amount_to_read, 0);
n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);

if (n == -1) {
if (errno == EINTR || errno == EAGAIN) {
Expand Down Expand Up @@ -140,7 +140,13 @@ static int php_random_bytes(void *bytes, size_t size)
return FAILURE;
}
/* Does the file exist and is it a character device? */
if (fstat(fd, &st) != 0 || !S_ISCHR(st.st_mode)) {
if (fstat(fd, &st) != 0 ||
# ifdef S_IFNAM
!(S_IFNAM(st.st_mode) || S_ISCHR(st.st_mode))
# else
!S_ISCHR(st.st_mode)
# endif
) {
close(fd);
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
return FAILURE;
Expand Down