]>
Commit | Line | Data |
---|---|---|
d9215cd8 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ |
2 | * | |
55e9959b | 3 | * libudev - interface to udev device information |
d59f11e1 | 4 | * |
1298001e | 5 | * Copyright (C) 2008 Kay Sievers <kay@vrfy.org> |
d59f11e1 | 6 | * |
4061ab9f KS |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
d59f11e1 KS |
11 | */ |
12 | ||
d59f11e1 | 13 | #include <errno.h> |
cf0fbc49 | 14 | #include <poll.h> |
d59f11e1 | 15 | #include <stddef.h> |
cf0fbc49 | 16 | #include <stdlib.h> |
d59f11e1 | 17 | #include <string.h> |
d59f11e1 KS |
18 | #include <sys/socket.h> |
19 | #include <sys/un.h> | |
cf0fbc49 | 20 | #include <unistd.h> |
d59f11e1 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
f97b34a6 | 24 | #include "format-util.h" |
3ffd4af2 | 25 | #include "socket-util.h" |
c15d02e5 | 26 | #include "udev.h" |
d59f11e1 | 27 | |
540f4669 | 28 | /* wire protocol magic must match */ |
912541b0 | 29 | #define UDEV_CTRL_MAGIC 0xdead1dea |
d59f11e1 KS |
30 | |
31 | enum udev_ctrl_msg_type { | |
912541b0 KS |
32 | UDEV_CTRL_UNKNOWN, |
33 | UDEV_CTRL_SET_LOG_LEVEL, | |
34 | UDEV_CTRL_STOP_EXEC_QUEUE, | |
35 | UDEV_CTRL_START_EXEC_QUEUE, | |
36 | UDEV_CTRL_RELOAD, | |
37 | UDEV_CTRL_SET_ENV, | |
38 | UDEV_CTRL_SET_CHILDREN_MAX, | |
39 | UDEV_CTRL_PING, | |
40 | UDEV_CTRL_EXIT, | |
d59f11e1 KS |
41 | }; |
42 | ||
b692a750 | 43 | struct udev_ctrl_msg_wire { |
912541b0 KS |
44 | char version[16]; |
45 | unsigned int magic; | |
46 | enum udev_ctrl_msg_type type; | |
47 | union { | |
48 | int intval; | |
49 | char buf[256]; | |
50 | }; | |
d59f11e1 KS |
51 | }; |
52 | ||
53 | struct udev_ctrl_msg { | |
912541b0 KS |
54 | int refcount; |
55 | struct udev_ctrl_connection *conn; | |
56 | struct udev_ctrl_msg_wire ctrl_msg_wire; | |
d59f11e1 KS |
57 | }; |
58 | ||
59 | struct udev_ctrl { | |
912541b0 KS |
60 | int refcount; |
61 | struct udev *udev; | |
62 | int sock; | |
6421348d | 63 | union sockaddr_union saddr; |
912541b0 KS |
64 | socklen_t addrlen; |
65 | bool bound; | |
66 | bool cleanup_socket; | |
67 | bool connected; | |
ff2c503d KS |
68 | }; |
69 | ||
70 | struct udev_ctrl_connection { | |
912541b0 KS |
71 | int refcount; |
72 | struct udev_ctrl *uctrl; | |
73 | int sock; | |
d59f11e1 KS |
74 | }; |
75 | ||
9ec6e95b | 76 | struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd) { |
912541b0 | 77 | struct udev_ctrl *uctrl; |
b97caef5 | 78 | const int on = 1; |
4bbdff75 | 79 | int r; |
912541b0 | 80 | |
955d98c9 | 81 | uctrl = new0(struct udev_ctrl, 1); |
912541b0 KS |
82 | if (uctrl == NULL) |
83 | return NULL; | |
84 | uctrl->refcount = 1; | |
85 | uctrl->udev = udev; | |
86 | ||
87 | if (fd < 0) { | |
88 | uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); | |
89 | if (uctrl->sock < 0) { | |
56f64d95 | 90 | log_error_errno(errno, "error getting socket: %m"); |
912541b0 KS |
91 | udev_ctrl_unref(uctrl); |
92 | return NULL; | |
93 | } | |
94 | } else { | |
95 | uctrl->bound = true; | |
96 | uctrl->sock = fd; | |
97 | } | |
25568304 KS |
98 | |
99 | /* | |
100 | * FIXME: remove it as soon as we can depend on this: | |
101 | * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949 | |
102 | */ | |
4bbdff75 TG |
103 | r = setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
104 | if (r < 0) | |
56f64d95 | 105 | log_warning_errno(errno, "could not set SO_PASSCRED: %m"); |
912541b0 | 106 | |
6421348d SL |
107 | uctrl->saddr.un.sun_family = AF_LOCAL; |
108 | strscpy(uctrl->saddr.un.sun_path, sizeof(uctrl->saddr.un.sun_path), "/run/udev/control"); | |
fc2fffe7 | 109 | uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un); |
912541b0 | 110 | return uctrl; |
fc1de713 KS |
111 | } |
112 | ||
9ec6e95b | 113 | struct udev_ctrl *udev_ctrl_new(struct udev *udev) { |
912541b0 | 114 | return udev_ctrl_new_from_fd(udev, -1); |
d59f11e1 KS |
115 | } |
116 | ||
9ec6e95b | 117 | int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { |
912541b0 KS |
118 | int err; |
119 | ||
120 | if (!uctrl->bound) { | |
6421348d | 121 | err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); |
912541b0 | 122 | if (err < 0 && errno == EADDRINUSE) { |
6421348d SL |
123 | unlink(uctrl->saddr.un.sun_path); |
124 | err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); | |
912541b0 KS |
125 | } |
126 | ||
ece174c5 | 127 | if (err < 0) |
76ef789d | 128 | return log_error_errno(errno, "bind failed: %m"); |
912541b0 KS |
129 | |
130 | err = listen(uctrl->sock, 0); | |
ece174c5 | 131 | if (err < 0) |
76ef789d | 132 | return log_error_errno(errno, "listen failed: %m"); |
912541b0 KS |
133 | |
134 | uctrl->bound = true; | |
135 | uctrl->cleanup_socket = true; | |
136 | } | |
137 | return 0; | |
d59f11e1 KS |
138 | } |
139 | ||
9ec6e95b | 140 | struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl) { |
912541b0 | 141 | return uctrl->udev; |
d59f11e1 KS |
142 | } |
143 | ||
9ec6e95b | 144 | static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) { |
35927d13 TG |
145 | if (uctrl) |
146 | uctrl->refcount++; | |
147 | ||
912541b0 | 148 | return uctrl; |
d59f11e1 KS |
149 | } |
150 | ||
9ec6e95b | 151 | struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) { |
35927d13 TG |
152 | if (uctrl && -- uctrl->refcount == 0) { |
153 | if (uctrl->sock >= 0) | |
154 | close(uctrl->sock); | |
155 | free(uctrl); | |
156 | } | |
157 | ||
912541b0 | 158 | return NULL; |
d59f11e1 KS |
159 | } |
160 | ||
9ec6e95b | 161 | int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { |
912541b0 KS |
162 | if (uctrl == NULL) |
163 | return 0; | |
164 | if (uctrl->cleanup_socket) | |
6421348d | 165 | unlink(uctrl->saddr.un.sun_path); |
912541b0 | 166 | return 0; |
1f5a5100 KS |
167 | } |
168 | ||
9ec6e95b | 169 | int udev_ctrl_get_fd(struct udev_ctrl *uctrl) { |
912541b0 KS |
170 | if (uctrl == NULL) |
171 | return -EINVAL; | |
172 | return uctrl->sock; | |
d59f11e1 KS |
173 | } |
174 | ||
9ec6e95b | 175 | struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) { |
912541b0 | 176 | struct udev_ctrl_connection *conn; |
39883f62 | 177 | struct ucred ucred = {}; |
912541b0 | 178 | const int on = 1; |
eff05270 | 179 | int r; |
912541b0 | 180 | |
eff05270 | 181 | conn = new(struct udev_ctrl_connection, 1); |
912541b0 KS |
182 | if (conn == NULL) |
183 | return NULL; | |
184 | conn->refcount = 1; | |
185 | conn->uctrl = uctrl; | |
186 | ||
187 | conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); | |
188 | if (conn->sock < 0) { | |
189 | if (errno != EINTR) | |
56f64d95 | 190 | log_error_errno(errno, "unable to receive ctrl connection: %m"); |
912541b0 KS |
191 | goto err; |
192 | } | |
193 | ||
194 | /* check peer credential of connection */ | |
eff05270 LP |
195 | r = getpeercred(conn->sock, &ucred); |
196 | if (r < 0) { | |
da927ba9 | 197 | log_error_errno(r, "unable to receive credentials of ctrl connection: %m"); |
912541b0 KS |
198 | goto err; |
199 | } | |
200 | if (ucred.uid > 0) { | |
1fa2f38f | 201 | log_error("sender uid="UID_FMT", message ignored", ucred.uid); |
912541b0 KS |
202 | goto err; |
203 | } | |
204 | ||
205 | /* enable receiving of the sender credentials in the messages */ | |
4bbdff75 TG |
206 | r = setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
207 | if (r < 0) | |
56f64d95 | 208 | log_warning_errno(errno, "could not set SO_PASSCRED: %m"); |
4bbdff75 | 209 | |
912541b0 KS |
210 | udev_ctrl_ref(uctrl); |
211 | return conn; | |
2c64f589 | 212 | err: |
912541b0 KS |
213 | if (conn->sock >= 0) |
214 | close(conn->sock); | |
6b430fdb | 215 | return mfree(conn); |
ff2c503d KS |
216 | } |
217 | ||
9ec6e95b | 218 | struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) { |
912541b0 KS |
219 | if (conn == NULL) |
220 | return NULL; | |
221 | conn->refcount++; | |
222 | return conn; | |
ff2c503d KS |
223 | } |
224 | ||
9ec6e95b | 225 | struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) { |
35927d13 TG |
226 | if (conn && -- conn->refcount == 0) { |
227 | if (conn->sock >= 0) | |
228 | close(conn->sock); | |
229 | ||
230 | udev_ctrl_unref(conn->uctrl); | |
231 | ||
232 | free(conn); | |
233 | } | |
234 | ||
912541b0 | 235 | return NULL; |
ff2c503d KS |
236 | } |
237 | ||
9ec6e95b | 238 | static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) { |
912541b0 KS |
239 | struct udev_ctrl_msg_wire ctrl_msg_wire; |
240 | int err = 0; | |
241 | ||
29804cc1 | 242 | memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire)); |
948aaa7c | 243 | strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION); |
912541b0 KS |
244 | ctrl_msg_wire.magic = UDEV_CTRL_MAGIC; |
245 | ctrl_msg_wire.type = type; | |
246 | ||
247 | if (buf != NULL) | |
d5a89d7d | 248 | strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf); |
912541b0 KS |
249 | else |
250 | ctrl_msg_wire.intval = intval; | |
251 | ||
252 | if (!uctrl->connected) { | |
6421348d | 253 | if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) { |
912541b0 KS |
254 | err = -errno; |
255 | goto out; | |
256 | } | |
257 | uctrl->connected = true; | |
258 | } | |
259 | if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) { | |
260 | err = -errno; | |
261 | goto out; | |
262 | } | |
263 | ||
264 | /* wait for peer message handling or disconnect */ | |
265 | for (;;) { | |
266 | struct pollfd pfd[1]; | |
267 | int r; | |
268 | ||
269 | pfd[0].fd = uctrl->sock; | |
270 | pfd[0].events = POLLIN; | |
9ea28c55 | 271 | r = poll(pfd, 1, timeout * MSEC_PER_SEC); |
912541b0 KS |
272 | if (r < 0) { |
273 | if (errno == EINTR) | |
274 | continue; | |
275 | err = -errno; | |
276 | break; | |
277 | } | |
278 | ||
279 | if (r > 0 && pfd[0].revents & POLLERR) { | |
280 | err = -EIO; | |
281 | break; | |
282 | } | |
283 | ||
284 | if (r == 0) | |
285 | err = -ETIMEDOUT; | |
286 | break; | |
287 | } | |
ff2c503d | 288 | out: |
912541b0 | 289 | return err; |
d59f11e1 KS |
290 | } |
291 | ||
9ec6e95b | 292 | int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) { |
912541b0 | 293 | return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout); |
ff2c503d KS |
294 | } |
295 | ||
9ec6e95b | 296 | int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) { |
912541b0 | 297 | return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout); |
d59f11e1 KS |
298 | } |
299 | ||
9ec6e95b | 300 | int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) { |
912541b0 | 301 | return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout); |
d59f11e1 KS |
302 | } |
303 | ||
9ec6e95b | 304 | int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) { |
912541b0 | 305 | return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout); |
d59f11e1 KS |
306 | } |
307 | ||
9ec6e95b | 308 | int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) { |
912541b0 | 309 | return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout); |
d59f11e1 KS |
310 | } |
311 | ||
9ec6e95b | 312 | int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) { |
912541b0 | 313 | return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout); |
d59f11e1 KS |
314 | } |
315 | ||
9ec6e95b | 316 | int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) { |
912541b0 | 317 | return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout); |
d59f11e1 KS |
318 | } |
319 | ||
9ec6e95b | 320 | int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) { |
912541b0 | 321 | return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout); |
bb38678e SJR |
322 | } |
323 | ||
9ec6e95b | 324 | struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) { |
912541b0 KS |
325 | struct udev_ctrl_msg *uctrl_msg; |
326 | ssize_t size; | |
912541b0 KS |
327 | struct cmsghdr *cmsg; |
328 | struct iovec iov; | |
912541b0 | 329 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; |
97fec53e ZJS |
330 | struct msghdr smsg = { |
331 | .msg_iov = &iov, | |
332 | .msg_iovlen = 1, | |
333 | .msg_control = cred_msg, | |
334 | .msg_controllen = sizeof(cred_msg), | |
335 | }; | |
336 | struct ucred *cred; | |
912541b0 | 337 | |
97fec53e | 338 | uctrl_msg = new0(struct udev_ctrl_msg, 1); |
912541b0 KS |
339 | if (uctrl_msg == NULL) |
340 | return NULL; | |
341 | uctrl_msg->refcount = 1; | |
342 | uctrl_msg->conn = conn; | |
343 | udev_ctrl_connection_ref(conn); | |
344 | ||
345 | /* wait for the incoming message */ | |
f168c273 | 346 | for (;;) { |
912541b0 KS |
347 | struct pollfd pfd[1]; |
348 | int r; | |
349 | ||
350 | pfd[0].fd = conn->sock; | |
351 | pfd[0].events = POLLIN; | |
352 | ||
353 | r = poll(pfd, 1, 10000); | |
354 | if (r < 0) { | |
355 | if (errno == EINTR) | |
356 | continue; | |
357 | goto err; | |
358 | } else if (r == 0) { | |
9f6445e3 | 359 | log_error("timeout waiting for ctrl message"); |
912541b0 KS |
360 | goto err; |
361 | } else { | |
362 | if (!(pfd[0].revents & POLLIN)) { | |
56f64d95 | 363 | log_error_errno(errno, "ctrl connection error: %m"); |
912541b0 KS |
364 | goto err; |
365 | } | |
366 | } | |
367 | ||
368 | break; | |
369 | } | |
370 | ||
371 | iov.iov_base = &uctrl_msg->ctrl_msg_wire; | |
372 | iov.iov_len = sizeof(struct udev_ctrl_msg_wire); | |
97fec53e | 373 | |
a38d9945 | 374 | size = recvmsg(conn->sock, &smsg, 0); |
912541b0 | 375 | if (size < 0) { |
56f64d95 | 376 | log_error_errno(errno, "unable to receive ctrl message: %m"); |
912541b0 KS |
377 | goto err; |
378 | } | |
1c8da044 LP |
379 | |
380 | cmsg_close_all(&smsg); | |
381 | ||
912541b0 | 382 | cmsg = CMSG_FIRSTHDR(&smsg); |
912541b0 KS |
383 | |
384 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
9f6445e3 | 385 | log_error("no sender credentials received, message ignored"); |
912541b0 KS |
386 | goto err; |
387 | } | |
388 | ||
93b1da85 KZ |
389 | cred = (struct ucred *) CMSG_DATA(cmsg); |
390 | ||
912541b0 | 391 | if (cred->uid != 0) { |
1fa2f38f | 392 | log_error("sender uid="UID_FMT", message ignored", cred->uid); |
912541b0 KS |
393 | goto err; |
394 | } | |
395 | ||
396 | if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) { | |
9f6445e3 | 397 | log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg->ctrl_msg_wire.magic); |
912541b0 KS |
398 | goto err; |
399 | } | |
400 | ||
912541b0 | 401 | return uctrl_msg; |
d59f11e1 | 402 | err: |
912541b0 KS |
403 | udev_ctrl_msg_unref(uctrl_msg); |
404 | return NULL; | |
d59f11e1 KS |
405 | } |
406 | ||
9ec6e95b | 407 | struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) { |
35927d13 TG |
408 | if (ctrl_msg && -- ctrl_msg->refcount == 0) { |
409 | udev_ctrl_connection_unref(ctrl_msg->conn); | |
410 | free(ctrl_msg); | |
411 | } | |
412 | ||
912541b0 | 413 | return NULL; |
d59f11e1 KS |
414 | } |
415 | ||
9ec6e95b | 416 | int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
417 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL) |
418 | return ctrl_msg->ctrl_msg_wire.intval; | |
419 | return -1; | |
d59f11e1 KS |
420 | } |
421 | ||
9ec6e95b | 422 | int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
423 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE) |
424 | return 1; | |
425 | return -1; | |
d59f11e1 KS |
426 | } |
427 | ||
9ec6e95b | 428 | int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
429 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE) |
430 | return 1; | |
431 | return -1; | |
d59f11e1 KS |
432 | } |
433 | ||
9ec6e95b | 434 | int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
435 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD) |
436 | return 1; | |
437 | return -1; | |
d59f11e1 KS |
438 | } |
439 | ||
9ec6e95b | 440 | const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
441 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV) |
442 | return ctrl_msg->ctrl_msg_wire.buf; | |
443 | return NULL; | |
d59f11e1 KS |
444 | } |
445 | ||
9ec6e95b | 446 | int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
447 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX) |
448 | return ctrl_msg->ctrl_msg_wire.intval; | |
449 | return -1; | |
d59f11e1 | 450 | } |
bb38678e | 451 | |
9ec6e95b | 452 | int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
453 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING) |
454 | return 1; | |
455 | return -1; | |
ff2c503d KS |
456 | } |
457 | ||
9ec6e95b | 458 | int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
459 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT) |
460 | return 1; | |
461 | return -1; | |
bb38678e | 462 | } |