]>
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" |
4ff9bc2e | 23 | #include "errno-util.h" |
3ffd4af2 | 24 | #include "fd-util.h" |
f97b34a6 | 25 | #include "format-util.h" |
5cfa2c3d | 26 | #include "io-util.h" |
3ffd4af2 | 27 | #include "socket-util.h" |
7d68eb1b YW |
28 | #include "strxcpyx.h" |
29 | #include "udev-ctrl.h" | |
ef118d00 | 30 | #include "util.h" |
d59f11e1 | 31 | |
540f4669 | 32 | /* wire protocol magic must match */ |
912541b0 | 33 | #define UDEV_CTRL_MAGIC 0xdead1dea |
d59f11e1 | 34 | |
b692a750 | 35 | struct udev_ctrl_msg_wire { |
912541b0 | 36 | char version[16]; |
14cb109d | 37 | unsigned magic; |
912541b0 | 38 | enum udev_ctrl_msg_type type; |
d02c6f54 | 39 | union udev_ctrl_msg_value value; |
d59f11e1 KS |
40 | }; |
41 | ||
42 | struct udev_ctrl { | |
8f71a0d1 | 43 | unsigned n_ref; |
912541b0 | 44 | int sock; |
d02c6f54 | 45 | int sock_connect; |
6421348d | 46 | union sockaddr_union saddr; |
912541b0 | 47 | socklen_t addrlen; |
d02c6f54 YW |
48 | bool bound:1; |
49 | bool cleanup_socket:1; | |
50 | bool connected:1; | |
78467aeb | 51 | bool maybe_disconnected:1; |
d02c6f54 YW |
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; | |
d59f11e1 KS |
57 | }; |
58 | ||
100bc5bf YW |
59 | int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) { |
60 | _cleanup_close_ int sock = -1; | |
912541b0 | 61 | struct udev_ctrl *uctrl; |
4bbdff75 | 62 | int r; |
912541b0 | 63 | |
100bc5bf | 64 | assert(ret); |
912541b0 KS |
65 | |
66 | if (fd < 0) { | |
100bc5bf YW |
67 | sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); |
68 | if (sock < 0) | |
69 | return log_error_errno(errno, "Failed to create socket: %m"); | |
912541b0 | 70 | } |
25568304 | 71 | |
100bc5bf YW |
72 | uctrl = new(struct udev_ctrl, 1); |
73 | if (!uctrl) | |
74 | return -ENOMEM; | |
75 | ||
76 | *uctrl = (struct udev_ctrl) { | |
77 | .n_ref = 1, | |
78 | .sock = fd >= 0 ? fd : TAKE_FD(sock), | |
2f5b282a | 79 | .sock_connect = -1, |
100bc5bf YW |
80 | .bound = fd >= 0, |
81 | }; | |
82 | ||
25568304 KS |
83 | /* |
84 | * FIXME: remove it as soon as we can depend on this: | |
85 | * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949 | |
86 | */ | |
2ff48e98 | 87 | r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 88 | if (r < 0) |
572909a3 | 89 | log_warning_errno(r, "Failed to set SO_PASSCRED: %m"); |
912541b0 | 90 | |
44ed5214 LP |
91 | uctrl->saddr.un = (struct sockaddr_un) { |
92 | .sun_family = AF_UNIX, | |
93 | .sun_path = "/run/udev/control", | |
94 | }; | |
95 | ||
fc2fffe7 | 96 | uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un); |
fc1de713 | 97 | |
100bc5bf YW |
98 | *ret = TAKE_PTR(uctrl); |
99 | return 0; | |
d59f11e1 KS |
100 | } |
101 | ||
9ec6e95b | 102 | int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { |
53bba2fb | 103 | int r; |
912541b0 | 104 | |
53bba2fb | 105 | assert(uctrl); |
912541b0 | 106 | |
53bba2fb YW |
107 | if (uctrl->bound) |
108 | return 0; | |
912541b0 | 109 | |
53bba2fb YW |
110 | r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); |
111 | if (r < 0 && errno == EADDRINUSE) { | |
112 | (void) sockaddr_un_unlink(&uctrl->saddr.un); | |
113 | r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); | |
912541b0 | 114 | } |
53bba2fb YW |
115 | |
116 | if (r < 0) | |
117 | return log_error_errno(errno, "Failed to bind udev control socket: %m"); | |
118 | ||
119 | if (listen(uctrl->sock, 0) < 0) | |
120 | return log_error_errno(errno, "Failed to listen udev control socket: %m"); | |
121 | ||
122 | uctrl->bound = true; | |
123 | uctrl->cleanup_socket = true; | |
124 | ||
912541b0 | 125 | return 0; |
d59f11e1 KS |
126 | } |
127 | ||
d02c6f54 YW |
128 | static void udev_ctrl_disconnect(struct udev_ctrl *uctrl) { |
129 | if (!uctrl) | |
130 | return; | |
131 | ||
132 | uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect); | |
133 | uctrl->sock_connect = safe_close(uctrl->sock_connect); | |
134 | } | |
135 | ||
8f71a0d1 YW |
136 | static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) { |
137 | assert(uctrl); | |
35927d13 | 138 | |
d02c6f54 YW |
139 | udev_ctrl_disconnect(uctrl); |
140 | ||
141 | sd_event_source_unref(uctrl->event_source); | |
8f71a0d1 | 142 | safe_close(uctrl->sock); |
d02c6f54 YW |
143 | |
144 | sd_event_unref(uctrl->event); | |
8f71a0d1 | 145 | return mfree(uctrl); |
d59f11e1 KS |
146 | } |
147 | ||
d02c6f54 | 148 | DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free); |
d59f11e1 | 149 | |
9ec6e95b | 150 | int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { |
9315f853 | 151 | if (!uctrl) |
912541b0 KS |
152 | return 0; |
153 | if (uctrl->cleanup_socket) | |
155b6876 | 154 | sockaddr_un_unlink(&uctrl->saddr.un); |
912541b0 | 155 | return 0; |
1f5a5100 KS |
156 | } |
157 | ||
d02c6f54 YW |
158 | int udev_ctrl_attach_event(struct udev_ctrl *uctrl, sd_event *event) { |
159 | int r; | |
160 | ||
161 | assert_return(uctrl, -EINVAL); | |
162 | assert_return(!uctrl->event, -EBUSY); | |
163 | ||
164 | if (event) | |
165 | uctrl->event = sd_event_ref(event); | |
166 | else { | |
167 | r = sd_event_default(&uctrl->event); | |
168 | if (r < 0) | |
169 | return r; | |
170 | } | |
171 | ||
172 | return 0; | |
d59f11e1 KS |
173 | } |
174 | ||
d02c6f54 YW |
175 | sd_event_source *udev_ctrl_get_event_source(struct udev_ctrl *uctrl) { |
176 | assert(uctrl); | |
177 | ||
178 | return uctrl->event_source; | |
179 | } | |
180 | ||
181 | static void udev_ctrl_disconnect_and_listen_again(struct udev_ctrl *uctrl) { | |
182 | udev_ctrl_disconnect(uctrl); | |
183 | udev_ctrl_unref(uctrl); | |
184 | (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON); | |
185 | } | |
186 | ||
187 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl *, udev_ctrl_disconnect_and_listen_again); | |
188 | ||
189 | static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
190 | _cleanup_(udev_ctrl_disconnect_and_listen_againp) struct udev_ctrl *uctrl = NULL; | |
191 | struct udev_ctrl_msg_wire msg_wire; | |
192 | struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(struct udev_ctrl_msg_wire)); | |
193 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; | |
194 | struct msghdr smsg = { | |
195 | .msg_iov = &iov, | |
196 | .msg_iovlen = 1, | |
197 | .msg_control = cred_msg, | |
198 | .msg_controllen = sizeof(cred_msg), | |
199 | }; | |
200 | struct cmsghdr *cmsg; | |
201 | struct ucred *cred; | |
202 | ssize_t size; | |
203 | ||
204 | assert(userdata); | |
205 | ||
206 | /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object. | |
207 | * To avoid the object freed, let's increment the refcount. */ | |
208 | uctrl = udev_ctrl_ref(userdata); | |
209 | ||
210 | size = next_datagram_size_fd(fd); | |
211 | if (size < 0) | |
212 | return log_error_errno(size, "Failed to get size of message: %m"); | |
213 | if (size == 0) | |
214 | return 0; /* Client disconnects? */ | |
215 | ||
216 | size = recvmsg(fd, &smsg, 0); | |
217 | if (size < 0) { | |
218 | if (errno != EINTR) | |
219 | return log_error_errno(errno, "Failed to receive ctrl message: %m"); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | cmsg_close_all(&smsg); | |
225 | ||
226 | cmsg = CMSG_FIRSTHDR(&smsg); | |
227 | ||
228 | if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
229 | log_error("No sender credentials received, ignoring message"); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | cred = (struct ucred *) CMSG_DATA(cmsg); | |
234 | ||
235 | if (cred->uid != 0) { | |
236 | log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | if (msg_wire.magic != UDEV_CTRL_MAGIC) { | |
241 | log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic); | |
242 | return 0; | |
243 | } | |
244 | ||
78467aeb YW |
245 | if (msg_wire.type == _UDEV_CTRL_END_MESSAGES) |
246 | return 0; | |
247 | ||
d02c6f54 YW |
248 | if (uctrl->callback) |
249 | (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata); | |
250 | ||
78467aeb YW |
251 | /* Do not disconnect and wait for next message. */ |
252 | uctrl = udev_ctrl_unref(uctrl); | |
d02c6f54 YW |
253 | return 0; |
254 | } | |
255 | ||
256 | static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
257 | struct udev_ctrl *uctrl = userdata; | |
258 | _cleanup_close_ int sock = -1; | |
259 | struct ucred ucred; | |
eff05270 | 260 | int r; |
912541b0 | 261 | |
d02c6f54 | 262 | assert(uctrl); |
912541b0 | 263 | |
d02c6f54 YW |
264 | sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); |
265 | if (sock < 0) { | |
4ff9bc2e LP |
266 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) |
267 | return 0; | |
268 | ||
269 | return log_error_errno(errno, "Failed to accept ctrl connection: %m"); | |
912541b0 KS |
270 | } |
271 | ||
272 | /* check peer credential of connection */ | |
d02c6f54 | 273 | r = getpeercred(sock, &ucred); |
eff05270 | 274 | if (r < 0) { |
572909a3 | 275 | log_error_errno(r, "Failed to receive credentials of ctrl connection: %m"); |
d02c6f54 | 276 | return 0; |
912541b0 | 277 | } |
d02c6f54 | 278 | |
912541b0 | 279 | if (ucred.uid > 0) { |
d02c6f54 YW |
280 | log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid); |
281 | return 0; | |
912541b0 KS |
282 | } |
283 | ||
284 | /* enable receiving of the sender credentials in the messages */ | |
d02c6f54 | 285 | r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 286 | if (r < 0) |
d02c6f54 | 287 | log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m"); |
4bbdff75 | 288 | |
d02c6f54 YW |
289 | r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl); |
290 | if (r < 0) { | |
291 | log_error_errno(r, "Failed to create event source for udev control connection: %m"); | |
292 | return 0; | |
293 | } | |
294 | ||
295 | (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection"); | |
ff2c503d | 296 | |
d02c6f54 YW |
297 | /* Do not accept multiple connection. */ |
298 | (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF); | |
35927d13 | 299 | |
d02c6f54 YW |
300 | uctrl->sock_connect = TAKE_FD(sock); |
301 | return 0; | |
ff2c503d KS |
302 | } |
303 | ||
d02c6f54 YW |
304 | int udev_ctrl_start(struct udev_ctrl *uctrl, udev_ctrl_handler_t callback, void *userdata) { |
305 | int r; | |
306 | ||
307 | assert(uctrl); | |
308 | ||
309 | if (!uctrl->event) { | |
310 | r = udev_ctrl_attach_event(uctrl, NULL); | |
311 | if (r < 0) | |
312 | return r; | |
313 | } | |
314 | ||
315 | r = udev_ctrl_enable_receiving(uctrl); | |
316 | if (r < 0) | |
317 | return r; | |
318 | ||
319 | uctrl->callback = callback; | |
320 | uctrl->userdata = userdata; | |
321 | ||
322 | r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl); | |
323 | if (r < 0) | |
324 | return r; | |
325 | ||
326 | (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl"); | |
327 | ||
328 | return 0; | |
329 | } | |
8f71a0d1 | 330 | |
78467aeb | 331 | int udev_ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf) { |
b9da6a09 ZJS |
332 | struct udev_ctrl_msg_wire ctrl_msg_wire = { |
333 | .version = "udev-" STRINGIFY(PROJECT_VERSION), | |
334 | .magic = UDEV_CTRL_MAGIC, | |
335 | .type = type, | |
336 | }; | |
912541b0 | 337 | |
78467aeb YW |
338 | if (uctrl->maybe_disconnected) |
339 | return -ENOANO; /* to distinguish this from other errors. */ | |
340 | ||
9315f853 | 341 | if (buf) |
d02c6f54 | 342 | strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), buf); |
912541b0 | 343 | else |
d02c6f54 | 344 | ctrl_msg_wire.value.intval = intval; |
912541b0 KS |
345 | |
346 | if (!uctrl->connected) { | |
b9da6a09 ZJS |
347 | if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) |
348 | return -errno; | |
912541b0 KS |
349 | uctrl->connected = true; |
350 | } | |
78467aeb | 351 | |
b9da6a09 ZJS |
352 | if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) |
353 | return -errno; | |
912541b0 | 354 | |
78467aeb YW |
355 | if (type == UDEV_CTRL_EXIT) |
356 | uctrl->maybe_disconnected = true; | |
d59f11e1 | 357 | |
78467aeb | 358 | return 0; |
ff2c503d KS |
359 | } |
360 | ||
78467aeb YW |
361 | static int udev_ctrl_wait_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
362 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
d59f11e1 KS |
363 | } |
364 | ||
78467aeb YW |
365 | int udev_ctrl_wait(struct udev_ctrl *uctrl, usec_t timeout) { |
366 | _cleanup_(sd_event_source_unrefp) sd_event_source *source_io = NULL, *source_timeout = NULL; | |
367 | int r; | |
d59f11e1 | 368 | |
78467aeb | 369 | assert(uctrl); |
d59f11e1 | 370 | |
78467aeb YW |
371 | if (uctrl->sock < 0) |
372 | return 0; | |
373 | if (!uctrl->connected) | |
374 | return 0; | |
d59f11e1 | 375 | |
78467aeb YW |
376 | if (!uctrl->maybe_disconnected) { |
377 | r = udev_ctrl_send(uctrl, _UDEV_CTRL_END_MESSAGES, 0, NULL); | |
378 | if (r < 0) | |
379 | return r; | |
380 | } | |
d59f11e1 | 381 | |
78467aeb YW |
382 | if (timeout == 0) |
383 | return 0; | |
384 | ||
385 | if (!uctrl->event) { | |
386 | r = udev_ctrl_attach_event(uctrl, NULL); | |
387 | if (r < 0) | |
388 | return r; | |
389 | } | |
390 | ||
391 | r = sd_event_add_io(uctrl->event, &source_io, uctrl->sock, EPOLLIN, udev_ctrl_wait_io_handler, NULL); | |
392 | if (r < 0) | |
393 | return r; | |
394 | ||
60ccab09 | 395 | (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io"); |
78467aeb YW |
396 | |
397 | if (timeout != USEC_INFINITY) { | |
398 | usec_t usec; | |
399 | ||
400 | usec = now(clock_boottime_or_monotonic()) + timeout; | |
401 | r = sd_event_add_time(uctrl->event, &source_timeout, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
402 | if (r < 0) | |
403 | return r; | |
404 | ||
405 | (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-io"); | |
406 | } | |
d59f11e1 | 407 | |
78467aeb | 408 | return sd_event_loop(uctrl->event); |
bb38678e | 409 | } |