random-seed.c revision c004493cdefc1f43a3956ca529e8070f8d70be56
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "fd-util.h"
#include "io-util.h"
#include "log.h"
#include "mkdir.h"
#include "string-util.h"
#include "util.h"
#define POOL_SIZE_MIN 512
ssize_t k;
int r;
FILE *f;
bool refresh_seed_file = true;
if (argc != 2) {
log_error("This program requires one argument.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
/* Read pool size, if possible */
if (f) {
/* poolsize is in bits on 2.6, but we want bytes */
buf_size /= 8;
fclose(f);
}
if (buf_size <= POOL_SIZE_MIN)
if (!buf) {
r = log_oom();
goto finish;
}
if (r < 0) {
goto finish;
}
/* When we load the seed we read it and write it to the device
* and then immediately update the saved seed with new data,
* to make sure the next boot gets seeded differently. */
if (seed_fd < 0) {
if (seed_fd < 0) {
goto finish;
}
refresh_seed_file = false;
}
if (random_fd < 0) {
if (random_fd < 0) {
goto finish;
}
}
if (k < 0)
else if (k == 0)
else {
if (r < 0)
log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
}
if (seed_fd < 0) {
goto finish;
}
if (random_fd < 0) {
goto finish;
}
} else {
r = -EINVAL;
goto finish;
}
if (refresh_seed_file) {
/* This is just a safety measure. Given that we are root and
* most likely created the file ourselves the mode and owner
* should be correct anyway. */
if (k < 0) {
r = log_error_errno(k, "Failed to read new seed from /dev/urandom: %m");
goto finish;
}
if (k == 0) {
r = -EIO;
goto finish;
}
if (r < 0)
log_error_errno(r, "Failed to write new random seed file: %m");
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}