]>
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 | 16 | #include <sys/un.h> |
cf0fbc49 | 17 | #include <unistd.h> |
d59f11e1 | 18 | |
d02c6f54 YW |
19 | #include "sd-event.h" |
20 | ||
b5efdb8a | 21 | #include "alloc-util.h" |
4ff9bc2e | 22 | #include "errno-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), | |
2f5b282a | 78 | .sock_connect = -1, |
100bc5bf YW |
79 | .bound = fd >= 0, |
80 | }; | |
81 | ||
25568304 KS |
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 | */ | |
2ff48e98 | 86 | r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 87 | if (r < 0) |
572909a3 | 88 | log_warning_errno(r, "Failed to set SO_PASSCRED: %m"); |
912541b0 | 89 | |
44ed5214 LP |
90 | uctrl->saddr.un = (struct sockaddr_un) { |
91 | .sun_family = AF_UNIX, | |
92 | .sun_path = "/run/udev/control", | |
93 | }; | |
94 | ||
fc2fffe7 | 95 | uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un); |
fc1de713 | 96 | |
100bc5bf YW |
97 | *ret = TAKE_PTR(uctrl); |
98 | return 0; | |
d59f11e1 KS |
99 | } |
100 | ||
9ec6e95b | 101 | int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { |
53bba2fb | 102 | int r; |
912541b0 | 103 | |
53bba2fb | 104 | assert(uctrl); |
912541b0 | 105 | |
53bba2fb YW |
106 | if (uctrl->bound) |
107 | return 0; | |
912541b0 | 108 | |
53bba2fb YW |
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); | |
912541b0 | 113 | } |
53bba2fb YW |
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 | ||
912541b0 | 124 | return 0; |
d59f11e1 KS |
125 | } |
126 | ||
d02c6f54 YW |
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 | ||
8f71a0d1 YW |
135 | static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) { |
136 | assert(uctrl); | |
35927d13 | 137 | |
d02c6f54 YW |
138 | udev_ctrl_disconnect(uctrl); |
139 | ||
140 | sd_event_source_unref(uctrl->event_source); | |
8f71a0d1 | 141 | safe_close(uctrl->sock); |
d02c6f54 YW |
142 | |
143 | sd_event_unref(uctrl->event); | |
8f71a0d1 | 144 | return mfree(uctrl); |
d59f11e1 KS |
145 | } |
146 | ||
d02c6f54 | 147 | DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free); |
d59f11e1 | 148 | |
9ec6e95b | 149 | int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { |
9315f853 | 150 | if (!uctrl) |
912541b0 KS |
151 | return 0; |
152 | if (uctrl->cleanup_socket) | |
155b6876 | 153 | sockaddr_un_unlink(&uctrl->saddr.un); |
912541b0 | 154 | return 0; |
1f5a5100 KS |
155 | } |
156 | ||
d02c6f54 YW |
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; | |
d59f11e1 KS |
172 | } |
173 | ||
d02c6f54 YW |
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)); | |
fb29cdbe | 192 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
d02c6f54 YW |
193 | struct msghdr smsg = { |
194 | .msg_iov = &iov, | |
195 | .msg_iovlen = 1, | |
fb29cdbe LP |
196 | .msg_control = &control, |
197 | .msg_controllen = sizeof(control), | |
d02c6f54 YW |
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 | ||
3691bcf3 LP |
215 | size = recvmsg_safe(fd, &smsg, 0); |
216 | if (size == -EINTR) | |
d02c6f54 | 217 | return 0; |
3691bcf3 LP |
218 | if (size < 0) |
219 | return log_error_errno(size, "Failed to receive ctrl message: %m"); | |
d02c6f54 YW |
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 | ||
78467aeb YW |
242 | if (msg_wire.type == _UDEV_CTRL_END_MESSAGES) |
243 | return 0; | |
244 | ||
d02c6f54 YW |
245 | if (uctrl->callback) |
246 | (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata); | |
247 | ||
78467aeb YW |
248 | /* Do not disconnect and wait for next message. */ |
249 | uctrl = udev_ctrl_unref(uctrl); | |
d02c6f54 YW |
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; | |
eff05270 | 257 | int r; |
912541b0 | 258 | |
d02c6f54 | 259 | assert(uctrl); |
912541b0 | 260 | |
d02c6f54 YW |
261 | sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); |
262 | if (sock < 0) { | |
4ff9bc2e LP |
263 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) |
264 | return 0; | |
265 | ||
266 | return log_error_errno(errno, "Failed to accept ctrl connection: %m"); | |
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 | ||
60ccab09 | 392 | (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io"); |
78467aeb YW |
393 | |
394 | if (timeout != USEC_INFINITY) { | |
f6e8ba81 YW |
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)); | |
78467aeb YW |
398 | if (r < 0) |
399 | return r; | |
400 | ||
f6e8ba81 | 401 | (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-timeout"); |
78467aeb | 402 | } |
d59f11e1 | 403 | |
78467aeb | 404 | return sd_event_loop(uctrl->event); |
bb38678e | 405 | } |