]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/random-seed.c
core: switch all log targets to go directly to the journal, instead via syslog
[thirdparty/systemd.git] / src / random-seed.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27
28 #include "log.h"
29 #include "util.h"
30
31 #define POOL_SIZE_MIN 512
32
33 int main(int argc, char *argv[]) {
34 int seed_fd = -1, random_fd = -1;
35 int ret = EXIT_FAILURE;
36 void* buf;
37 size_t buf_size = 0;
38 ssize_t r;
39 FILE *f;
40
41 if (argc != 2) {
42 log_error("This program requires one argument.");
43 return EXIT_FAILURE;
44 }
45
46 log_set_target(LOG_TARGET_AUTO);
47 log_parse_environment();
48 log_open();
49
50 umask(0022);
51
52 /* Read pool size, if possible */
53 if ((f = fopen("/proc/sys/kernel/random/poolsize", "re"))) {
54 if (fscanf(f, "%zu", &buf_size) > 0) {
55 /* poolsize is in bits on 2.6, but we want bytes */
56 buf_size /= 8;
57 }
58
59 fclose(f);
60 }
61
62 if (buf_size <= POOL_SIZE_MIN)
63 buf_size = POOL_SIZE_MIN;
64
65 if (!(buf = malloc(buf_size))) {
66 log_error("Failed to allocate buffer.");
67 goto finish;
68 }
69
70 if (mkdir_parents(RANDOM_SEED, 0755) < 0) {
71 log_error("Failed to create directories parents of %s: %m", RANDOM_SEED);
72 goto finish;
73 }
74
75 /* When we load the seed we read it and write it to the device
76 * and then immediately update the saved seed with new data,
77 * to make sure the next boot gets seeded differently. */
78
79 if (streq(argv[1], "load")) {
80
81 if ((seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
82 if ((seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
83 log_error("Failed to open random seed: %m");
84 goto finish;
85 }
86 }
87
88 if ((random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
89 if ((random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
90 log_error("Failed to open /dev/urandom: %m");
91 goto finish;
92 }
93 }
94
95 if ((r = loop_read(seed_fd, buf, buf_size, false)) <= 0) {
96
97 if (r != 0)
98 log_error("Failed to read seed file: %m");
99 } else {
100 lseek(seed_fd, 0, SEEK_SET);
101
102 if ((r = loop_write(random_fd, buf, (size_t) r, false)) <= 0)
103 log_error("Failed to write seed to /dev/random: %s", r < 0 ? strerror(errno) : "short write");
104 }
105
106 } else if (streq(argv[1], "save")) {
107
108 if ((seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
109 log_error("Failed to open random seed: %m");
110 goto finish;
111 }
112
113 if ((random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
114 log_error("Failed to open /dev/urandom: %m");
115 goto finish;
116 }
117 } else {
118 log_error("Unknown verb %s.", argv[1]);
119 goto finish;
120 }
121
122 /* This is just a safety measure. Given that we are root and
123 * most likely created the file ourselves the mode and owner
124 * should be correct anyway. */
125 fchmod(seed_fd, 0600);
126 fchown(seed_fd, 0, 0);
127
128 if ((r = loop_read(random_fd, buf, buf_size, false)) <= 0)
129 log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(errno) : "EOF");
130 else {
131 if ((r = loop_write(seed_fd, buf, (size_t) r, false)) <= 0)
132 log_error("Failed to write new random seed file: %s", r < 0 ? strerror(errno) : "short write");
133 }
134
135 ret = EXIT_SUCCESS;
136
137 finish:
138 if (random_fd >= 0)
139 close_nointr_nofail(random_fd);
140
141 if (seed_fd >= 0)
142 close_nointr_nofail(seed_fd);
143
144 free(buf);
145
146 return ret;
147 }