blob: 6c556fe80fdfe6b0f81220a47a8aa9a14d7ed29f [file] [log] [blame]
Andrew Geissler028142b2023-05-05 11:29:21 -05001From 924937cbc0bf692bc6e5b3a0bd3c18347d9521e9 Mon Sep 17 00:00:00 2001
2From: Yu Watanabe <watanabe.yu+github@gmail.com>
3Date: Thu, 13 Apr 2023 16:40:36 +0900
4Subject: [PATCH 1/7] timesync: drop unnecessary initialization
5
6Upstream-Status: Submitted [https://github.com/systemd/systemd/pull/27253]
7Signed-off-by: Khem Raj <raj.khem@gmail.com>
8---
9 src/timesync/timesyncd-manager.c | 2 +-
10 1 file changed, 1 insertion(+), 1 deletion(-)
11
12--- a/src/timesync/timesyncd-manager.c
13+++ b/src/timesync/timesyncd-manager.c
14@@ -410,7 +410,7 @@ static int manager_receive_response(sd_e
15 .msg_name = &server_addr,
16 .msg_namelen = sizeof(server_addr),
17 };
18- struct timespec *recv_time = NULL;
19+ struct timespec *recv_time;
20 triple_timestamp dts;
21 ssize_t len;
22 double origin, receive, trans, dest, delay, offset, root_distance;
23@@ -445,7 +445,7 @@ static int manager_receive_response(sd_e
24 return 0;
25 }
26
27- recv_time = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_TIMESTAMPNS, struct timespec);
28+ recv_time = CMSG_FIND_AND_COPY_DATA(&msghdr, SOL_SOCKET, SCM_TIMESTAMPNS, struct timespec);
29 if (!recv_time)
30 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Packet timestamp missing.");
31
32--- a/src/basic/socket-util.h
33+++ b/src/basic/socket-util.h
34@@ -183,17 +183,22 @@ int flush_accept(int fd);
35 * riscv32. */
36 #define CMSG_TYPED_DATA(cmsg, type) \
37 ({ \
38- struct cmsghdr *_cmsg = cmsg; \
39- assert_cc(__alignof__(type) <= __alignof__(struct cmsghdr)); \
40+ struct cmsghdr *_cmsg = (cmsg); \
41+ assert_cc(alignof(type) <= alignof(struct cmsghdr)); \
42 _cmsg ? CAST_ALIGN_PTR(type, CMSG_DATA(_cmsg)) : (type*) NULL; \
43 })
44
45 struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
46+void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len);
47
48 /* Type-safe, dereferencing version of cmsg_find() */
49 #define CMSG_FIND_DATA(mh, level, type, ctype) \
50 CMSG_TYPED_DATA(cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))), ctype)
51
52+/* Type-safe version of cmsg_find_and_copy_data() */
53+#define CMSG_FIND_AND_COPY_DATA(mh, level, type, ctype) \
54+ (ctype*) cmsg_find_and_copy_data(mh, level, type, &(ctype){}, sizeof(ctype))
55+
56 /* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
57 * itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"
58 * structures. */
59--- a/src/boot/efi/pe.c
60+++ b/src/boot/efi/pe.c
61@@ -197,7 +197,7 @@ static uint32_t get_compatibility_entry_
62 uint32_t entry_point;
63 } _packed_ LinuxPeCompat1;
64
65- while (size >= sizeof(LinuxPeCompat1) && addr % __alignof__(LinuxPeCompat1) == 0) {
66+ while (size >= sizeof(LinuxPeCompat1) && addr % alignof(LinuxPeCompat1) == 0) {
67 LinuxPeCompat1 *compat = (LinuxPeCompat1 *) ((uint8_t *) dos + addr);
68
69 if (compat->type == 0 || compat->size == 0 || compat->size > size)
70--- a/src/fundamental/macro-fundamental.h
71+++ b/src/fundamental/macro-fundamental.h
72@@ -6,12 +6,13 @@
73 #endif
74
75 #include <limits.h>
76+#include <stdalign.h>
77 #include <stdbool.h>
78 #include <stddef.h>
79 #include <stdint.h>
80
81 #define _align_(x) __attribute__((__aligned__(x)))
82-#define _alignas_(x) __attribute__((__aligned__(__alignof__(x))))
83+#define _alignas_(x) __attribute__((__aligned__(alignof(x))))
84 #define _alignptr_ __attribute__((__aligned__(sizeof(void *))))
85 #define _cleanup_(x) __attribute__((__cleanup__(x)))
86 #define _const_ __attribute__((__const__))
87@@ -346,9 +347,9 @@ static inline size_t ALIGN_TO(size_t l,
88 #endif
89
90 /* Checks if the specified pointer is aligned as appropriate for the specific type */
91-#define IS_ALIGNED16(p) (((uintptr_t) p) % __alignof__(uint16_t) == 0)
92-#define IS_ALIGNED32(p) (((uintptr_t) p) % __alignof__(uint32_t) == 0)
93-#define IS_ALIGNED64(p) (((uintptr_t) p) % __alignof__(uint64_t) == 0)
94+#define IS_ALIGNED16(p) (((uintptr_t) p) % alignof(uint16_t) == 0)
95+#define IS_ALIGNED32(p) (((uintptr_t) p) % alignof(uint32_t) == 0)
96+#define IS_ALIGNED64(p) (((uintptr_t) p) % alignof(uint64_t) == 0)
97
98 /* Same as ALIGN_TO but callable in constant contexts. */
99 #define CONST_ALIGN_TO(l, ali) \
100@@ -366,7 +367,7 @@ static inline size_t ALIGN_TO(size_t l,
101 #define CAST_ALIGN_PTR(t, p) \
102 ({ \
103 const void *_p = (p); \
104- assert(((uintptr_t) _p) % __alignof__(t) == 0); \
105+ assert(((uintptr_t) _p) % alignof(t) == 0); \
106 (t *) _p; \
107 })
108
109--- a/src/network/networkd-nexthop.c
110+++ b/src/network/networkd-nexthop.c
111@@ -894,7 +894,7 @@ int manager_rtnl_process_nexthop(sd_netl
112 return 0;
113 }
114
115- assert((uintptr_t) group % __alignof__(struct nexthop_grp) == 0);
116+ assert((uintptr_t) group % alignof(struct nexthop_grp) == 0);
117
118 n_group = raw_group_size / sizeof(struct nexthop_grp);
119 for (size_t i = 0; i < n_group; i++) {
120--- a/src/test/test-sizeof.c
121+++ b/src/test/test-sizeof.c
122@@ -17,16 +17,16 @@
123 DISABLE_WARNING_TYPE_LIMITS;
124
125 #define info_no_sign(t) \
126- printf("%s → %zu bits, %zu byte alignment\n", STRINGIFY(t), \
127+ printf("%s → %zu bits, %zu byte alignment\n", STRINGIFY(t), \
128 sizeof(t)*CHAR_BIT, \
129- __alignof__(t))
130+ alignof(t))
131
132 #define info(t) \
133- printf("%s → %zu bits%s, %zu byte alignment\n", STRINGIFY(t), \
134+ printf("%s → %zu bits%s, %zu byte alignment\n", STRINGIFY(t), \
135 sizeof(t)*CHAR_BIT, \
136 strstr(STRINGIFY(t), "signed") ? "" : \
137 (t)-1 < (t)0 ? ", signed" : ", unsigned", \
138- __alignof__(t))
139+ alignof(t))
140
141 enum Enum {
142 enum_value,
143@@ -44,7 +44,7 @@ enum BigEnum2 {
144 int main(void) {
145 int (*function_pointer)(void);
146
147- info_no_sign(function_pointer);
148+ info_no_sign(typeof(function_pointer));
149 info_no_sign(void*);
150 info(char*);
151
152--- a/src/basic/socket-util.c
153+++ b/src/basic/socket-util.c
154@@ -1171,6 +1171,18 @@ struct cmsghdr* cmsg_find(struct msghdr
155 return NULL;
156 }
157
158+void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) {
159+ struct cmsghdr *cmsg;
160+
161+ assert(mh);
162+
163+ cmsg = cmsg_find(mh, level, type, buf_len == SIZE_MAX ? (socklen_t) -1 : CMSG_LEN(buf_len));
164+ if (!cmsg)
165+ return NULL;
166+
167+ return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len == SIZE_MAX ? cmsg->cmsg_len : buf_len);
168+}
169+
170 int socket_ioctl_fd(void) {
171 int fd;
172
173--- a/src/journal/journald-server.c
174+++ b/src/journal/journald-server.c
175@@ -1385,7 +1385,7 @@ int server_process_datagram(
176 size_t label_len = 0, m;
177 Server *s = ASSERT_PTR(userdata);
178 struct ucred *ucred = NULL;
179- struct timeval *tv = NULL;
180+ struct timeval tv_buf, *tv = NULL;
181 struct cmsghdr *cmsg;
182 char *label = NULL;
183 struct iovec iovec;
184@@ -1461,10 +1461,10 @@ int server_process_datagram(
185 label = CMSG_TYPED_DATA(cmsg, char);
186 label_len = cmsg->cmsg_len - CMSG_LEN(0);
187 } else if (cmsg->cmsg_level == SOL_SOCKET &&
188- cmsg->cmsg_type == SO_TIMESTAMP &&
189+ cmsg->cmsg_type == SCM_TIMESTAMP &&
190 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) {
191 assert(!tv);
192- tv = CMSG_TYPED_DATA(cmsg, struct timeval);
193+ tv = memcpy(&tv_buf, CMSG_DATA(cmsg), sizeof(struct timeval));
194 } else if (cmsg->cmsg_level == SOL_SOCKET &&
195 cmsg->cmsg_type == SCM_RIGHTS) {
196 assert(!fds);
197--- a/src/libsystemd-network/icmp6-util.c
198+++ b/src/libsystemd-network/icmp6-util.c
199@@ -199,9 +199,11 @@ int icmp6_receive(int fd, void *buffer,
200 }
201
202 if (cmsg->cmsg_level == SOL_SOCKET &&
203- cmsg->cmsg_type == SO_TIMESTAMP &&
204- cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
205- triple_timestamp_from_realtime(&t, timeval_load(CMSG_TYPED_DATA(cmsg, struct timeval)));
206+ cmsg->cmsg_type == SCM_TIMESTAMP &&
207+ cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) {
208+ struct timeval *tv = memcpy(&(struct timeval) {}, CMSG_DATA(cmsg), sizeof(struct timeval));
209+ triple_timestamp_from_realtime(&t, timeval_load(tv));
210+ }
211 }
212
213 if (!triple_timestamp_is_set(&t))
214--- a/src/libsystemd-network/sd-dhcp6-client.c
215+++ b/src/libsystemd-network/sd-dhcp6-client.c
216@@ -1276,7 +1276,6 @@ static int client_receive_message(
217 .msg_control = &control,
218 .msg_controllen = sizeof(control),
219 };
220- struct cmsghdr *cmsg;
221 triple_timestamp t = {};
222 _cleanup_free_ DHCP6Message *message = NULL;
223 struct in6_addr *server_address = NULL;
224@@ -1320,12 +1319,9 @@ static int client_receive_message(
225 server_address = &sa.in6.sin6_addr;
226 }
227
228- CMSG_FOREACH(cmsg, &msg) {
229- if (cmsg->cmsg_level == SOL_SOCKET &&
230- cmsg->cmsg_type == SO_TIMESTAMP &&
231- cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
232- triple_timestamp_from_realtime(&t, timeval_load(CMSG_TYPED_DATA(cmsg, struct timeval)));
233- }
234+ struct timeval *tv = CMSG_FIND_AND_COPY_DATA(&msg, SOL_SOCKET, SCM_TIMESTAMP, struct timeval);
235+ if (tv)
236+ triple_timestamp_from_realtime(&t, timeval_load(tv));
237
238 if (client->transaction_id != (message->transaction_id & htobe32(0x00ffffff)))
239 return 0;
240--- a/src/libsystemd-network/sd-dhcp-server.c
241+++ b/src/libsystemd-network/sd-dhcp-server.c
242@@ -407,7 +407,7 @@ static int dhcp_server_send_udp(sd_dhcp_
243 rather than binding the socket. This will be mostly useful
244 when we gain support for arbitrary number of server addresses
245 */
246- pktinfo = (struct in_pktinfo*) CMSG_DATA(cmsg);
247+ pktinfo = CMSG_TYPED_DATA(cmsg, struct in_pktinfo);
248 assert(pktinfo);
249
250 pktinfo->ipi_ifindex = server->ifindex;
251@@ -1270,7 +1270,6 @@ static int server_receive_message(sd_eve
252 .msg_control = &control,
253 .msg_controllen = sizeof(control),
254 };
255- struct cmsghdr *cmsg;
256 ssize_t datagram_size, len;
257 int r;
258
259@@ -1306,19 +1305,10 @@ static int server_receive_message(sd_eve
260 if ((size_t) len < sizeof(DHCPMessage))
261 return 0;
262
263- CMSG_FOREACH(cmsg, &msg)
264- if (cmsg->cmsg_level == IPPROTO_IP &&
265- cmsg->cmsg_type == IP_PKTINFO &&
266- cmsg->cmsg_len == CMSG_LEN(sizeof(struct in_pktinfo))) {
267- struct in_pktinfo *info = CMSG_TYPED_DATA(cmsg, struct in_pktinfo);
268-
269- /* TODO figure out if this can be done as a filter on
270- * the socket, like for IPv6 */
271- if (server->ifindex != info->ipi_ifindex)
272- return 0;
273-
274- break;
275- }
276+ /* TODO figure out if this can be done as a filter on the socket, like for IPv6 */
277+ struct in_pktinfo *info = CMSG_FIND_DATA(&msg, IPPROTO_IP, IP_PKTINFO, struct in_pktinfo);
278+ if (info && info->ipi_ifindex != server->ifindex)
279+ return 0;
280
281 if (sd_dhcp_server_is_in_relay_mode(server)) {
282 r = dhcp_server_relay_message(server, message, len - sizeof(DHCPMessage), buflen);
283--- a/src/libsystemd/sd-daemon/sd-daemon.c
284+++ b/src/libsystemd/sd-daemon/sd-daemon.c
285@@ -567,7 +567,7 @@ _public_ int sd_pid_notify_with_fds(
286 cmsg->cmsg_type = SCM_CREDENTIALS;
287 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
288
289- ucred = (struct ucred*) CMSG_DATA(cmsg);
290+ ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
291 ucred->pid = pid != 0 ? pid : getpid_cached();
292 ucred->uid = getuid();
293 ucred->gid = getgid();
294--- a/src/resolve/resolved-manager.c
295+++ b/src/resolve/resolved-manager.c
296@@ -984,7 +984,7 @@ static int manager_ipv4_send(
297 cmsg->cmsg_level = IPPROTO_IP;
298 cmsg->cmsg_type = IP_PKTINFO;
299
300- pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
301+ pi = CMSG_TYPED_DATA(cmsg, struct in_pktinfo);
302 pi->ipi_ifindex = ifindex;
303
304 if (source)
305@@ -1040,7 +1040,7 @@ static int manager_ipv6_send(
306 cmsg->cmsg_level = IPPROTO_IPV6;
307 cmsg->cmsg_type = IPV6_PKTINFO;
308
309- pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
310+ pi = CMSG_TYPED_DATA(cmsg, struct in6_pktinfo);
311 pi->ipi6_ifindex = ifindex;
312
313 if (source)