mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-03-22 08:51:30 +03:00
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:
parent
1911501c7d
commit
9d609098cd
@ -27,6 +27,7 @@ libspl_la_SOURCES = \
|
|||||||
%D%/mutex.c \
|
%D%/mutex.c \
|
||||||
%D%/page.c \
|
%D%/page.c \
|
||||||
%D%/procfs_list.c \
|
%D%/procfs_list.c \
|
||||||
|
%D%/random.c \
|
||||||
%D%/rwlock.c \
|
%D%/rwlock.c \
|
||||||
%D%/strlcat.c \
|
%D%/strlcat.c \
|
||||||
%D%/strlcpy.c \
|
%D%/strlcpy.c \
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/misc.h>
|
#include <sys/misc.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
#include "libspl_impl.h"
|
||||||
|
|
||||||
uint64_t physmem;
|
uint64_t physmem;
|
||||||
struct utsname hw_utsname;
|
struct utsname hw_utsname;
|
||||||
@ -48,9 +49,12 @@ libspl_init(void)
|
|||||||
physmem = sysconf(_SC_PHYS_PAGES);
|
physmem = sysconf(_SC_PHYS_PAGES);
|
||||||
|
|
||||||
VERIFY0(uname(&hw_utsname));
|
VERIFY0(uname(&hw_utsname));
|
||||||
|
|
||||||
|
random_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
libspl_fini(void)
|
libspl_fini(void)
|
||||||
{
|
{
|
||||||
|
random_fini();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,5 +21,12 @@
|
|||||||
* CDDL HEADER END
|
* CDDL HEADER END
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBSPL_IMPL_H
|
||||||
|
#define _LIBSPL_IMPL_H
|
||||||
|
|
||||||
extern ssize_t getexecname_impl(char *execname);
|
extern ssize_t getexecname_impl(char *execname);
|
||||||
|
|
||||||
|
extern void random_init(void);
|
||||||
|
extern void random_fini(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
84
lib/libspl/random.c
Normal file
84
lib/libspl/random.c
Normal file
@ -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 <stdint.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <sys/random.h>
|
||||||
|
#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));
|
||||||
|
}
|
||||||
@ -342,57 +342,6 @@ lowbit64(uint64_t i)
|
|||||||
return (__builtin_ffsll(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
|
int
|
||||||
ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
|
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;
|
hostid = (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0;
|
||||||
|
|
||||||
random_init();
|
|
||||||
|
|
||||||
system_taskq_init();
|
system_taskq_init();
|
||||||
icp_init();
|
icp_init();
|
||||||
|
|
||||||
@ -531,8 +478,6 @@ kernel_fini(void)
|
|||||||
icp_fini();
|
icp_fini();
|
||||||
system_taskq_fini();
|
system_taskq_fini();
|
||||||
|
|
||||||
random_fini();
|
|
||||||
|
|
||||||
libspl_fini();
|
libspl_fini();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user