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