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