]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
udev: drop meaningless size optimization
[thirdparty/systemd.git] / src / udev / udev-ctrl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 *
3 * libudev - interface to udev device information
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 */
10
11 #include <errno.h>
12 #include <poll.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/un.h>
17 #include <unistd.h>
18
19 #include "sd-event.h"
20
21 #include "alloc-util.h"
22 #include "errno-util.h"
23 #include "fd-util.h"
24 #include "format-util.h"
25 #include "io-util.h"
26 #include "socket-util.h"
27 #include "strxcpyx.h"
28 #include "udev-ctrl.h"
29 #include "util.h"
30
31 /* wire protocol magic must match */
32 #define UDEV_CTRL_MAGIC 0xdead1dea
33
34 struct udev_ctrl_msg_wire {
35 char version[16];
36 unsigned magic;
37 enum udev_ctrl_msg_type type;
38 union udev_ctrl_msg_value value;
39 };
40
41 struct udev_ctrl {
42 unsigned n_ref;
43 int sock;
44 int sock_connect;
45 union sockaddr_union saddr;
46 socklen_t addrlen;
47 bool bound;
48 bool cleanup_socket;
49 bool connected;
50 bool maybe_disconnected;
51 sd_event *event;
52 sd_event_source *event_source;
53 sd_event_source *event_source_connect;
54 udev_ctrl_handler_t callback;
55 void *userdata;
56 };
57
58 int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) {
59 _cleanup_close_ int sock = -1;
60 struct udev_ctrl *uctrl;
61
62 assert(ret);
63
64 if (fd < 0) {
65 sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
66 if (sock < 0)
67 return log_error_errno(errno, "Failed to create socket: %m");
68 }
69
70 uctrl = new(struct udev_ctrl, 1);
71 if (!uctrl)
72 return -ENOMEM;
73
74 *uctrl = (struct udev_ctrl) {
75 .n_ref = 1,
76 .sock = fd >= 0 ? fd : TAKE_FD(sock),
77 .sock_connect = -1,
78 .bound = fd >= 0,
79 };
80
81 uctrl->saddr.un = (struct sockaddr_un) {
82 .sun_family = AF_UNIX,
83 .sun_path = "/run/udev/control",
84 };
85
86 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
87
88 *ret = TAKE_PTR(uctrl);
89 return 0;
90 }
91
92 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
93 int r;
94
95 assert(uctrl);
96
97 if (uctrl->bound)
98 return 0;
99
100 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
101 if (r < 0 && errno == EADDRINUSE) {
102 (void) sockaddr_un_unlink(&uctrl->saddr.un);
103 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
104 }
105
106 if (r < 0)
107 return log_error_errno(errno, "Failed to bind udev control socket: %m");
108
109 if (listen(uctrl->sock, 0) < 0)
110 return log_error_errno(errno, "Failed to listen udev control socket: %m");
111
112 uctrl->bound = true;
113 uctrl->cleanup_socket = true;
114
115 return 0;
116 }
117
118 static void udev_ctrl_disconnect(struct udev_ctrl *uctrl) {
119 if (!uctrl)
120 return;
121
122 uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect);
123 uctrl->sock_connect = safe_close(uctrl->sock_connect);
124 }
125
126 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
127 assert(uctrl);
128
129 udev_ctrl_disconnect(uctrl);
130
131 sd_event_source_unref(uctrl->event_source);
132 safe_close(uctrl->sock);
133
134 sd_event_unref(uctrl->event);
135 return mfree(uctrl);
136 }
137
138 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
139
140 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
141 if (!uctrl)
142 return 0;
143 if (uctrl->cleanup_socket)
144 sockaddr_un_unlink(&uctrl->saddr.un);
145 return 0;
146 }
147
148 int udev_ctrl_attach_event(struct udev_ctrl *uctrl, sd_event *event) {
149 int r;
150
151 assert_return(uctrl, -EINVAL);
152 assert_return(!uctrl->event, -EBUSY);
153
154 if (event)
155 uctrl->event = sd_event_ref(event);
156 else {
157 r = sd_event_default(&uctrl->event);
158 if (r < 0)
159 return r;
160 }
161
162 return 0;
163 }
164
165 sd_event_source *udev_ctrl_get_event_source(struct udev_ctrl *uctrl) {
166 assert(uctrl);
167
168 return uctrl->event_source;
169 }
170
171 static void udev_ctrl_disconnect_and_listen_again(struct udev_ctrl *uctrl) {
172 udev_ctrl_disconnect(uctrl);
173 udev_ctrl_unref(uctrl);
174 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON);
175 }
176
177 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl *, udev_ctrl_disconnect_and_listen_again);
178
179 static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
180 _cleanup_(udev_ctrl_disconnect_and_listen_againp) struct udev_ctrl *uctrl = NULL;
181 struct udev_ctrl_msg_wire msg_wire;
182 struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(struct udev_ctrl_msg_wire));
183 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
184 struct msghdr smsg = {
185 .msg_iov = &iov,
186 .msg_iovlen = 1,
187 .msg_control = &control,
188 .msg_controllen = sizeof(control),
189 };
190 struct cmsghdr *cmsg;
191 struct ucred *cred;
192 ssize_t size;
193
194 assert(userdata);
195
196 /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object.
197 * To avoid the object freed, let's increment the refcount. */
198 uctrl = udev_ctrl_ref(userdata);
199
200 size = next_datagram_size_fd(fd);
201 if (size < 0)
202 return log_error_errno(size, "Failed to get size of message: %m");
203 if (size == 0)
204 return 0; /* Client disconnects? */
205
206 size = recvmsg_safe(fd, &smsg, 0);
207 if (size == -EINTR)
208 return 0;
209 if (size < 0)
210 return log_error_errno(size, "Failed to receive ctrl message: %m");
211
212 cmsg_close_all(&smsg);
213
214 cmsg = CMSG_FIRSTHDR(&smsg);
215
216 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
217 log_error("No sender credentials received, ignoring message");
218 return 0;
219 }
220
221 cred = (struct ucred *) CMSG_DATA(cmsg);
222
223 if (cred->uid != 0) {
224 log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid);
225 return 0;
226 }
227
228 if (msg_wire.magic != UDEV_CTRL_MAGIC) {
229 log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic);
230 return 0;
231 }
232
233 if (msg_wire.type == _UDEV_CTRL_END_MESSAGES)
234 return 0;
235
236 if (uctrl->callback)
237 (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata);
238
239 /* Do not disconnect and wait for next message. */
240 uctrl = udev_ctrl_unref(uctrl);
241 return 0;
242 }
243
244 static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
245 struct udev_ctrl *uctrl = userdata;
246 _cleanup_close_ int sock = -1;
247 struct ucred ucred;
248 int r;
249
250 assert(uctrl);
251
252 sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
253 if (sock < 0) {
254 if (ERRNO_IS_ACCEPT_AGAIN(errno))
255 return 0;
256
257 return log_error_errno(errno, "Failed to accept ctrl connection: %m");
258 }
259
260 /* check peer credential of connection */
261 r = getpeercred(sock, &ucred);
262 if (r < 0) {
263 log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
264 return 0;
265 }
266
267 if (ucred.uid > 0) {
268 log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid);
269 return 0;
270 }
271
272 /* enable receiving of the sender credentials in the messages */
273 r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true);
274 if (r < 0)
275 log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m");
276
277 r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl);
278 if (r < 0) {
279 log_error_errno(r, "Failed to create event source for udev control connection: %m");
280 return 0;
281 }
282
283 (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection");
284
285 /* Do not accept multiple connection. */
286 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF);
287
288 uctrl->sock_connect = TAKE_FD(sock);
289 return 0;
290 }
291
292 int udev_ctrl_start(struct udev_ctrl *uctrl, udev_ctrl_handler_t callback, void *userdata) {
293 int r;
294
295 assert(uctrl);
296
297 if (!uctrl->event) {
298 r = udev_ctrl_attach_event(uctrl, NULL);
299 if (r < 0)
300 return r;
301 }
302
303 r = udev_ctrl_enable_receiving(uctrl);
304 if (r < 0)
305 return r;
306
307 uctrl->callback = callback;
308 uctrl->userdata = userdata;
309
310 r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl);
311 if (r < 0)
312 return r;
313
314 (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl");
315
316 return 0;
317 }
318
319 int udev_ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf) {
320 struct udev_ctrl_msg_wire ctrl_msg_wire = {
321 .version = "udev-" STRINGIFY(PROJECT_VERSION),
322 .magic = UDEV_CTRL_MAGIC,
323 .type = type,
324 };
325
326 if (uctrl->maybe_disconnected)
327 return -ENOANO; /* to distinguish this from other errors. */
328
329 if (buf)
330 strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), buf);
331 else
332 ctrl_msg_wire.value.intval = intval;
333
334 if (!uctrl->connected) {
335 if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
336 return -errno;
337 uctrl->connected = true;
338 }
339
340 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
341 return -errno;
342
343 if (type == UDEV_CTRL_EXIT)
344 uctrl->maybe_disconnected = true;
345
346 return 0;
347 }
348
349 int udev_ctrl_wait(struct udev_ctrl *uctrl, usec_t timeout) {
350 _cleanup_(sd_event_source_unrefp) sd_event_source *source_io = NULL, *source_timeout = NULL;
351 int r;
352
353 assert(uctrl);
354
355 if (uctrl->sock < 0)
356 return 0;
357 if (!uctrl->connected)
358 return 0;
359
360 if (!uctrl->maybe_disconnected) {
361 r = udev_ctrl_send(uctrl, _UDEV_CTRL_END_MESSAGES, 0, NULL);
362 if (r < 0)
363 return r;
364 }
365
366 if (timeout == 0)
367 return 0;
368
369 if (!uctrl->event) {
370 r = udev_ctrl_attach_event(uctrl, NULL);
371 if (r < 0)
372 return r;
373 }
374
375 r = sd_event_add_io(uctrl->event, &source_io, uctrl->sock, EPOLLIN, NULL, INT_TO_PTR(0));
376 if (r < 0)
377 return r;
378
379 (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io");
380
381 if (timeout != USEC_INFINITY) {
382 r = sd_event_add_time_relative(
383 uctrl->event, &source_timeout, clock_boottime_or_monotonic(),
384 timeout,
385 0, NULL, INT_TO_PTR(-ETIMEDOUT));
386 if (r < 0)
387 return r;
388
389 (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-timeout");
390 }
391
392 return sd_event_loop(uctrl->event);
393 }