X Tutup
Skip to content

Commit 2afb2f4

Browse files
authored
Merge pull request systemd#22885 from poettering/kill-clock-boottime-or-monotonic
time-util: assume CLOCK_BOOTTIME always exists
2 parents f3b3cab + ba4e042 commit 2afb2f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+122
-176
lines changed

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LICENSE:
3030
LGPL-2.1-or-later for all code, exceptions noted in LICENSES/README.md
3131

3232
REQUIREMENTS:
33-
Linux kernel >= 3.13
33+
Linux kernel >= 3.15
3434
Linux kernel >= 4.2 for unified cgroup hierarchy support
3535
Linux kernel >= 4.10 for cgroup-bpf egress and ingress hooks
3636
Linux kernel >= 4.15 for cgroup-bpf device hook

src/basic/time-util.c

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ triple_timestamp* triple_timestamp_get(triple_timestamp *ts) {
7777

7878
ts->realtime = now(CLOCK_REALTIME);
7979
ts->monotonic = now(CLOCK_MONOTONIC);
80-
ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY;
80+
ts->boottime = now(CLOCK_BOOTTIME);
8181

8282
return ts;
8383
}
@@ -150,9 +150,7 @@ triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u)
150150

151151
ts->realtime = u;
152152
ts->monotonic = map_clock_usec_internal(u, nowr, now(CLOCK_MONOTONIC));
153-
ts->boottime = clock_boottime_supported() ?
154-
map_clock_usec_internal(u, nowr, now(CLOCK_BOOTTIME)) :
155-
USEC_INFINITY;
153+
ts->boottime = map_clock_usec_internal(u, nowr, now(CLOCK_BOOTTIME));
156154

157155
return ts;
158156
}
@@ -170,23 +168,16 @@ dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
170168
return ts;
171169
}
172170

173-
dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
174-
clockid_t cid;
171+
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
175172
usec_t nowm;
176173

177174
if (u == USEC_INFINITY) {
178175
ts->realtime = ts->monotonic = USEC_INFINITY;
179176
return ts;
180177
}
181178

182-
cid = clock_boottime_or_monotonic();
183-
nowm = now(cid);
184-
185-
if (cid == CLOCK_MONOTONIC)
186-
ts->monotonic = u;
187-
else
188-
ts->monotonic = map_clock_usec_internal(u, nowm, now(CLOCK_MONOTONIC));
189-
179+
nowm = now(CLOCK_BOOTTIME);
180+
ts->monotonic = map_clock_usec_internal(u, nowm, now(CLOCK_MONOTONIC));
190181
ts->realtime = map_clock_usec_internal(u, nowm, now(CLOCK_REALTIME));
191182
return ts;
192183
}
@@ -1461,50 +1452,17 @@ int verify_timezone(const char *name, int log_level) {
14611452
return 0;
14621453
}
14631454

1464-
bool clock_boottime_supported(void) {
1465-
static int supported = -1;
1466-
1467-
/* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1468-
1469-
if (supported < 0) {
1470-
int fd;
1471-
1472-
fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1473-
if (fd < 0)
1474-
supported = false;
1475-
else {
1476-
safe_close(fd);
1477-
supported = true;
1478-
}
1479-
}
1480-
1481-
return supported;
1482-
}
1483-
1484-
clockid_t clock_boottime_or_monotonic(void) {
1485-
if (clock_boottime_supported())
1486-
return CLOCK_BOOTTIME;
1487-
else
1488-
return CLOCK_MONOTONIC;
1489-
}
1490-
14911455
bool clock_supported(clockid_t clock) {
14921456
struct timespec ts;
14931457

14941458
switch (clock) {
14951459

14961460
case CLOCK_MONOTONIC:
14971461
case CLOCK_REALTIME:
1498-
return true;
1499-
15001462
case CLOCK_BOOTTIME:
1501-
return clock_boottime_supported();
1502-
1503-
case CLOCK_BOOTTIME_ALARM:
1504-
if (!clock_boottime_supported())
1505-
return false;
1463+
/* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1464+
return true;
15061465

1507-
_fallthrough_;
15081466
default:
15091467
/* For everything else, check properly */
15101468
return clock_gettime(clock, &ts) >= 0;

src/basic/time-util.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock);
8282
dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
8383
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
8484
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u);
85-
dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u);
85+
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u);
8686

