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