diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/time-util.c | 46 | ||||
-rw-r--r-- | src/random-seed/random-seed.c | 22 |
2 files changed, 34 insertions, 34 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 17352a7a96..3f3863910b 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -188,9 +188,11 @@ struct timeval *timeval_store(struct timeval *tv, usec_t u) { return tv; } -static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc) { +static char *format_timestamp_internal(char *buf, size_t l, usec_t t, + bool utc, bool us) { struct tm tm; time_t sec; + int k; assert(buf); assert(l > 0); @@ -201,48 +203,36 @@ static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc) sec = (time_t) (t / USEC_PER_SEC); localtime_or_gmtime_r(&sec, &tm, utc); - if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm) <= 0) + if (us) + k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm); + else + k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm); + + if (k <= 0) return NULL; + if (us) { + snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC)); + if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0) + return NULL; + } return buf; } char *format_timestamp(char *buf, size_t l, usec_t t) { - return format_timestamp_internal(buf, l, t, false); + return format_timestamp_internal(buf, l, t, false, false); } char *format_timestamp_utc(char *buf, size_t l, usec_t t) { - return format_timestamp_internal(buf, l, t, true); -} - -static char *format_timestamp_internal_us(char *buf, size_t l, usec_t t, bool utc) { - struct tm tm; - time_t sec; - - assert(buf); - assert(l > 0); - - if (t <= 0 || t == USEC_INFINITY) - return NULL; - - sec = (time_t) (t / USEC_PER_SEC); - localtime_or_gmtime_r(&sec, &tm, utc); - - if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0) - return NULL; - snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC)); - if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0) - return NULL; - - return buf; + return format_timestamp_internal(buf, l, t, true, false); } char *format_timestamp_us(char *buf, size_t l, usec_t t) { - return format_timestamp_internal_us(buf, l, t, false); + return format_timestamp_internal(buf, l, t, false, true); } char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) { - return format_timestamp_internal_us(buf, l, t, true); + return format_timestamp_internal(buf, l, t, true, true); } char *format_timestamp_relative(char *buf, size_t l, usec_t t) { diff --git a/src/random-seed/random-seed.c b/src/random-seed/random-seed.c index d857ade36a..0041258d41 100644 --- a/src/random-seed/random-seed.c +++ b/src/random-seed/random-seed.c @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) { _cleanup_free_ void* buf = NULL; size_t buf_size = 0; ssize_t k; - int r; + int r, open_rw_error; FILE *f; bool refresh_seed_file = true; @@ -87,14 +87,23 @@ int main(int argc, char *argv[]) { if (streq(argv[1], "load")) { seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600); + open_rw_error = -errno; if (seed_fd < 0) { + refresh_seed_file = false; + seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY); if (seed_fd < 0) { - r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m"); + bool missing = errno == ENOENT; + + log_full_errno(missing ? LOG_DEBUG : LOG_ERR, + open_rw_error, "Failed to open " RANDOM_SEED " for writing: %m"); + r = log_full_errno(missing ? LOG_DEBUG : LOG_ERR, + errno, "Failed to open " RANDOM_SEED " for reading: %m"); + if (missing) + r = 0; + goto finish; } - - refresh_seed_file = false; } random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600); @@ -109,9 +118,10 @@ int main(int argc, char *argv[]) { k = loop_read(seed_fd, buf, buf_size, false); if (k < 0) r = log_error_errno(k, "Failed to read seed from " RANDOM_SEED ": %m"); - else if (k == 0) + else if (k == 0) { + r = 0; log_debug("Seed file " RANDOM_SEED " not yet initialized, proceeding."); - else { + } else { (void) lseek(seed_fd, 0, SEEK_SET); r = loop_write(random_fd, buf, (size_t) k, false); |