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_un sun;
37 * udev_monitor_new_from_socket:
38 * @udev: udev library context
39 * @socket_path: unix socket path
41 * Create new udev monitor, setup and connect to a specified socket. The
42 * path to a socket can point to an existing socket file, or it will be
43 * created if needed. If neccessary, the permissions adjustment as well as
44 * the later cleanup of the socket file, needs to be done by the caller.
45 * If the socket path starts with a '@' character, an abstract namespace
46 * socket will be used.
48 * The initial refcount is 1, and needs to be decremented to
49 * release the resources of the udev monitor.
51 * Returns: a new udev monitor, or #NULL, in case of an error
53 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
55 struct udev_monitor *udev_monitor;
60 if (socket_path == NULL)
62 udev_monitor = calloc(1, sizeof(struct udev_monitor));
63 if (udev_monitor == NULL)
65 udev_monitor->refcount = 1;
66 udev_monitor->udev = udev;
68 udev_monitor->sun.sun_family = AF_LOCAL;
69 if (socket_path[0] == '@') {
70 /* translate leading '@' to abstract namespace */
71 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
72 udev_monitor->sun.sun_path[0] = '\0';
73 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
74 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
75 /* existing socket file */
76 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
77 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
79 /* no socket file, assume abstract namespace socket */
80 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
81 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
83 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
84 if (udev_monitor->sock == -1) {
85 err(udev, "error getting socket: %m\n");
89 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
93 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
95 struct udev_monitor *udev_monitor;
99 udev_monitor = calloc(1, sizeof(struct udev_monitor));
100 if (udev_monitor == NULL)
102 udev_monitor->refcount = 1;
103 udev_monitor->udev = udev;
105 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
106 if (udev_monitor->sock == -1) {
107 err(udev, "error getting socket: %m\n");
112 udev_monitor->snl.nl_family = AF_NETLINK;
113 udev_monitor->snl.nl_pid = getpid();
114 udev_monitor->snl.nl_groups = 1;
116 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
120 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
125 if (udev_monitor->snl.nl_family != 0) {
126 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
128 err(udev_monitor->udev, "bind failed: %m\n");
131 dbg(udev_monitor->udev, "monitor %p listening on netlink\n", udev_monitor);
132 } else if (udev_monitor->sun.sun_family != 0) {
133 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
135 err(udev_monitor->udev, "bind failed: %m\n");
138 /* enable receiving of the sender credentials */
139 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
140 dbg(udev_monitor->udev, "monitor %p listening on socket\n", udev_monitor);
145 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
147 if (udev_monitor == NULL)
149 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
154 * @udev_monitor: udev monitor
156 * Take a reference of a udev monitor.
158 * Returns: the passed udev monitor
160 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
162 if (udev_monitor == NULL)
164 udev_monitor->refcount++;
169 * udev_monitor_unref:
170 * @udev_monitor: udev monitor
172 * Drop a reference ofa udev monitor. If the refcount reaches zero,
173 * the bound socket will be closed, and the resources of the monitor
177 void udev_monitor_unref(struct udev_monitor *udev_monitor)
179 if (udev_monitor == NULL)
181 udev_monitor->refcount--;
182 if (udev_monitor->refcount > 0)
184 if (udev_monitor->sock >= 0)
185 close(udev_monitor->sock);
186 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
191 * udev_monitor_get_udev:
192 * @udev_monitor: udev monitor
194 * Retrieve the udev library context the monitor was created with.
196 * Returns: the udev library context
198 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
200 if (udev_monitor == NULL)
202 return udev_monitor->udev;
206 * udev_monitor_get_fd:
207 * @udev_monitor: udev monitor
209 * Retrieve the socket file descriptor associated with the monitor.
211 * Returns: the socket file descriptor
213 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
215 if (udev_monitor == NULL)
217 return udev_monitor->sock;
221 * udev_monitor_receive_device:
222 * @udev_monitor: udev monitor
224 * Receive data from the udev monitor socket, allocate a new udev
225 * device, fill in the received data, and return the device.
227 * Only socket connections with uid=0 are accepted. The caller
228 * needs to make sure that there is data to read from the socket.
229 * The call will block until the socket becomes readable.
231 * The initial refcount is 1, and needs to be decremented to
232 * release the resources of the udev device.
234 * Returns: a new udev device, or #NULL, in case of an error
236 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
238 struct udev_device *udev_device;
241 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
245 int subsystem_set = 0;
250 if (udev_monitor == NULL)
252 memset(buf, 0x00, sizeof(buf));
254 iov.iov_len = sizeof(buf);
255 memset (&smsg, 0x00, sizeof(struct msghdr));
258 smsg.msg_control = cred_msg;
259 smsg.msg_controllen = sizeof(cred_msg);
261 if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
263 info(udev_monitor->udev, "unable to receive message");
267 if (udev_monitor->sun.sun_family != 0) {
268 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
269 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
271 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
272 info(udev_monitor->udev, "no sender credentials received, message ignored");
276 if (cred->uid != 0) {
277 info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
283 bufpos = strlen(buf) + 1;
284 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
285 info(udev_monitor->udev, "invalid message length");
289 /* check message header */
290 if (strstr(buf, "@/") == NULL) {
291 info(udev_monitor->udev, "unrecognized message header");
295 udev_device = device_new(udev_monitor->udev);
296 if (udev_device == NULL) {
300 while (bufpos < sizeof(buf)) {
305 keylen = strlen(key);
308 bufpos += keylen + 1;
310 if (strncmp(key, "DEVPATH=", 8) == 0) {
311 char path[UTIL_PATH_SIZE];
313 util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
314 util_strlcat(path, &key[8], sizeof(path));
315 udev_device_set_syspath(udev_device, path);
317 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
318 udev_device_set_subsystem(udev_device, &key[10]);
320 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
321 udev_device_set_devtype(udev_device, &key[8]);
322 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
323 udev_device_set_devnode(udev_device, &key[8]);
324 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
325 char devlinks[UTIL_PATH_SIZE];
329 util_strlcpy(devlinks, &key[9], sizeof(devlinks));
331 next = strchr(slink, ' ');
332 while (next != NULL) {
334 udev_device_add_devlink(udev_device, slink);
336 next = strchr(slink, ' ');
338 if (slink[0] != '\0')
339 udev_device_add_devlink(udev_device, slink);
340 } else if (strncmp(key, "DRIVER=", 7) == 0) {
341 udev_device_set_driver(udev_device, &key[7]);
342 } else if (strncmp(key, "ACTION=", 7) == 0) {
343 udev_device_set_action(udev_device, &key[7]);
345 } else if (strncmp(key, "MAJOR=", 6) == 0) {
346 maj = strtoull(&key[6], NULL, 10);
347 } else if (strncmp(key, "MINOR=", 6) == 0) {
348 min = strtoull(&key[6], NULL, 10);
349 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
350 udev_device_set_devpath_old(udev_device, &key[12]);
351 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
352 udev_device_set_physdevpath(udev_device, &key[12]);
353 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
354 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
355 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
356 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
357 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
358 /* skip deprecated values */
361 udev_device_add_property_from_string(udev_device, key);
364 if (!devpath_set || !subsystem_set || !action_set) {
365 info(udev_monitor->udev, "missing values, skip\n");
366 udev_device_unref(udev_device);
370 udev_device_set_devnum(udev_device, makedev(maj, min));
371 udev_device_set_info_loaded(udev_device);
375 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
381 len = udev_device_get_properties_monitor_buf(udev_device, &buf);
384 count = sendto(udev_monitor->sock,
386 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
387 info(udev_monitor->udev, "passed %zi bytes to monitor %p, \n", count, udev_monitor);