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