diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-04-21 18:08:09 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-04-21 18:08:09 +0200 |
commit | 1509fb87c0269a76a76e0badb07156c98a053ba2 (patch) | |
tree | c4bf084655e24e5c703b6d0d63a7c649cd15b698 /src | |
parent | 10a8700606d832ba88706f35e63d37b98c409d10 (diff) |
random-seed: minor cleanups, in particular when it comes to logging
Diffstat (limited to 'src')
-rw-r--r-- | src/random-seed/random-seed.c | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/src/random-seed/random-seed.c b/src/random-seed/random-seed.c index ce1bd195d2..92176b1e9f 100644 --- a/src/random-seed/random-seed.c +++ b/src/random-seed/random-seed.c @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) { ssize_t k; int r; FILE *f; - bool cleanup_seed_file = true; + bool refresh_seed_file = true; if (argc != 2) { log_error("This program requires one argument."); @@ -87,33 +87,29 @@ int main(int argc, char *argv[]) { if (seed_fd < 0) { seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY); if (seed_fd < 0) { - log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m"); - r = -errno; + r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m"); goto finish; } - cleanup_seed_file = false; + + refresh_seed_file = false; } random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600); if (random_fd < 0) { random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600); if (random_fd < 0) { - log_error_errno(errno, "Failed to open /dev/urandom: %m"); - r = -errno; + r = log_error_errno(errno, "Failed to open /dev/urandom: %m"); goto finish; } } k = loop_read(seed_fd, buf, buf_size, false); - if (k <= 0) { - - if (r != 0) - log_error_errno(errno, "Failed to read seed from " RANDOM_SEED ": %m"); - - r = k == 0 ? -EIO : (int) k; - - } else { - lseek(seed_fd, 0, SEEK_SET); + if (k < 0) + r = log_error_errno(k, "Failed to read seed from " RANDOM_SEED ": %m"); + else if (k == 0) + log_debug("Seed file " RANDOM_SEED " not yet initialized, proceeding."); + else { + (void) lseek(seed_fd, 0, SEEK_SET); r = loop_write(random_fd, buf, (size_t) k, false); if (r < 0) @@ -124,40 +120,44 @@ int main(int argc, char *argv[]) { seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600); if (seed_fd < 0) { - log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m"); - r = -errno; + r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m"); goto finish; } random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY); if (random_fd < 0) { - log_error_errno(errno, "Failed to open /dev/urandom: %m"); - r = -errno; + r = log_error_errno(errno, "Failed to open /dev/urandom: %m"); goto finish; } } else { - log_error("Unknown verb %s.", argv[1]); + log_error("Unknown verb '%s'.", argv[1]); r = -EINVAL; goto finish; } - if (cleanup_seed_file) { + 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. */ - fchmod(seed_fd, 0600); - fchown(seed_fd, 0, 0); + (void) fchmod(seed_fd, 0600); + (void) fchown(seed_fd, 0, 0); k = loop_read(random_fd, buf, buf_size, false); - if (k <= 0) { - log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(-r) : "EOF"); - r = k == 0 ? -EIO : (int) k; - } else { - r = loop_write(seed_fd, buf, (size_t) k, false); - if (r < 0) - log_error_errno(r, "Failed to write new random seed file: %m"); + if (k < 0) { + r = log_error_errno(k, "Failed to read new seed from /dev/urandom: %m"); + goto finish; } + if (k == 0) { + log_error("Got EOF while reading from /dev/urandom."); + r = -EIO; + goto finish; + } + + r = loop_write(seed_fd, buf, (size_t) k, false); + if (r < 0) + log_error_errno(r, "Failed to write new random seed file: %m"); } finish: |