]>
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 | |
b5efdb8a | 20 | #include "alloc-util.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
f97b34a6 | 22 | #include "format-util.h" |
5cfa2c3d | 23 | #include "io-util.h" |
3ffd4af2 | 24 | #include "socket-util.h" |
7d68eb1b YW |
25 | #include "strxcpyx.h" |
26 | #include "udev-ctrl.h" | |
ef118d00 | 27 | #include "util.h" |
d59f11e1 | 28 | |
540f4669 | 29 | /* wire protocol magic must match */ |
912541b0 | 30 | #define UDEV_CTRL_MAGIC 0xdead1dea |
d59f11e1 KS |
31 | |
32 | enum udev_ctrl_msg_type { | |
912541b0 KS |
33 | UDEV_CTRL_UNKNOWN, |
34 | UDEV_CTRL_SET_LOG_LEVEL, | |
35 | UDEV_CTRL_STOP_EXEC_QUEUE, | |
36 | UDEV_CTRL_START_EXEC_QUEUE, | |
37 | UDEV_CTRL_RELOAD, | |
38 | UDEV_CTRL_SET_ENV, | |
39 | UDEV_CTRL_SET_CHILDREN_MAX, | |
40 | UDEV_CTRL_PING, | |
41 | UDEV_CTRL_EXIT, | |
d59f11e1 KS |
42 | }; |
43 | ||
b692a750 | 44 | struct udev_ctrl_msg_wire { |
912541b0 | 45 | char version[16]; |
14cb109d | 46 | unsigned magic; |
912541b0 KS |
47 | enum udev_ctrl_msg_type type; |
48 | union { | |
49 | int intval; | |
50 | char buf[256]; | |
51 | }; | |
d59f11e1 KS |
52 | }; |
53 | ||
54 | struct udev_ctrl_msg { | |
8f71a0d1 | 55 | unsigned n_ref; |
912541b0 KS |
56 | struct udev_ctrl_connection *conn; |
57 | struct udev_ctrl_msg_wire ctrl_msg_wire; | |
d59f11e1 KS |
58 | }; |
59 | ||
60 | struct udev_ctrl { | |
8f71a0d1 | 61 | unsigned n_ref; |
912541b0 | 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 { | |
8f71a0d1 | 71 | unsigned n_ref; |
912541b0 KS |
72 | struct udev_ctrl *uctrl; |
73 | int sock; | |
d59f11e1 KS |
74 | }; |
75 | ||
100bc5bf YW |
76 | int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) { |
77 | _cleanup_close_ int sock = -1; | |
912541b0 | 78 | struct udev_ctrl *uctrl; |
4bbdff75 | 79 | int r; |
912541b0 | 80 | |
100bc5bf | 81 | assert(ret); |
912541b0 KS |
82 | |
83 | if (fd < 0) { | |
100bc5bf YW |
84 | sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); |
85 | if (sock < 0) | |
86 | return log_error_errno(errno, "Failed to create socket: %m"); | |
912541b0 | 87 | } |
25568304 | 88 | |
100bc5bf YW |
89 | uctrl = new(struct udev_ctrl, 1); |
90 | if (!uctrl) | |
91 | return -ENOMEM; | |
92 | ||
93 | *uctrl = (struct udev_ctrl) { | |
94 | .n_ref = 1, | |
95 | .sock = fd >= 0 ? fd : TAKE_FD(sock), | |
96 | .bound = fd >= 0, | |
97 | }; | |
98 | ||
25568304 KS |
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 | */ | |
2ff48e98 | 103 | r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 104 | if (r < 0) |
572909a3 | 105 | log_warning_errno(r, "Failed to set SO_PASSCRED: %m"); |
912541b0 | 106 | |
44ed5214 LP |
107 | uctrl->saddr.un = (struct sockaddr_un) { |
108 | .sun_family = AF_UNIX, | |
109 | .sun_path = "/run/udev/control", | |
110 | }; | |
111 | ||
fc2fffe7 | 112 | uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un); |
fc1de713 | 113 | |
100bc5bf YW |
114 | *ret = TAKE_PTR(uctrl); |
115 | return 0; | |
d59f11e1 KS |
116 | } |
117 | ||
9ec6e95b | 118 | int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { |
53bba2fb | 119 | int r; |
912541b0 | 120 | |
53bba2fb | 121 | assert(uctrl); |
912541b0 | 122 | |
53bba2fb YW |
123 | if (uctrl->bound) |
124 | return 0; | |
912541b0 | 125 | |
53bba2fb YW |
126 | r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); |
127 | if (r < 0 && errno == EADDRINUSE) { | |
128 | (void) sockaddr_un_unlink(&uctrl->saddr.un); | |
129 | r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen); | |
912541b0 | 130 | } |
53bba2fb YW |
131 | |
132 | if (r < 0) | |
133 | return log_error_errno(errno, "Failed to bind udev control socket: %m"); | |
134 | ||
135 | if (listen(uctrl->sock, 0) < 0) | |
136 | return log_error_errno(errno, "Failed to listen udev control socket: %m"); | |
137 | ||
138 | uctrl->bound = true; | |
139 | uctrl->cleanup_socket = true; | |
140 | ||
912541b0 | 141 | return 0; |
d59f11e1 KS |
142 | } |
143 | ||
8f71a0d1 YW |
144 | static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) { |
145 | assert(uctrl); | |
35927d13 | 146 | |
8f71a0d1 YW |
147 | safe_close(uctrl->sock); |
148 | return mfree(uctrl); | |
d59f11e1 KS |
149 | } |
150 | ||
8f71a0d1 YW |
151 | DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl); |
152 | DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free); | |
d59f11e1 | 153 | |
9ec6e95b | 154 | int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { |
9315f853 | 155 | if (!uctrl) |
912541b0 KS |
156 | return 0; |
157 | if (uctrl->cleanup_socket) | |
155b6876 | 158 | sockaddr_un_unlink(&uctrl->saddr.un); |
912541b0 | 159 | return 0; |
1f5a5100 KS |
160 | } |
161 | ||
9ec6e95b | 162 | int udev_ctrl_get_fd(struct udev_ctrl *uctrl) { |
9315f853 | 163 | if (!uctrl) |
912541b0 KS |
164 | return -EINVAL; |
165 | return uctrl->sock; | |
d59f11e1 KS |
166 | } |
167 | ||
9ec6e95b | 168 | struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) { |
912541b0 | 169 | struct udev_ctrl_connection *conn; |
39883f62 | 170 | struct ucred ucred = {}; |
eff05270 | 171 | int r; |
912541b0 | 172 | |
eff05270 | 173 | conn = new(struct udev_ctrl_connection, 1); |
9315f853 | 174 | if (!conn) |
912541b0 | 175 | return NULL; |
8f71a0d1 | 176 | conn->n_ref = 1; |
912541b0 KS |
177 | conn->uctrl = uctrl; |
178 | ||
179 | conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); | |
180 | if (conn->sock < 0) { | |
181 | if (errno != EINTR) | |
572909a3 | 182 | log_error_errno(errno, "Failed to receive ctrl connection: %m"); |
912541b0 KS |
183 | goto err; |
184 | } | |
185 | ||
186 | /* check peer credential of connection */ | |
eff05270 LP |
187 | r = getpeercred(conn->sock, &ucred); |
188 | if (r < 0) { | |
572909a3 | 189 | log_error_errno(r, "Failed to receive credentials of ctrl connection: %m"); |
912541b0 KS |
190 | goto err; |
191 | } | |
192 | if (ucred.uid > 0) { | |
572909a3 | 193 | log_error("Sender uid="UID_FMT", message ignored", ucred.uid); |
912541b0 KS |
194 | goto err; |
195 | } | |
196 | ||
197 | /* enable receiving of the sender credentials in the messages */ | |
2ff48e98 | 198 | r = setsockopt_int(conn->sock, SOL_SOCKET, SO_PASSCRED, true); |
4bbdff75 | 199 | if (r < 0) |
572909a3 | 200 | log_warning_errno(r, "Failed to set SO_PASSCRED: %m"); |
4bbdff75 | 201 | |
912541b0 KS |
202 | udev_ctrl_ref(uctrl); |
203 | return conn; | |
2c64f589 | 204 | err: |
a4209121 | 205 | safe_close(conn->sock); |
6b430fdb | 206 | return mfree(conn); |
ff2c503d KS |
207 | } |
208 | ||
8f71a0d1 YW |
209 | static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) { |
210 | assert(conn); | |
35927d13 | 211 | |
8f71a0d1 YW |
212 | safe_close(conn->sock); |
213 | udev_ctrl_unref(conn->uctrl); | |
214 | return mfree(conn); | |
ff2c503d KS |
215 | } |
216 | ||
8f71a0d1 YW |
217 | DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free); |
218 | ||
3797776e | 219 | static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, usec_t timeout) { |
b9da6a09 ZJS |
220 | struct udev_ctrl_msg_wire ctrl_msg_wire = { |
221 | .version = "udev-" STRINGIFY(PROJECT_VERSION), | |
222 | .magic = UDEV_CTRL_MAGIC, | |
223 | .type = type, | |
224 | }; | |
912541b0 | 225 | |
9315f853 | 226 | if (buf) |
d5a89d7d | 227 | strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf); |
912541b0 KS |
228 | else |
229 | ctrl_msg_wire.intval = intval; | |
230 | ||
231 | if (!uctrl->connected) { | |
b9da6a09 ZJS |
232 | if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) |
233 | return -errno; | |
912541b0 KS |
234 | uctrl->connected = true; |
235 | } | |
b9da6a09 ZJS |
236 | if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) |
237 | return -errno; | |
912541b0 KS |
238 | |
239 | /* wait for peer message handling or disconnect */ | |
240 | for (;;) { | |
b9da6a09 ZJS |
241 | struct pollfd pfd = { |
242 | .fd = uctrl->sock, | |
243 | .events = POLLIN, | |
244 | }; | |
912541b0 KS |
245 | int r; |
246 | ||
3797776e | 247 | r = poll(&pfd, 1, DIV_ROUND_UP(timeout, USEC_PER_MSEC)); |
49c603bd | 248 | if (r < 0) { |
912541b0 KS |
249 | if (errno == EINTR) |
250 | continue; | |
b9da6a09 | 251 | return -errno; |
912541b0 | 252 | } |
912541b0 | 253 | if (r == 0) |
b9da6a09 ZJS |
254 | return -ETIMEDOUT; |
255 | if (pfd.revents & POLLERR) | |
256 | return -EIO; | |
257 | return 0; | |
912541b0 | 258 | } |
d59f11e1 KS |
259 | } |
260 | ||
3797776e | 261 | int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, usec_t timeout) { |
912541b0 | 262 | return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout); |
ff2c503d KS |
263 | } |
264 | ||
3797776e | 265 | int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) { |
912541b0 | 266 | return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout); |
d59f11e1 KS |
267 | } |
268 | ||
3797776e | 269 | int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) { |
912541b0 | 270 | return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout); |
d59f11e1 KS |
271 | } |
272 | ||
3797776e | 273 | int udev_ctrl_send_reload(struct udev_ctrl *uctrl, usec_t timeout) { |
912541b0 | 274 | return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout); |
d59f11e1 KS |
275 | } |
276 | ||
3797776e | 277 | int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, usec_t timeout) { |
912541b0 | 278 | return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout); |
d59f11e1 KS |
279 | } |
280 | ||
3797776e | 281 | int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, usec_t timeout) { |
912541b0 | 282 | return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout); |
d59f11e1 KS |
283 | } |
284 | ||
3797776e | 285 | int udev_ctrl_send_ping(struct udev_ctrl *uctrl, usec_t timeout) { |
912541b0 | 286 | return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout); |
d59f11e1 KS |
287 | } |
288 | ||
3797776e | 289 | int udev_ctrl_send_exit(struct udev_ctrl *uctrl, usec_t timeout) { |
912541b0 | 290 | return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout); |
bb38678e SJR |
291 | } |
292 | ||
9ec6e95b | 293 | struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) { |
912541b0 KS |
294 | struct udev_ctrl_msg *uctrl_msg; |
295 | ssize_t size; | |
912541b0 KS |
296 | struct cmsghdr *cmsg; |
297 | struct iovec iov; | |
912541b0 | 298 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; |
97fec53e ZJS |
299 | struct msghdr smsg = { |
300 | .msg_iov = &iov, | |
301 | .msg_iovlen = 1, | |
302 | .msg_control = cred_msg, | |
303 | .msg_controllen = sizeof(cred_msg), | |
304 | }; | |
305 | struct ucred *cred; | |
912541b0 | 306 | |
97fec53e | 307 | uctrl_msg = new0(struct udev_ctrl_msg, 1); |
9315f853 | 308 | if (!uctrl_msg) |
912541b0 | 309 | return NULL; |
8f71a0d1 | 310 | uctrl_msg->n_ref = 1; |
912541b0 KS |
311 | uctrl_msg->conn = conn; |
312 | udev_ctrl_connection_ref(conn); | |
313 | ||
314 | /* wait for the incoming message */ | |
f168c273 | 315 | for (;;) { |
912541b0 KS |
316 | struct pollfd pfd[1]; |
317 | int r; | |
318 | ||
319 | pfd[0].fd = conn->sock; | |
320 | pfd[0].events = POLLIN; | |
321 | ||
322 | r = poll(pfd, 1, 10000); | |
49c603bd | 323 | if (r < 0) { |
912541b0 KS |
324 | if (errno == EINTR) |
325 | continue; | |
326 | goto err; | |
327 | } else if (r == 0) { | |
572909a3 | 328 | log_error("Timeout waiting for ctrl message"); |
912541b0 KS |
329 | goto err; |
330 | } else { | |
331 | if (!(pfd[0].revents & POLLIN)) { | |
572909a3 | 332 | log_error("Invalid ctrl connection: %m"); |
912541b0 KS |
333 | goto err; |
334 | } | |
335 | } | |
336 | ||
337 | break; | |
338 | } | |
339 | ||
5cfa2c3d | 340 | iov = IOVEC_MAKE(&uctrl_msg->ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire)); |
97fec53e | 341 | |
a38d9945 | 342 | size = recvmsg(conn->sock, &smsg, 0); |
572909a3 YW |
343 | if (size < 0) { |
344 | log_error_errno(errno, "Failed to receive ctrl message: %m"); | |
912541b0 KS |
345 | goto err; |
346 | } | |
1c8da044 LP |
347 | |
348 | cmsg_close_all(&smsg); | |
349 | ||
912541b0 | 350 | cmsg = CMSG_FIRSTHDR(&smsg); |
912541b0 | 351 | |
9315f853 | 352 | if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) { |
572909a3 | 353 | log_error("No sender credentials received, ignoring message"); |
912541b0 KS |
354 | goto err; |
355 | } | |
356 | ||
93b1da85 KZ |
357 | cred = (struct ucred *) CMSG_DATA(cmsg); |
358 | ||
912541b0 | 359 | if (cred->uid != 0) { |
572909a3 | 360 | log_error("Sender uid="UID_FMT", ignoring message", cred->uid); |
912541b0 KS |
361 | goto err; |
362 | } | |
363 | ||
364 | if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) { | |
572909a3 | 365 | log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg->ctrl_msg_wire.magic); |
912541b0 KS |
366 | goto err; |
367 | } | |
368 | ||
912541b0 | 369 | return uctrl_msg; |
d59f11e1 | 370 | err: |
912541b0 KS |
371 | udev_ctrl_msg_unref(uctrl_msg); |
372 | return NULL; | |
d59f11e1 KS |
373 | } |
374 | ||
8f71a0d1 YW |
375 | static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) { |
376 | assert(ctrl_msg); | |
35927d13 | 377 | |
8f71a0d1 YW |
378 | udev_ctrl_connection_unref(ctrl_msg->conn); |
379 | return mfree(ctrl_msg); | |
d59f11e1 KS |
380 | } |
381 | ||
8f71a0d1 YW |
382 | DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg, udev_ctrl_msg, udev_ctrl_msg_free); |
383 | ||
9ec6e95b | 384 | int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
385 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL) |
386 | return ctrl_msg->ctrl_msg_wire.intval; | |
387 | return -1; | |
d59f11e1 KS |
388 | } |
389 | ||
9ec6e95b | 390 | int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
391 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE) |
392 | return 1; | |
393 | return -1; | |
d59f11e1 KS |
394 | } |
395 | ||
9ec6e95b | 396 | int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
397 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE) |
398 | return 1; | |
399 | return -1; | |
d59f11e1 KS |
400 | } |
401 | ||
9ec6e95b | 402 | int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
403 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD) |
404 | return 1; | |
405 | return -1; | |
d59f11e1 KS |
406 | } |
407 | ||
9ec6e95b | 408 | const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
409 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV) |
410 | return ctrl_msg->ctrl_msg_wire.buf; | |
411 | return NULL; | |
d59f11e1 KS |
412 | } |
413 | ||
9ec6e95b | 414 | int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
415 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX) |
416 | return ctrl_msg->ctrl_msg_wire.intval; | |
417 | return -1; | |
d59f11e1 | 418 | } |
bb38678e | 419 | |
9ec6e95b | 420 | int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
421 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING) |
422 | return 1; | |
423 | return -1; | |
ff2c503d KS |
424 | } |
425 | ||
9ec6e95b | 426 | int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) { |
912541b0 KS |
427 | if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT) |
428 | return 1; | |
429 | return -1; | |
bb38678e | 430 | } |