]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
udev-ctrl: refactor udev_ctrl_enable_receiving()
[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 #include "util.h"
28
29 /* wire protocol magic must match */
30 #define UDEV_CTRL_MAGIC 0xdead1dea
31
32 enum udev_ctrl_msg_type {
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,
42 };
43
44 struct udev_ctrl_msg_wire {
45 char version[16];
46 unsigned magic;
47 enum udev_ctrl_msg_type type;
48 union {
49 int intval;
50 char buf[256];
51 };
52 };
53
54 struct udev_ctrl_msg {
55 unsigned n_ref;
56 struct udev_ctrl_connection *conn;
57 struct udev_ctrl_msg_wire ctrl_msg_wire;
58 };
59
60 struct udev_ctrl {
61 unsigned n_ref;
62 int sock;
63 union sockaddr_union saddr;
64 socklen_t addrlen;
65 bool bound;
66 bool cleanup_socket;
67 bool connected;
68 };
69
70 struct udev_ctrl_connection {
71 unsigned n_ref;
72 struct udev_ctrl *uctrl;
73 int sock;
74 };
75
76 int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) {
77 _cleanup_close_ int sock = -1;
78 struct udev_ctrl *uctrl;
79 int r;
80
81 assert(ret);
82
83 if (fd < 0) {
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");
87 }
88
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
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 */
103 r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true);
104 if (r < 0)
105 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
106
107 uctrl->saddr.un = (struct sockaddr_un) {
108 .sun_family = AF_UNIX,
109 .sun_path = "/run/udev/control",
110 };
111
112 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
113
114 *ret = TAKE_PTR(uctrl);
115 return 0;
116 }
117
118 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
119 int r;
120
121 assert(uctrl);
122
123 if (uctrl->bound)
124 return 0;
125
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);
130 }
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
141 return 0;
142 }
143
144 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
145 assert(uctrl);
146
147 safe_close(uctrl->sock);
148 return mfree(uctrl);
149 }
150
151 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl);
152 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
153
154 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
155 if (!uctrl)
156 return 0;
157 if (uctrl->cleanup_socket)
158 sockaddr_un_unlink(&uctrl->saddr.un);
159 return 0;
160 }
161
162 int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
163 if (!uctrl)
164 return -EINVAL;
165 return uctrl->sock;
166 }
167
168 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
169 struct udev_ctrl_connection *conn;
170 struct ucred ucred = {};
171 int r;
172
173 conn = new(struct udev_ctrl_connection, 1);
174 if (!conn)
175 return NULL;
176 conn->n_ref = 1;
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)
182 log_error_errno(errno, "Failed to receive ctrl connection: %m");
183 goto err;
184 }
185
186 /* check peer credential of connection */
187 r = getpeercred(conn->sock, &ucred);
188 if (r < 0) {
189 log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
190 goto err;
191 }
192 if (ucred.uid > 0) {
193 log_error("Sender uid="UID_FMT", message ignored", ucred.uid);
194 goto err;
195 }
196
197 /* enable receiving of the sender credentials in the messages */
198 r = setsockopt_int(conn->sock, SOL_SOCKET, SO_PASSCRED, true);
199 if (r < 0)
200 log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
201
202 udev_ctrl_ref(uctrl);
203 return conn;
204 err:
205 safe_close(conn->sock);
206 return mfree(conn);
207 }
208
209 static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) {
210 assert(conn);
211
212 safe_close(conn->sock);
213 udev_ctrl_unref(conn->uctrl);
214 return mfree(conn);
215 }
216
217 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
218
219 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, usec_t timeout) {
220 struct udev_ctrl_msg_wire ctrl_msg_wire = {
221 .version = "udev-" STRINGIFY(PROJECT_VERSION),
222 .magic = UDEV_CTRL_MAGIC,
223 .type = type,
224 };
225
226 if (buf)
227 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
228 else
229 ctrl_msg_wire.intval = intval;
230
231 if (!uctrl->connected) {
232 if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
233 return -errno;
234 uctrl->connected = true;
235 }
236 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
237 return -errno;
238
239 /* wait for peer message handling or disconnect */
240 for (;;) {
241 struct pollfd pfd = {
242 .fd = uctrl->sock,
243 .events = POLLIN,
244 };
245 int r;
246
247 r = poll(&pfd, 1, DIV_ROUND_UP(timeout, USEC_PER_MSEC));
248 if (r < 0) {
249 if (errno == EINTR)
250 continue;
251 return -errno;
252 }
253 if (r == 0)
254 return -ETIMEDOUT;
255 if (pfd.revents & POLLERR)
256 return -EIO;
257 return 0;
258 }
259 }
260
261 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, usec_t timeout) {
262 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
263 }
264
265 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) {
266 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
267 }
268
269 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, usec_t timeout) {
270 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
271 }
272
273 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, usec_t timeout) {
274 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
275 }
276
277 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, usec_t timeout) {
278 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
279 }
280
281 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, usec_t timeout) {
282 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
283 }
284
285 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, usec_t timeout) {
286 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
287 }
288
289 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, usec_t timeout) {
290 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
291 }
292
293 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
294 struct udev_ctrl_msg *uctrl_msg;
295 ssize_t size;
296 struct cmsghdr *cmsg;
297 struct iovec iov;
298 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
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;
306
307 uctrl_msg = new0(struct udev_ctrl_msg, 1);
308 if (!uctrl_msg)
309 return NULL;
310 uctrl_msg->n_ref = 1;
311 uctrl_msg->conn = conn;
312 udev_ctrl_connection_ref(conn);
313
314 /* wait for the incoming message */
315 for (;;) {
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);
323 if (r < 0) {
324 if (errno == EINTR)
325 continue;
326 goto err;
327 } else if (r == 0) {
328 log_error("Timeout waiting for ctrl message");
329 goto err;
330 } else {
331 if (!(pfd[0].revents & POLLIN)) {
332 log_error("Invalid ctrl connection: %m");
333 goto err;
334 }
335 }
336
337 break;
338 }
339
340 iov = IOVEC_MAKE(&uctrl_msg->ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
341
342 size = recvmsg(conn->sock, &smsg, 0);
343 if (size < 0) {
344 log_error_errno(errno, "Failed to receive ctrl message: %m");
345 goto err;
346 }
347
348 cmsg_close_all(&smsg);
349
350 cmsg = CMSG_FIRSTHDR(&smsg);
351
352 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
353 log_error("No sender credentials received, ignoring message");
354 goto err;
355 }
356
357 cred = (struct ucred *) CMSG_DATA(cmsg);
358
359 if (cred->uid != 0) {
360 log_error("Sender uid="UID_FMT", ignoring message", cred->uid);
361 goto err;
362 }
363
364 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
365 log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg->ctrl_msg_wire.magic);
366 goto err;
367 }
368
369 return uctrl_msg;
370 err:
371 udev_ctrl_msg_unref(uctrl_msg);
372 return NULL;
373 }
374
375 static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) {
376 assert(ctrl_msg);
377
378 udev_ctrl_connection_unref(ctrl_msg->conn);
379 return mfree(ctrl_msg);
380 }
381
382 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg, udev_ctrl_msg, udev_ctrl_msg_free);
383
384 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
385 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
386 return ctrl_msg->ctrl_msg_wire.intval;
387 return -1;
388 }
389
390 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
391 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
392 return 1;
393 return -1;
394 }
395
396 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
397 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
398 return 1;
399 return -1;
400 }
401
402 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
403 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
404 return 1;
405 return -1;
406 }
407
408 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
409 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
410 return ctrl_msg->ctrl_msg_wire.buf;
411 return NULL;
412 }
413
414 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
415 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
416 return ctrl_msg->ctrl_msg_wire.intval;
417 return -1;
418 }
419
420 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
421 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
422 return 1;
423 return -1;
424 }
425
426 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
427 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
428 return 1;
429 return -1;
430 }