]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
udev-ctrl: use sd_event and introduce udev_ctrl_start()
[thirdparty/systemd.git] / src / udev / udev-ctrl.c
1 /* SPDX-License-Identifier: LGPL-2.1+
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/socket.h>
17 #include <sys/un.h>
18 #include <unistd.h>
19
20 #include "sd-event.h"
21
22 #include "alloc-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:1;
48 bool cleanup_socket:1;
49 bool connected:1;
50 sd_event *event;
51 sd_event_source *event_source;
52 sd_event_source *event_source_connect;
53 udev_ctrl_handler_t callback;
54 void *userdata;
55 };
56
57 int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) {
58 _cleanup_close_ int sock = -1;
59 struct udev_ctrl *uctrl;
60 int r;
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 .bound = fd >= 0,
78 };
79
80 /*
81 * FIXME: remove it as soon as we can depend on this:
82 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
83 */
84 r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true);
85 if (r < 0)
86 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
87
88 uctrl->saddr.un = (struct sockaddr_un) {
89 .sun_family = AF_UNIX,
90 .sun_path = "/run/udev/control",
91 };
92
93 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
94
95 *ret = TAKE_PTR(uctrl);
96 return 0;
97 }
98
99 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
100 int r;
101
102 assert(uctrl);
103
104 if (uctrl->bound)
105 return 0;
106
107 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
108 if (r < 0 && errno == EADDRINUSE) {
109 (void) sockaddr_un_unlink(&uctrl->saddr.un);
110 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
111 }
112
113 if (r < 0)
114 return log_error_errno(errno, "Failed to bind udev control socket: %m");
115
116 if (listen(uctrl->sock, 0) < 0)
117 return log_error_errno(errno, "Failed to listen udev control socket: %m");
118
119 uctrl->bound = true;
120 uctrl->cleanup_socket = true;
121
122 return 0;
123 }
124
125 static void udev_ctrl_disconnect(struct udev_ctrl *uctrl) {
126 if (!uctrl)
127 return;
128
129 uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect);
130 uctrl->sock_connect = safe_close(uctrl->sock_connect);
131 }
132
133 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
134 assert(uctrl);
135
136 udev_ctrl_disconnect(uctrl);
137
138 sd_event_source_unref(uctrl->event_source);
139 safe_close(uctrl->sock);
140
141 sd_event_unref(uctrl->event);
142 return mfree(uctrl);
143 }
144
145 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
146
147 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
148 if (!uctrl)
149 return 0;
150 if (uctrl->cleanup_socket)
151 sockaddr_un_unlink(&uctrl->saddr.un);
152 return 0;
153 }
154
155 int udev_ctrl_attach_event(struct udev_ctrl *uctrl, sd_event *event) {
156 int r;
157
158 assert_return(uctrl, -EINVAL);
159 assert_return(!uctrl->event, -EBUSY);
160
161 if (event)
162 uctrl->event = sd_event_ref(event);
163 else {
164 r = sd_event_default(&uctrl->event);
165 if (r < 0)
166 return r;
167 }
168
169 return 0;
170 }
171
172 sd_event_source *udev_ctrl_get_event_source(struct udev_ctrl *uctrl) {
173 assert(uctrl);
174
175 return uctrl->event_source;
176 }
177
178 static void udev_ctrl_disconnect_and_listen_again(struct udev_ctrl *uctrl) {
179 udev_ctrl_disconnect(uctrl);
180 udev_ctrl_unref(uctrl);
181 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON);
182 }
183
184 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl *, udev_ctrl_disconnect_and_listen_again);
185
186 static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
187 _cleanup_(udev_ctrl_disconnect_and_listen_againp) struct udev_ctrl *uctrl = NULL;
188 struct udev_ctrl_msg_wire msg_wire;
189 struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(struct udev_ctrl_msg_wire));
190 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
191 struct msghdr smsg = {
192 .msg_iov = &iov,
193 .msg_iovlen = 1,
194 .msg_control = cred_msg,
195 .msg_controllen = sizeof(cred_msg),
196 };
197 struct cmsghdr *cmsg;
198 struct ucred *cred;
199 ssize_t size;
200
201 assert(userdata);
202
203 /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object.
204 * To avoid the object freed, let's increment the refcount. */
205 uctrl = udev_ctrl_ref(userdata);
206
207 size = next_datagram_size_fd(fd);
208 if (size < 0)
209 return log_error_errno(size, "Failed to get size of message: %m");
210 if (size == 0)
211 return 0; /* Client disconnects? */
212
213 size = recvmsg(fd, &smsg, 0);
214 if (size < 0) {
215 if (errno != EINTR)
216 return log_error_errno(errno, "Failed to receive ctrl message: %m");
217
218 return 0;
219 }
220
221 cmsg_close_all(&smsg);
222
223 cmsg = CMSG_FIRSTHDR(&smsg);
224
225 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
226 log_error("No sender credentials received, ignoring message");
227 return 0;
228 }
229
230 cred = (struct ucred *) CMSG_DATA(cmsg);
231
232 if (cred->uid != 0) {
233 log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid);
234 return 0;
235 }
236
237 if (msg_wire.magic != UDEV_CTRL_MAGIC) {
238 log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic);
239 return 0;
240 }
241
242 if (uctrl->callback)
243 (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata);
244
245 return 0;
246 }
247
248 static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
249 struct udev_ctrl *uctrl = userdata;
250 _cleanup_close_ int sock = -1;
251 struct ucred ucred;
252 int r;
253
254 assert(uctrl);
255
256 sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
257 if (sock < 0) {
258 if (errno != EINTR)
259 log_error_errno(errno, "Failed to accept ctrl connection: %m");
260 return 0;
261 }
262
263 /* check peer credential of connection */
264 r = getpeercred(sock, &ucred);
265 if (r < 0) {
266 log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
267 return 0;
268 }
269
270 if (ucred.uid > 0) {
271 log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid);
272 return 0;
273 }
274
275 /* enable receiving of the sender credentials in the messages */
276 r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true);
277 if (r < 0)
278 log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m");
279
280 r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl);
281 if (r < 0) {
282 log_error_errno(r, "Failed to create event source for udev control connection: %m");
283 return 0;
284 }
285
286 (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection");
287
288 /* Do not accept multiple connection. */
289 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF);
290
291 uctrl->sock_connect = TAKE_FD(sock);
292 return 0;
293 }
294
295 int udev_ctrl_start(struct udev_ctrl *uctrl, udev_ctrl_handler_t callback, void *userdata) {
296 int r;
297
298 assert(uctrl);
299
300 if (!uctrl->event) {
301 r = udev_ctrl_attach_event(uctrl, NULL);
302 if (r < 0)
303 return r;
304 }
305
306 r = udev_ctrl_enable_receiving(uctrl);
307 if (r < 0)
308 return r;
309
310 uctrl->callback = callback;
311 uctrl->userdata = userdata;
312
313 r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl);
314 if (r < 0)
315 return r;
316
317 (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl");
318
319 return 0;
320 }
321
322 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, usec_t timeout) {
323 struct udev_ctrl_msg_wire ctrl_msg_wire = {
324 .version = "udev-" STRINGIFY(PROJECT_VERSION),
325 .magic = UDEV_CTRL_MAGIC,
326 .type = type,
327 };
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 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
340 return -errno;
341
342 /* wait for peer message handling or disconnect */
343 for (;;) {
344 struct pollfd pfd = {
345 .fd = uctrl->sock,
346 .events = POLLIN,
347 };
348 int r;
349
350 r = poll(&pfd, 1, DIV_ROUND_UP(timeout, USEC_PER_MSEC));
351 if (r < 0) {
352 if (errno == EINTR)
353 continue;
354 return -errno;
355 }
356 if (r == 0)
357 return -ETIMEDOUT;
358 if (pfd.revents & POLLERR)
359 return -EIO;
360 return 0;
361 }
362 }
363
364 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, usec_t timeout) {
365 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
366 }
367
368 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) {
369 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
370 }
371
372 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) {
373 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
374 }
375
376 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, usec_t timeout) {
377 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
378 }
379
380 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, usec_t timeout) {
381 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
382 }
383
384 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, usec_t timeout) {
385 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
386 }
387
388 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, usec_t timeout) {
389 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
390 }
391
392 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, usec_t timeout) {
393 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
394 }