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 struct udev_ctrl
*udev_ctrl_new_from_fd(int fd
) {
77 struct udev_ctrl
*uctrl
;
80 uctrl
= new0(struct udev_ctrl
, 1);
86 uctrl
->sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
87 if (uctrl
->sock
< 0) {
88 log_error_errno(errno
, "Failed to create socket: %m");
89 udev_ctrl_unref(uctrl
);
98 * FIXME: remove it as soon as we can depend on this:
99 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
101 r
= setsockopt_int(uctrl
->sock
, SOL_SOCKET
, SO_PASSCRED
, true);
103 log_warning_errno(r
, "Failed to set SO_PASSCRED: %m");
105 uctrl
->saddr
.un
= (struct sockaddr_un
) {
106 .sun_family
= AF_UNIX
,
107 .sun_path
= "/run/udev/control",
110 uctrl
->addrlen
= SOCKADDR_UN_LEN(uctrl
->saddr
.un
);
114 struct udev_ctrl
*udev_ctrl_new(void) {
115 return udev_ctrl_new_from_fd(-1);
118 int udev_ctrl_enable_receiving(struct udev_ctrl
*uctrl
) {
122 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
123 if (err
< 0 && errno
== EADDRINUSE
) {
124 (void) sockaddr_un_unlink(&uctrl
->saddr
.un
);
125 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
129 return log_error_errno(errno
, "Failed to bind socket: %m");
131 err
= listen(uctrl
->sock
, 0);
133 return log_error_errno(errno
, "Failed to listen: %m");
136 uctrl
->cleanup_socket
= true;
141 static struct udev_ctrl
*udev_ctrl_free(struct udev_ctrl
*uctrl
) {
144 safe_close(uctrl
->sock
);
148 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl
, udev_ctrl
);
149 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl
, udev_ctrl
, udev_ctrl_free
);
151 int udev_ctrl_cleanup(struct udev_ctrl
*uctrl
) {
154 if (uctrl
->cleanup_socket
)
155 sockaddr_un_unlink(&uctrl
->saddr
.un
);
159 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
) {
165 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
) {
166 struct udev_ctrl_connection
*conn
;
167 struct ucred ucred
= {};
170 conn
= new(struct udev_ctrl_connection
, 1);
176 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
|SOCK_NONBLOCK
);
177 if (conn
->sock
< 0) {
179 log_error_errno(errno
, "Failed to receive ctrl connection: %m");
183 /* check peer credential of connection */
184 r
= getpeercred(conn
->sock
, &ucred
);
186 log_error_errno(r
, "Failed to receive credentials of ctrl connection: %m");
190 log_error("Sender uid="UID_FMT
", message ignored", ucred
.uid
);
194 /* enable receiving of the sender credentials in the messages */
195 r
= setsockopt_int(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, true);
197 log_warning_errno(r
, "Failed to set SO_PASSCRED: %m");
199 udev_ctrl_ref(uctrl
);
202 safe_close(conn
->sock
);
206 static struct udev_ctrl_connection
*udev_ctrl_connection_free(struct udev_ctrl_connection
*conn
) {
209 safe_close(conn
->sock
);
210 udev_ctrl_unref(conn
->uctrl
);
214 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection
, udev_ctrl_connection
, udev_ctrl_connection_free
);
216 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, int timeout
) {
217 struct udev_ctrl_msg_wire ctrl_msg_wire
;
220 memzero(&ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
221 strcpy(ctrl_msg_wire
.version
, "udev-" PACKAGE_VERSION
);
222 ctrl_msg_wire
.magic
= UDEV_CTRL_MAGIC
;
223 ctrl_msg_wire
.type
= type
;
226 strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
228 ctrl_msg_wire
.intval
= intval
;
230 if (!uctrl
->connected
) {
231 if (connect(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
) < 0) {
235 uctrl
->connected
= true;
237 if (send(uctrl
->sock
, &ctrl_msg_wire
, sizeof(ctrl_msg_wire
), 0) < 0) {
242 /* wait for peer message handling or disconnect */
244 struct pollfd pfd
[1];
247 pfd
[0].fd
= uctrl
->sock
;
248 pfd
[0].events
= POLLIN
;
249 r
= poll(pfd
, 1, timeout
* MSEC_PER_SEC
);
257 if (r
> 0 && pfd
[0].revents
& POLLERR
) {
270 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, int timeout
) {
271 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
274 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
275 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
278 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
279 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
282 int udev_ctrl_send_reload(struct udev_ctrl
*uctrl
, int timeout
) {
283 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD
, 0, NULL
, timeout
);
286 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, int timeout
) {
287 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
290 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, int timeout
) {
291 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
294 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, int timeout
) {
295 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
298 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, int timeout
) {
299 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
302 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
) {
303 struct udev_ctrl_msg
*uctrl_msg
;
305 struct cmsghdr
*cmsg
;
307 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
308 struct msghdr smsg
= {
311 .msg_control
= cred_msg
,
312 .msg_controllen
= sizeof(cred_msg
),
316 uctrl_msg
= new0(struct udev_ctrl_msg
, 1);
319 uctrl_msg
->n_ref
= 1;
320 uctrl_msg
->conn
= conn
;
321 udev_ctrl_connection_ref(conn
);
323 /* wait for the incoming message */
325 struct pollfd pfd
[1];
328 pfd
[0].fd
= conn
->sock
;
329 pfd
[0].events
= POLLIN
;
331 r
= poll(pfd
, 1, 10000);
337 log_error("Timeout waiting for ctrl message");
340 if (!(pfd
[0].revents
& POLLIN
)) {
341 log_error("Invalid ctrl connection: %m");
349 iov
= IOVEC_MAKE(&uctrl_msg
->ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
351 size
= recvmsg(conn
->sock
, &smsg
, 0);
353 log_error_errno(errno
, "Failed to receive ctrl message: %m");
357 cmsg_close_all(&smsg
);
359 cmsg
= CMSG_FIRSTHDR(&smsg
);
361 if (!cmsg
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
362 log_error("No sender credentials received, ignoring message");
366 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
368 if (cred
->uid
!= 0) {
369 log_error("Sender uid="UID_FMT
", ignoring message", cred
->uid
);
373 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
374 log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg
->ctrl_msg_wire
.magic
);
380 udev_ctrl_msg_unref(uctrl_msg
);
384 static struct udev_ctrl_msg
*udev_ctrl_msg_free(struct udev_ctrl_msg
*ctrl_msg
) {
387 udev_ctrl_connection_unref(ctrl_msg
->conn
);
388 return mfree(ctrl_msg
);
391 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg
, udev_ctrl_msg
, udev_ctrl_msg_free
);
393 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg
*ctrl_msg
) {
394 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_LOG_LEVEL
)
395 return ctrl_msg
->ctrl_msg_wire
.intval
;
399 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
400 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
405 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
406 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_START_EXEC_QUEUE
)
411 int udev_ctrl_get_reload(struct udev_ctrl_msg
*ctrl_msg
) {
412 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD
)
417 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
) {
418 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
419 return ctrl_msg
->ctrl_msg_wire
.buf
;
423 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
) {
424 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
425 return ctrl_msg
->ctrl_msg_wire
.intval
;
429 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
) {
430 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
435 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
) {
436 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)