From: Timo Sirainen Date: Fri, 24 Jan 2020 21:33:05 +0000 (+0200) Subject: global: Replace most gettimeofday() calls with i_gettimeofday() X-Git-Tag: 2.3.10~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c669953774f9fa3be321d9db2c8c4939e07925c9;p=thirdparty%2Fdovecot%2Fcore.git global: Replace most gettimeofday() calls with i_gettimeofday() Replace the ones where error handling is: * Nonexistent * i_fatal() * i_panic() --- diff --git a/src/lib-dns/dns-lookup.c b/src/lib-dns/dns-lookup.c index 91bbb988d1..bef006c5e6 100644 --- a/src/lib-dns/dns-lookup.c +++ b/src/lib-dns/dns-lookup.c @@ -151,8 +151,7 @@ static void dns_lookup_save_msecs(struct dns_lookup *lookup) struct timeval now; int diff; - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&now); diff = timeval_diff_msecs(&now, &lookup->start_time); if (diff > 0) @@ -375,8 +374,7 @@ dns_client_lookup_common(struct dns_client *client, lookup = p_new(pool, struct dns_lookup, 1); lookup->pool = pool; - if (gettimeofday(&lookup->start_time, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&lookup->start_time); lookup->client = client; lookup->callback = callback; diff --git a/src/lib-fs/fs-api.c b/src/lib-fs/fs-api.c index 6d3c5fec08..8788089dff 100644 --- a/src/lib-fs/fs-api.c +++ b/src/lib-fs/fs-api.c @@ -446,10 +446,8 @@ static void fs_file_timing_start(struct fs_file *file, enum fs_op op) { if (!file->fs->set.enable_timing) return; - if (file->timing_start[op].tv_sec == 0) { - if (gettimeofday(&file->timing_start[op], NULL) < 0) - i_fatal("gettimeofday() failed: %m"); - } + if (file->timing_start[op].tv_sec == 0) + i_gettimeofday(&file->timing_start[op]); } static void @@ -458,8 +456,7 @@ fs_timing_end(struct stats_dist **timing, const struct timeval *start_tv) struct timeval now; long long diff; - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&now); diff = timeval_diff_usecs(&now, start_tv); if (diff > 0) { @@ -1197,10 +1194,8 @@ fs_iter_init_with_event(struct fs *fs, struct event *event, (fs_get_properties(fs) & FS_PROPERTY_OBJECTIDS) != 0); fs->stats.iter_count++; - if (fs->set.enable_timing) { - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); - } + if (fs->set.enable_timing) + i_gettimeofday(&now); if (fs->v.iter_init == NULL) { iter = i_new(struct fs_iter, 1); iter->fs = fs; diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index c5aacf384a..4199e2bdda 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -11,6 +11,7 @@ #include "write-full.h" #include "file-lock.h" #include "file-dotlock.h" +#include "time-util.h" #include "fs-api-private.h" #include @@ -539,8 +540,7 @@ static int fs_posix_write_finish(struct posix_fs_file *file) If requested, use utimes() to explicitly set a more accurate mtime. */ struct timeval tv[2]; - if (gettimeofday(&tv[0], NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&tv[0]); tv[1] = tv[0]; if ((utimes(file->temp_path, tv)) < 0) { fs_set_error_errno(file->file.event, diff --git a/src/lib-sql/driver-cassandra.c b/src/lib-sql/driver-cassandra.c index 4d691e3aaa..f636da60e7 100644 --- a/src/lib-sql/driver-cassandra.c +++ b/src/lib-sql/driver-cassandra.c @@ -953,8 +953,7 @@ static void driver_cassandra_log_result(struct cassandra_result *result, struct timeval now; unsigned int row_count; - if (gettimeofday(&now, NULL) < 0) - i_fatal("cassandra: gettimeofday() failed: %m"); + i_gettimeofday(&now); string_t *str = t_str_new(128); str_printfa(str, "Finished %squery '%s' (", diff --git a/src/lib-storage/index/index-search.c b/src/lib-storage/index/index-search.c index b4e29c5b2a..7d4b90b4eb 100644 --- a/src/lib-storage/index/index-search.c +++ b/src/lib-storage/index/index-search.c @@ -1288,8 +1288,7 @@ index_storage_search_init(struct mailbox_transaction_context *t, if (ctx->mail_ctx.max_mails == 0) ctx->mail_ctx.max_mails = UINT_MAX; ctx->next_time_check_cost = SEARCH_INITIAL_MAX_COST; - if (gettimeofday(&ctx->last_nonblock_timeval, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&ctx->last_nonblock_timeval); mailbox_get_open_status(t->box, STATUS_MESSAGES, &status); ctx->mail_ctx.progress_max = status.messages; @@ -1553,8 +1552,7 @@ static bool search_would_block(struct index_search_context *ctx) if (ctx->cost < ctx->next_time_check_cost) return FALSE; - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&now); usecs = timeval_diff_usecs(&now, &ctx->last_nonblock_timeval); if (usecs < 0) { diff --git a/src/lib/failures.c b/src/lib/failures.c index b8d6a0c407..9ce004938a 100644 --- a/src/lib/failures.c +++ b/src/lib/failures.c @@ -10,6 +10,7 @@ #include "backtrace-string.h" #include "printf-format-fix.h" #include "write-full.h" +#include "time-util.h" #include "failures-private.h" #include @@ -320,8 +321,7 @@ static void log_timestamp_add(const struct failure_context *ctx, string_t *str) if (log_stamp_format != NULL) { if (tm == NULL) { - if (gettimeofday(&now, NULL) < 0) - i_panic("gettimeofday() failed: %m"); + i_gettimeofday(&now); tm = localtime(&now.tv_sec); } else { now.tv_usec = ctx->timestamp_usecs; diff --git a/src/lib/file-lock.c b/src/lib/file-lock.c index a34ca5f066..84d9897d5c 100644 --- a/src/lib/file-lock.c +++ b/src/lib/file-lock.c @@ -324,8 +324,7 @@ int file_wait_lock_error(int fd, const char *path, int lock_type, lock->path = i_strdup(path); lock->lock_type = lock_type; lock->lock_method = lock_method; - if (gettimeofday(&lock->locked_time, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&lock->locked_time); *lock_r = lock; return 1; } @@ -363,8 +362,7 @@ struct file_lock *file_lock_from_dotlock(struct dotlock **dotlock) lock->path = i_strdup(file_dotlock_get_lock_path(*dotlock)); lock->lock_type = F_WRLCK; lock->lock_method = FILE_LOCK_METHOD_DOTLOCK; - if (gettimeofday(&lock->locked_time, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&lock->locked_time); lock->dotlock = *dotlock; *dotlock = NULL; @@ -467,8 +465,7 @@ void file_lock_wait_start(void) { i_assert(lock_wait_start.tv_sec == 0); - if (gettimeofday(&lock_wait_start, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&lock_wait_start); } static void file_lock_wait_init_warning(void) @@ -505,8 +502,7 @@ static void file_lock_log_warning_if_slow(struct file_lock *lock) } struct timeval now; - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&now); int diff = timeval_diff_msecs(&now, &lock->locked_time); if (diff > file_lock_slow_warning_usecs/1000) { @@ -521,8 +517,7 @@ void file_lock_wait_end(const char *lock_name) i_assert(lock_wait_start.tv_sec != 0); - if (gettimeofday(&now, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&now); long long diff = timeval_diff_usecs(&now, &lock_wait_start); if (diff < 0) { /* time moved backwards */ diff --git a/src/lib/ioloop-notify-inotify.c b/src/lib/ioloop-notify-inotify.c index 152b28a896..796ac31f9c 100644 --- a/src/lib/ioloop-notify-inotify.c +++ b/src/lib/ioloop-notify-inotify.c @@ -10,6 +10,7 @@ #include "buffer.h" #include "net.h" #include "ipwd.h" +#include "time-util.h" #include #include @@ -50,8 +51,7 @@ static bool inotify_input_more(struct ioloop *ioloop) i_fatal("read(inotify) failed: %m"); } - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&ioloop_timeval); ioloop_time = ioloop_timeval.tv_sec; for (pos = 0; pos < ret; ) { diff --git a/src/lib/ioloop-notify-kqueue.c b/src/lib/ioloop-notify-kqueue.c index ab86291a31..9831f0549c 100644 --- a/src/lib/ioloop-notify-kqueue.c +++ b/src/lib/ioloop-notify-kqueue.c @@ -67,8 +67,7 @@ static void event_callback(struct ioloop_notify_handler_context *ctx) i_fatal("kevent(notify) failed: %m"); } - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&ioloop_timeval); ioloop_time = ioloop_timeval.tv_sec; for (i = 0; i < ret; i++) { diff --git a/src/lib/ioloop.c b/src/lib/ioloop.c index f72690d9e2..84e415e532 100644 --- a/src/lib/ioloop.c +++ b/src/lib/ioloop.c @@ -227,11 +227,10 @@ void io_set_never_wait_alone(struct io *io, bool set) static void timeout_update_next(struct timeout *timeout, struct timeval *tv_now) { - if (tv_now == NULL) { - if (gettimeofday(&timeout->next_run, NULL) < 0) - i_fatal("gettimeofday(): %m"); - } else { - timeout->next_run.tv_sec = tv_now->tv_sec; + if (tv_now == NULL) + i_gettimeofday(&timeout->next_run); + else { + timeout->next_run.tv_sec = tv_now->tv_sec; timeout->next_run.tv_usec = tv_now->tv_usec; } @@ -449,10 +448,8 @@ static int timeout_get_wait_time(struct timeout *timeout, struct timeval *tv_r, { int ret; - if (tv_now->tv_sec == 0) { - if (gettimeofday(tv_now, NULL) < 0) - i_fatal("gettimeofday(): %m"); - } + if (tv_now->tv_sec == 0) + i_gettimeofday(tv_now); tv_r->tv_sec = tv_now->tv_sec; tv_r->tv_usec = tv_now->tv_usec; @@ -504,8 +501,7 @@ static int io_loop_get_wait_time(struct ioloop *ioloop, struct timeval *tv_r) } if (ioloop->io_pending_count > 0) { - if (gettimeofday(&tv_now, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&tv_now); msecs = 0; tv_r->tv_sec = 0; tv_r->tv_usec = 0; @@ -633,8 +629,7 @@ static void io_loop_handle_timeouts_real(struct ioloop *ioloop) data_stack_frame_t t_id; tv_old = ioloop_timeval; - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&ioloop_timeval); diff_usecs = timeval_diff_usecs(&ioloop_timeval, &tv_old); if (unlikely(diff_usecs < 0)) { @@ -643,8 +638,7 @@ static void io_loop_handle_timeouts_real(struct ioloop *ioloop) ioloop->time_moved_callback(&tv_old, &ioloop_timeval); i_assert(ioloop == current_ioloop); /* the callback may have slept, so check the time again. */ - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&ioloop_timeval); } else { diff_usecs = timeval_diff_usecs(&ioloop->next_max_time, &ioloop_timeval); @@ -803,8 +797,7 @@ bool io_loop_is_running(struct ioloop *ioloop) void io_loop_time_refresh(void) { - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&ioloop_timeval); ioloop_time = ioloop_timeval.tv_sec; } @@ -818,8 +811,7 @@ struct ioloop *io_loop_create(void) } /* initialize time */ - if (gettimeofday(&ioloop_timeval, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&ioloop_timeval); ioloop_time = ioloop_timeval.tv_sec; ioloop = i_new(struct ioloop, 1); diff --git a/src/lib/lib-event.c b/src/lib/lib-event.c index 61f1d00083..1950f5698f 100644 --- a/src/lib/lib-event.c +++ b/src/lib/lib-event.c @@ -364,8 +364,7 @@ event_create_internal(struct event *parent, const char *source_filename, event->pool = pool; event->tv_created_ioloop = ioloop_timeval; event->min_log_level = LOG_TYPE_INFO; - if (gettimeofday(&event->tv_created, NULL) < 0) - i_panic("gettimeofday() failed: %m"); + i_gettimeofday(&event->tv_created); event->source_filename = p_strdup(pool, source_filename); event->source_linenum = source_linenum; if (parent != NULL) { @@ -963,8 +962,7 @@ void event_send(struct event *event, struct failure_context *ctx, void event_vsend(struct event *event, struct failure_context *ctx, const char *fmt, va_list args) { - if (gettimeofday(&event->tv_last_sent, NULL) < 0) - i_panic("gettimeofday() failed: %m"); + i_gettimeofday(&event->tv_last_sent); if (event_call_callbacks(event, EVENT_CALLBACK_TYPE_SEND, ctx, fmt, args)) { if (ctx->type != LOG_TYPE_DEBUG || diff --git a/src/lib/net.c b/src/lib/net.c index 43735b3287..d94f9a5986 100644 --- a/src/lib/net.c +++ b/src/lib/net.c @@ -318,8 +318,7 @@ int net_connect_unix_with_retries(const char *path, unsigned int msecs) struct timeval start, now; int fd; - if (gettimeofday(&start, NULL) < 0) - i_panic("gettimeofday() failed: %m"); + i_gettimeofday(&start); do { fd = net_connect_unix(path); @@ -328,8 +327,7 @@ int net_connect_unix_with_retries(const char *path, unsigned int msecs) /* busy. wait for a while. */ usleep(i_rand_minmax(1, 10) * 10000); - if (gettimeofday(&now, NULL) < 0) - i_panic("gettimeofday() failed: %m"); + i_gettimeofday(&now); } while (timeval_diff_msecs(&now, &start) < (int)msecs); return fd; } diff --git a/src/lib/test-ioloop.c b/src/lib/test-ioloop.c index 703efa22c6..792d15a30a 100644 --- a/src/lib/test-ioloop.c +++ b/src/lib/test-ioloop.c @@ -16,8 +16,7 @@ struct test_ctx { static void timeout_callback(struct timeval *tv) { - if (gettimeofday(tv, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(tv); io_loop_stop(current_ioloop); } @@ -116,8 +115,7 @@ static void test_ioloop_timeout(void) /* add the timeout we're actually testing below */ to = timeout_add(1000, timeout_callback, &tv_callback); - if (gettimeofday(&tv_start, NULL) < 0) - i_fatal("gettimeofday() failed: %m"); + i_gettimeofday(&tv_start); io_loop_run(ioloop); test_assert(timeval_diff_msecs(&tv_callback, &tv_start) >= 500); timeout_remove(&to); diff --git a/src/login-common/client-common.c b/src/login-common/client-common.c index 611535cde8..8b0faa087c 100644 --- a/src/login-common/client-common.c +++ b/src/login-common/client-common.c @@ -17,6 +17,7 @@ #include "base64.h" #include "str-sanitize.h" #include "safe-memset.h" +#include "time-util.h" #include "var-expand.h" #include "master-interface.h" #include "master-service.h" @@ -678,8 +679,7 @@ const char *client_get_session_id(struct client *client) buf = t_buffer_create(24); base64_buf = t_buffer_create(24*2); - if (gettimeofday(&tv, NULL) < 0) - i_fatal("gettimeofday(): %m"); + i_gettimeofday(&tv); timestamp = tv.tv_usec + (long long)tv.tv_sec * 1000ULL*1000ULL; /* add lowest 48 bits of the timestamp. this gives us a bit less than diff --git a/src/plugins/fts-squat/squat-test.c b/src/plugins/fts-squat/squat-test.c index e2c5a9c355..29b0fc4665 100644 --- a/src/plugins/fts-squat/squat-test.c +++ b/src/plugins/fts-squat/squat-test.c @@ -60,7 +60,7 @@ int main(int argc ATTR_UNUSED, char *argv[]) FILE_LOCK_METHOD_FCNTL, 0, 0600, (gid_t)-1); clock_start = clock(); - gettimeofday(&tv_start, NULL); + i_gettimeofday(&tv_start); fd = open(argv[1], O_RDONLY); if (fd == -1) @@ -138,7 +138,7 @@ int main(int argc ATTR_UNUSED, char *argv[]) } clock_end = clock(); - (void)gettimeofday(&tv_end, NULL); + i_gettimeofday(&tv_end); cputime = (double)(clock_end - clock_start) / CLOCKS_PER_SEC; fprintf(stderr, "\n - Index time: %.2f CPU seconds, " @@ -177,14 +177,14 @@ int main(int argc ATTR_UNUSED, char *argv[]) ret = strlen(str)-1; str[ret] = 0; - gettimeofday(&tv_start, NULL); + i_gettimeofday(&tv_start); ret = squat_trie_lookup(trie, str, SQUAT_INDEX_TYPE_HEADER | SQUAT_INDEX_TYPE_BODY, &definite_uids, &maybe_uids); if (ret < 0) printf("error\n"); else { - gettimeofday(&tv_end, NULL); + i_gettimeofday(&tv_end); printf(" - Search took %.05f CPU seconds\n", timeval_diff_usecs(&tv_end, &tv_start)/1000000.0); printf(" - definite uids: "); diff --git a/src/plugins/old-stats/mail-stats-fill.c b/src/plugins/old-stats/mail-stats-fill.c index 6477190299..10d8c398f9 100644 --- a/src/plugins/old-stats/mail-stats-fill.c +++ b/src/plugins/old-stats/mail-stats-fill.c @@ -140,7 +140,7 @@ void mail_stats_fill(struct stats_user *suser, struct mail_stats *stats_r) stats_r->invol_cs = usage.ru_nivcsw; stats_r->disk_input = (unsigned long long)usage.ru_inblock * 512ULL; stats_r->disk_output = (unsigned long long)usage.ru_oublock * 512ULL; - (void)gettimeofday(&stats_r->clock_time, NULL); + i_gettimeofday(&stats_r->clock_time); process_read_io_stats(stats_r); user_trans_stats_get(suser, stats_r); }