]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/random-seed/random-seed.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "alloc-util.h"
32 #include "string-util.h"
35 #define POOL_SIZE_MIN 512
37 int main(int argc
, char *argv
[]) {
38 _cleanup_close_
int seed_fd
= -1, random_fd
= -1;
39 _cleanup_free_
void* buf
= NULL
;
44 bool refresh_seed_file
= true;
47 log_error("This program requires one argument.");
51 log_set_target(LOG_TARGET_AUTO
);
52 log_parse_environment();
57 /* Read pool size, if possible */
58 f
= fopen("/proc/sys/kernel/random/poolsize", "re");
60 if (fscanf(f
, "%zu", &buf_size
) > 0)
61 /* poolsize is in bits on 2.6, but we want bytes */
67 if (buf_size
<= POOL_SIZE_MIN
)
68 buf_size
= POOL_SIZE_MIN
;
70 buf
= malloc(buf_size
);
76 r
= mkdir_parents_label(RANDOM_SEED
, 0755);
78 log_error_errno(r
, "Failed to create directory " RANDOM_SEED_DIR
": %m");
82 /* When we load the seed we read it and write it to the device
83 * and then immediately update the saved seed with new data,
84 * to make sure the next boot gets seeded differently. */
86 if (streq(argv
[1], "load")) {
88 seed_fd
= open(RANDOM_SEED
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_CREAT
, 0600);
89 open_rw_error
= -errno
;
91 refresh_seed_file
= false;
93 seed_fd
= open(RANDOM_SEED
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
95 bool missing
= errno
== ENOENT
;
97 log_full_errno(missing
? LOG_DEBUG
: LOG_ERR
,
98 open_rw_error
, "Failed to open " RANDOM_SEED
" for writing: %m");
99 r
= log_full_errno(missing
? LOG_DEBUG
: LOG_ERR
,
100 errno
, "Failed to open " RANDOM_SEED
" for reading: %m");
108 random_fd
= open("/dev/urandom", O_RDWR
|O_CLOEXEC
|O_NOCTTY
, 0600);
110 random_fd
= open("/dev/urandom", O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600);
112 r
= log_error_errno(errno
, "Failed to open /dev/urandom: %m");
117 k
= loop_read(seed_fd
, buf
, buf_size
, false);
119 r
= log_error_errno(k
, "Failed to read seed from " RANDOM_SEED
": %m");
122 log_debug("Seed file " RANDOM_SEED
" not yet initialized, proceeding.");
124 (void) lseek(seed_fd
, 0, SEEK_SET
);
126 r
= loop_write(random_fd
, buf
, (size_t) k
, false);
128 log_error_errno(r
, "Failed to write seed to /dev/urandom: %m");
131 } else if (streq(argv
[1], "save")) {
133 seed_fd
= open(RANDOM_SEED
, O_WRONLY
|O_CLOEXEC
|O_NOCTTY
|O_CREAT
, 0600);
135 r
= log_error_errno(errno
, "Failed to open " RANDOM_SEED
": %m");
139 random_fd
= open("/dev/urandom", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
141 r
= log_error_errno(errno
, "Failed to open /dev/urandom: %m");
146 log_error("Unknown verb '%s'.", argv
[1]);
151 if (refresh_seed_file
) {
153 /* This is just a safety measure. Given that we are root and
154 * most likely created the file ourselves the mode and owner
155 * should be correct anyway. */
156 (void) fchmod(seed_fd
, 0600);
157 (void) fchown(seed_fd
, 0, 0);
159 k
= loop_read(random_fd
, buf
, buf_size
, false);
161 r
= log_error_errno(k
, "Failed to read new seed from /dev/urandom: %m");
165 log_error("Got EOF while reading from /dev/urandom.");
170 r
= loop_write(seed_fd
, buf
, (size_t) k
, false);
172 log_error_errno(r
, "Failed to write new random seed file: %m");
176 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;