]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timesyncd: use structured initialization more
authorLennart Poettering <lennart@poettering.net>
Fri, 18 Mar 2022 13:03:23 +0000 (14:03 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 18 Mar 2022 22:52:36 +0000 (23:52 +0100)
src/timesync/timesyncd-manager.c
src/timesync/timesyncd-server.c

index 918da195d811083cbeaf0c2dbeefaba84a22ccd8..c14878bd21486bc0a21c9e28465624af49657e5a 100644 (file)
@@ -244,7 +244,7 @@ static int manager_clock_watch_setup(Manager *m) {
 }
 
 static int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
-        struct timex tmx = {};
+        struct timex tmx;
         int r;
 
         assert(m);
@@ -254,21 +254,22 @@ static int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
          * clock to the NTP time, larger deltas are just directly set.
          */
         if (fabs(offset) < NTP_MAX_ADJUST) {
-                tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
-                tmx.status = STA_PLL;
-                tmx.offset = offset * NSEC_PER_SEC;
-                tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4;
-                tmx.maxerror = 0;
-                tmx.esterror = 0;
+                tmx = (struct timex) {
+                        .modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR,
+                        .status = STA_PLL,
+                        .offset = offset * NSEC_PER_SEC,
+                        .constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4,
+                };
+
                 log_debug("  adjust (slew): %+.3f sec", offset);
         } else {
-                tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET | ADJ_MAXERROR | ADJ_ESTERROR;
+                tmx = (struct timex) {
+                        .modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET | ADJ_MAXERROR | ADJ_ESTERROR,
 
-                /* ADJ_NANO uses nanoseconds in the microseconds field */
-                tmx.time.tv_sec = (long)offset;
-                tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
-                tmx.maxerror = 0;
-                tmx.esterror = 0;
+                        /* ADJ_NANO uses nanoseconds in the microseconds field */
+                        .time.tv_sec = (long)offset,
+                        .time.tv_usec = (offset - (double) (long) offset) * NSEC_PER_SEC,
+                };
 
                 /* the kernel expects -0.3s as {-1, 7000.000.000} */
                 if (tmx.time.tv_usec < 0) {
@@ -1098,21 +1099,27 @@ int manager_new(Manager **ret) {
 
         assert(ret);
 
-        m = new0(Manager, 1);
+        m = new(Manager, 1);
         if (!m)
                 return -ENOMEM;
 
-        m->root_distance_max_usec = NTP_ROOT_DISTANCE_MAX_USEC;
-        m->poll_interval_min_usec = NTP_POLL_INTERVAL_MIN_USEC;
-        m->poll_interval_max_usec = NTP_POLL_INTERVAL_MAX_USEC;
+        *m = (Manager) {
+                .root_distance_max_usec = NTP_ROOT_DISTANCE_MAX_USEC,
+                .poll_interval_min_usec = NTP_POLL_INTERVAL_MIN_USEC,
+                .poll_interval_max_usec = NTP_POLL_INTERVAL_MAX_USEC,
 
-        m->connection_retry_usec = DEFAULT_CONNECTION_RETRY_USEC;
+                .connection_retry_usec = DEFAULT_CONNECTION_RETRY_USEC,
 
-        m->server_socket = m->clock_watch_fd = -1;
+                .server_socket = -1,
+                .clock_watch_fd = -1,
 
-        m->ratelimit = (RateLimit) { RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST };
+                .ratelimit = (RateLimit) {
+                        RATELIMIT_INTERVAL_USEC,
+                        RATELIMIT_BURST
+                },
 
-        m->save_time_interval_usec = DEFAULT_SAVE_TIME_INTERVAL_USEC;
+                .save_time_interval_usec = DEFAULT_SAVE_TIME_INTERVAL_USEC,
+        };
 
         r = sd_event_default(&m->event);
         if (r < 0)
index 79dfd47266965193d04795bca9f52634b1ac5f00..dd168917341ef43399221831c848304bed660f66 100644 (file)
@@ -16,16 +16,19 @@ int server_address_new(
         assert(socklen >= offsetof(struct sockaddr, sa_data));
         assert(socklen <= sizeof(union sockaddr_union));
 
-        a = new0(ServerAddress, 1);
+        a = new(ServerAddress, 1);
         if (!a)
                 return -ENOMEM;
 
+        *a = (ServerAddress) {
+                .name = n,
+                .socklen = socklen,
+        };
+
         memcpy(&a->sockaddr, sockaddr, socklen);
-        a->socklen = socklen;
 
         LIST_FIND_TAIL(addresses, n->addresses, tail);
         LIST_INSERT_AFTER(addresses, n->addresses, tail, a);
-        a->name = n;
 
         if (ret)
                 *ret = a;
@@ -58,12 +61,16 @@ int server_name_new(
         assert(m);
         assert(string);
 
-        n = new0(ServerName, 1);
+        n = new(ServerName, 1);
         if (!n)
                 return -ENOMEM;
 
-        n->type = type;
-        n->string = strdup(string);
+        *n = (ServerName) {
+                .manager = m,
+                .type = type,
+                .string = strdup(string),
+        };
+
         if (!n->string) {
                 free(n);
                 return -ENOMEM;
@@ -81,8 +88,6 @@ int server_name_new(
         } else
                 assert_not_reached();
 
-        n->manager = m;
-
         if (type != SERVER_FALLBACK &&
             m->current_server_name &&
             m->current_server_name->type == SERVER_FALLBACK)