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