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;
38 * udev_monitor_new_from_socket:
39 * @udev: udev library context
40 * @socket_path: unix socket path
42 * Create new udev monitor, setup and connect to a specified socket. The
43 * path to a socket can point to an existing socket file, or it will be
44 * created if needed. If neccessary, the permissions adjustment as well as
45 * the later cleanup of the socket file, needs to be done by the caller.
46 * If the socket path starts with a '@' character, an abstract namespace
47 * socket will be used.
49 * The initial refcount is 1, and needs to be decremented to
50 * release the resources of the udev monitor.
52 * Returns: a new udev monitor, or #NULL, in case of an error
54 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
56 struct udev_monitor *udev_monitor;
61 if (socket_path == NULL)
63 udev_monitor = calloc(1, sizeof(struct udev_monitor));
64 if (udev_monitor == NULL)
66 udev_monitor->refcount = 1;
67 udev_monitor->udev = udev;
69 udev_monitor->sun.sun_family = AF_LOCAL;
70 if (socket_path[0] == '@') {
71 /* translate leading '@' to abstract namespace */
72 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
73 udev_monitor->sun.sun_path[0] = '\0';
74 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
75 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
76 /* existing socket file */
77 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
78 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
80 /* no socket file, assume abstract namespace socket */
81 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
82 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
84 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
85 if (udev_monitor->sock == -1) {
86 err(udev, "error getting socket: %m\n");
90 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
94 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, unsigned int group)
96 struct udev_monitor *udev_monitor;
100 udev_monitor = calloc(1, sizeof(struct udev_monitor));
101 if (udev_monitor == NULL)
103 udev_monitor->refcount = 1;
104 udev_monitor->udev = udev;
106 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
107 if (udev_monitor->sock == -1) {
108 err(udev, "error getting socket: %m\n");
113 udev_monitor->snl.nl_family = AF_NETLINK;
114 udev_monitor->snl.nl_groups = group;
115 udev_monitor->snl_peer.nl_family = AF_NETLINK;
116 udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV;
118 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
122 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
127 if (udev_monitor->snl.nl_family != 0) {
128 err = bind(udev_monitor->sock,
129 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
131 err(udev_monitor->udev, "bind failed: %m\n");
134 dbg(udev_monitor->udev, "monitor %p listening on netlink\n", udev_monitor);
135 } else if (udev_monitor->sun.sun_family != 0) {
136 err = bind(udev_monitor->sock,
137 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
139 err(udev_monitor->udev, "bind failed: %m\n");
142 /* enable receiving of the sender credentials */
143 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
144 dbg(udev_monitor->udev, "monitor %p listening on socket\n", udev_monitor);
149 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
151 if (udev_monitor == NULL)
153 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
158 * @udev_monitor: udev monitor
160 * Take a reference of a udev monitor.
162 * Returns: the passed udev monitor
164 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
166 if (udev_monitor == NULL)
168 udev_monitor->refcount++;
173 * udev_monitor_unref:
174 * @udev_monitor: udev monitor
176 * Drop a reference ofa udev monitor. If the refcount reaches zero,
177 * the bound socket will be closed, and the resources of the monitor
181 void udev_monitor_unref(struct udev_monitor *udev_monitor)
183 if (udev_monitor == NULL)
185 udev_monitor->refcount--;
186 if (udev_monitor->refcount > 0)
188 if (udev_monitor->sock >= 0)
189 close(udev_monitor->sock);
190 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
195 * udev_monitor_get_udev:
196 * @udev_monitor: udev monitor
198 * Retrieve the udev library context the monitor was created with.
200 * Returns: the udev library context
202 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
204 if (udev_monitor == NULL)
206 return udev_monitor->udev;
210 * udev_monitor_get_fd:
211 * @udev_monitor: udev monitor
213 * Retrieve the socket file descriptor associated with the monitor.
215 * Returns: the socket file descriptor
217 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
219 if (udev_monitor == NULL)
221 return udev_monitor->sock;
225 * udev_monitor_receive_device:
226 * @udev_monitor: udev monitor
228 * Receive data from the udev monitor socket, allocate a new udev
229 * device, fill in the received data, and return the device.
231 * Only socket connections with uid=0 are accepted. The caller
232 * needs to make sure that there is data to read from the socket.
233 * The call will block until the socket becomes readable.
235 * The initial refcount is 1, and needs to be decremented to
236 * release the resources of the udev device.
238 * Returns: a new udev device, or #NULL, in case of an error
240 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
242 struct udev_device *udev_device;
245 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
249 int subsystem_set = 0;
254 if (udev_monitor == NULL)
256 memset(buf, 0x00, sizeof(buf));
258 iov.iov_len = sizeof(buf);
259 memset (&smsg, 0x00, sizeof(struct msghdr));
262 smsg.msg_control = cred_msg;
263 smsg.msg_controllen = sizeof(cred_msg);
265 if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
267 info(udev_monitor->udev, "unable to receive message");
271 if (udev_monitor->sun.sun_family != 0) {
272 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
273 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
275 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
276 info(udev_monitor->udev, "no sender credentials received, message ignored");
280 if (cred->uid != 0) {
281 info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
287 bufpos = strlen(buf) + 1;
288 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
289 info(udev_monitor->udev, "invalid message length");
293 /* check message header */
294 if (strstr(buf, "@/") == NULL) {
295 info(udev_monitor->udev, "unrecognized message header");
299 udev_device = device_new(udev_monitor->udev);
300 if (udev_device == NULL) {
304 while (bufpos < sizeof(buf)) {
309 keylen = strlen(key);
312 bufpos += keylen + 1;
314 if (strncmp(key, "DEVPATH=", 8) == 0) {
315 char path[UTIL_PATH_SIZE];
317 util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
318 util_strlcat(path, &key[8], sizeof(path));
319 udev_device_set_syspath(udev_device, path);
321 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
322 udev_device_set_subsystem(udev_device, &key[10]);
324 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
325 udev_device_set_devtype(udev_device, &key[8]);
326 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
327 udev_device_set_devnode(udev_device, &key[8]);
328 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
329 char devlinks[UTIL_PATH_SIZE];
333 util_strlcpy(devlinks, &key[9], sizeof(devlinks));
335 next = strchr(slink, ' ');
336 while (next != NULL) {
338 udev_device_add_devlink(udev_device, slink);
340 next = strchr(slink, ' ');
342 if (slink[0] != '\0')
343 udev_device_add_devlink(udev_device, slink);
344 } else if (strncmp(key, "DRIVER=", 7) == 0) {
345 udev_device_set_driver(udev_device, &key[7]);
346 } else if (strncmp(key, "ACTION=", 7) == 0) {
347 udev_device_set_action(udev_device, &key[7]);
349 } else if (strncmp(key, "MAJOR=", 6) == 0) {
350 maj = strtoull(&key[6], NULL, 10);
351 } else if (strncmp(key, "MINOR=", 6) == 0) {
352 min = strtoull(&key[6], NULL, 10);
353 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
354 udev_device_set_devpath_old(udev_device, &key[12]);
355 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
356 udev_device_set_physdevpath(udev_device, &key[12]);
357 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
358 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
359 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
360 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
361 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
362 /* skip deprecated values */
365 udev_device_add_property_from_string(udev_device, key);
368 if (!devpath_set || !subsystem_set || !action_set) {
369 info(udev_monitor->udev, "missing values, skip\n");
370 udev_device_unref(udev_device);
374 udev_device_set_devnum(udev_device, makedev(maj, min));
375 udev_device_set_info_loaded(udev_device);
379 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
385 len = udev_device_get_properties_monitor_buf(udev_device, &buf);
388 if (udev_monitor->sun.sun_family != 0) {
389 count = sendto(udev_monitor->sock,
391 (struct sockaddr *)&udev_monitor->sun,
392 udev_monitor->addrlen);
394 /* no destination besides the muticast group, we will always get -1 ECONNREFUSED */
395 count = sendto(udev_monitor->sock,
397 (struct sockaddr *)&udev_monitor->snl_peer,
398 sizeof(struct sockaddr_nl));
400 info(udev_monitor->udev, "passed %zi bytes to monitor %p, \n", count, udev_monitor);