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.
20 #include <sys/socket.h>
22 #include <linux/netlink.h>
25 #include "libudev-private.h"
31 struct sockaddr_nl snl
;
32 struct sockaddr_nl snl_peer
;
33 struct sockaddr_un sun
;
37 enum udev_monitor_netlink_group
{
38 UDEV_MONITOR_KERNEL
= 1,
39 UDEV_MONITOR_UDEV
= 2,
43 * udev_monitor_new_from_socket:
44 * @udev: udev library context
45 * @socket_path: unix socket path
47 * Create new udev monitor, setup and connect to a specified socket. The
48 * path to a socket can point to an existing socket file, or it will be
49 * created if needed. If neccessary, the permissions adjustment as well as
50 * the later cleanup of the socket file, needs to be done by the caller.
51 * If the socket path starts with a '@' character, an abstract namespace
52 * socket will be used.
54 * The initial refcount is 1, and needs to be decremented to
55 * release the resources of the udev monitor.
57 * Returns: a new udev monitor, or #NULL, in case of an error
59 struct udev_monitor
*udev_monitor_new_from_socket(struct udev
*udev
, const char *socket_path
)
61 struct udev_monitor
*udev_monitor
;
66 if (socket_path
== NULL
)
68 udev_monitor
= calloc(1, sizeof(struct udev_monitor
));
69 if (udev_monitor
== NULL
)
71 udev_monitor
->refcount
= 1;
72 udev_monitor
->udev
= udev
;
74 udev_monitor
->sun
.sun_family
= AF_LOCAL
;
75 if (socket_path
[0] == '@') {
76 /* translate leading '@' to abstract namespace */
77 util_strlcpy(udev_monitor
->sun
.sun_path
, socket_path
, sizeof(udev_monitor
->sun
.sun_path
));
78 udev_monitor
->sun
.sun_path
[0] = '\0';
79 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
);
80 } else if (stat(socket_path
, &statbuf
) == 0 && S_ISSOCK(statbuf
.st_mode
)) {
81 /* existing socket file */
82 util_strlcpy(udev_monitor
->sun
.sun_path
, socket_path
, sizeof(udev_monitor
->sun
.sun_path
));
83 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
);
85 /* no socket file, assume abstract namespace socket */
86 util_strlcpy(&udev_monitor
->sun
.sun_path
[1], socket_path
, sizeof(udev_monitor
->sun
.sun_path
)-1);
87 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
)+1;
89 udev_monitor
->sock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
90 if (udev_monitor
->sock
== -1) {
91 err(udev
, "error getting socket: %m\n");
95 dbg(udev
, "monitor %p created with '%s'\n", udev_monitor
, socket_path
);
99 struct udev_monitor
*udev_monitor_new_from_netlink(struct udev
*udev
, const char *name
)
101 struct udev_monitor
*udev_monitor
;
109 if (strcmp(name
, "kernel") == 0)
110 group
= UDEV_MONITOR_KERNEL
;
111 else if (strcmp(name
, "udev") == 0)
112 group
= UDEV_MONITOR_UDEV
;
116 udev_monitor
= calloc(1, sizeof(struct udev_monitor
));
117 if (udev_monitor
== NULL
)
119 udev_monitor
->refcount
= 1;
120 udev_monitor
->udev
= udev
;
122 udev_monitor
->sock
= socket(PF_NETLINK
, SOCK_DGRAM
, NETLINK_KOBJECT_UEVENT
);
123 if (udev_monitor
->sock
== -1) {
124 err(udev
, "error getting socket: %m\n");
129 udev_monitor
->snl
.nl_family
= AF_NETLINK
;
130 udev_monitor
->snl
.nl_groups
= group
;
131 udev_monitor
->snl_peer
.nl_family
= AF_NETLINK
;
132 udev_monitor
->snl_peer
.nl_groups
= UDEV_MONITOR_UDEV
;
134 dbg(udev
, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor
, group
);
138 int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
143 if (udev_monitor
->snl
.nl_family
!= 0) {
144 err
= bind(udev_monitor
->sock
,
145 (struct sockaddr
*)&udev_monitor
->snl
, sizeof(struct sockaddr_nl
));
147 err(udev_monitor
->udev
, "bind failed: %m\n");
150 dbg(udev_monitor
->udev
, "monitor %p listening on netlink\n", udev_monitor
);
151 } else if (udev_monitor
->sun
.sun_family
!= 0) {
152 err
= bind(udev_monitor
->sock
,
153 (struct sockaddr
*)&udev_monitor
->sun
, udev_monitor
->addrlen
);
155 err(udev_monitor
->udev
, "bind failed: %m\n");
158 /* enable receiving of the sender credentials */
159 setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
160 dbg(udev_monitor
->udev
, "monitor %p listening on socket\n", udev_monitor
);
165 int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
167 if (udev_monitor
== NULL
)
169 return setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
));
174 * @udev_monitor: udev monitor
176 * Take a reference of a udev monitor.
178 * Returns: the passed udev monitor
180 struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
182 if (udev_monitor
== NULL
)
184 udev_monitor
->refcount
++;
189 * udev_monitor_unref:
190 * @udev_monitor: udev monitor
192 * Drop a reference ofa udev monitor. If the refcount reaches zero,
193 * the bound socket will be closed, and the resources of the monitor
197 void udev_monitor_unref(struct udev_monitor
*udev_monitor
)
199 if (udev_monitor
== NULL
)
201 udev_monitor
->refcount
--;
202 if (udev_monitor
->refcount
> 0)
204 if (udev_monitor
->sock
>= 0)
205 close(udev_monitor
->sock
);
206 dbg(udev_monitor
->udev
, "monitor %p released\n", udev_monitor
);
211 * udev_monitor_get_udev:
212 * @udev_monitor: udev monitor
214 * Retrieve the udev library context the monitor was created with.
216 * Returns: the udev library context
218 struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
220 if (udev_monitor
== NULL
)
222 return udev_monitor
->udev
;
226 * udev_monitor_get_fd:
227 * @udev_monitor: udev monitor
229 * Retrieve the socket file descriptor associated with the monitor.
231 * Returns: the socket file descriptor
233 int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
235 if (udev_monitor
== NULL
)
237 return udev_monitor
->sock
;
241 * udev_monitor_receive_device:
242 * @udev_monitor: udev monitor
244 * Receive data from the udev monitor socket, allocate a new udev
245 * device, fill in the received data, and return the device.
247 * Only socket connections with uid=0 are accepted. The caller
248 * needs to make sure that there is data to read from the socket.
249 * The call will block until the socket becomes readable.
251 * The initial refcount is 1, and needs to be decremented to
252 * release the resources of the udev device.
254 * Returns: a new udev device, or #NULL, in case of an error
256 struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
258 struct udev_device
*udev_device
;
261 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
265 int subsystem_set
= 0;
270 if (udev_monitor
== NULL
)
272 memset(buf
, 0x00, sizeof(buf
));
274 iov
.iov_len
= sizeof(buf
);
275 memset (&smsg
, 0x00, sizeof(struct msghdr
));
278 smsg
.msg_control
= cred_msg
;
279 smsg
.msg_controllen
= sizeof(cred_msg
);
281 if (recvmsg(udev_monitor
->sock
, &smsg
, 0) < 0) {
283 info(udev_monitor
->udev
, "unable to receive message");
287 if (udev_monitor
->sun
.sun_family
!= 0) {
288 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(&smsg
);
289 struct ucred
*cred
= (struct ucred
*)CMSG_DATA (cmsg
);
291 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
292 info(udev_monitor
->udev
, "no sender credentials received, message ignored");
296 if (cred
->uid
!= 0) {
297 info(udev_monitor
->udev
, "sender uid=%d, message ignored", cred
->uid
);
303 bufpos
= strlen(buf
) + 1;
304 if (bufpos
< sizeof("a@/d") || bufpos
>= sizeof(buf
)) {
305 info(udev_monitor
->udev
, "invalid message length");
309 /* check message header */
310 if (strstr(buf
, "@/") == NULL
) {
311 info(udev_monitor
->udev
, "unrecognized message header");
315 udev_device
= device_new(udev_monitor
->udev
);
316 if (udev_device
== NULL
) {
320 while (bufpos
< sizeof(buf
)) {
325 keylen
= strlen(key
);
328 bufpos
+= keylen
+ 1;
330 if (strncmp(key
, "DEVPATH=", 8) == 0) {
331 char path
[UTIL_PATH_SIZE
];
333 util_strlcpy(path
, udev_get_sys_path(udev_monitor
->udev
), sizeof(path
));
334 util_strlcat(path
, &key
[8], sizeof(path
));
335 udev_device_set_syspath(udev_device
, path
);
337 } else if (strncmp(key
, "SUBSYSTEM=", 10) == 0) {
338 udev_device_set_subsystem(udev_device
, &key
[10]);
340 } else if (strncmp(key
, "DEVTYPE=", 8) == 0) {
341 udev_device_set_devtype(udev_device
, &key
[8]);
342 } else if (strncmp(key
, "DEVNAME=", 8) == 0) {
343 udev_device_set_devnode(udev_device
, &key
[8]);
344 } else if (strncmp(key
, "DEVLINKS=", 9) == 0) {
345 char devlinks
[UTIL_PATH_SIZE
];
349 util_strlcpy(devlinks
, &key
[9], sizeof(devlinks
));
351 next
= strchr(slink
, ' ');
352 while (next
!= NULL
) {
354 udev_device_add_devlink(udev_device
, slink
);
356 next
= strchr(slink
, ' ');
358 if (slink
[0] != '\0')
359 udev_device_add_devlink(udev_device
, slink
);
360 } else if (strncmp(key
, "DRIVER=", 7) == 0) {
361 udev_device_set_driver(udev_device
, &key
[7]);
362 } else if (strncmp(key
, "ACTION=", 7) == 0) {
363 udev_device_set_action(udev_device
, &key
[7]);
365 } else if (strncmp(key
, "MAJOR=", 6) == 0) {
366 maj
= strtoull(&key
[6], NULL
, 10);
367 } else if (strncmp(key
, "MINOR=", 6) == 0) {
368 min
= strtoull(&key
[6], NULL
, 10);
369 } else if (strncmp(key
, "DEVPATH_OLD=", 12) == 0) {
370 udev_device_set_devpath_old(udev_device
, &key
[12]);
371 } else if (strncmp(key
, "PHYSDEVPATH=", 12) == 0) {
372 udev_device_set_physdevpath(udev_device
, &key
[12]);
373 } else if (strncmp(key
, "SEQNUM=", 7) == 0) {
374 udev_device_set_seqnum(udev_device
, strtoull(&key
[7], NULL
, 10));
375 } else if (strncmp(key
, "TIMEOUT=", 8) == 0) {
376 udev_device_set_timeout(udev_device
, strtoull(&key
[8], NULL
, 10));
377 } else if (strncmp(key
, "PHYSDEV", 7) == 0) {
378 /* skip deprecated values */
381 udev_device_add_property_from_string(udev_device
, key
);
384 if (!devpath_set
|| !subsystem_set
|| !action_set
) {
385 info(udev_monitor
->udev
, "missing values, skip\n");
386 udev_device_unref(udev_device
);
390 udev_device_set_devnum(udev_device
, makedev(maj
, min
));
391 udev_device_set_info_loaded(udev_device
);
395 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
401 len
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
404 if (udev_monitor
->sun
.sun_family
!= 0) {
405 count
= sendto(udev_monitor
->sock
,
407 (struct sockaddr
*)&udev_monitor
->sun
,
408 udev_monitor
->addrlen
);
410 /* no destination besides the muticast group, we will always get -1 ECONNREFUSED */
411 count
= sendto(udev_monitor
->sock
,
413 (struct sockaddr
*)&udev_monitor
->snl_peer
,
414 sizeof(struct sockaddr_nl
));
416 info(udev_monitor
->udev
, "passed %zi bytes to monitor %p, \n", count
, udev_monitor
);