8787
triple_timestamp* triple_timestamp_get(triple_timestamp *ts);
8888
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u);
@@ -155,9 +155,7 @@ static inline bool timezone_is_valid(const char *name, int log_level) {
155155
return verify_timezone(name, log_level) >= 0;
156156
}
157157

158-
bool clock_boottime_supported(void);
159158
bool clock_supported(clockid_t clock);
160-
clockid_t clock_boottime_or_monotonic(void);
161159

162160
usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
163161

src/core/timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct Timer {
6464
char *stamp_path;
6565
};
6666

67-
#define TIMER_MONOTONIC_CLOCK(t) ((t)->wake_system && clock_boottime_supported() ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC)
67+
#define TIMER_MONOTONIC_CLOCK(t) ((t)->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC)
6868

6969
void timer_free_values(Timer *t);
7070

src/import/curl-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata
148148
if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0)
149149
return -1;
150150
} else {
151-
if (sd_event_add_time_relative(g->event, &g->timer, clock_boottime_or_monotonic(), usec, 0, curl_glue_on_timer, g) < 0)
151+
if (sd_event_add_time_relative(g->event, &g->timer, CLOCK_BOOTTIME, usec, 0, curl_glue_on_timer, g) < 0)
152152
return -1;
153153

154154
(void) sd_event_source_set_description(g->timer, "curl-timer");

src/libsystemd-network/lldp-neighbor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ void lldp_neighbor_start_ttl(sd_lldp_neighbor *n) {
333333
usec_t base;
334334

335335
/* Use the packet's timestamp if there is one known */
336-
base = triple_timestamp_by_clock(&n->timestamp, clock_boottime_or_monotonic());
336+
base = triple_timestamp_by_clock(&n->timestamp, CLOCK_BOOTTIME);
337337
if (!timestamp_is_set(base))
338-
base = now(clock_boottime_or_monotonic()); /* Otherwise, take the current time */
338+
base = now(CLOCK_BOOTTIME); /* Otherwise, take the current time */
339339

340340
n->until = usec_add(base, n->ttl * USEC_PER_SEC);
341341
} else

src/libsystemd-network/sd-dhcp-client.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ static int client_message_init(
821821

822822
/* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
823823
refuse to issue an DHCP lease if 'secs' is set to zero */
824-
r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
824+
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now);
825825
if (r < 0)
826826
return r;
827827
assert(time_now >= client->start_time);
@@ -1246,7 +1246,7 @@ static int client_timeout_resend(
12461246
assert(client);
12471247
assert(client->event);
12481248

1249-
r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
1249+
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now);
12501250
if (r < 0)
12511251
goto error;
12521252

@@ -1294,7 +1294,7 @@ static int client_timeout_resend(
12941294
}
12951295

12961296
r = event_reset_time(client->event, &client->timeout_resend,
1297-
clock_boottime_or_monotonic(),
1297+
CLOCK_BOOTTIME,
12981298
next_timeout, 10 * USEC_PER_MSEC,
12991299
client_timeout_resend, client,
13001300
client->event_priority, "dhcp4-resend-timer", true);
@@ -1394,12 +1394,12 @@ static int client_initialize_time_events(sd_dhcp_client *client) {
13941394
assert(client->event);
13951395

13961396
if (client->start_delay > 0) {
1397-
assert_se(sd_event_now(client->event, clock_boottime_or_monotonic(), &usec) >= 0);
1397+
assert_se(sd_event_now(client->event, CLOCK_BOOTTIME, &usec) >= 0);
13981398
usec += client->start_delay;
13991399
}
14001400

14011401
r = event_reset_time(client->event, &client->timeout_resend,
1402-
clock_boottime_or_monotonic(),
1402+
CLOCK_BOOTTIME,
14031403
usec, 0,
14041404
client_timeout_resend, client,
14051405
client->event_priority, "dhcp4-resend-timer", true);
@@ -1440,7 +1440,7 @@ static int client_start_delayed(sd_dhcp_client *client) {
14401440
client->fd = r;
14411441

14421442
if (IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_INIT_REBOOT))
1443-
client->start_time = now(clock_boottime_or_monotonic());
1443+
client->start_time = now(CLOCK_BOOTTIME);
14441444

14451445
return client_initialize_events(client, client_receive_message_raw);
14461446
}
@@ -1684,7 +1684,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
16841684
return 0;
16851685
}
16861686

