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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #define POOL_SIZE_MIN 512
33 int main(int argc, char *argv[]) {
34 int seed_fd = -1, random_fd = -1;
35 int ret = EXIT_FAILURE;
42 log_error("This program requires one argument.");
46 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
47 log_parse_environment();
50 /* Read pool size, if possible */
51 if ((f = fopen("/proc/sys/kernel/random/poolsize", "re"))) {
52 fscanf(f, "%zu", &buf_size);
56 if (buf_size <= POOL_SIZE_MIN)
57 buf_size = POOL_SIZE_MIN;
59 if (!(buf = malloc(buf_size))) {
60 log_error("Failed to allocate buffer.");
64 if (mkdir_parents(RANDOM_SEED, 0755) < 0) {
65 log_error("Failed to create directories parents of %s: %m", RANDOM_SEED);
69 /* When we load the seed we read it and write it to the device
70 * and then immediately update the saved seed with new data,
71 * to make sure the next boot gets seeded differently. */
73 if (streq(argv[1], "load")) {
75 if ((seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
76 if ((seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
77 log_error("Failed to open random seed: %m");
82 if ((random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
83 if ((random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
84 log_error("Failed to open /dev/urandom: %m");
89 if ((r = loop_read(seed_fd, buf, buf_size, false)) <= 0) {
92 log_error("Failed to read seed file: %m");
94 lseek(seed_fd, 0, SEEK_SET);
96 if ((r = loop_write(random_fd, buf, (size_t) r, false)) <= 0)
97 log_error("Failed to write seed to /dev/random: %s", r < 0 ? strerror(errno) : "short write");
100 } else if (streq(argv[1], "save")) {
102 if ((seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
103 log_error("Failed to open random seed: %m");
107 if ((random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
108 log_error("Failed to open /dev/urandom: %m");
112 log_error("Unknown verb %s.", argv[1]);
116 /* This is just a safety measure. Given that we are root and
117 * most likely created the file ourselves the mode and owner
118 * should be correct anyway. */
119 fchmod(seed_fd, 0600);
120 fchown(seed_fd, 0, 0);
122 if ((r = loop_read(random_fd, buf, buf_size, false)) <= 0)
123 log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(errno) : "EOF");
125 if ((r = loop_write(seed_fd, buf, (size_t) r, false)) <= 0)
126 log_error("Failed to write new random seed file: %s", r < 0 ? strerror(errno) : "short write");
133 close_nointr_nofail(random_fd);
136 close_nointr_nofail(seed_fd);