2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/socket.h>
32 #include <linux/netlink.h>
35 #include "libudev-private.h"
41 struct sockaddr_nl snl;
42 struct sockaddr_un sun;
47 * udev_monitor_new_from_socket:
48 * @udev: udev library context
49 * @socket_path: unix socket path
51 * Create new udev monitor, setup and connect to a specified socket. The
52 * path to a socket can point to an existing socket file, or it will be
53 * created if needed. If neccessary, the permissions adjustment as well as
54 * the later cleanup of the socket file, needs to be done by the caller.
55 * If the socket path starts with a '@' character, an abstract namespace
56 * socket will be used.
58 * The initial refcount is 1, and needs to be decremented to
59 * release the ressources of the udev monitor.
61 * Returns: a new udev monitor, or #NULL, in case of an error
63 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
65 struct udev_monitor *udev_monitor;
69 if (socket_path == NULL)
71 udev_monitor = malloc(sizeof(struct udev_monitor));
72 if (udev_monitor == NULL)
74 memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
75 udev_monitor->refcount = 1;
76 udev_monitor->udev = udev;
78 udev_monitor->sun.sun_family = AF_LOCAL;
79 strcpy(udev_monitor->sun.sun_path, socket_path);
80 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->sun.sun_path);
82 /* translate leading '@' to abstract namespace */
83 if (udev_monitor->sun.sun_path[0] == '@')
84 udev_monitor->sun.sun_path[0] = '\0';
86 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
87 if (udev_monitor->sock == -1) {
88 err(udev, "error getting socket: %s\n", strerror(errno));
92 info(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
96 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
98 struct udev_monitor *udev_monitor;
102 udev_monitor = malloc(sizeof(struct udev_monitor));
103 if (udev_monitor == NULL)
105 memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
106 udev_monitor->refcount = 1;
107 udev_monitor->udev = udev;
109 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
110 if (udev_monitor->sock == -1) {
111 err(udev, "error getting socket: %s\n", strerror(errno));
116 udev_monitor->snl.nl_family = AF_NETLINK;
117 udev_monitor->snl.nl_pid = getpid();
118 udev_monitor->snl.nl_groups = 1;
120 info(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
124 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
129 if (udev_monitor->snl.nl_family != 0) {
130 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
132 err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
135 info(udev_monitor->udev, "monitor %p listening on netlink\n", udev_monitor);
136 } else if (udev_monitor->sun.sun_family != 0) {
137 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
139 err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
142 /* enable receiving of the sender credentials */
143 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
144 info(udev_monitor->udev, "monitor %p listening on socket\n", udev_monitor);
151 * @udev_monitor: udev monitor
153 * Take a reference of a udev monitor.
155 * Returns: the passed udev monitor
157 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
159 if (udev_monitor == NULL)
161 udev_monitor->refcount++;
166 * udev_monitor_unref:
167 * @udev_monitor: udev monitor
169 * Drop a reference ofa udev monitor. If the refcount reaches zero,
170 * the bound socket will be closed, and the ressources of the monitor
174 void udev_monitor_unref(struct udev_monitor *udev_monitor)
176 if (udev_monitor == NULL)
178 udev_monitor->refcount--;
179 if (udev_monitor->refcount > 0)
181 if (udev_monitor->sock >= 0)
182 close(udev_monitor->sock);
183 info(udev_monitor->udev, "monitor %p released\n", udev_monitor);
188 * udev_monitor_get_udev:
189 * @udev_monitor: udev monitor
191 * Retrieve the udev library context the monitor was created with.
193 * Returns: the udev library context
195 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
197 if (udev_monitor == NULL)
199 return udev_monitor->udev;
203 * udev_monitor_get_fd:
204 * @udev_monitor: udev monitor
206 * Retrieve the socket file descriptor associated with the monitor.
208 * Returns: the socket file descriptor
210 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
212 if (udev_monitor == NULL)
214 return udev_monitor->sock;
218 * udev_monitor_receive_device:
219 * @udev_monitor: udev monitor
221 * Receive data from the udev monitor socket, allocate a new udev
222 * device, fill in the received data, and return the device.
224 * Only socket connections with uid=0 are accepted. The caller
225 * needs to make sure, that there is data to read from the socket,
226 * the call will block until the socket becomes readable.
228 * The initial refcount is 1, and needs to be decremented to
229 * release the ressources of the udev device.
231 * Returns: a new udev device, or #NULL, in case of an error
233 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
235 struct udev_device *udev_device;
238 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
244 if (udev_monitor == NULL)
246 memset(buf, 0x00, sizeof(buf));
248 iov.iov_len = sizeof(buf);
249 memset (&smsg, 0x00, sizeof(struct msghdr));
252 smsg.msg_control = cred_msg;
253 smsg.msg_controllen = sizeof(cred_msg);
255 if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
257 info(udev_monitor->udev, "unable to receive message");
261 if (udev_monitor->sun.sun_family != 0) {
262 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
263 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
265 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
266 info(udev_monitor->udev, "no sender credentials received, message ignored");
270 if (cred->uid != 0) {
271 info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
277 bufpos = strlen(buf) + 1;
278 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
279 info(udev_monitor->udev, "invalid message length");
283 /* check message header */
284 if (strstr(buf, "@/") == NULL) {
285 info(udev_monitor->udev, "unrecognized message header");
289 udev_device = device_init(udev_monitor->udev);
290 if (udev_device == NULL) {
294 while (bufpos < sizeof(buf)) {
299 keylen = strlen(key);
302 bufpos += keylen + 1;
304 if (strncmp(key, "DEVPATH=", 8) == 0) {
305 device_set_devpath(udev_device, &key[8]);
306 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
307 device_set_subsystem(udev_device, &key[10]);
308 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
309 device_set_devname(udev_device, &key[8]);
310 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
311 char *slink = &key[9];
312 char *next = strchr(slink, ' ');
314 while (next != NULL) {
316 device_add_devlink(udev_device, slink);
318 next = strchr(slink, ' ');
320 if (slink[0] != '\0')
321 device_add_devlink(udev_device, slink);
322 } else if (strncmp(key, "DRIVER=", 7) == 0) {
323 device_set_driver(udev_device, &key[7]);
324 } else if (strncmp(key, "ACTION=", 7) == 0) {
325 device_set_action(udev_device, &key[7]);
326 } else if (strncmp(key, "MAJOR=", 6) == 0) {
327 maj = strtoull(&key[6], NULL, 10);
328 } else if (strncmp(key, "MINOR=", 6) == 0) {
329 min = strtoull(&key[6], NULL, 10);
330 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
331 device_set_devpath_old(udev_device, &key[12]);
332 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
333 device_set_physdevpath(udev_device, &key[12]);
334 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
335 device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
336 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
337 device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
339 device_add_property_from_string(udev_device, key);
341 device_set_devnum(udev_device, makedev(maj, min));