libspl: move random impl from libzpool

Sponsored-by: https://despairlabs.com/sponsor/
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes #17861
This commit is contained in:
Rob Norris
2025-10-24 23:58:42 +11:00
committed by Brian Behlendorf
parent 1911501c7d
commit 9d609098cd
5 changed files with 96 additions and 55 deletions
-55
View File
@@ -342,57 +342,6 @@ lowbit64(uint64_t i)
return (__builtin_ffsll(i));
}
const char *random_path = "/dev/random";
const char *urandom_path = "/dev/urandom";
static int random_fd = -1, urandom_fd = -1;
void
random_init(void)
{
VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
}
void
random_fini(void)
{
close(random_fd);
close(urandom_fd);
random_fd = -1;
urandom_fd = -1;
}
static int
random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
{
size_t resid = len;
ssize_t bytes;
ASSERT(fd != -1);
while (resid != 0) {
bytes = read(fd, ptr, resid);
ASSERT3S(bytes, >=, 0);
ptr += bytes;
resid -= bytes;
}
return (0);
}
int
random_get_bytes(uint8_t *ptr, size_t len)
{
return (random_get_bytes_common(ptr, len, random_fd));
}
int
random_get_pseudo_bytes(uint8_t *ptr, size_t len)
{
return (random_get_bytes_common(ptr, len, urandom_fd));
}
int
ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
{
@@ -505,8 +454,6 @@ kernel_init(int mode)
hostid = (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0;
random_init();
system_taskq_init();
icp_init();
@@ -531,8 +478,6 @@ kernel_fini(void)
icp_fini();
system_taskq_fini();
random_fini();
libspl_fini();
}