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 and connect to a specified socket. The
48 * path to a socket either points to an existing socket file, or if
49 * the socket path starts with a '@' character, an abstract namespace
50 * socket will be used.
52 * A socket file will not be created. If it does not already exist,
53 * it will fall-back and connect to an abstract namespace socket with
54 * the given path. The permissions adjustment of a socket file, as
55 * well as the later cleanup, needs to be done by the caller.
57 * The initial refcount is 1, and needs to be decremented to
58 * release the resources of the udev monitor.
60 * Returns: a new udev monitor, or #NULL, in case of an error
62 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
64 struct udev_monitor *udev_monitor;
69 if (socket_path == NULL)
71 udev_monitor = calloc(1, sizeof(struct udev_monitor));
72 if (udev_monitor == NULL)
74 udev_monitor->refcount = 1;
75 udev_monitor->udev = udev;
77 udev_monitor->sun.sun_family = AF_LOCAL;
78 if (socket_path[0] == '@') {
79 /* translate leading '@' to abstract namespace */
80 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
81 udev_monitor->sun.sun_path[0] = '\0';
82 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
83 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
84 /* existing socket file */
85 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
86 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
88 /* no socket file, assume abstract namespace socket */
89 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
90 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
92 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
93 if (udev_monitor->sock == -1) {
94 err(udev, "error getting socket: %m\n");
98 util_set_fd_cloexec(udev_monitor->sock);
100 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
105 * udev_monitor_new_from_netlink:
106 * @udev: udev library context
107 * @name: name of event source
109 * Create new udev monitor and connect to a specified event
110 * source. Valid sources identifiers are "udev" and "kernel".
112 * Applications should usually not connect directly to the
113 * "kernel" events, because the devices might not be useable
114 * at that time, before udev has configured them, and created
117 * Accessing devices at the same time as udev, might result
118 * in unpredictable behavior.
120 * The "udev" events are sent out after udev has finished its
121 * event processing, all rules have been processed, and needed
122 * device nodes are created.
124 * The initial refcount is 1, and needs to be decremented to
125 * release the resources of the udev monitor.
127 * Returns: a new udev monitor, or #NULL, in case of an error
129 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
131 struct udev_monitor *udev_monitor;
139 if (strcmp(name, "kernel") == 0)
140 group = UDEV_MONITOR_KERNEL;
141 else if (strcmp(name, "udev") == 0)
142 group = UDEV_MONITOR_UDEV;
146 udev_monitor = calloc(1, sizeof(struct udev_monitor));
147 if (udev_monitor == NULL)
149 udev_monitor->refcount = 1;
150 udev_monitor->udev = udev;
152 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
153 if (udev_monitor->sock == -1) {
154 err(udev, "error getting socket: %m\n");
158 util_set_fd_cloexec(udev_monitor->sock);
160 udev_monitor->snl.nl_family = AF_NETLINK;
161 udev_monitor->snl.nl_groups = group;
162 udev_monitor->snl_peer.nl_family = AF_NETLINK;
163 udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV;
165 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
169 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
174 if (udev_monitor->sun.sun_family != 0)
175 err = bind(udev_monitor->sock,
176 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
177 else if (udev_monitor->snl.nl_family != 0)
178 err = bind(udev_monitor->sock,
179 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
184 err(udev_monitor->udev, "bind failed: %m\n");
188 /* enable receiving of sender credentials */
189 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
193 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
195 if (udev_monitor == NULL)
197 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
202 * @udev_monitor: udev monitor
204 * Take a reference of a udev monitor.
206 * Returns: the passed udev monitor
208 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
210 if (udev_monitor == NULL)
212 udev_monitor->refcount++;
217 * udev_monitor_unref:
218 * @udev_monitor: udev monitor
220 * Drop a reference of a udev monitor. If the refcount reaches zero,
221 * the bound socket will be closed, and the resources of the monitor
225 void udev_monitor_unref(struct udev_monitor *udev_monitor)
227 if (udev_monitor == NULL)
229 udev_monitor->refcount--;
230 if (udev_monitor->refcount > 0)
232 if (udev_monitor->sock >= 0)
233 close(udev_monitor->sock);
234 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
239 * udev_monitor_get_udev:
240 * @udev_monitor: udev monitor
242 * Retrieve the udev library context the monitor was created with.
244 * Returns: the udev library context
246 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
248 if (udev_monitor == NULL)
250 return udev_monitor->udev;
254 * udev_monitor_get_fd:
255 * @udev_monitor: udev monitor
257 * Retrieve the socket file descriptor associated with the monitor.
259 * Returns: the socket file descriptor
261 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
263 if (udev_monitor == NULL)
265 return udev_monitor->sock;
269 * udev_monitor_receive_device:
270 * @udev_monitor: udev monitor
272 * Receive data from the udev monitor socket, allocate a new udev
273 * device, fill in the received data, and return the device.
275 * Only socket connections with uid=0 are accepted. The caller
276 * needs to make sure that there is data to read from the socket.
277 * The call will block until the socket becomes readable.
279 * The initial refcount is 1, and needs to be decremented to
280 * release the resources of the udev device.
282 * Returns: a new udev device, or #NULL, in case of an error
284 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
286 struct udev_device *udev_device;
289 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
290 struct cmsghdr *cmsg;
291 struct sockaddr_nl snl;
296 int subsystem_set = 0;
301 if (udev_monitor == NULL)
303 memset(buf, 0x00, sizeof(buf));
305 iov.iov_len = sizeof(buf);
306 memset (&smsg, 0x00, sizeof(struct msghdr));
309 smsg.msg_control = cred_msg;
310 smsg.msg_controllen = sizeof(cred_msg);
312 if (udev_monitor->snl.nl_family != 0) {
313 smsg.msg_name = &snl;
314 smsg.msg_namelen = sizeof snl;
317 if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
319 info(udev_monitor->udev, "unable to receive message\n");
323 if (udev_monitor->snl.nl_family != 0) {
324 if (snl.nl_groups == 0) {
325 info(udev_monitor->udev, "unicast netlink message ignored\n");
328 if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) {
329 info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
334 cmsg = CMSG_FIRSTHDR(&smsg);
335 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
336 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
340 cred = (struct ucred *)CMSG_DATA(cmsg);
341 if (cred->uid != 0) {
342 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
347 bufpos = strlen(buf) + 1;
348 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
349 info(udev_monitor->udev, "invalid message length\n");
353 /* check message header */
354 if (strstr(buf, "@/") == NULL) {
355 info(udev_monitor->udev, "unrecognized message header\n");
359 udev_device = device_new(udev_monitor->udev);
360 if (udev_device == NULL) {
364 while (bufpos < sizeof(buf)) {
369 keylen = strlen(key);
372 bufpos += keylen + 1;
374 if (strncmp(key, "DEVPATH=", 8) == 0) {
375 char path[UTIL_PATH_SIZE];
377 util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
378 util_strlcat(path, &key[8], sizeof(path));
379 udev_device_set_syspath(udev_device, path);
381 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
382 udev_device_set_subsystem(udev_device, &key[10]);
384 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
385 udev_device_set_devtype(udev_device, &key[8]);
386 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
387 udev_device_set_devnode(udev_device, &key[8]);
388 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
389 char devlinks[UTIL_PATH_SIZE];
393 util_strlcpy(devlinks, &key[9], sizeof(devlinks));
395 next = strchr(slink, ' ');
396 while (next != NULL) {
398 udev_device_add_devlink(udev_device, slink);
400 next = strchr(slink, ' ');
402 if (slink[0] != '\0')
403 udev_device_add_devlink(udev_device, slink);
404 } else if (strncmp(key, "DRIVER=", 7) == 0) {
405 udev_device_set_driver(udev_device, &key[7]);
406 } else if (strncmp(key, "ACTION=", 7) == 0) {
407 udev_device_set_action(udev_device, &key[7]);
409 } else if (strncmp(key, "MAJOR=", 6) == 0) {
410 maj = strtoull(&key[6], NULL, 10);
411 } else if (strncmp(key, "MINOR=", 6) == 0) {
412 min = strtoull(&key[6], NULL, 10);
413 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
414 udev_device_set_devpath_old(udev_device, &key[12]);
415 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
416 udev_device_set_physdevpath(udev_device, &key[12]);
417 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
418 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
419 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
420 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
421 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
422 /* skip deprecated values */
425 udev_device_add_property_from_string(udev_device, key);
428 if (!devpath_set || !subsystem_set || !action_set) {
429 info(udev_monitor->udev, "missing values, skip\n");
430 udev_device_unref(udev_device);
434 udev_device_set_devnum(udev_device, makedev(maj, min));
435 udev_device_set_info_loaded(udev_device);
439 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
445 len = udev_device_get_properties_monitor_buf(udev_device, &buf);
448 if (udev_monitor->sun.sun_family != 0)
449 count = sendto(udev_monitor->sock,
451 (struct sockaddr *)&udev_monitor->sun,
452 udev_monitor->addrlen);
453 else if (udev_monitor->snl.nl_family != 0)
454 /* no destination besides the muticast group, we will always get ECONNREFUSED */
455 count = sendto(udev_monitor->sock,
457 (struct sockaddr *)&udev_monitor->snl_peer,
458 sizeof(struct sockaddr_nl));
462 info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);