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