1 /* SPDX-License-Identifier: LGPL-2.1+
3 * libudev - interface to udev device information
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.
16 #include <sys/socket.h>
20 #include "alloc-util.h"
22 #include "format-util.h"
24 #include "socket-util.h"
26 #include "udev-ctrl.h"
29 /* wire protocol magic must match */
30 #define UDEV_CTRL_MAGIC 0xdead1dea
32 enum udev_ctrl_msg_type
{
34 UDEV_CTRL_SET_LOG_LEVEL
,
35 UDEV_CTRL_STOP_EXEC_QUEUE
,
36 UDEV_CTRL_START_EXEC_QUEUE
,
39 UDEV_CTRL_SET_CHILDREN_MAX
,
44 struct udev_ctrl_msg_wire
{
47 enum udev_ctrl_msg_type type
;
54 struct udev_ctrl_msg
{
56 struct udev_ctrl_connection
*conn
;
57 struct udev_ctrl_msg_wire ctrl_msg_wire
;
63 union sockaddr_union saddr
;
70 struct udev_ctrl_connection
{
72 struct udev_ctrl
*uctrl
;
76 int udev_ctrl_new_from_fd(struct udev_ctrl
**ret
, int fd
) {
77 _cleanup_close_
int sock
= -1;
78 struct udev_ctrl
*uctrl
;
84 sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
86 return log_error_errno(errno
, "Failed to create socket: %m");
89 uctrl
= new(struct udev_ctrl
, 1);
93 *uctrl
= (struct udev_ctrl
) {
95 .sock
= fd
>= 0 ? fd
: TAKE_FD(sock
),
100 * FIXME: remove it as soon as we can depend on this:
101 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
103 r
= setsockopt_int(uctrl
->sock
, SOL_SOCKET
, SO_PASSCRED
, true);
105 log_warning_errno(r
, "Failed to set SO_PASSCRED: %m");
107 uctrl
->saddr
.un
= (struct sockaddr_un
) {
108 .sun_family
= AF_UNIX
,
109 .sun_path
= "/run/udev/control",
112 uctrl
->addrlen
= SOCKADDR_UN_LEN(uctrl
->saddr
.un
);
114 *ret
= TAKE_PTR(uctrl
);
118 int udev_ctrl_enable_receiving(struct udev_ctrl
*uctrl
) {
126 r
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
127 if (r
< 0 && errno
== EADDRINUSE
) {
128 (void) sockaddr_un_unlink(&uctrl
->saddr
.un
);
129 r
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
133 return log_error_errno(errno
, "Failed to bind udev control socket: %m");
135 if (listen(uctrl
->sock
, 0) < 0)
136 return log_error_errno(errno
, "Failed to listen udev control socket: %m");
139 uctrl
->cleanup_socket
= true;
144 static struct udev_ctrl
*udev_ctrl_free(struct udev_ctrl
*uctrl
) {
147 safe_close(uctrl
->sock
);
151 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl
, udev_ctrl
);
152 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl
, udev_ctrl
, udev_ctrl_free
);
154 int udev_ctrl_cleanup(struct udev_ctrl
*uctrl
) {
157 if (uctrl
->cleanup_socket
)
158 sockaddr_un_unlink(&uctrl
->saddr
.un
);
162 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
) {
168 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
) {
169 struct udev_ctrl_connection
*conn
;
170 struct ucred ucred
= {};
173 conn
= new(struct udev_ctrl_connection
, 1);
179 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
|SOCK_NONBLOCK
);
180 if (conn
->sock
< 0) {
182 log_error_errno(errno
, "Failed to receive ctrl connection: %m");
186 /* check peer credential of connection */
187 r
= getpeercred(conn
->sock
, &ucred
);
189 log_error_errno(r
, "Failed to receive credentials of ctrl connection: %m");
193 log_error("Sender uid="UID_FMT
", message ignored", ucred
.uid
);
197 /* enable receiving of the sender credentials in the messages */
198 r
= setsockopt_int(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, true);
200 log_warning_errno(r
, "Failed to set SO_PASSCRED: %m");
202 udev_ctrl_ref(uctrl
);
205 safe_close(conn
->sock
);
209 static struct udev_ctrl_connection
*udev_ctrl_connection_free(struct udev_ctrl_connection
*conn
) {
212 safe_close(conn
->sock
);
213 udev_ctrl_unref(conn
->uctrl
);
217 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection
, udev_ctrl_connection
, udev_ctrl_connection_free
);
219 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, usec_t timeout
) {
220 struct udev_ctrl_msg_wire ctrl_msg_wire
= {
221 .version
= "udev-" STRINGIFY(PROJECT_VERSION
),
222 .magic
= UDEV_CTRL_MAGIC
,
227 strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
229 ctrl_msg_wire
.intval
= intval
;
231 if (!uctrl
->connected
) {
232 if (connect(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
) < 0)
234 uctrl
->connected
= true;
236 if (send(uctrl
->sock
, &ctrl_msg_wire
, sizeof(ctrl_msg_wire
), 0) < 0)
239 /* wait for peer message handling or disconnect */
241 struct pollfd pfd
= {
247 r
= poll(&pfd
, 1, DIV_ROUND_UP(timeout
, USEC_PER_MSEC
));
255 if (pfd
.revents
& POLLERR
)
261 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, usec_t timeout
) {
262 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
265 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, usec_t timeout
) {
266 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
269 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, usec_t timeout
) {
270 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
273 int udev_ctrl_send_reload(struct udev_ctrl
*uctrl
, usec_t timeout
) {
274 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD
, 0, NULL
, timeout
);
277 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, usec_t timeout
) {
278 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
281 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, usec_t timeout
) {
282 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
285 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, usec_t timeout
) {
286 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
289 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, usec_t timeout
) {
290 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
293 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
) {
294 struct udev_ctrl_msg
*uctrl_msg
;
296 struct cmsghdr
*cmsg
;
298 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
299 struct msghdr smsg
= {
302 .msg_control
= cred_msg
,
303 .msg_controllen
= sizeof(cred_msg
),
307 uctrl_msg
= new0(struct udev_ctrl_msg
, 1);
310 uctrl_msg
->n_ref
= 1;
311 uctrl_msg
->conn
= conn
;
312 udev_ctrl_connection_ref(conn
);
314 /* wait for the incoming message */
316 struct pollfd pfd
[1];
319 pfd
[0].fd
= conn
->sock
;
320 pfd
[0].events
= POLLIN
;
322 r
= poll(pfd
, 1, 10000);
328 log_error("Timeout waiting for ctrl message");
331 if (!(pfd
[0].revents
& POLLIN
)) {
332 log_error("Invalid ctrl connection: %m");
340 iov
= IOVEC_MAKE(&uctrl_msg
->ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
342 size
= recvmsg(conn
->sock
, &smsg
, 0);
344 log_error_errno(errno
, "Failed to receive ctrl message: %m");
348 cmsg_close_all(&smsg
);
350 cmsg
= CMSG_FIRSTHDR(&smsg
);
352 if (!cmsg
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
353 log_error("No sender credentials received, ignoring message");
357 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
359 if (cred
->uid
!= 0) {
360 log_error("Sender uid="UID_FMT
", ignoring message", cred
->uid
);
364 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
365 log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg
->ctrl_msg_wire
.magic
);
371 udev_ctrl_msg_unref(uctrl_msg
);
375 static struct udev_ctrl_msg
*udev_ctrl_msg_free(struct udev_ctrl_msg
*ctrl_msg
) {
378 udev_ctrl_connection_unref(ctrl_msg
->conn
);
379 return mfree(ctrl_msg
);
382 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg
, udev_ctrl_msg
, udev_ctrl_msg_free
);
384 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg
*ctrl_msg
) {
385 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_LOG_LEVEL
)
386 return ctrl_msg
->ctrl_msg_wire
.intval
;
390 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
391 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
396 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
397 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_START_EXEC_QUEUE
)
402 int udev_ctrl_get_reload(struct udev_ctrl_msg
*ctrl_msg
) {
403 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD
)
408 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
) {
409 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
410 return ctrl_msg
->ctrl_msg_wire
.buf
;
414 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
) {
415 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
416 return ctrl_msg
->ctrl_msg_wire
.intval
;
420 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
) {
421 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
426 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
) {
427 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)