1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * libudev - interface to udev device information
6 * Copyright (C) 2008 Kay Sievers <kay@vrfy.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
19 #include <sys/socket.h>
23 #include "alloc-util.h"
25 #include "formats-util.h"
26 #include "socket-util.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
;
64 union sockaddr_union saddr
;
71 struct udev_ctrl_connection
{
73 struct udev_ctrl
*uctrl
;
77 struct udev_ctrl
*udev_ctrl_new_from_fd(struct udev
*udev
, int fd
) {
78 struct udev_ctrl
*uctrl
;
82 uctrl
= new0(struct udev_ctrl
, 1);
89 uctrl
->sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
90 if (uctrl
->sock
< 0) {
91 log_error_errno(errno
, "error getting socket: %m");
92 udev_ctrl_unref(uctrl
);
101 * FIXME: remove it as soon as we can depend on this:
102 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
104 r
= setsockopt(uctrl
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
106 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
108 uctrl
->saddr
.un
.sun_family
= AF_LOCAL
;
109 strscpy(uctrl
->saddr
.un
.sun_path
, sizeof(uctrl
->saddr
.un
.sun_path
), "/run/udev/control");
110 uctrl
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(uctrl
->saddr
.un
.sun_path
);
114 struct udev_ctrl
*udev_ctrl_new(struct udev
*udev
) {
115 return udev_ctrl_new_from_fd(udev
, -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 unlink(uctrl
->saddr
.un
.sun_path
);
125 err
= bind(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
);
129 return log_error_errno(errno
, "bind failed: %m");
131 err
= listen(uctrl
->sock
, 0);
133 return log_error_errno(errno
, "listen failed: %m");
136 uctrl
->cleanup_socket
= true;
141 struct udev
*udev_ctrl_get_udev(struct udev_ctrl
*uctrl
) {
145 static struct udev_ctrl
*udev_ctrl_ref(struct udev_ctrl
*uctrl
) {
152 struct udev_ctrl
*udev_ctrl_unref(struct udev_ctrl
*uctrl
) {
153 if (uctrl
&& -- uctrl
->refcount
== 0) {
154 if (uctrl
->sock
>= 0)
162 int udev_ctrl_cleanup(struct udev_ctrl
*uctrl
) {
165 if (uctrl
->cleanup_socket
)
166 unlink(uctrl
->saddr
.un
.sun_path
);
170 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
) {
176 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
) {
177 struct udev_ctrl_connection
*conn
;
178 struct ucred ucred
= {};
182 conn
= new(struct udev_ctrl_connection
, 1);
188 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
|SOCK_NONBLOCK
);
189 if (conn
->sock
< 0) {
191 log_error_errno(errno
, "unable to receive ctrl connection: %m");
195 /* check peer credential of connection */
196 r
= getpeercred(conn
->sock
, &ucred
);
198 log_error_errno(r
, "unable to receive credentials of ctrl connection: %m");
202 log_error("sender uid="UID_FMT
", message ignored", ucred
.uid
);
206 /* enable receiving of the sender credentials in the messages */
207 r
= setsockopt(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
209 log_warning_errno(errno
, "could not set SO_PASSCRED: %m");
211 udev_ctrl_ref(uctrl
);
220 struct udev_ctrl_connection
*udev_ctrl_connection_ref(struct udev_ctrl_connection
*conn
) {
227 struct udev_ctrl_connection
*udev_ctrl_connection_unref(struct udev_ctrl_connection
*conn
) {
228 if (conn
&& -- conn
->refcount
== 0) {
232 udev_ctrl_unref(conn
->uctrl
);
240 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, int timeout
) {
241 struct udev_ctrl_msg_wire ctrl_msg_wire
;
244 memzero(&ctrl_msg_wire
, sizeof(struct udev_ctrl_msg_wire
));
245 strcpy(ctrl_msg_wire
.version
, "udev-" VERSION
);
246 ctrl_msg_wire
.magic
= UDEV_CTRL_MAGIC
;
247 ctrl_msg_wire
.type
= type
;
250 strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
252 ctrl_msg_wire
.intval
= intval
;
254 if (!uctrl
->connected
) {
255 if (connect(uctrl
->sock
, &uctrl
->saddr
.sa
, uctrl
->addrlen
) < 0) {
259 uctrl
->connected
= true;
261 if (send(uctrl
->sock
, &ctrl_msg_wire
, sizeof(ctrl_msg_wire
), 0) < 0) {
266 /* wait for peer message handling or disconnect */
268 struct pollfd pfd
[1];
271 pfd
[0].fd
= uctrl
->sock
;
272 pfd
[0].events
= POLLIN
;
273 r
= poll(pfd
, 1, timeout
* MSEC_PER_SEC
);
281 if (r
> 0 && pfd
[0].revents
& POLLERR
) {
294 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, int timeout
) {
295 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
298 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
299 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
302 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, int timeout
) {
303 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
306 int udev_ctrl_send_reload(struct udev_ctrl
*uctrl
, int timeout
) {
307 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD
, 0, NULL
, timeout
);
310 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, int timeout
) {
311 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
314 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, int timeout
) {
315 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
318 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, int timeout
) {
319 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
322 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, int timeout
) {
323 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
326 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
) {
327 struct udev_ctrl_msg
*uctrl_msg
;
329 struct cmsghdr
*cmsg
;
331 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
332 struct msghdr smsg
= {
335 .msg_control
= cred_msg
,
336 .msg_controllen
= sizeof(cred_msg
),
340 uctrl_msg
= new0(struct udev_ctrl_msg
, 1);
341 if (uctrl_msg
== NULL
)
343 uctrl_msg
->refcount
= 1;
344 uctrl_msg
->conn
= conn
;
345 udev_ctrl_connection_ref(conn
);
347 /* wait for the incoming message */
349 struct pollfd pfd
[1];
352 pfd
[0].fd
= conn
->sock
;
353 pfd
[0].events
= POLLIN
;
355 r
= poll(pfd
, 1, 10000);
361 log_error("timeout waiting for ctrl message");
364 if (!(pfd
[0].revents
& POLLIN
)) {
365 log_error_errno(errno
, "ctrl connection error: %m");
373 iov
.iov_base
= &uctrl_msg
->ctrl_msg_wire
;
374 iov
.iov_len
= sizeof(struct udev_ctrl_msg_wire
);
376 size
= recvmsg(conn
->sock
, &smsg
, 0);
378 log_error_errno(errno
, "unable to receive ctrl message: %m");
382 cmsg_close_all(&smsg
);
384 cmsg
= CMSG_FIRSTHDR(&smsg
);
386 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
387 log_error("no sender credentials received, message ignored");
391 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
393 if (cred
->uid
!= 0) {
394 log_error("sender uid="UID_FMT
", message ignored", cred
->uid
);
398 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
399 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg
->ctrl_msg_wire
.magic
);
405 udev_ctrl_msg_unref(uctrl_msg
);
409 struct udev_ctrl_msg
*udev_ctrl_msg_unref(struct udev_ctrl_msg
*ctrl_msg
) {
410 if (ctrl_msg
&& -- ctrl_msg
->refcount
== 0) {
411 udev_ctrl_connection_unref(ctrl_msg
->conn
);
418 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg
*ctrl_msg
) {
419 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_LOG_LEVEL
)
420 return ctrl_msg
->ctrl_msg_wire
.intval
;
424 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
425 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
430 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg
*ctrl_msg
) {
431 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_START_EXEC_QUEUE
)
436 int udev_ctrl_get_reload(struct udev_ctrl_msg
*ctrl_msg
) {
437 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD
)
442 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
) {
443 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
444 return ctrl_msg
->ctrl_msg_wire
.buf
;
448 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
) {
449 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
450 return ctrl_msg
->ctrl_msg_wire
.intval
;
454 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
) {
455 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
460 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
) {
461 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)