mirror of
https://github.com/hyprwm/wlroots-hyprland.git
synced 2024-11-02 11:55:59 +01:00
55be74ad86
TOKEN_STRLEN is not actually the strlen() of the token. It's the size taken by the token included the final zero byte. Change the name to make this clearer, and remove unnecessary +1's.
40 lines
977 B
C
40 lines
977 B
C
#define _POSIX_C_SOURCE 200809L
|
|
#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;
|
|
}
|
|
|