]>
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), | |
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)); | |
192 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; | |
193 | 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 | 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 | ||
215 | size = recvmsg(fd, &smsg, 0); | |
216 | if (size < 0) { | |
217 | if (errno != EINTR) | |
218 | return log_error_errno(errno, "Failed to receive ctrl message: %m"); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | cmsg_close_all(&smsg); | |
224 | ||
225 | cmsg = CMSG_FIRSTHDR(&smsg); | |
226 | ||
227 | if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
228 | log_error("No sender credentials received, ignoring message"); | |
229 | return 0; | |
230 | } | |
231 | ||
232 | cred = (struct ucred *) CMSG_DATA(cmsg); | |
233 | ||
234 | if (cred->uid != 0) { | |
235 | log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | if (msg_wire.magic != UDEV_CTRL_MAGIC) { | |
240 | log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic); | |
241 | return 0; | |
242 | } | |
243 | ||
78467aeb YW |
244 | if (msg_wire.type == _UDEV_CTRL_END_MESSAGES) |
245 | return 0; | |
246 | ||
d02c6f54 YW |
247 | if (uctrl->callback) |
248 | (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata); | |
249 | ||
78467aeb YW |
250 | /* Do not disconnect and wait for next message. */ |
251 | uctrl = udev_ctrl_unref(uctrl); | |
d02c6f54 YW |
252 | return 0; |
253 | } | |
254 | ||
255 | static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
256 | struct udev_ctrl *uctrl = userdata; | |
257 | _cleanup_close_ int sock = -1; | |
258 | struct ucred ucred; | |
eff05270 | 259 | int r; |
912541b0 | 260 | |
d02c6f54 | 261 | assert(uctrl); |
912541b0 | 262 | |
d02c6f54 YW |
263 | sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); |
264 | if (sock < 0) { | |
4ff9bc2e LP |
265 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) |
266 | return 0; | |
267 | ||
268 | return log_error_errno(errno, "Failed to accept ctrl connection: %m"); | |
912541b0 KS |
269 | } |
270 | ||
271 | /* check peer credential of connection */ | |
d02c6f54 | 272 | r = getpeercred(sock, &ucred); |
eff05270 | 273 | if (r < 0) { |
572909a3 | 274 | log_error_errno(r, "Failed to receive credentials of ctrl connection: %m"); |
d02c6f54 | 275 | return 0; |
912541b0 | 276 | } |
d02c6f54 | 277 | |
912541b0 | 278 | if (ucred.uid > 0) { |
d02c6f54 YW |
279 | log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid); |
280 | return 0; | |
912541b0 KS |
281 | } |
282 | ||
283 | /* enable receiving of the sender credentials in the messages */ | |
d02c6f54 | 284 | r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 285 | if (r < 0) |
d02c6f54 | 286 | log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m"); |
4bbdff75 | 287 | |
d02c6f54 YW |
288 | r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl); |
289 | if (r < 0) { | |
290 | log_error_errno(r, "Failed to create event source for udev control connection: %m"); | |
291 | return 0; | |
292 | } | |
293 | ||
294 | (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection"); | |
ff2c503d | 295 | |
d02c6f54 YW |
296 | /* Do not accept multiple connection. */ |
297 | (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF); | |
35927d13 | 298 | |
d02c6f54 YW |
299 | uctrl->sock_connect = TAKE_FD(sock); |
300 | return 0; | |
ff2c503d KS |
301 | } |
302 | ||
d02c6f54 YW |
303 | int udev_ctrl_start(struct udev_ctrl *uctrl, udev_ctrl_handler_t callback, void *userdata) { |
304 | int r; | |
305 | ||
306 | assert(uctrl); | |
307 | ||
308 | if (!uctrl->event) { | |
309 | r = udev_ctrl_attach_event(uctrl, NULL); | |
310 | if (r < 0) | |
311 | return r; | |
312 | } | |
313 | ||
314 | r = udev_ctrl_enable_receiving(uctrl); | |
315 | if (r < 0) | |
316 | return r; | |
317 | ||
318 | uctrl->callback = callback; | |
319 | uctrl->userdata = userdata; | |
320 | ||
321 | r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl); | |
322 | if (r < 0) | |
323 | return r; | |
324 | ||
325 | (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl"); | |
326 | ||
327 | return 0; | |
328 | } | |
8f71a0d1 | 329 | |
78467aeb | 330 | int udev_ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf) { |
b9da6a09 ZJS |
331 | struct udev_ctrl_msg_wire ctrl_msg_wire = { |
332 | .version = "udev-" STRINGIFY(PROJECT_VERSION), | |
333 | .magic = UDEV_CTRL_MAGIC, | |
334 | .type = type, | |
335 | }; | |
912541b0 | 336 | |
78467aeb YW |
337 | if (uctrl->maybe_disconnected) |
338 | return -ENOANO; /* to distinguish this from other errors. */ | |
339 | ||
9315f853 | 340 | if (buf) |
d02c6f54 | 341 | strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), buf); |
912541b0 | 342 | else |
d02c6f54 | 343 | ctrl_msg_wire.value.intval = intval; |
912541b0 KS |
344 | |
345 | if (!uctrl->connected) { | |
b9da6a09 ZJS |
346 | if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) |
347 | return -errno; | |
912541b0 KS |
348 | uctrl->connected = true; |
349 | } | |
78467aeb | 350 | |
b9da6a09 ZJS |
351 | if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) |
352 | return -errno; | |
912541b0 | 353 | |
78467aeb YW |
354 | if (type == UDEV_CTRL_EXIT) |
355 | uctrl->maybe_disconnected = true; | |
d59f11e1 | 356 | |
78467aeb | 357 | return 0; |
ff2c503d KS |
358 | } |
359 | ||
78467aeb YW |
360 | static int udev_ctrl_wait_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
361 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
d59f11e1 KS |
362 | } |
363 | ||
78467aeb YW |
364 | int udev_ctrl_wait(struct udev_ctrl *uctrl, usec_t timeout) { |
365 | _cleanup_(sd_event_source_unrefp) sd_event_source *source_io = NULL, *source_timeout = NULL; | |
366 | int r; | |
d59f11e1 | 367 | |
78467aeb | 368 | assert(uctrl); |
d59f11e1 | 369 | |
78467aeb YW |
370 | if (uctrl->sock < 0) |
371 | return 0; | |
372 | if (!uctrl->connected) | |
373 | return 0; | |
d59f11e1 | 374 | |
78467aeb YW |
375 | if (!uctrl->maybe_disconnected) { |
376 | r = udev_ctrl_send(uctrl, _UDEV_CTRL_END_MESSAGES, 0, NULL); | |
377 | if (r < 0) | |
378 | return r; | |
379 | } | |
d59f11e1 | 380 | |
78467aeb YW |
381 | if (timeout == 0) |
382 | return 0; | |
383 | ||
384 | if (!uctrl->event) { | |
385 | r = udev_ctrl_attach_event(uctrl, NULL); | |
386 | if (r < 0) | |
387 | return r; | |
388 | } | |
389 | ||
390 | r = sd_event_add_io(uctrl->event, &source_io, uctrl->sock, EPOLLIN, udev_ctrl_wait_io_handler, NULL); | |
391 | if (r < 0) | |
392 | return r; | |
393 | ||
60ccab09 | 394 | (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io"); |
78467aeb YW |
395 | |
396 | if (timeout != USEC_INFINITY) { | |
397 | usec_t usec; | |
398 | ||
399 | usec = now(clock_boottime_or_monotonic()) + timeout; | |
400 | r = sd_event_add_time(uctrl->event, &source_timeout, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
401 | if (r < 0) | |
402 | return r; | |
403 | ||
404 | (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-io"); | |
405 | } | |
d59f11e1 | 406 | |
78467aeb | 407 | return sd_event_loop(uctrl->event); |
bb38678e | 408 | } |