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