]>
Commit | Line | Data |
---|---|---|
d9215cd8 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ |
2 | * | |
55e9959b | 3 | * libudev - interface to udev device information |
d59f11e1 | 4 | * |
4061ab9f KS |
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. | |
d59f11e1 KS |
9 | */ |
10 | ||
d59f11e1 | 11 | #include <errno.h> |
cf0fbc49 | 12 | #include <poll.h> |
d59f11e1 | 13 | #include <stddef.h> |
cf0fbc49 | 14 | #include <stdlib.h> |
d59f11e1 | 15 | #include <string.h> |
d59f11e1 KS |
16 | #include <sys/socket.h> |
17 | #include <sys/un.h> | |
cf0fbc49 | 18 | #include <unistd.h> |
d59f11e1 | 19 | |
d02c6f54 YW |
20 | #include "sd-event.h" |
21 | ||
b5efdb8a | 22 | #include "alloc-util.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
f97b34a6 | 24 | #include "format-util.h" |
5cfa2c3d | 25 | #include "io-util.h" |
3ffd4af2 | 26 | #include "socket-util.h" |
7d68eb1b YW |
27 | #include "strxcpyx.h" |
28 | #include "udev-ctrl.h" | |
ef118d00 | 29 | #include "util.h" |
d59f11e1 | 30 | |
540f4669 | 31 | /* wire protocol magic must match */ |
912541b0 | 32 | #define UDEV_CTRL_MAGIC 0xdead1dea |
d59f11e1 | 33 | |
b692a750 | 34 | struct udev_ctrl_msg_wire { |
912541b0 | 35 | char version[16]; |
14cb109d | 36 | unsigned magic; |
912541b0 | 37 | enum udev_ctrl_msg_type type; |
d02c6f54 | 38 | union udev_ctrl_msg_value value; |
d59f11e1 KS |
39 | }; |
40 | ||
41 | struct udev_ctrl { | |
8f71a0d1 | 42 | unsigned n_ref; |
912541b0 | 43 | int sock; |
d02c6f54 | 44 | int sock_connect; |
6421348d | 45 | union sockaddr_union saddr; |
912541b0 | 46 | socklen_t addrlen; |
d02c6f54 YW |
47 | bool bound:1; |
48 | bool cleanup_socket:1; | |
49 | bool connected:1; | |
78467aeb | 50 | bool maybe_disconnected:1; |
d02c6f54 YW |
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; | |
d59f11e1 KS |
56 | }; |
57 | ||
100bc5bf YW |
58 | int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) { |
59 | _cleanup_close_ int sock = -1; | |
912541b0 | 60 | struct udev_ctrl *uctrl; |
4bbdff75 | 61 | int r; |
912541b0 | 62 | |
100bc5bf | 63 | assert(ret); |
912541b0 KS |
64 | |
65 | if (fd < 0) { | |
100bc5bf YW |
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"); | |
912541b0 | 69 | } |
25568304 | 70 | |
100bc5bf YW |
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 | ||
25568304 KS |
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 | */ | |
2ff48e98 | 85 | r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 86 | if (r < 0) |
572909a3 | 87 | log_warning_errno(r, "Failed to set SO_PASSCRED: %m"); |
912541b0 | 88 | |
44ed5214 LP |
89 | uctrl->saddr.un = (struct sockaddr_un) { |
90 | .sun_family = AF_UNIX, | |
91 | .sun_path = "/run/udev/control", | |
92 | }; | |
93 | ||
fc2fffe7 | 94 | uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un); |
fc1de713 | 95 | |
100bc5bf YW |
96 | *ret = TAKE_PTR(uctrl); |
97 | return 0; | |
d59f11e1 KS |
98 | } |
99 | ||
9ec6e95b | 100 | int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { |
53bba2fb | 101 | int r; |
912541b0 | 102 | |
53bba2fb | 103 | assert(uctrl); |
912541b0 | 104 | |
53bba2fb YW |
105 | if (uctrl->bound) |
106 | return 0; | |
912541b0 | 107 | |
53bba2fb YW |
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); | |
912541b0 | 112 | } |
53bba2fb YW |
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 | ||
912541b0 | 123 | return 0; |
d59f11e1 KS |
124 | } |
125 | ||
d02c6f54 YW |
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 | ||
8f71a0d1 YW |
134 | static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) { |
135 | assert(uctrl); | |
35927d13 | 136 | |
d02c6f54 YW |
137 | udev_ctrl_disconnect(uctrl); |
138 | ||
139 | sd_event_source_unref(uctrl->event_source); | |
8f71a0d1 | 140 | safe_close(uctrl->sock); |
d02c6f54 YW |
141 | |
142 | sd_event_unref(uctrl->event); | |
8f71a0d1 | 143 | return mfree(uctrl); |
d59f11e1 KS |
144 | } |
145 | ||
d02c6f54 | 146 | DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free); |
d59f11e1 | 147 | |
9ec6e95b | 148 | int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { |
9315f853 | 149 | if (!uctrl) |
912541b0 KS |
150 | return 0; |
151 | if (uctrl->cleanup_socket) | |
155b6876 | 152 | sockaddr_un_unlink(&uctrl->saddr.un); |
912541b0 | 153 | return 0; |
1f5a5100 KS |
154 | } |
155 | ||
d02c6f54 YW |
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; | |
d59f11e1 KS |
171 | } |
172 | ||
d02c6f54 YW |
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 | ||
78467aeb YW |
243 | if (msg_wire.type == _UDEV_CTRL_END_MESSAGES) |
244 | return 0; | |
245 | ||
d02c6f54 YW |
246 | if (uctrl->callback) |
247 | (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata); | |
248 | ||
78467aeb YW |
249 | /* Do not disconnect and wait for next message. */ |
250 | uctrl = udev_ctrl_unref(uctrl); | |
d02c6f54 YW |
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; | |
eff05270 | 258 | int r; |
912541b0 | 259 | |
d02c6f54 | 260 | assert(uctrl); |
912541b0 | 261 | |
d02c6f54 YW |
262 | sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); |
263 | if (sock < 0) { | |
912541b0 | 264 | if (errno != EINTR) |
d02c6f54 YW |
265 | log_error_errno(errno, "Failed to accept ctrl connection: %m"); |
266 | return 0; | |
912541b0 KS |
267 | } |
268 | ||
269 | /* check peer credential of connection */ | |
d02c6f54 | 270 | r = getpeercred(sock, &ucred); |
eff05270 | 271 | if (r < 0) { |
572909a3 | 272 | log_error_errno(r, "Failed to receive credentials of ctrl connection: %m"); |
d02c6f54 | 273 | return 0; |
912541b0 | 274 | } |
d02c6f54 | 275 | |
912541b0 | 276 | if (ucred.uid > 0) { |
d02c6f54 YW |
277 | log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid); |
278 | return 0; | |
912541b0 KS |
279 | } |
280 | ||
281 | /* enable receiving of the sender credentials in the messages */ | |
d02c6f54 | 282 | r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 283 | if (r < 0) |
d02c6f54 | 284 | log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m"); |
4bbdff75 | 285 | |
d02c6f54 YW |
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"); | |
ff2c503d | 293 | |
d02c6f54 YW |
294 | /* Do not accept multiple connection. */ |
295 | (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF); | |
35927d13 | 296 | |
d02c6f54 YW |
297 | uctrl->sock_connect = TAKE_FD(sock); |
298 | return 0; | |
ff2c503d KS |
299 | } |
300 | ||
d02c6f54 YW |
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 | } | |
8f71a0d1 | 327 | |
78467aeb | 328 | int udev_ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf) { |
b9da6a09 ZJS |
329 | struct udev_ctrl_msg_wire ctrl_msg_wire = { |
330 | .version = "udev-" STRINGIFY(PROJECT_VERSION), | |
331 | .magic = UDEV_CTRL_MAGIC, | |
332 | .type = type, | |
333 | }; | |
912541b0 | 334 | |
78467aeb YW |
335 | if (uctrl->maybe_disconnected) |
336 | return -ENOANO; /* to distinguish this from other errors. */ | |
337 | ||
9315f853 | 338 | if (buf) |
d02c6f54 | 339 | strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), buf); |
912541b0 | 340 | else |
d02c6f54 | 341 | ctrl_msg_wire.value.intval = intval; |
912541b0 KS |
342 | |
343 | if (!uctrl->connected) { | |
b9da6a09 ZJS |
344 | if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) |
345 | return -errno; | |
912541b0 KS |
346 | uctrl->connected = true; |
347 | } | |
78467aeb | 348 | |
b9da6a09 ZJS |
349 | if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) |
350 | return -errno; | |
912541b0 | 351 | |
78467aeb YW |
352 | if (type == UDEV_CTRL_EXIT) |
353 | uctrl->maybe_disconnected = true; | |
d59f11e1 | 354 | |
78467aeb | 355 | return 0; |
ff2c503d KS |
356 | } |
357 | ||
78467aeb YW |
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); | |
d59f11e1 KS |
360 | } |
361 | ||
78467aeb YW |
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; | |
d59f11e1 | 365 | |
78467aeb | 366 | assert(uctrl); |
d59f11e1 | 367 | |
78467aeb YW |
368 | if (uctrl->sock < 0) |
369 | return 0; | |
370 | if (!uctrl->connected) | |
371 | return 0; | |
d59f11e1 | 372 | |
78467aeb YW |
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 | } | |
d59f11e1 | 378 | |
78467aeb YW |
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 | } | |
d59f11e1 | 404 | |
78467aeb | 405 | return sd_event_loop(uctrl->event); |
bb38678e | 406 | } |