]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
global: Replace most gettimeofday() calls with i_gettimeofday()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 24 Jan 2020 21:33:05 +0000 (23:33 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Thu, 6 Feb 2020 06:46:01 +0000 (06:46 +0000)
Replace the ones where error handling is:
 * Nonexistent
 * i_fatal()
 * i_panic()

16 files changed:
src/lib-dns/dns-lookup.c
src/lib-fs/fs-api.c
src/lib-fs/fs-posix.c
src/lib-sql/driver-cassandra.c
src/lib-storage/index/index-search.c
src/lib/failures.c
src/lib/file-lock.c
src/lib/ioloop-notify-inotify.c
src/lib/ioloop-notify-kqueue.c
src/lib/ioloop.c
src/lib/lib-event.c
src/lib/net.c
src/lib/test-ioloop.c
src/login-common/client-common.c
src/plugins/fts-squat/squat-test.c
src/plugins/old-stats/mail-stats-fill.c

index 91bbb988d1ffc4638a08bc2b4865134b8b49e194..bef006c5e693c64404469c254a069af44560ae19 100644 (file)
@@ -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;
index 6d3c5fec087182d6cca0a4868e2792bb2b38c81a..8788089dffeadad6f369bc04e4cacd51cc018905 100644 (file)
@@ -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;
index c5aacf384ab6f93047dc4648e2b47bc0d73f0d86..4199e2bdda0f07ea8802e126f924e382fabe6cb0 100644 (file)
@@ -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 <stdio.h>
@@ -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,
index 4d691e3aaa6e6b0ae54bcf47626be5b75df38db0..f636da60e7cf9806d93a520c5e1b6bcc7a78c793 100644 (file)
@@ -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' (",
index b4e29c5b2af6698eafd79a87bbd0066cd4d46b50..7d4b90b4ebcb8d0d0d893758ab20ff30ed41a77a 100644 (file)
@@ -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) {
index b8d6a0c40780eccd7fd59cd526c41ead6458f1ed..9ce004938a0581e393d9295e555a830167423b00 100644 (file)
@@ -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 <unistd.h>
@@ -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;
index a34ca5f066f2dd6ada2cebae104ed51b57290388..84d9897d5c0b7e4c50fe58a75cb7f04286038b32 100644 (file)
@@ -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 */
index 152b28a8960cf73765037c4cc434f957fff9dc54..796ac31f9c862dd0a2e44e60308e9542f31c242a 100644 (file)
@@ -10,6 +10,7 @@
 #include "buffer.h"
 #include "net.h"
 #include "ipwd.h"
+#include "time-util.h"
 
 #include <stdio.h>
 #include <unistd.h>
@@ -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; ) {
index ab86291a31b455c01fc9afcd9a1426c3948beb13..9831f0549c1c47158f43e4513680d91c2cecacea 100644 (file)
@@ -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++) {
index f72690d9e2607cb01a0acb681c0ec632142b3bba..84e415e532eefdb17f8418520d22f5c70724451e 100644 (file)
@@ -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);
index 61f1d00083f4e61f8eee0ec125e3a8096251d428..1950f5698f786ca755e18c9099e66d9800d52c2d 100644 (file)
@@ -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 ||
index 43735b3287197735402c949195590d0c18a12c30..d94f9a5986c91abb92d899359417985bce02746f 100644 (file)
@@ -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;
 }
index 703efa22c6d79c7fbdbe48d3183be2d1a6b7fc75..792d15a30a1a219d6e19d0ed09bb3e38c114965b 100644 (file)
@@ -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);
index 611535cde8734015a0789598c1f8fc4915b7235f..8b0faa087c4e86cffb4716c5af76f58b7d4edc29 100644 (file)
@@ -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
index e2c5a9c3551a5a38f137f706d43e7b8fae8491ca..29b0fc4665983f837d045c110fa9a8d79c147e8c 100644 (file)
@@ -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: ");
index 64771902994c5ccaf2673b98a47696f65eae37b0..10d8c398f9a8b987d9917f13ea055f6ae7a152ef 100644 (file)
@@ -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);
 }