2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@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.
18 #include <sys/types.h>
20 #include <sys/socket.h>
24 #include "libudev-private.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
,
34 UDEV_CTRL_RELOAD_RULES
,
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
;
61 struct sockaddr_un saddr
;
66 struct udev_ctrl_connection
{
68 struct udev_ctrl
*uctrl
;
72 static struct udev_ctrl
*udev_ctrl_new(struct udev
*udev
)
74 struct udev_ctrl
*uctrl
;
76 uctrl
= calloc(1, sizeof(struct udev_ctrl
));
84 struct udev_ctrl
*udev_ctrl_new_from_socket(struct udev
*udev
, const char *socket_path
)
86 struct udev_ctrl
*uctrl
;
88 uctrl
= udev_ctrl_new(udev
);
92 uctrl
->sock
= socket(AF_LOCAL
, SOCK_SEQPACKET
, 0);
93 if (uctrl
->sock
< 0) {
94 err(udev
, "error getting socket: %m\n");
95 udev_ctrl_unref(uctrl
);
99 uctrl
->saddr
.sun_family
= AF_LOCAL
;
100 strcpy(uctrl
->saddr
.sun_path
, socket_path
);
101 uctrl
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(uctrl
->saddr
.sun_path
);
102 /* translate leading '@' to abstract namespace */
103 if (uctrl
->saddr
.sun_path
[0] == '@')
104 uctrl
->saddr
.sun_path
[0] = '\0';
108 struct udev_ctrl
*udev_ctrl_new_from_fd(struct udev
*udev
, int fd
)
110 struct udev_ctrl
*uctrl
;
112 uctrl
= udev_ctrl_new(udev
);
120 int udev_ctrl_enable_receiving(struct udev_ctrl
*uctrl
)
124 if (uctrl
->addrlen
> 0) {
125 err
= bind(uctrl
->sock
, (struct sockaddr
*)&uctrl
->saddr
, uctrl
->addrlen
);
128 err(uctrl
->udev
, "bind failed: %m\n");
131 err
= listen(uctrl
->sock
, 0);
134 err(uctrl
->udev
, "listen failed: %m\n");
141 struct udev
*udev_ctrl_get_udev(struct udev_ctrl
*uctrl
)
146 struct udev_ctrl
*udev_ctrl_ref(struct udev_ctrl
*uctrl
)
154 struct udev_ctrl
*udev_ctrl_unref(struct udev_ctrl
*uctrl
)
159 if (uctrl
->refcount
> 0)
161 if (uctrl
->sock
>= 0)
167 int udev_ctrl_get_fd(struct udev_ctrl
*uctrl
)
174 struct udev_ctrl_connection
*udev_ctrl_get_connection(struct udev_ctrl
*uctrl
)
176 struct udev_ctrl_connection
*conn
;
179 conn
= calloc(1, sizeof(struct udev_ctrl_connection
));
185 conn
->sock
= accept4(uctrl
->sock
, NULL
, NULL
, SOCK_CLOEXEC
);
186 if (conn
->sock
< 0) {
191 /* enable receiving of the sender credentials */
192 setsockopt(conn
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
193 udev_ctrl_ref(uctrl
);
197 struct udev_ctrl_connection
*udev_ctrl_connection_ref(struct udev_ctrl_connection
*conn
)
205 struct udev_ctrl_connection
*udev_ctrl_connection_unref(struct udev_ctrl_connection
*conn
)
210 if (conn
->refcount
> 0)
214 udev_ctrl_unref(conn
->uctrl
);
219 static int ctrl_send(struct udev_ctrl
*uctrl
, enum udev_ctrl_msg_type type
, int intval
, const char *buf
, int timeout
)
221 struct udev_ctrl_msg_wire ctrl_msg_wire
;
224 memset(&ctrl_msg_wire
, 0x00, sizeof(struct udev_ctrl_msg_wire
));
225 strcpy(ctrl_msg_wire
.version
, "udev-" VERSION
);
226 ctrl_msg_wire
.magic
= UDEV_CTRL_MAGIC
;
227 ctrl_msg_wire
.type
= type
;
230 util_strscpy(ctrl_msg_wire
.buf
, sizeof(ctrl_msg_wire
.buf
), buf
);
232 ctrl_msg_wire
.intval
= intval
;
234 if (!uctrl
->connected
) {
235 if (connect(uctrl
->sock
, (struct sockaddr
*)&uctrl
->saddr
, uctrl
->addrlen
) < 0) {
239 uctrl
->connected
= true;
241 if (send(uctrl
->sock
, &ctrl_msg_wire
, sizeof(ctrl_msg_wire
), 0) < 0) {
246 /* wait for peer message handling or disconnect */
248 struct pollfd pfd
[1];
251 pfd
[0].fd
= uctrl
->sock
;
252 pfd
[0].events
= POLLIN
;
253 r
= poll(pfd
, 1, timeout
* 1000);
261 if (r
> 0 && pfd
[0].revents
& POLLERR
) {
274 int udev_ctrl_send_set_log_level(struct udev_ctrl
*uctrl
, int priority
, int timeout
)
276 return ctrl_send(uctrl
, UDEV_CTRL_SET_LOG_LEVEL
, priority
, NULL
, timeout
);
279 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl
*uctrl
, int timeout
)
281 return ctrl_send(uctrl
, UDEV_CTRL_STOP_EXEC_QUEUE
, 0, NULL
, timeout
);
284 int udev_ctrl_send_start_exec_queue(struct udev_ctrl
*uctrl
, int timeout
)
286 return ctrl_send(uctrl
, UDEV_CTRL_START_EXEC_QUEUE
, 0, NULL
, timeout
);
289 int udev_ctrl_send_reload_rules(struct udev_ctrl
*uctrl
, int timeout
)
291 return ctrl_send(uctrl
, UDEV_CTRL_RELOAD_RULES
, 0, NULL
, timeout
);
294 int udev_ctrl_send_set_env(struct udev_ctrl
*uctrl
, const char *key
, int timeout
)
296 return ctrl_send(uctrl
, UDEV_CTRL_SET_ENV
, 0, key
, timeout
);
299 int udev_ctrl_send_set_children_max(struct udev_ctrl
*uctrl
, int count
, int timeout
)
301 return ctrl_send(uctrl
, UDEV_CTRL_SET_CHILDREN_MAX
, count
, NULL
, timeout
);
304 int udev_ctrl_send_ping(struct udev_ctrl
*uctrl
, int timeout
)
306 return ctrl_send(uctrl
, UDEV_CTRL_PING
, 0, NULL
, timeout
);
309 int udev_ctrl_send_exit(struct udev_ctrl
*uctrl
, int timeout
)
311 return ctrl_send(uctrl
, UDEV_CTRL_EXIT
, 0, NULL
, timeout
);
314 struct udev_ctrl_msg
*udev_ctrl_receive_msg(struct udev_ctrl_connection
*conn
)
316 struct udev
*udev
= conn
->uctrl
->udev
;
317 struct udev_ctrl_msg
*uctrl_msg
;
320 struct cmsghdr
*cmsg
;
323 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
325 uctrl_msg
= calloc(1, sizeof(struct udev_ctrl_msg
));
326 if (uctrl_msg
== NULL
)
328 uctrl_msg
->refcount
= 1;
329 uctrl_msg
->conn
= conn
;
330 udev_ctrl_connection_ref(conn
);
332 iov
.iov_base
= &uctrl_msg
->ctrl_msg_wire
;
333 iov
.iov_len
= sizeof(struct udev_ctrl_msg_wire
);
334 memset(&smsg
, 0x00, sizeof(struct msghdr
));
337 smsg
.msg_control
= cred_msg
;
338 smsg
.msg_controllen
= sizeof(cred_msg
);
339 size
= recvmsg(conn
->sock
, &smsg
, 0);
341 err(udev
, "unable to receive user udevd message: %m\n");
344 cmsg
= CMSG_FIRSTHDR(&smsg
);
345 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
347 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
348 err(udev
, "no sender credentials received, message ignored\n");
352 if (cred
->uid
!= 0) {
353 err(udev
, "sender uid=%i, message ignored\n", cred
->uid
);
357 if (uctrl_msg
->ctrl_msg_wire
.magic
!= UDEV_CTRL_MAGIC
) {
358 err(udev
, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg
->ctrl_msg_wire
.magic
);
362 dbg(udev
, "created ctrl_msg %p (%i)\n", uctrl_msg
, uctrl_msg
->ctrl_msg_wire
.type
);
365 udev_ctrl_msg_unref(uctrl_msg
);
369 struct udev_ctrl_msg
*udev_ctrl_msg_ref(struct udev_ctrl_msg
*ctrl_msg
)
371 if (ctrl_msg
== NULL
)
373 ctrl_msg
->refcount
++;
377 struct udev_ctrl_msg
*udev_ctrl_msg_unref(struct udev_ctrl_msg
*ctrl_msg
)
379 if (ctrl_msg
== NULL
)
381 ctrl_msg
->refcount
--;
382 if (ctrl_msg
->refcount
> 0)
384 dbg(ctrl_msg
->conn
->uctrl
->udev
, "release ctrl_msg %p\n", ctrl_msg
);
385 udev_ctrl_connection_unref(ctrl_msg
->conn
);
390 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
)
399 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_STOP_EXEC_QUEUE
)
404 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_rules(struct udev_ctrl_msg
*ctrl_msg
)
413 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_RELOAD_RULES
)
418 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg
*ctrl_msg
)
420 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_ENV
)
421 return ctrl_msg
->ctrl_msg_wire
.buf
;
425 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg
*ctrl_msg
)
427 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_SET_CHILDREN_MAX
)
428 return ctrl_msg
->ctrl_msg_wire
.intval
;
432 int udev_ctrl_get_ping(struct udev_ctrl_msg
*ctrl_msg
)
434 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_PING
)
439 int udev_ctrl_get_exit(struct udev_ctrl_msg
*ctrl_msg
)
441 if (ctrl_msg
->ctrl_msg_wire
.type
== UDEV_CTRL_EXIT
)