]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-ctrl.c
udev-ctrl: move prototypes of udev_ctrl_*() to udev-ctrl.h
[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 "socket-util.h"
24 #include "strxcpyx.h"
25 #include "udev-ctrl.h"
26
27 /* wire protocol magic must match */
28 #define UDEV_CTRL_MAGIC 0xdead1dea
29
30 enum udev_ctrl_msg_type {
31 UDEV_CTRL_UNKNOWN,
32 UDEV_CTRL_SET_LOG_LEVEL,
33 UDEV_CTRL_STOP_EXEC_QUEUE,
34 UDEV_CTRL_START_EXEC_QUEUE,
35 UDEV_CTRL_RELOAD,
36 UDEV_CTRL_SET_ENV,
37 UDEV_CTRL_SET_CHILDREN_MAX,
38 UDEV_CTRL_PING,
39 UDEV_CTRL_EXIT,
40 };
41
42 struct udev_ctrl_msg_wire {
43 char version[16];
44 unsigned int magic;
45 enum udev_ctrl_msg_type type;
46 union {
47 int intval;
48 char buf[256];
49 };
50 };
51
52 struct udev_ctrl_msg {
53 unsigned n_ref;
54 struct udev_ctrl_connection *conn;
55 struct udev_ctrl_msg_wire ctrl_msg_wire;
56 };
57
58 struct udev_ctrl {
59 unsigned n_ref;
60 int sock;
61 union sockaddr_union saddr;
62 socklen_t addrlen;
63 bool bound;
64 bool cleanup_socket;
65 bool connected;
66 };
67
68 struct udev_ctrl_connection {
69 unsigned n_ref;
70 struct udev_ctrl *uctrl;
71 int sock;
72 };
73
74 struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
75 struct udev_ctrl *uctrl;
76 const int on = 1;
77 int r;
78
79 uctrl = new0(struct udev_ctrl, 1);
80 if (uctrl == NULL)
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, "error getting 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(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
101 if (r < 0)
102 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
103
104 uctrl->saddr.un.sun_family = AF_LOCAL;
105 strscpy(uctrl->saddr.un.sun_path, sizeof(uctrl->saddr.un.sun_path), "/run/udev/control");
106 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
107 return uctrl;
108 }
109
110 struct udev_ctrl *udev_ctrl_new(void) {
111 return udev_ctrl_new_from_fd(-1);
112 }
113
114 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
115 int err;
116
117 if (!uctrl->bound) {
118 err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
119 if (err < 0 && errno == EADDRINUSE) {
120 unlink(uctrl->saddr.un.sun_path);
121 err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
122 }
123
124 if (err < 0)
125 return log_error_errno(errno, "bind failed: %m");
126
127 err = listen(uctrl->sock, 0);
128 if (err < 0)
129 return log_error_errno(errno, "listen failed: %m");
130
131 uctrl->bound = true;
132 uctrl->cleanup_socket = true;
133 }
134 return 0;
135 }
136
137 static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
138 assert(uctrl);
139
140 safe_close(uctrl->sock);
141 return mfree(uctrl);
142 }
143
144 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl);
145 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
146
147 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
148 if (uctrl == NULL)
149 return 0;
150 if (uctrl->cleanup_socket)
151 unlink(uctrl->saddr.un.sun_path);
152 return 0;
153 }
154
155 int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
156 if (uctrl == NULL)
157 return -EINVAL;
158 return uctrl->sock;
159 }
160
161 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
162 struct udev_ctrl_connection *conn;
163 struct ucred ucred = {};
164 const int on = 1;
165 int r;
166
167 conn = new(struct udev_ctrl_connection, 1);
168 if (conn == NULL)
169 return NULL;
170 conn->n_ref = 1;
171 conn->uctrl = uctrl;
172
173 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
174 if (conn->sock < 0) {
175 if (errno != EINTR)
176 log_error_errno(errno, "unable to receive ctrl connection: %m");
177 goto err;
178 }
179
180 /* check peer credential of connection */
181 r = getpeercred(conn->sock, &ucred);
182 if (r < 0) {
183 log_error_errno(r, "unable to receive credentials of ctrl connection: %m");
184 goto err;
185 }
186 if (ucred.uid > 0) {
187 log_error("sender uid="UID_FMT", message ignored", ucred.uid);
188 goto err;
189 }
190
191 /* enable receiving of the sender credentials in the messages */
192 r = setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
193 if (r < 0)
194 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
195
196 udev_ctrl_ref(uctrl);
197 return conn;
198 err:
199 if (conn->sock >= 0)
200 close(conn->sock);
201 return mfree(conn);
202 }
203
204 static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) {
205 assert(conn);
206
207 safe_close(conn->sock);
208 udev_ctrl_unref(conn->uctrl);
209 return mfree(conn);
210 }
211
212 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
213
214 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
215 struct udev_ctrl_msg_wire ctrl_msg_wire;
216 int err = 0;
217
218 memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
219 strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION);
220 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
221 ctrl_msg_wire.type = type;
222
223 if (buf != NULL)
224 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
225 else
226 ctrl_msg_wire.intval = intval;
227
228 if (!uctrl->connected) {
229 if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
230 err = -errno;
231 goto out;
232 }
233 uctrl->connected = true;
234 }
235 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
236 err = -errno;
237 goto out;
238 }
239
240 /* wait for peer message handling or disconnect */
241 for (;;) {
242 struct pollfd pfd[1];
243 int r;
244
245 pfd[0].fd = uctrl->sock;
246 pfd[0].events = POLLIN;
247 r = poll(pfd, 1, timeout * MSEC_PER_SEC);
248 if (r < 0) {
249 if (errno == EINTR)
250 continue;
251 err = -errno;
252 break;
253 }
254
255 if (r > 0 && pfd[0].revents & POLLERR) {
256 err = -EIO;
257 break;
258 }
259
260 if (r == 0)
261 err = -ETIMEDOUT;
262 break;
263 }
264 out:
265 return err;
266 }
267
268 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
269 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
270 }
271
272 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
273 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
274 }
275
276 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
277 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
278 }
279
280 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
281 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
282 }
283
284 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
285 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
286 }
287
288 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
289 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
290 }
291
292 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
293 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
294 }
295
296 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
297 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
298 }
299
300 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
301 struct udev_ctrl_msg *uctrl_msg;
302 ssize_t size;
303 struct cmsghdr *cmsg;
304 struct iovec iov;
305 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
306 struct msghdr smsg = {
307 .msg_iov = &iov,
308 .msg_iovlen = 1,
309 .msg_control = cred_msg,
310 .msg_controllen = sizeof(cred_msg),
311 };
312 struct ucred *cred;
313
314 uctrl_msg = new0(struct udev_ctrl_msg, 1);
315 if (uctrl_msg == NULL)
316 return NULL;
317 uctrl_msg->n_ref = 1;
318 uctrl_msg->conn = conn;
319 udev_ctrl_connection_ref(conn);
320
321 /* wait for the incoming message */
322 for (;;) {
323 struct pollfd pfd[1];
324 int r;
325
326 pfd[0].fd = conn->sock;
327 pfd[0].events = POLLIN;
328
329 r = poll(pfd, 1, 10000);
330 if (r < 0) {
331 if (errno == EINTR)
332 continue;
333 goto err;
334 } else if (r == 0) {
335 log_error("timeout waiting for ctrl message");
336 goto err;
337 } else {
338 if (!(pfd[0].revents & POLLIN)) {
339 log_error_errno(errno, "ctrl connection error: %m");
340 goto err;
341 }
342 }
343
344 break;
345 }
346
347 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
348 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
349
350 size = recvmsg(conn->sock, &smsg, 0);
351 if (size < 0) {
352 log_error_errno(errno, "unable 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 == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
361 log_error("no sender credentials received, message ignored");
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", message ignored", 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, ignore it", 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 }