2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay@vrfy.org>
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.
17 #include <sys/socket.h>
21 #include "alloc-util.h"
23 #include "formats-util.h"
24 #include "socket-util.h"
27 /* wire protocol magic must match */
28 #define UDEV_CTRL_MAGIC 0xdead1dea
30 enum udev_ctrl_msg_type
{
32 UDEV_CTRL_SET_LOG_LEVEL
,
33 UDEV_CTRL_STOP_EXEC_QUEUE
,
34 UDEV_CTRL_START_EXEC_QUEUE
,
37 UDEV_CTRL_SET_CHILDREN_MAX
,
42 struct udev_ctrl_msg_wire
{
45 enum udev_ctrl_msg_type type
;
52 struct udev_ctrl_msg
{
54 struct udev_ctrl_connection
*conn
;
55 struct udev_ctrl_msg_wire ctrl_msg_wire
;
62 union sockaddr_union saddr
;
69 struct udev_ctrl_connection
{
71 struct udev_ctrl
*uctrl
;
75 struct udev_ctrl
*udev_ctrl_new_from_fd(struct udev
*udev
, int fd
) {
76 struct udev_ctrl
*uctrl
;
80 uctrl
= new0(struct udev_ctrl
, 1);
87 uctrl
->sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
88 if (uctrl
->sock
< 0) {
89 log_error_errno(errno
, "error getting socket: %m");
90 udev_ctrl_unref(uctrl
);
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
102 r
= setsockopt(uctrl
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
104 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
106 uctrl
->saddr
.un
.sun_family
= AF_LOCAL
;
107 strscpy(uctrl
->saddr
.un
.sun_path
, sizeof(uctrl
->saddr
.un
.sun_path
), "/run/udev/control");
108 uctrl
->addrlen
= SOCKADDR_UN_LEN(uctrl
->saddr
.un
);
112 struct udev_ctrl
*udev_ctrl_new(struct udev
*udev
) {
113 return udev_ctrl_new_from_fd(udev
, -1);
116 int udev_ctrl_enable_receiving(struct udev_ctrl
*uctrl
) {
120 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
121 if (err
< 0 && errno
== EADDRINUSE
) {
122 unlink(uctrl
->saddr
.un
.sun_path
);
123 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
127 return log_error_errno(errno
, "bind failed: %m");
129 err
= listen(uctrl
->sock
, 0);
131 return log_error_errno(errno
, "listen failed: %m");
134 uctrl
->cleanup_socket
= true;
139 struct udev
*udev_ctrl_get_udev(struct udev_ctrl
*uctrl
) {
143 static struct udev_ctrl
*udev_ctrl_ref(struct udev_ctrl
*uctrl
) {
150 struct udev_ctrl
*udev_ctrl_unref(struct udev_ctrl
*uctrl
) {
151 if (uctrl
&& -- uctrl
->refcount
== 0) {
152 if (uctrl
->sock
>= 0)
160 int udev_ctrl_cleanup(struct udev_ctrl
*uctrl
) {
163 if (uctrl
->cleanup_socket
)
164 unlink(uctrl
->saddr
.un
.sun_path
);
168 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
) {
174 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
) {
175 struct udev_ctrl_connection
*conn
;
176 struct ucred ucred
= {};
180 conn
= new(struct udev_ctrl_connection
, 1);
186 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
|SOCK_NONBLOCK
);
187 if (conn
->sock
< 0) {
189 log_error_errno(errno
, "unable to receive ctrl connection: %m");
193 /* check peer credential of connection */
194 r
= getpeercred(conn
->sock
, &ucred
);
196 log_error_errno(r
, "unable to receive credentials of ctrl connection: %m");
200 log_error("sender uid="UID_FMT
", message ignored", ucred
.uid
);
204 /* enable receiving of the sender credentials in the messages */
205 r
= setsockopt(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
207 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
209 udev_ctrl_ref(uctrl
);
218 struct udev_ctrl_connection
*udev_ctrl_connection_ref(struct udev_ctrl_connection
*conn
) {
225 struct udev_ctrl_connection
*udev_ctrl_connection_unref(struct udev_ctrl_connection
*conn
) {
226 if (conn
&& -- conn
->refcount
== 0) {
230 udev_ctrl_unref(conn
->uctrl
);
238 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, int timeout
) {
239 struct udev_ctrl_msg_wire ctrl_msg_wire
;
242 memzero(&ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
243 strcpy(ctrl_msg_wire
.version
, "udev-" VERSION
);
244 ctrl_msg_wire
.magic
= UDEV_CTRL_MAGIC
;
245 ctrl_msg_wire
.type
= type
;
248 strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
250 ctrl_msg_wire
.intval
= intval
;
252 if (!uctrl
->connected
) {
253 if (connect(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
) < 0) {
257 uctrl
->connected
= true;
259 if (send(uctrl
->sock
, &ctrl_msg_wire
, sizeof(ctrl_msg_wire
), 0) < 0) {
264 /* wait for peer message handling or disconnect */
266 struct pollfd pfd
[1];
269 pfd
[0].fd
= uctrl
->sock
;
270 pfd
[0].events
= POLLIN
;
271 r
= poll(pfd
, 1, timeout
* MSEC_PER_SEC
);
279 if (r
> 0 && pfd
[0].revents
& POLLERR
) {
292 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, int timeout
) {
293 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
296 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
297 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
300 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
301 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
304 int udev_ctrl_send_reload(struct udev_ctrl
*uctrl
, int timeout
) {
305 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD
, 0, NULL
, timeout
);
308 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, int timeout
) {
309 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
312 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, int timeout
) {
313 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
316 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, int timeout
) {
317 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
320 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, int timeout
) {
321 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
324 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
) {
325 struct udev_ctrl_msg
*uctrl_msg
;
327 struct cmsghdr
*cmsg
;
329 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
330 struct msghdr smsg
= {
333 .msg_control
= cred_msg
,
334 .msg_controllen
= sizeof(cred_msg
),
338 uctrl_msg
= new0(struct udev_ctrl_msg
, 1);
339 if (uctrl_msg
== NULL
)
341 uctrl_msg
->refcount
= 1;
342 uctrl_msg
->conn
= conn
;
343 udev_ctrl_connection_ref(conn
);
345 /* wait for the incoming message */
347 struct pollfd pfd
[1];
350 pfd
[0].fd
= conn
->sock
;
351 pfd
[0].events
= POLLIN
;
353 r
= poll(pfd
, 1, 10000);
359 log_error("timeout waiting for ctrl message");
362 if (!(pfd
[0].revents
& POLLIN
)) {
363 log_error_errno(errno
, "ctrl connection error: %m");
371 iov
.iov_base
= &uctrl_msg
->ctrl_msg_wire
;
372 iov
.iov_len
= sizeof(struct udev_ctrl_msg_wire
);
374 size
= recvmsg(conn
->sock
, &smsg
, 0);
376 log_error_errno(errno
, "unable to receive ctrl message: %m");
380 cmsg_close_all(&smsg
);
382 cmsg
= CMSG_FIRSTHDR(&smsg
);
384 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
385 log_error("no sender credentials received, message ignored");
389 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
391 if (cred
->uid
!= 0) {
392 log_error("sender uid="UID_FMT
", message ignored", cred
->uid
);
396 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
397 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg
->ctrl_msg_wire
.magic
);
403 udev_ctrl_msg_unref(uctrl_msg
);
407 struct udev_ctrl_msg
*udev_ctrl_msg_unref(struct udev_ctrl_msg
*ctrl_msg
) {
408 if (ctrl_msg
&& -- ctrl_msg
->refcount
== 0) {
409 udev_ctrl_connection_unref(ctrl_msg
->conn
);
416 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg
*ctrl_msg
) {
417 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_LOG_LEVEL
)
418 return ctrl_msg
->ctrl_msg_wire
.intval
;
422 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
423 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
428 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
429 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_START_EXEC_QUEUE
)
434 int udev_ctrl_get_reload(struct udev_ctrl_msg
*ctrl_msg
) {
435 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD
)
440 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
) {
441 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
442 return ctrl_msg
->ctrl_msg_wire
.buf
;
446 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
) {
447 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
448 return ctrl_msg
->ctrl_msg_wire
.intval
;
452 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
) {
453 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
458 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
) {
459 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)