wlroots/util/token.c
Simon Ser 842093bb84 Define _POSIX_C_SOURCE globally
Stop trying to maintain a per-file _POSIX_C_SOURCE. Instead,
require POSIX.1-2008 globally. A lot of core source files depend
on that already.

Some care must be taken on a few select files where we need a bit
more than POSIX. Some files need XSI extensions (_XOPEN_SOURCE) and
some files need BSD extensions (_DEFAULT_SOURCE). In both cases,
these feature test macros imply _POSIX_C_SOURCE. Make sure to not
define both these macros and _POSIX_C_SOURCE explicitly to avoid
POSIX requirement conflicts (e.g. _POSIX_C_SOURCE says POSIX.1-2001
but _XOPEN_SOURCE says POSIX.1-2008).

Additionally, there is one special case in render/vulkan/vulkan.c.
That file needs major()/minor(), and these are system-specific.
On FreeBSD, _POSIX_C_SOURCE hides system-specific symbols so we need
to make sure it's not defined for this file. On Linux, we can
explicitly include <sys/sysmacros.h> and ensure that apart from
symbols defined there the file only uses POSIX toys.
2024-02-15 15:41:12 +01:00

39 lines
945 B
C

#include "util/token.h"
#include "wlr/util/log.h"
#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
bool generate_token(char out[static TOKEN_SIZE]) {
static FILE *urandom = NULL;
uint64_t data[2];
if (!urandom) {
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
wlr_log_errno(WLR_ERROR, "Failed to open random device");
return false;
}
if (!(urandom = fdopen(fd, "r"))) {
wlr_log_errno(WLR_ERROR, "fdopen failed");
close(fd);
return false;
}
}
if (fread(data, sizeof(data), 1, urandom) != 1) {
wlr_log_errno(WLR_ERROR, "Failed to read from random device");
return false;
}
if (snprintf(out, TOKEN_SIZE, "%016" PRIx64 "%016" PRIx64, data[0], data[1]) != TOKEN_SIZE - 1) {
wlr_log_errno(WLR_ERROR, "Failed to format hex string token");
return false;
}
return true;
}