policy is that all public functions which have a public declaration should be defined in a context where that public declaration is visible, to avoid preventable type mismatches. an audit performed using GCC's -Wmissing-declarations turned up the violations corrected here. in some cases the public header had not been included; in others, a feature test macro needed to make the declaration visible had been omitted. in the case of gethostent and getnetent, the omission seems to have been intentional, as a hack to admit a single stub definition for both functions. this kind of hack is no longer acceptable; it's UB and would not fly with LTO or advanced toolchains. the hack is undone to make exposure of the declarations possible.
34 lines
496 B
C
34 lines
496 B
C
#define _BSD_SOURCE
|
|
#include <unistd.h>
|
|
#include <sys/random.h>
|
|
#include <pthread.h>
|
|
#include <errno.h>
|
|
|
|
int getentropy(void *buffer, size_t len)
|
|
{
|
|
int cs, ret;
|
|
char *pos = buffer;
|
|
|
|
if (len > 256) {
|
|
errno = EIO;
|
|
return -1;
|
|
}
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
|
|
|
while (len) {
|
|
ret = getrandom(pos, len, 0);
|
|
if (ret < 0) {
|
|
if (errno == EINTR) continue;
|
|
else break;
|
|
}
|
|
pos += ret;
|
|
len -= ret;
|
|
ret = 0;
|
|
}
|
|
|
|
pthread_setcancelstate(cs, 0);
|
|
|
|
return ret;
|
|
}
|