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