]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
Merge pull request #10813 from poettering/cgroup-exec-start-pre
[thirdparty/systemd.git] / src / udev / udev-ctrl.c
1 /* SPDX-License-Identifier: LGPL-2.1+
2 *
3 * libudev - interface to udev device information
4 *
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.
9 */
10
11 #include <errno.h>
12 #include <poll.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <unistd.h>
19
20 #include "alloc-util.h"
21 #include "fd-util.h"
22 #include "format-util.h"
23 #include "io-util.h"
24 #include "socket-util.h"
25 #include "strxcpyx.h"
26 #include "udev-ctrl.h"
27
28 /* wire protocol magic must match */
29 #define UDEV_CTRL_MAGIC 0xdead1dea
30
31 enum udev_ctrl_msg_type {
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,
41 };
42
43 struct udev_ctrl_msg_wire {
44 char version[16];
45 unsigned magic;
46 enum udev_ctrl_msg_type type;
47 union {
48 int intval;
49 char buf[256];
50 };
51 };
52
53 struct udev_ctrl_msg {
54 unsigned n_ref;
55 struct udev_ctrl_connection *conn;
56 struct udev_ctrl_msg_wire ctrl_msg_wire;
57 };
58
59 struct udev_ctrl {
60 unsigned n_ref;
61 int sock;
62 union sockaddr_union saddr;
63 socklen_t addrlen;
64 bool bound;
65 bool cleanup_socket;
66 bool connected;
67 };
68
69 struct udev_ctrl_connection {
70 unsigned n_ref;
71 struct udev_ctrl *uctrl;
72 int sock;
73 };
74
75 struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
76 struct udev_ctrl *uctrl;
77 int r;
78
79 uctrl = new0(struct udev_ctrl, 1);
80 if (!uctrl)
81 return NULL;
82 uctrl->n_ref = 1;
83
84 if (fd < 0) {
85 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
86 if (uctrl->sock < 0) {
87 log_error_errno(errno, "Failed to create socket: %m");
88 udev_ctrl_unref(uctrl);
89 return NULL;
90 }
91 } else {
92 uctrl->bound = true;
93 uctrl->sock = fd;
94 }
95
96 /*
97 * FIXME: remove it as soon as we can depend on this:
98 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
99 */
100 r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true);
101 if (r < 0)
102 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
103
104 uctrl->saddr.un = (struct sockaddr_un) {
105 .sun_family = AF_UNIX,
106 .sun_path = "/run/udev/control",
107 };
108
109 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
110 return uctrl;
111 }
112
113 struct udev_ctrl *udev_ctrl_new(void) {
114 return udev_ctrl_new_from_fd(-1);
115 }
116
117 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
118 int err;
119
120 if (!uctrl->bound) {
121 err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
122 if (err < 0 && errno == EADDRINUSE) {
123 (void) sockaddr_un_unlink(&uctrl->saddr.un);
124 err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
125 }
126
127 if (err < 0)
128 return log_error_errno(errno, "Failed to bind socket: %m");
129
130 err = listen(uctrl->sock, 0);
131 if (err < 0)
132 return log_error_errno(errno, "Failed to listen: %m");
133
134 uctrl->bound = true;
135 uctrl->cleanup_socket = true;
136 }
137 return 0;
138 }
139
140 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
141 assert(uctrl);
142
143 safe_close(uctrl->sock);
144 return mfree(uctrl);
145 }
146
147 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl);
148 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
149
150 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
151 if (!uctrl)
152 return 0;
153 if (uctrl->cleanup_socket)
154 sockaddr_un_unlink(&uctrl->saddr.un);
155 return 0;
156 }
157
158 int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
159 if (!uctrl)
160 return -EINVAL;
161 return uctrl->sock;
162 }
163
164 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
165 struct udev_ctrl_connection *conn;
166 struct ucred ucred = {};
167 int r;
168
169 conn = new(struct udev_ctrl_connection, 1);
170 if (!conn)
171 return NULL;
172 conn->n_ref = 1;
173 conn->uctrl = uctrl;
174
175 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
176 if (conn->sock < 0) {
177 if (errno != EINTR)
178 log_error_errno(errno, "Failed to receive ctrl connection: %m");
179 goto err;
180 }
181
182 /* check peer credential of connection */
183 r = getpeercred(conn->sock, &ucred);
184 if (r < 0) {
185 log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
186 goto err;
187 }
188 if (ucred.uid > 0) {
189 log_error("Sender uid="UID_FMT", message ignored", ucred.uid);
190 goto err;
191 }
192
193 /* enable receiving of the sender credentials in the messages */
194 r = setsockopt_int(conn->sock, SOL_SOCKET, SO_PASSCRED, true);
195 if (r < 0)
196 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
197
198 udev_ctrl_ref(uctrl);
199 return conn;
200 err:
201 safe_close(conn->sock);
202 return mfree(conn);
203 }
204
205 static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) {
206 assert(conn);
207
208 safe_close(conn->sock);
209 udev_ctrl_unref(conn->uctrl);
210 return mfree(conn);
211 }
212
213 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
214
215 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
216 struct udev_ctrl_msg_wire ctrl_msg_wire;
217 int err = 0;
218
219 memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
220 strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION);
221 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
222 ctrl_msg_wire.type = type;
223
224 if (buf)
225 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
226 else
227 ctrl_msg_wire.intval = intval;
228
229 if (!uctrl->connected) {
230 if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
231 err = -errno;
232 goto out;
233 }
234 uctrl->connected = true;
235 }
236 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
237 err = -errno;
238 goto out;
239 }
240
241 /* wait for peer message handling or disconnect */
242 for (;;) {
243 struct pollfd pfd[1];
244 int r;
245
246 pfd[0].fd = uctrl->sock;
247 pfd[0].events = POLLIN;
248 r = poll(pfd, 1, timeout * MSEC_PER_SEC);
249 if (r < 0) {
250 if (errno == EINTR)
251 continue;
252 err = -errno;
253 break;
254 }
255
256 if (r > 0 && pfd[0].revents & POLLERR) {
257 err = -EIO;
258 break;
259 }
260
261 if (r == 0)
262 err = -ETIMEDOUT;
263 break;
264 }
265 out:
266 return err;
267 }
268
269 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
270 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
271 }
272
273 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
274 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
275 }
276
277 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
278 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
279 }
280
281 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
282 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
283 }
284
285 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
286 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
287 }
288
289 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
290 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
291 }
292
293 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
294 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
295 }
296
297 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
298 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
299 }
300
301 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
302 struct udev_ctrl_msg *uctrl_msg;
303 ssize_t size;
304 struct cmsghdr *cmsg;
305 struct iovec iov;
306 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
307 struct msghdr smsg = {
308 .msg_iov = &iov,
309 .msg_iovlen = 1,
310 .msg_control = cred_msg,
311 .msg_controllen = sizeof(cred_msg),
312 };
313 struct ucred *cred;
314
315 uctrl_msg = new0(struct udev_ctrl_msg, 1);
316 if (!uctrl_msg)
317 return NULL;
318 uctrl_msg->n_ref = 1;
319 uctrl_msg->conn = conn;
320 udev_ctrl_connection_ref(conn);
321
322 /* wait for the incoming message */
323 for (;;) {
324 struct pollfd pfd[1];
325 int r;
326
327 pfd[0].fd = conn->sock;
328 pfd[0].events = POLLIN;
329
330 r = poll(pfd, 1, 10000);
331 if (r < 0) {
332 if (errno == EINTR)
333 continue;
334 goto err;
335 } else if (r == 0) {
336 log_error("Timeout waiting for ctrl message");
337 goto err;
338 } else {
339 if (!(pfd[0].revents & POLLIN)) {
340 log_error("Invalid ctrl connection: %m");
341 goto err;
342 }
343 }
344
345 break;
346 }
347
348 iov = IOVEC_MAKE(&uctrl_msg->ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
349
350 size = recvmsg(conn->sock, &smsg, 0);
351 if (size < 0) {
352 log_error_errno(errno, "Failed to receive ctrl message: %m");
353 goto err;
354 }
355
356 cmsg_close_all(&smsg);
357
358 cmsg = CMSG_FIRSTHDR(&smsg);
359
360 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
361 log_error("No sender credentials received, ignoring message");
362 goto err;
363 }
364
365 cred = (struct ucred *) CMSG_DATA(cmsg);
366
367 if (cred->uid != 0) {
368 log_error("Sender uid="UID_FMT", ignoring message", cred->uid);
369 goto err;
370 }
371
372 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
373 log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg->ctrl_msg_wire.magic);
374 goto err;
375 }
376
377 return uctrl_msg;
378 err:
379 udev_ctrl_msg_unref(uctrl_msg);
380 return NULL;
381 }
382
383 static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) {
384 assert(ctrl_msg);
385
386 udev_ctrl_connection_unref(ctrl_msg->conn);
387 return mfree(ctrl_msg);
388 }
389
390 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg, udev_ctrl_msg, udev_ctrl_msg_free);
391
392 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
393 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
394 return ctrl_msg->ctrl_msg_wire.intval;
395 return -1;
396 }
397
398 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
399 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
400 return 1;
401 return -1;
402 }
403
404 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
405 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
406 return 1;
407 return -1;
408 }
409
410 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
411 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
412 return 1;
413 return -1;
414 }
415
416 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
417 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
418 return ctrl_msg->ctrl_msg_wire.buf;
419 return NULL;
420 }
421
422 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
423 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
424 return ctrl_msg->ctrl_msg_wire.intval;
425 return -1;
426 }
427
428 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
429 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
430 return 1;
431 return -1;
432 }
433
434 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
435 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
436 return 1;
437 return -1;
438 }