1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #define POOL_SIZE_MIN 512
34 int main(int argc, char *argv[]) {
35 _cleanup_close_ int seed_fd = -1, random_fd = -1;
36 _cleanup_free_ void* buf = NULL;
41 bool cleanup_seed_file = true;
44 log_error("This program requires one argument.");
48 log_set_target(LOG_TARGET_AUTO);
49 log_parse_environment();
54 /* Read pool size, if possible */
55 f = fopen("/proc/sys/kernel/random/poolsize", "re");
57 if (fscanf(f, "%zu", &buf_size) > 0) {
58 /* poolsize is in bits on 2.6, but we want bytes */
65 if (buf_size <= POOL_SIZE_MIN)
66 buf_size = POOL_SIZE_MIN;
68 buf = malloc(buf_size);
74 r = mkdir_parents_label(RANDOM_SEED, 0755);
76 log_error_errno(r, "Failed to create directory " RANDOM_SEED_DIR ": %m");
80 /* When we load the seed we read it and write it to the device
81 * and then immediately update the saved seed with new data,
82 * to make sure the next boot gets seeded differently. */
84 if (streq(argv[1], "load")) {
86 seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
88 seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY);
90 log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m");
94 cleanup_seed_file = false;
97 random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
99 random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600);
101 log_error_errno(errno, "Failed to open /dev/urandom: %m");
107 k = loop_read(seed_fd, buf, buf_size, false);
111 log_error_errno(errno, "Failed to read seed from " RANDOM_SEED ": %m");
113 r = k == 0 ? -EIO : (int) k;
116 lseek(seed_fd, 0, SEEK_SET);
118 r = loop_write(random_fd, buf, (size_t) k, false);
120 log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
123 } else if (streq(argv[1], "save")) {
125 seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
127 log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m");
132 random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
134 log_error_errno(errno, "Failed to open /dev/urandom: %m");
140 log_error("Unknown verb %s.", argv[1]);
145 if (cleanup_seed_file) {
146 /* This is just a safety measure. Given that we are root and
147 * most likely created the file ourselves the mode and owner
148 * should be correct anyway. */
149 fchmod(seed_fd, 0600);
150 fchown(seed_fd, 0, 0);
152 k = loop_read(random_fd, buf, buf_size, false);
154 log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(-r) : "EOF");
155 r = k == 0 ? -EIO : (int) k;
157 r = loop_write(seed_fd, buf, (size_t) k, false);
159 log_error_errno(r, "Failed to write new random seed file: %m");
164 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;