]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-ctrl.c
treewide: use log_*_errno whenever %m is in the format string
[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
KS
12#include <errno.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <stddef.h>
16#include <string.h>
17#include <unistd.h>
18#include <sys/types.h>
ff2c503d 19#include <sys/poll.h>
d59f11e1
KS
20#include <sys/socket.h>
21#include <sys/un.h>
22
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;
60 struct sockaddr_un saddr;
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 }
4bbdff75
TG
95 r = setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
96 if (r < 0)
56f64d95 97 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
912541b0
KS
98
99 uctrl->saddr.sun_family = AF_LOCAL;
d5a89d7d 100 strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
912541b0
KS
101 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
102 return uctrl;
fc1de713
KS
103}
104
9ec6e95b 105struct udev_ctrl *udev_ctrl_new(struct udev *udev) {
912541b0 106 return udev_ctrl_new_from_fd(udev, -1);
d59f11e1
KS
107}
108
9ec6e95b 109int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
912541b0
KS
110 int err;
111
112 if (!uctrl->bound) {
113 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
114 if (err < 0 && errno == EADDRINUSE) {
115 unlink(uctrl->saddr.sun_path);
116 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
117 }
118
119 if (err < 0) {
120 err = -errno;
56f64d95 121 log_error_errno(errno, "bind failed: %m");
912541b0
KS
122 return err;
123 }
124
125 err = listen(uctrl->sock, 0);
126 if (err < 0) {
127 err = -errno;
56f64d95 128 log_error_errno(errno, "listen failed: %m");
912541b0
KS
129 return err;
130 }
131
132 uctrl->bound = true;
133 uctrl->cleanup_socket = true;
134 }
135 return 0;
d59f11e1
KS
136}
137
9ec6e95b 138struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl) {
912541b0 139 return uctrl->udev;
d59f11e1
KS
140}
141
9ec6e95b 142static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) {
912541b0
KS
143 if (uctrl == NULL)
144 return NULL;
145 uctrl->refcount++;
146 return uctrl;
d59f11e1
KS
147}
148
9ec6e95b 149struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) {
912541b0
KS
150 if (uctrl == NULL)
151 return NULL;
152 uctrl->refcount--;
153 if (uctrl->refcount > 0)
154 return uctrl;
155 if (uctrl->sock >= 0)
156 close(uctrl->sock);
157 free(uctrl);
158 return NULL;
d59f11e1
KS
159}
160
9ec6e95b 161int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
912541b0
KS
162 if (uctrl == NULL)
163 return 0;
164 if (uctrl->cleanup_socket)
165 unlink(uctrl->saddr.sun_path);
166 return 0;
1f5a5100
KS
167}
168
9ec6e95b 169int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
912541b0
KS
170 if (uctrl == NULL)
171 return -EINVAL;
172 return uctrl->sock;
d59f11e1
KS
173}
174
9ec6e95b 175struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
912541b0 176 struct udev_ctrl_connection *conn;
39883f62 177 struct ucred ucred = {};
912541b0 178 const int on = 1;
eff05270 179 int r;
912541b0 180
eff05270 181 conn = new(struct udev_ctrl_connection, 1);
912541b0
KS
182 if (conn == NULL)
183 return NULL;
184 conn->refcount = 1;
185 conn->uctrl = uctrl;
186
187 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
188 if (conn->sock < 0) {
189 if (errno != EINTR)
56f64d95 190 log_error_errno(errno, "unable to receive ctrl connection: %m");
912541b0
KS
191 goto err;
192 }
193
194 /* check peer credential of connection */
eff05270
LP
195 r = getpeercred(conn->sock, &ucred);
196 if (r < 0) {
da927ba9 197 log_error_errno(r, "unable to receive credentials of ctrl connection: %m");
912541b0
KS
198 goto err;
199 }
200 if (ucred.uid > 0) {
9f6445e3 201 log_error("sender uid=%i, message ignored", ucred.uid);
912541b0
KS
202 goto err;
203 }
204
205 /* enable receiving of the sender credentials in the messages */
4bbdff75
TG
206 r = setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
207 if (r < 0)
56f64d95 208 log_warning_errno(errno, "could not set SO_PASSCRED: %m");
4bbdff75 209
912541b0
KS
210 udev_ctrl_ref(uctrl);
211 return conn;
2c64f589 212err:
912541b0
KS
213 if (conn->sock >= 0)
214 close(conn->sock);
215 free(conn);
216 return NULL;
ff2c503d
KS
217}
218
9ec6e95b 219struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) {
912541b0
KS
220 if (conn == NULL)
221 return NULL;
222 conn->refcount++;
223 return conn;
ff2c503d
KS
224}
225
9ec6e95b 226struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) {
912541b0
KS
227 if (conn == NULL)
228 return NULL;
229 conn->refcount--;
230 if (conn->refcount > 0)
231 return conn;
232 if (conn->sock >= 0)
233 close(conn->sock);
234 udev_ctrl_unref(conn->uctrl);
235 free(conn);
236 return NULL;
ff2c503d
KS
237}
238
9ec6e95b 239static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
912541b0
KS
240 struct udev_ctrl_msg_wire ctrl_msg_wire;
241 int err = 0;
242
29804cc1 243 memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
912541b0
KS
244 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
245 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
246 ctrl_msg_wire.type = type;
247
248 if (buf != NULL)
d5a89d7d 249 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
912541b0
KS
250 else
251 ctrl_msg_wire.intval = intval;
252
253 if (!uctrl->connected) {
254 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
255 err = -errno;
256 goto out;
257 }
258 uctrl->connected = true;
259 }
260 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
261 err = -errno;
262 goto out;
263 }
264
265 /* wait for peer message handling or disconnect */
266 for (;;) {
267 struct pollfd pfd[1];
268 int r;
269
270 pfd[0].fd = uctrl->sock;
271 pfd[0].events = POLLIN;
9ea28c55 272 r = poll(pfd, 1, timeout * MSEC_PER_SEC);
912541b0
KS
273 if (r < 0) {
274 if (errno == EINTR)
275 continue;
276 err = -errno;
277 break;
278 }
279
280 if (r > 0 && pfd[0].revents & POLLERR) {
281 err = -EIO;
282 break;
283 }
284
285 if (r == 0)
286 err = -ETIMEDOUT;
287 break;
288 }
ff2c503d 289out:
912541b0 290 return err;
d59f11e1
KS
291}
292
9ec6e95b 293int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
912541b0 294 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
ff2c503d
KS
295}
296
9ec6e95b 297int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
912541b0 298 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
d59f11e1
KS
299}
300
9ec6e95b 301int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
912541b0 302 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
d59f11e1
KS
303}
304
9ec6e95b 305int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
912541b0 306 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
d59f11e1
KS
307}
308
9ec6e95b 309int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
912541b0 310 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
d59f11e1
KS
311}
312
9ec6e95b 313int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
912541b0 314 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
d59f11e1
KS
315}
316
9ec6e95b 317int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
912541b0 318 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
d59f11e1
KS
319}
320
9ec6e95b 321int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
912541b0 322 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
bb38678e
SJR
323}
324
9ec6e95b 325struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
912541b0
KS
326 struct udev_ctrl_msg *uctrl_msg;
327 ssize_t size;
912541b0
KS
328 struct cmsghdr *cmsg;
329 struct iovec iov;
912541b0 330 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
97fec53e
ZJS
331 struct msghdr smsg = {
332 .msg_iov = &iov,
333 .msg_iovlen = 1,
334 .msg_control = cred_msg,
335 .msg_controllen = sizeof(cred_msg),
336 };
337 struct ucred *cred;
912541b0 338
97fec53e 339 uctrl_msg = new0(struct udev_ctrl_msg, 1);
912541b0
KS
340 if (uctrl_msg == NULL)
341 return NULL;
342 uctrl_msg->refcount = 1;
343 uctrl_msg->conn = conn;
344 udev_ctrl_connection_ref(conn);
345
346 /* wait for the incoming message */
f168c273 347 for (;;) {
912541b0
KS
348 struct pollfd pfd[1];
349 int r;
350
351 pfd[0].fd = conn->sock;
352 pfd[0].events = POLLIN;
353
354 r = poll(pfd, 1, 10000);
355 if (r < 0) {
356 if (errno == EINTR)
357 continue;
358 goto err;
359 } else if (r == 0) {
9f6445e3 360 log_error("timeout waiting for ctrl message");
912541b0
KS
361 goto err;
362 } else {
363 if (!(pfd[0].revents & POLLIN)) {
56f64d95 364 log_error_errno(errno, "ctrl connection error: %m");
912541b0
KS
365 goto err;
366 }
367 }
368
369 break;
370 }
371
372 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
373 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
97fec53e 374
912541b0
KS
375 size = recvmsg(conn->sock, &smsg, 0);
376 if (size < 0) {
56f64d95 377 log_error_errno(errno, "unable to receive ctrl message: %m");
912541b0
KS
378 goto err;
379 }
380 cmsg = CMSG_FIRSTHDR(&smsg);
381 cred = (struct ucred *) CMSG_DATA(cmsg);
382
383 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
9f6445e3 384 log_error("no sender credentials received, message ignored");
912541b0
KS
385 goto err;
386 }
387
388 if (cred->uid != 0) {
9f6445e3 389 log_error("sender uid=%i, message ignored", cred->uid);
912541b0
KS
390 goto err;
391 }
392
393 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
9f6445e3 394 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg->ctrl_msg_wire.magic);
912541b0
KS
395 goto err;
396 }
397
912541b0 398 return uctrl_msg;
d59f11e1 399err:
912541b0
KS
400 udev_ctrl_msg_unref(uctrl_msg);
401 return NULL;
d59f11e1
KS
402}
403
9ec6e95b 404struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
405 if (ctrl_msg == NULL)
406 return NULL;
407 ctrl_msg->refcount--;
408 if (ctrl_msg->refcount > 0)
409 return ctrl_msg;
912541b0
KS
410 udev_ctrl_connection_unref(ctrl_msg->conn);
411 free(ctrl_msg);
412 return NULL;
d59f11e1
KS
413}
414
9ec6e95b 415int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
416 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
417 return ctrl_msg->ctrl_msg_wire.intval;
418 return -1;
d59f11e1
KS
419}
420
9ec6e95b 421int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
422 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
423 return 1;
424 return -1;
d59f11e1
KS
425}
426
9ec6e95b 427int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
428 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
429 return 1;
430 return -1;
d59f11e1
KS
431}
432
9ec6e95b 433int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
434 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
435 return 1;
436 return -1;
d59f11e1
KS
437}
438
9ec6e95b 439const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
440 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
441 return ctrl_msg->ctrl_msg_wire.buf;
442 return NULL;
d59f11e1
KS
443}
444
9ec6e95b 445int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
446 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
447 return ctrl_msg->ctrl_msg_wire.intval;
448 return -1;
d59f11e1 449}
bb38678e 450
9ec6e95b 451int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
452 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
453 return 1;
454 return -1;
ff2c503d
KS
455}
456
9ec6e95b 457int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
912541b0
KS
458 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
459 return 1;
460 return -1;
bb38678e 461}