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"
23 #include "socket-util.h"
26 /* wire protocol magic must match */
27 #define UDEV_CTRL_MAGIC 0xdead1dea
29 enum udev_ctrl_msg_type
{
31 UDEV_CTRL_SET_LOG_LEVEL
,
32 UDEV_CTRL_STOP_EXEC_QUEUE
,
33 UDEV_CTRL_START_EXEC_QUEUE
,
36 UDEV_CTRL_SET_CHILDREN_MAX
,
41 struct udev_ctrl_msg_wire
{
44 enum udev_ctrl_msg_type type
;
51 struct udev_ctrl_msg
{
53 struct udev_ctrl_connection
*conn
;
54 struct udev_ctrl_msg_wire ctrl_msg_wire
;
60 union sockaddr_union saddr
;
67 struct udev_ctrl_connection
{
69 struct udev_ctrl
*uctrl
;
73 struct udev_ctrl
*udev_ctrl_new_from_fd(int fd
) {
74 struct udev_ctrl
*uctrl
;
78 uctrl
= new0(struct udev_ctrl
, 1);
84 uctrl
->sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
85 if (uctrl
->sock
< 0) {
86 log_error_errno(errno
, "error getting socket: %m");
87 udev_ctrl_unref(uctrl
);
96 * FIXME: remove it as soon as we can depend on this:
97 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
99 r
= setsockopt(uctrl
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
101 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
103 uctrl
->saddr
.un
.sun_family
= AF_LOCAL
;
104 strscpy(uctrl
->saddr
.un
.sun_path
, sizeof(uctrl
->saddr
.un
.sun_path
), "/run/udev/control");
105 uctrl
->addrlen
= SOCKADDR_UN_LEN(uctrl
->saddr
.un
);
109 struct udev_ctrl
*udev_ctrl_new(void) {
110 return udev_ctrl_new_from_fd(-1);
113 int udev_ctrl_enable_receiving(struct udev_ctrl
*uctrl
) {
117 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
118 if (err
< 0 && errno
== EADDRINUSE
) {
119 unlink(uctrl
->saddr
.un
.sun_path
);
120 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
124 return log_error_errno(errno
, "bind failed: %m");
126 err
= listen(uctrl
->sock
, 0);
128 return log_error_errno(errno
, "listen failed: %m");
131 uctrl
->cleanup_socket
= true;
136 static struct udev_ctrl
*udev_ctrl_free(struct udev_ctrl
*uctrl
) {
139 safe_close(uctrl
->sock
);
143 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl
, udev_ctrl
);
144 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl
, udev_ctrl
, udev_ctrl_free
);
146 int udev_ctrl_cleanup(struct udev_ctrl
*uctrl
) {
149 if (uctrl
->cleanup_socket
)
150 unlink(uctrl
->saddr
.un
.sun_path
);
154 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
) {
160 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
) {
161 struct udev_ctrl_connection
*conn
;
162 struct ucred ucred
= {};
166 conn
= new(struct udev_ctrl_connection
, 1);
172 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
|SOCK_NONBLOCK
);
173 if (conn
->sock
< 0) {
175 log_error_errno(errno
, "unable to receive ctrl connection: %m");
179 /* check peer credential of connection */
180 r
= getpeercred(conn
->sock
, &ucred
);
182 log_error_errno(r
, "unable to receive credentials of ctrl connection: %m");
186 log_error("sender uid="UID_FMT
", message ignored", ucred
.uid
);
190 /* enable receiving of the sender credentials in the messages */
191 r
= setsockopt(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
193 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
195 udev_ctrl_ref(uctrl
);
203 static struct udev_ctrl_connection
*udev_ctrl_connection_free(struct udev_ctrl_connection
*conn
) {
206 safe_close(conn
->sock
);
207 udev_ctrl_unref(conn
->uctrl
);
211 DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection
, udev_ctrl_connection
, udev_ctrl_connection_free
);
213 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, int timeout
) {
214 struct udev_ctrl_msg_wire ctrl_msg_wire
;
217 memzero(&ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
218 strcpy(ctrl_msg_wire
.version
, "udev-" PACKAGE_VERSION
);
219 ctrl_msg_wire
.magic
= UDEV_CTRL_MAGIC
;
220 ctrl_msg_wire
.type
= type
;
223 strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
225 ctrl_msg_wire
.intval
= intval
;
227 if (!uctrl
->connected
) {
228 if (connect(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
) < 0) {
232 uctrl
->connected
= true;
234 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
[1];
244 pfd
[0].fd
= uctrl
->sock
;
245 pfd
[0].events
= POLLIN
;
246 r
= poll(pfd
, 1, timeout
* MSEC_PER_SEC
);
254 if (r
> 0 && pfd
[0].revents
& POLLERR
) {
267 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, int timeout
) {
268 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
271 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
272 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
275 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
276 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
279 int udev_ctrl_send_reload(struct udev_ctrl
*uctrl
, int timeout
) {
280 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD
, 0, NULL
, timeout
);
283 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, int timeout
) {
284 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
287 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, int timeout
) {
288 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
291 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, int timeout
) {
292 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
295 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, int timeout
) {
296 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
299 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
) {
300 struct udev_ctrl_msg
*uctrl_msg
;
302 struct cmsghdr
*cmsg
;
304 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
305 struct msghdr smsg
= {
308 .msg_control
= cred_msg
,
309 .msg_controllen
= sizeof(cred_msg
),
313 uctrl_msg
= new0(struct udev_ctrl_msg
, 1);
314 if (uctrl_msg
== NULL
)
316 uctrl_msg
->n_ref
= 1;
317 uctrl_msg
->conn
= conn
;
318 udev_ctrl_connection_ref(conn
);
320 /* wait for the incoming message */
322 struct pollfd pfd
[1];
325 pfd
[0].fd
= conn
->sock
;
326 pfd
[0].events
= POLLIN
;
328 r
= poll(pfd
, 1, 10000);
334 log_error("timeout waiting for ctrl message");
337 if (!(pfd
[0].revents
& POLLIN
)) {
338 log_error_errno(errno
, "ctrl connection error: %m");
346 iov
.iov_base
= &uctrl_msg
->ctrl_msg_wire
;
347 iov
.iov_len
= sizeof(struct udev_ctrl_msg_wire
);
349 size
= recvmsg(conn
->sock
, &smsg
, 0);
351 log_error_errno(errno
, "unable to receive ctrl message: %m");
355 cmsg_close_all(&smsg
);
357 cmsg
= CMSG_FIRSTHDR(&smsg
);
359 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
360 log_error("no sender credentials received, message ignored");
364 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
366 if (cred
->uid
!= 0) {
367 log_error("sender uid="UID_FMT
", message ignored", cred
->uid
);
371 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
372 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg
->ctrl_msg_wire
.magic
);
378 udev_ctrl_msg_unref(uctrl_msg
);
382 static struct udev_ctrl_msg
*udev_ctrl_msg_free(struct udev_ctrl_msg
*ctrl_msg
) {
385 udev_ctrl_connection_unref(ctrl_msg
->conn
);
386 return mfree(ctrl_msg
);
389 DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg
, udev_ctrl_msg
, udev_ctrl_msg_free
);
391 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg
*ctrl_msg
) {
392 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_LOG_LEVEL
)
393 return ctrl_msg
->ctrl_msg_wire
.intval
;
397 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
398 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
403 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
404 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_START_EXEC_QUEUE
)
409 int udev_ctrl_get_reload(struct udev_ctrl_msg
*ctrl_msg
) {
410 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD
)
415 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
) {
416 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
417 return ctrl_msg
->ctrl_msg_wire
.buf
;
421 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
) {
422 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
423 return ctrl_msg
->ctrl_msg_wire
.intval
;
427 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
) {
428 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
433 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
) {
434 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)