From 9d609098cd7e23069bf14a443fbf5c4b257cfe56 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Fri, 24 Oct 2025 23:58:42 +1100 Subject: [PATCH] libspl: move random impl from libzpool Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Brian Behlendorf Signed-off-by: Rob Norris Closes #17861 --- lib/libspl/Makefile.am | 1 + lib/libspl/libspl.c | 4 ++ lib/libspl/libspl_impl.h | 7 ++++ lib/libspl/random.c | 84 ++++++++++++++++++++++++++++++++++++++++ lib/libzpool/kernel.c | 55 -------------------------- 5 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 lib/libspl/random.c diff --git a/lib/libspl/Makefile.am b/lib/libspl/Makefile.am index 3d46829ed..c075be60b 100644 --- a/lib/libspl/Makefile.am +++ b/lib/libspl/Makefile.am @@ -27,6 +27,7 @@ libspl_la_SOURCES = \ %D%/mutex.c \ %D%/page.c \ %D%/procfs_list.c \ + %D%/random.c \ %D%/rwlock.c \ %D%/strlcat.c \ %D%/strlcpy.c \ diff --git a/lib/libspl/libspl.c b/lib/libspl/libspl.c index 84e44ef51..63e948bc2 100644 --- a/lib/libspl/libspl.c +++ b/lib/libspl/libspl.c @@ -32,6 +32,7 @@ #include #include #include +#include "libspl_impl.h" uint64_t physmem; struct utsname hw_utsname; @@ -48,9 +49,12 @@ libspl_init(void) physmem = sysconf(_SC_PHYS_PAGES); VERIFY0(uname(&hw_utsname)); + + random_init(); } void libspl_fini(void) { + random_fini(); } diff --git a/lib/libspl/libspl_impl.h b/lib/libspl/libspl_impl.h index 39392da09..446801f25 100644 --- a/lib/libspl/libspl_impl.h +++ b/lib/libspl/libspl_impl.h @@ -21,5 +21,12 @@ * CDDL HEADER END */ +#ifndef _LIBSPL_IMPL_H +#define _LIBSPL_IMPL_H extern ssize_t getexecname_impl(char *execname); + +extern void random_init(void); +extern void random_fini(void); + +#endif diff --git a/lib/libspl/random.c b/lib/libspl/random.c new file mode 100644 index 000000000..039273790 --- /dev/null +++ b/lib/libspl/random.c @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: CDDL-1.0 +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or https://opensource.org/licenses/CDDL-1.0. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ +/* + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2018 by Delphix. All rights reserved. + * Copyright (c) 2016 Actifio, Inc. All rights reserved. + * Copyright (c) 2025, Klara, Inc. + */ + +#include +#include +#include +#include +#include "libspl_impl.h" + +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)); +} diff --git a/lib/libzpool/kernel.c b/lib/libzpool/kernel.c index 0d658be13..44a85f1ad 100644 --- a/lib/libzpool/kernel.c +++ b/lib/libzpool/kernel.c @@ -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(); }