1687-
r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
1687+
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now);
16881688
if (r < 0)
16891689
return r;
16901690
assert(client->request_sent <= time_now);
@@ -1717,7 +1717,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
17171717

17181718
/* arm lifetime timeout */
17191719
r = event_reset_time(client->event, &client->timeout_expire,
1720-
clock_boottime_or_monotonic(),
1720+
CLOCK_BOOTTIME,
17211721
client->expire_time, 10 * USEC_PER_MSEC,
17221722
client_timeout_expire, client,
17231723
client->event_priority, "dhcp4-lifetime", true);
@@ -1733,7 +1733,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
17331733

17341734
/* arm T2 timeout */
17351735
r = event_reset_time(client->event, &client->timeout_t2,
1736-
clock_boottime_or_monotonic(),
1736+
CLOCK_BOOTTIME,
17371737
client->t2_time, 10 * USEC_PER_MSEC,
17381738
client_timeout_t2, client,
17391739
client->event_priority, "dhcp4-t2-timeout", true);
@@ -1749,7 +1749,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
17491749

17501750
/* arm T1 timeout */
17511751
r = event_reset_time(client->event, &client->timeout_t1,
1752-
clock_boottime_or_monotonic(),
1752+
CLOCK_BOOTTIME,
17531753
client->t1_time, 10 * USEC_PER_MSEC,
17541754
client_timeout_t1, client,
17551755
client->event_priority, "dhcp4-t1-timer", true);
@@ -1784,7 +1784,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, i
17841784
client->attempt = 0;
17851785

17861786
r = event_reset_time(client->event, &client->timeout_resend,
1787-
clock_boottime_or_monotonic(),
1787+
CLOCK_BOOTTIME,
17881788
0, 0,
17891789
client_timeout_resend, client,
17901790
client->event_priority, "dhcp4-resend-timer", true);

