]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-ctrl.c
udev: drop meaningless size optimization
[thirdparty/systemd.git] / src / udev / udev-ctrl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later
d9215cd8 2 *
55e9959b 3 * libudev - interface to udev device information
d59f11e1 4 *
4061ab9f
KS
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
d59f11e1
KS
9 */
10
d59f11e1 11#include <errno.h>
cf0fbc49 12#include <poll.h>
d59f11e1 13#include <stddef.h>
cf0fbc49 14#include <stdlib.h>
d59f11e1 15#include <string.h>
d59f11e1 16#include <sys/un.h>
cf0fbc49 17#include <unistd.h>
d59f11e1 18
d02c6f54
YW
19#include "sd-event.h"
20
b5efdb8a 21#include "alloc-util.h"
4ff9bc2e 22#include "errno-util.h"
3ffd4af2 23#include "fd-util.h"
f97b34a6 24#include "format-util.h"
5cfa2c3d 25#include "io-util.h"
3ffd4af2 26#include "socket-util.h"
7d68eb1b
YW
27#include "strxcpyx.h"
28#include "udev-ctrl.h"
ef118d00 29#include "util.h"
d59f11e1 30
540f4669 31/* wire protocol magic must match */
912541b0 32#define UDEV_CTRL_MAGIC 0xdead1dea
d59f11e1 33
b692a750 34struct udev_ctrl_msg_wire {
912541b0 35 char version[16];
14cb109d 36 unsigned magic;
912541b0 37 enum udev_ctrl_msg_type type;
d02c6f54 38 union udev_ctrl_msg_value value;
d59f11e1
KS
39};
40
41struct udev_ctrl {
8f71a0d1 42 unsigned n_ref;
912541b0 43 int sock;
d02c6f54 44 int sock_connect;
6421348d 45 union sockaddr_union saddr;
912541b0 46 socklen_t addrlen;
481f24d1
YW
47 bool bound;
48 bool cleanup_socket;
49 bool connected;
50 bool maybe_disconnected;
d02c6f54
YW
51 sd_event *event;
52 sd_event_source *event_source;
53 sd_event_source *event_source_connect;
54 udev_ctrl_handler_t callback;
55 void *userdata;
d59f11e1
KS
56};
57
100bc5bf
YW
58int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) {
59 _cleanup_close_ int sock = -1;
912541b0
KS
60 struct udev_ctrl *uctrl;
61
100bc5bf 62 assert(ret);
912541b0
KS
63
64 if (fd < 0) {
100bc5bf
YW
65 sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
66 if (sock < 0)
67 return log_error_errno(errno, "Failed to create socket: %m");
912541b0 68 }
25568304 69
100bc5bf
YW
70 uctrl = new(struct udev_ctrl, 1);
71 if (!uctrl)
72 return -ENOMEM;
73
74 *uctrl = (struct udev_ctrl) {
75 .n_ref = 1,
76 .sock = fd >= 0 ? fd : TAKE_FD(sock),
2f5b282a 77 .sock_connect = -1,
100bc5bf
YW
78 .bound = fd >= 0,
79 };
80
44ed5214
LP
81 uctrl->saddr.un = (struct sockaddr_un) {
82 .sun_family = AF_UNIX,
83 .sun_path = "/run/udev/control",
84 };
85
fc2fffe7 86 uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
fc1de713 87
100bc5bf
YW
88 *ret = TAKE_PTR(uctrl);
89 return 0;
d59f11e1
KS
90}
91
9ec6e95b 92int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
53bba2fb 93 int r;
912541b0 94
53bba2fb 95 assert(uctrl);
912541b0 96
53bba2fb
YW
97 if (uctrl->bound)
98 return 0;
912541b0 99
53bba2fb
YW
100 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
101 if (r < 0 && errno == EADDRINUSE) {
102 (void) sockaddr_un_unlink(&uctrl->saddr.un);
103 r = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
912541b0 104 }
53bba2fb
YW
105
106 if (r < 0)
107 return log_error_errno(errno, "Failed to bind udev control socket: %m");
108
109 if (listen(uctrl->sock, 0) < 0)
110 return log_error_errno(errno, "Failed to listen udev control socket: %m");
111
112 uctrl->bound = true;
113 uctrl->cleanup_socket = true;
114
912541b0 115 return 0;
d59f11e1
KS
116}
117
d02c6f54
YW
118static void udev_ctrl_disconnect(struct udev_ctrl *uctrl) {
119 if (!uctrl)
120 return;
121
122 uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect);
123 uctrl->sock_connect = safe_close(uctrl->sock_connect);
124}
125
8f71a0d1
YW
126static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
127 assert(uctrl);
35927d13 128
d02c6f54
YW
129 udev_ctrl_disconnect(uctrl);
130
131 sd_event_source_unref(uctrl->event_source);
8f71a0d1 132 safe_close(uctrl->sock);
d02c6f54
YW
133
134 sd_event_unref(uctrl->event);
8f71a0d1 135 return mfree(uctrl);
d59f11e1
KS
136}
137
d02c6f54 138DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
d59f11e1 139
9ec6e95b 140int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
9315f853 141 if (!uctrl)
912541b0
KS
142 return 0;
143 if (uctrl->cleanup_socket)
155b6876 144 sockaddr_un_unlink(&uctrl->saddr.un);
912541b0 145 return 0;
1f5a5100
KS
146}
147
d02c6f54
YW
148int udev_ctrl_attach_event(struct udev_ctrl *uctrl, sd_event *event) {
149 int r;
150
151 assert_return(uctrl, -EINVAL);
152 assert_return(!uctrl->event, -EBUSY);
153
154 if (event)
155 uctrl->event = sd_event_ref(event);
156 else {
157 r = sd_event_default(&uctrl->event);
158 if (r < 0)
159 return r;
160 }
161
162 return 0;
d59f11e1
KS
163}
164
d02c6f54
YW
165sd_event_source *udev_ctrl_get_event_source(struct udev_ctrl *uctrl) {
166 assert(uctrl);
167
168 return uctrl->event_source;
169}
170
171static void udev_ctrl_disconnect_and_listen_again(struct udev_ctrl *uctrl) {
172 udev_ctrl_disconnect(uctrl);
173 udev_ctrl_unref(uctrl);
174 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON);
175}
176
177DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl *, udev_ctrl_disconnect_and_listen_again);
178
179static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
180 _cleanup_(udev_ctrl_disconnect_and_listen_againp) struct udev_ctrl *uctrl = NULL;
181 struct udev_ctrl_msg_wire msg_wire;
182 struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(struct udev_ctrl_msg_wire));
fb29cdbe 183 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
d02c6f54
YW
184 struct msghdr smsg = {
185 .msg_iov = &iov,
186 .msg_iovlen = 1,
fb29cdbe
LP
187 .msg_control = &control,
188 .msg_controllen = sizeof(control),
d02c6f54
YW
189 };
190 struct cmsghdr *cmsg;
191 struct ucred *cred;
192 ssize_t size;
193
194 assert(userdata);
195
196 /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object.
197 * To avoid the object freed, let's increment the refcount. */
198 uctrl = udev_ctrl_ref(userdata);
199
200 size = next_datagram_size_fd(fd);
201 if (size < 0)
202 return log_error_errno(size, "Failed to get size of message: %m");
203 if (size == 0)
204 return 0; /* Client disconnects? */
205
3691bcf3
LP
206 size = recvmsg_safe(fd, &smsg, 0);
207 if (size == -EINTR)
d02c6f54 208 return 0;
3691bcf3
LP
209 if (size < 0)
210 return log_error_errno(size, "Failed to receive ctrl message: %m");
d02c6f54
YW
211
212 cmsg_close_all(&smsg);
213
214 cmsg = CMSG_FIRSTHDR(&smsg);
215
216 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
217 log_error("No sender credentials received, ignoring message");
218 return 0;
219 }
220
221 cred = (struct ucred *) CMSG_DATA(cmsg);
222
223 if (cred->uid != 0) {
224 log_error("Invalid sender uid "UID_FMT", ignoring message", cred->uid);
225 return 0;
226 }
227
228 if (msg_wire.magic != UDEV_CTRL_MAGIC) {
229 log_error("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic);
230 return 0;
231 }
232
78467aeb
YW
233 if (msg_wire.type == _UDEV_CTRL_END_MESSAGES)
234 return 0;
235
d02c6f54
YW
236 if (uctrl->callback)
237 (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata);
238
78467aeb
YW
239 /* Do not disconnect and wait for next message. */
240 uctrl = udev_ctrl_unref(uctrl);
d02c6f54
YW
241 return 0;
242}
243
244static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
245 struct udev_ctrl *uctrl = userdata;
246 _cleanup_close_ int sock = -1;
247 struct ucred ucred;
eff05270 248 int r;
912541b0 249
d02c6f54 250 assert(uctrl);
912541b0 251
d02c6f54
YW
252 sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
253 if (sock < 0) {
4ff9bc2e
LP
254 if (ERRNO_IS_ACCEPT_AGAIN(errno))
255 return 0;
256
257 return log_error_errno(errno, "Failed to accept ctrl connection: %m");
912541b0
KS
258 }
259
260 /* check peer credential of connection */
d02c6f54 261 r = getpeercred(sock, &ucred);
eff05270 262 if (r < 0) {
572909a3 263 log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
d02c6f54 264 return 0;
912541b0 265 }
d02c6f54 266
912541b0 267 if (ucred.uid > 0) {
d02c6f54
YW
268 log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid);
269 return 0;
912541b0
KS
270 }
271
272 /* enable receiving of the sender credentials in the messages */
d02c6f54 273 r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true);
4bbdff75 274 if (r < 0)
d02c6f54 275 log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m");
4bbdff75 276
d02c6f54
YW
277 r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl);
278 if (r < 0) {
279 log_error_errno(r, "Failed to create event source for udev control connection: %m");
280 return 0;
281 }
282
283 (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection");
ff2c503d 284
d02c6f54
YW
285 /* Do not accept multiple connection. */
286 (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF);
35927d13 287
d02c6f54
YW
288 uctrl->sock_connect = TAKE_FD(sock);
289 return 0;
ff2c503d
KS
290}
291
d02c6f54
YW
292int udev_ctrl_start(struct udev_ctrl *uctrl, udev_ctrl_handler_t callback, void *userdata) {
293 int r;
294
295 assert(uctrl);
296
297 if (!uctrl->event) {
298 r = udev_ctrl_attach_event(uctrl, NULL);
299 if (r < 0)
300 return r;
301 }
302
303 r = udev_ctrl_enable_receiving(uctrl);
304 if (r < 0)
305 return r;
306
307 uctrl->callback = callback;
308 uctrl->userdata = userdata;
309
310 r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl);
311 if (r < 0)
312 return r;
313
314 (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl");
315
316 return 0;
317}
8f71a0d1 318
78467aeb 319int udev_ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf) {
b9da6a09
ZJS
320 struct udev_ctrl_msg_wire ctrl_msg_wire = {
321 .version = "udev-" STRINGIFY(PROJECT_VERSION),
322 .magic = UDEV_CTRL_MAGIC,
323 .type = type,
324 };
912541b0 325
78467aeb
YW
326 if (uctrl->maybe_disconnected)
327 return -ENOANO; /* to distinguish this from other errors. */
328
9315f853 329 if (buf)
d02c6f54 330 strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), buf);
912541b0 331 else
d02c6f54 332 ctrl_msg_wire.value.intval = intval;
912541b0
KS
333
334 if (!uctrl->connected) {
b9da6a09
ZJS
335 if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
336 return -errno;
912541b0
KS
337 uctrl->connected = true;
338 }
78467aeb 339
b9da6a09
ZJS
340 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
341 return -errno;
912541b0 342
78467aeb
YW
343 if (type == UDEV_CTRL_EXIT)
344 uctrl->maybe_disconnected = true;
d59f11e1 345
78467aeb 346 return 0;
ff2c503d
KS
347}
348
78467aeb
YW
349int udev_ctrl_wait(struct udev_ctrl *uctrl, usec_t timeout) {
350 _cleanup_(sd_event_source_unrefp) sd_event_source *source_io = NULL, *source_timeout = NULL;
351 int r;
d59f11e1 352
78467aeb 353 assert(uctrl);
d59f11e1 354
78467aeb
YW
355 if (uctrl->sock < 0)
356 return 0;
357 if (!uctrl->connected)
358 return 0;
d59f11e1 359
78467aeb
YW
360 if (!uctrl->maybe_disconnected) {
361 r = udev_ctrl_send(uctrl, _UDEV_CTRL_END_MESSAGES, 0, NULL);
362 if (r < 0)
363 return r;
364 }
d59f11e1 365
78467aeb
YW
366 if (timeout == 0)
367 return 0;
368
369 if (!uctrl->event) {
370 r = udev_ctrl_attach_event(uctrl, NULL);
371 if (r < 0)
372 return r;
373 }
374
cbda8bd5 375 r = sd_event_add_io(uctrl->event, &source_io, uctrl->sock, EPOLLIN, NULL, INT_TO_PTR(0));
78467aeb
YW
376 if (r < 0)
377 return r;
378
60ccab09 379 (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io");
78467aeb
YW
380
381 if (timeout != USEC_INFINITY) {
39cf0351
LP
382 r = sd_event_add_time_relative(
383 uctrl->event, &source_timeout, clock_boottime_or_monotonic(),
384 timeout,
385 0, NULL, INT_TO_PTR(-ETIMEDOUT));
78467aeb
YW
386 if (r < 0)
387 return r;
388
f6e8ba81 389 (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-timeout");
78467aeb 390 }
d59f11e1 391
78467aeb 392 return sd_event_loop(uctrl->event);
bb38678e 393}