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