]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
Merge pull request #11556 from yuwata/udev-ctrl-use-sd-event
[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 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 .bound = fd >= 0,
79 };
80
81 /*
82 * FIXME: remove it as soon as we can depend on this:
83 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
84 */
85 r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true);
86 if (r < 0)
87 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
88
89 uctrl->saddr.un = (struct sockaddr_un) {
90 .sun_family = AF_UNIX,
91 .sun_path = "/run/udev/control",
92 };
93
94 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
95
96 *ret = TAKE_PTR(uctrl);
97 return 0;
98 }
99
100 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
101 int r;
102
103 assert(uctrl);
104
105 if (uctrl->bound)
106 return 0;
107
108 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
109 if (r < 0 && errno == EADDRINUSE) {
110 (void) sockaddr_un_unlink(&uctrl->saddr.un);
111 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
112 }
113
114 if (r < 0)
115 return log_error_errno(errno, "Failed to bind udev control socket: %m");
116
117 if (listen(uctrl->sock, 0) < 0)
118 return log_error_errno(errno, "Failed to listen udev control socket: %m");
119
120 uctrl->bound = true;
121 uctrl->cleanup_socket = true;
122
123 return 0;
124 }
125
126 static void udev_ctrl_disconnect(struct udev_ctrl *uctrl) {
127 if (!uctrl)
128 return;
129
130 uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect);
131 uctrl->sock_connect = safe_close(uctrl->sock_connect);
132 }
133
134 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
135 assert(uctrl);
136
137 udev_ctrl_disconnect(uctrl);
138
139 sd_event_source_unref(uctrl->event_source);
140 safe_close(uctrl->sock);
141
142 sd_event_unref(uctrl->event);
143 return mfree(uctrl);
144 }
145
146 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
147
148 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
149 if (!uctrl)
150 return 0;
151 if (uctrl->cleanup_socket)
152 sockaddr_un_unlink(&uctrl->saddr.un);
153 return 0;
154 }
155
156 int udev_ctrl_attach_event(struct udev_ctrl *uctrl, sd_event *event) {
157 int r;
158
159 assert_return(uctrl, -EINVAL);
160 assert_return(!uctrl->event, -EBUSY);
161
162 if (event)
163 uctrl->event = sd_event_ref(event);
164 else {
165 r = sd_event_default(&uctrl->event);
166 if (r < 0)
167 return r;
168 }
169
170 return 0;
171 }
172
173 sd_event_source *udev_ctrl_get_event_source(struct udev_ctrl *uctrl) {
174 assert(uctrl);
175
176 return uctrl->event_source;
177 }
178
179 static void udev_ctrl_disconnect_and_listen_again(struct udev_ctrl *uctrl) {
180 udev_ctrl_disconnect(uctrl);
181 udev_ctrl_unref(uctrl);
182 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON);
183 }
184
185 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl *, udev_ctrl_disconnect_and_listen_again);
186
187 static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
188 _cleanup_(udev_ctrl_disconnect_and_listen_againp) struct udev_ctrl *uctrl = NULL;
189 struct udev_ctrl_msg_wire msg_wire;
190 struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(struct udev_ctrl_msg_wire));
191 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
192 struct msghdr smsg = {
193 .msg_iov = &iov,
194 .msg_iovlen = 1,
195 .msg_control = cred_msg,
196 .msg_controllen = sizeof(cred_msg),
197 };
198 struct cmsghdr *cmsg;
199 struct ucred *cred;
200 ssize_t size;
201
202 assert(userdata);
203
204 /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object.
205 * To avoid the object freed, let's increment the refcount. */
206 uctrl = udev_ctrl_ref(userdata);
207
208 size = next_datagram_size_fd(fd);
209 if (size < 0)
210 return log_error_errno(size, "Failed to get size of message: %m");
211 if (size == 0)
212 return 0; /* Client disconnects? */
213
214 size = recvmsg(fd, &smsg, 0);
215 if (size < 0) {
216 if (errno != EINTR)
217 return log_error_errno(errno, "Failed to receive ctrl message: %m");
218
219 return 0;
220 }
221
222 cmsg_close_all(&smsg);
223
224 cmsg = CMSG_FIRSTHDR(&smsg);
225
226 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
227 log_error("No sender credentials received, ignoring message");
228 return 0;
229 }
230
231 cred = (struct ucred *) CMSG_DATA(cmsg);
232
233 if (cred->uid != 0) {
234 log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid);
235 return 0;
236 }
237
238 if (msg_wire.magic != UDEV_CTRL_MAGIC) {
239 log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic);
240 return 0;
241 }
242
243 if (msg_wire.type == _UDEV_CTRL_END_MESSAGES)
244 return 0;
245
246 if (uctrl->callback)
247 (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata);
248
249 /* Do not disconnect and wait for next message. */
250 uctrl = udev_ctrl_unref(uctrl);
251 return 0;
252 }
253
254 static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
255 struct udev_ctrl *uctrl = userdata;
256 _cleanup_close_ int sock = -1;
257 struct ucred ucred;
258 int r;
259
260 assert(uctrl);
261
262 sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
263 if (sock < 0) {
264 if (errno != EINTR)
265 log_error_errno(errno, "Failed to accept ctrl connection: %m");
266 return 0;
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(uctrl->event_source, "udev-ctrl-wait-io");
393
394 if (timeout != USEC_INFINITY) {
395 usec_t usec;
396
397 usec = now(clock_boottime_or_monotonic()) + timeout;
398 r = sd_event_add_time(uctrl->event, &source_timeout, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT));
399 if (r < 0)
400 return r;
401
402 (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-io");
403 }
404
405 return sd_event_loop(uctrl->event);
406 }