src/libsystemd-network/sd-dhcp-server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ static int server_ack_request(sd_dhcp_server *server, DHCPRequest *req, DHCPLeas
987987
assert(req);
988988
assert(address != 0);
989989

990-
r = sd_event_now(server->event, clock_boottime_or_monotonic(), &time_now);
990+
r = sd_event_now(server->event, CLOCK_BOOTTIME, &time_now);
991991
if (r < 0)
992992
return r;
993993

@@ -1039,7 +1039,7 @@ static int dhcp_server_cleanup_expired_leases(sd_dhcp_server *server) {
10391039

10401040
assert(server);
10411041

1042-
r = sd_event_now(server->event, clock_boottime_or_monotonic(), &time_now);
1042+
r = sd_event_now(server->event, CLOCK_BOOTTIME, &time_now);
10431043
if (r < 0)
10441044
return r;
10451045

src/libsystemd-network/sd-dhcp6-client.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int dhcp6_client_send_message(sd_dhcp6_client *client) {
649649
assert(client);
650650
assert(client->event);
651651

652-
r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
652+
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now);
653653
if (r < 0)
654654
return r;
655655

@@ -830,7 +830,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, void *userda
830830
FORMAT_TIMESPAN(client->retransmit_time, USEC_PER_SEC));
831831

832832
r = event_reset_time_relative(client->event, &client->timeout_resend,
833-
clock_boottime_or_monotonic(),
833+
CLOCK_BOOTTIME,
834834
client->retransmit_time, 10 * USEC_PER_MSEC,
835835
client_timeout_resend, client,
836836
client->event_priority, "dhcp6-resend-timer", true);
@@ -872,12 +872,12 @@ static int client_start_transaction(sd_dhcp6_client *client, DHCP6State state) {
872872
client->retransmit_count = 0;
873873
client->transaction_id = random_u32() & htobe32(0x00ffffff);
874874

875-
r = sd_event_now(client->event, clock_boottime_or_monotonic(), &client->transaction_start);
875+
r = sd_event_now(client->event, CLOCK_BOOTTIME, &client->transaction_start);
876876
if (r < 0)
877877
goto error;
878878

879879
r = event_reset_time(client->event, &client->timeout_resend,
880-
clock_boottime_or_monotonic(),
880+
CLOCK_BOOTTIME,
881881
0, 0,
882882
client_timeout_resend, client,
883883
client->event_priority, "dhcp6-resend-timeout", true);
@@ -969,7 +969,7 @@ static int client_enter_bound_state(sd_dhcp6_client *client) {
969969
} else {
970970
log_dhcp6_client(client, "T1 expires in %s", FORMAT_TIMESPAN(lifetime_t1, USEC_PER_SEC));
971971
r = event_reset_time_relative(client->event, &client->timeout_t1,
972-
clock_boottime_or_monotonic(),
972+
CLOCK_BOOTTIME,
973973
lifetime_t1, 10 * USEC_PER_SEC,
974974
client_timeout_t1, client,
975975
client->event_priority, "dhcp6-t1-timeout", true);
@@ -983,7 +983,7 @@ static int client_enter_bound_state(sd_dhcp6_client *client) {
983983
} else {
984984
log_dhcp6_client(client, "T2 expires in %s", FORMAT_TIMESPAN(lifetime_t2, USEC_PER_SEC));
985985
r = event_reset_time_relative(client->event, &client->timeout_t2,
986-
clock_boottime_or_monotonic(),
986+
CLOCK_BOOTTIME,
987987
lifetime_t2, 10 * USEC_PER_SEC,
988988
client_timeout_t2, client,
989989
client->event_priority, "dhcp6-t2-timeout", true);
@@ -998,7 +998,7 @@ static int client_enter_bound_state(sd_dhcp6_client *client) {
998998
log_dhcp6_client(client, "Valid lifetime expires in %s", FORMAT_TIMESPAN(lifetime_valid, USEC_PER_SEC));
999999

10001000
r = event_reset_time_relative(client->event, &client->timeout_expire,
1001-
clock_boottime_or_monotonic(),
1001+
CLOCK_BOOTTIME,
10021002
lifetime_valid, USEC_PER_SEC,
10031003
client_timeout_expire, client,
10041004
client->event_priority, "dhcp6-lease-expire", true);

src/libsystemd-network/sd-ipv4acd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ static int ipv4acd_set_next_wakeup(sd_ipv4acd *acd, usec_t usec, usec_t random_u
200200
if (random_usec > 0)
201201
next_timeout += (usec_t) random_u64() % random_usec;
202202

203-
assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
203+
assert_se(sd_event_now(acd->event, CLOCK_BOOTTIME, &time_now) >= 0);
204204

205205
return event_reset_time(acd->event, &acd->timer_event_source,
206-
clock_boottime_or_monotonic(),
206+
CLOCK_BOOTTIME,
207207
time_now + next_timeout, 0,
208208
ipv4acd_on_timeout, acd,
209209
acd->event_priority, "ipv4acd-timer", true);
@@ -381,7 +381,7 @@ static int ipv4acd_on_packet(
381381
if (ipv4acd_arp_conflict(acd, &packet, true)) {
382382
usec_t ts;
383383

384-
assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &ts) >= 0);
384+
assert_se(sd_event_now(acd->event, CLOCK_BOOTTIME, &ts) >= 0);
385385

386386
/* Defend address */
387387
if (ts > acd->defend_window) {

0 commit comments

Comments
 (0)
X Tutup