Skip to content

Commit 9d60909

Browse files
robnbehlendorf
authored andcommitted
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
1 parent 1911501 commit 9d60909

File tree

5 files changed

+96
-55
lines changed

5 files changed

+96
-55
lines changed

lib/libspl/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ libspl_la_SOURCES = \
2727
%D%/mutex.c \
2828
%D%/page.c \
2929
%D%/procfs_list.c \
30+
%D%/random.c \
3031
%D%/rwlock.c \
3132
%D%/strlcat.c \
3233
%D%/strlcpy.c \

lib/libspl/libspl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <unistd.h>
3333
#include <sys/misc.h>
3434
#include <sys/utsname.h>
35+
#include "libspl_impl.h"
3536

3637
uint64_t physmem;
3738
struct utsname hw_utsname;
@@ -48,9 +49,12 @@ libspl_init(void)
4849
physmem = sysconf(_SC_PHYS_PAGES);
4950

5051
VERIFY0(uname(&hw_utsname));
52+
53+
random_init();
5154
}
5255

5356
void
5457
libspl_fini(void)
5558
{
59+
random_fini();
5660
}

lib/libspl/libspl_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@
2121
* CDDL HEADER END
2222
*/
2323

24+
#ifndef _LIBSPL_IMPL_H
25+
#define _LIBSPL_IMPL_H
2426

2527
extern ssize_t getexecname_impl(char *execname);
28+
29+
extern void random_init(void);
30+
extern void random_fini(void);
31+
32+
#endif

lib/libspl/random.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-License-Identifier: CDDL-1.0
2+
/*
3+
* CDDL HEADER START
4+
*
5+
* The contents of this file are subject to the terms of the
6+
* Common Development and Distribution License (the "License").
7+
* You may not use this file except in compliance with the License.
8+
*
9+
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
* or https://opensource.org/licenses/CDDL-1.0.
11+
* See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*
14+
* When distributing Covered Code, include this CDDL HEADER in each
15+
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
* If applicable, add the following below this CDDL HEADER, with the
17+
* fields enclosed by brackets "[]" replaced with your own identifying
18+
* information: Portions Copyright [yyyy] [name of copyright owner]
19+
*
20+
* CDDL HEADER END
21+
*/
22+
/*
23+
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24+
* Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25+
* Copyright (c) 2016 Actifio, Inc. All rights reserved.
26+
* Copyright (c) 2025, Klara, Inc.
27+
*/
28+
29+
#include <stdint.h>
30+
#include <fcntl.h>
31+
#include <assert.h>
32+
#include <sys/random.h>
33+
#include "libspl_impl.h"
34+
35+
const char *random_path = "/dev/random";
36+
const char *urandom_path = "/dev/urandom";
37+
static int random_fd = -1, urandom_fd = -1;
38+
39+
void
40+
random_init(void)
41+
{
42+
VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
43+
VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
44+
}
45+
46+
void
47+
random_fini(void)
48+
{
49+
close(random_fd);
50+
close(urandom_fd);
51+
52+
random_fd = -1;
53+
urandom_fd = -1;
54+
}
55+
56+
static int
57+
random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
58+
{
59+
size_t resid = len;
60+
ssize_t bytes;
61+
62+
ASSERT(fd != -1);
63+
64+
while (resid != 0) {
65+
bytes = read(fd, ptr, resid);
66+
ASSERT3S(bytes, >=, 0);
67+
ptr += bytes;
68+
resid -= bytes;
69+
}
70+
71+
return (0);
72+
}
73+
74+
int
75+
random_get_bytes(uint8_t *ptr, size_t len)
76+
{
77+
return (random_get_bytes_common(ptr, len, random_fd));
78+
}
79+
80+
int
81+
random_get_pseudo_bytes(uint8_t *ptr, size_t len)
82+
{
83+
return (random_get_bytes_common(ptr, len, urandom_fd));
84+
}

lib/libzpool/kernel.c

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -342,57 +342,6 @@ lowbit64(uint64_t i)
342342
return (__builtin_ffsll(i));
343343
}
344344

345-
const char *random_path = "/dev/random";
346-
const char *urandom_path = "/dev/urandom";
347-
static int random_fd = -1, urandom_fd = -1;
348-
349-
void
350-
random_init(void)
351-
{
352-
VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
353-
VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
354-
}
355-
356-
void
357-
random_fini(void)
358-
{
359-
close(random_fd);
360-
close(urandom_fd);
361-
362-
random_fd = -1;
363-
urandom_fd = -1;
364-
}
365-
366-
static int
367-
random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
368-
{
369-
size_t resid = len;
370-
ssize_t bytes;
371-
372-
ASSERT(fd != -1);
373-
374-
while (resid != 0) {
375-
bytes = read(fd, ptr, resid);
376-
ASSERT3S(bytes, >=, 0);
377-
ptr += bytes;
378-
resid -= bytes;
379-
}
380-
381-
return (0);
382-
}
383-
384-
int
385-
random_get_bytes(uint8_t *ptr, size_t len)
386-
{
387-
return (random_get_bytes_common(ptr, len, random_fd));
388-
}
389-
390-
int
391-
random_get_pseudo_bytes(uint8_t *ptr, size_t len)
392-
{
393-
return (random_get_bytes_common(ptr, len, urandom_fd));
394-
}
395-
396345
int
397346
ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
398347
{
@@ -505,8 +454,6 @@ kernel_init(int mode)
505454

506455
hostid = (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0;
507456

508-
random_init();
509-
510457
system_taskq_init();
511458
icp_init();
512459

@@ -531,8 +478,6 @@ kernel_fini(void)
531478
icp_fini();
532479
system_taskq_fini();
533480

534-
random_fini();
535-
536481
libspl_fini();
537482
}
538483

0 commit comments

Comments
 (0)