chiark / gitweb /
c0aa99c65a668b4015b45a83ecac2e8613800ff6
[elogind.git] / udev / lib / libudev-monitor.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <linux/netlink.h>
23
24 #include "libudev.h"
25 #include "libudev-private.h"
26
27 struct udev_monitor {
28         struct udev *udev;
29         int refcount;
30         int sock;
31         struct sockaddr_nl snl;
32         struct sockaddr_un sun;
33         socklen_t addrlen;
34 };
35
36 /**
37  * udev_monitor_new_from_socket:
38  * @udev: udev library context
39  * @socket_path: unix socket path
40  *
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.
47  *
48  * The initial refcount is 1, and needs to be decremented to
49  * release the resources of the udev monitor.
50  *
51  * Returns: a new udev monitor, or #NULL, in case of an error
52  **/
53 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
54 {
55         struct udev_monitor *udev_monitor;
56         struct stat statbuf;
57
58         if (udev == NULL)
59                 return NULL;
60         if (socket_path == NULL)
61                 return NULL;
62         udev_monitor = calloc(1, sizeof(struct udev_monitor));
63         if (udev_monitor == NULL)
64                 return NULL;
65         udev_monitor->refcount = 1;
66         udev_monitor->udev = udev;
67
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);
78         } else {
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;
82         }
83         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
84         if (udev_monitor->sock == -1) {
85                 err(udev, "error getting socket: %m\n");
86                 free(udev_monitor);
87                 return NULL;
88         }
89         dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
90         return udev_monitor;
91 }
92
93 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
94 {
95         struct udev_monitor *udev_monitor;
96
97         if (udev == NULL)
98                 return NULL;
99         udev_monitor = calloc(1, sizeof(struct udev_monitor));
100         if (udev_monitor == NULL)
101                 return NULL;
102         udev_monitor->refcount = 1;
103         udev_monitor->udev = udev;
104
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");
108                 free(udev_monitor);
109                 return NULL;
110         }
111
112         udev_monitor->snl.nl_family = AF_NETLINK;
113         udev_monitor->snl.nl_pid = getpid();
114         udev_monitor->snl.nl_groups = 1;
115
116         dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
117         return udev_monitor;
118 }
119
120 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
121 {
122         int err;
123         const int on = 1;
124
125         if (udev_monitor->snl.nl_family != 0) {
126                 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
127                 if (err < 0) {
128                         err(udev_monitor->udev, "bind failed: %m\n");
129                         return err;
130                 }
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);
134                 if (err < 0) {
135                         err(udev_monitor->udev, "bind failed: %m\n");
136                         return err;
137                 }
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);
141         }
142         return 0;
143 }
144
145 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
146 {
147         if (udev_monitor == NULL)
148                 return -1;
149         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
150 }
151
152 /**
153  * udev_monitor_ref:
154  * @udev_monitor: udev monitor
155  *
156  * Take a reference of a udev monitor.
157  *
158  * Returns: the passed udev monitor
159  **/
160 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
161 {
162         if (udev_monitor == NULL)
163                 return NULL;
164         udev_monitor->refcount++;
165         return udev_monitor;
166 }
167
168 /**
169  * udev_monitor_unref:
170  * @udev_monitor: udev monitor
171  *
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
174  * will be released.
175  *
176  **/
177 void udev_monitor_unref(struct udev_monitor *udev_monitor)
178 {
179         if (udev_monitor == NULL)
180                 return;
181         udev_monitor->refcount--;
182         if (udev_monitor->refcount > 0)
183                 return;
184         if (udev_monitor->sock >= 0)
185                 close(udev_monitor->sock);
186         dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
187         free(udev_monitor);
188 }
189
190 /**
191  * udev_monitor_get_udev:
192  * @udev_monitor: udev monitor
193  *
194  * Retrieve the udev library context the monitor was created with.
195  *
196  * Returns: the udev library context
197  **/
198 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
199 {
200         if (udev_monitor == NULL)
201                 return NULL;
202         return udev_monitor->udev;
203 }
204
205 /**
206  * udev_monitor_get_fd:
207  * @udev_monitor: udev monitor
208  *
209  * Retrieve the socket file descriptor associated with the monitor.
210  *
211  * Returns: the socket file descriptor
212  **/
213 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
214 {
215         if (udev_monitor == NULL)
216                 return -1;
217         return udev_monitor->sock;
218 }
219
220 /**
221  * udev_monitor_receive_device:
222  * @udev_monitor: udev monitor
223  *
224  * Receive data from the udev monitor socket, allocate a new udev
225  * device, fill in the received data, and return the device.
226  *
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.
230  *
231  * The initial refcount is 1, and needs to be decremented to
232  * release the resources of the udev device.
233  *
234  * Returns: a new udev device, or #NULL, in case of an error
235  **/
236 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
237 {
238         struct udev_device *udev_device;
239         struct msghdr smsg;
240         struct iovec iov;
241         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
242         char buf[4096];
243         size_t bufpos;
244         int devpath_set = 0;
245         int subsystem_set = 0;
246         int action_set = 0;
247         int maj = 0;
248         int min = 0;
249
250         if (udev_monitor == NULL)
251                 return NULL;
252         memset(buf, 0x00, sizeof(buf));
253         iov.iov_base = &buf;
254         iov.iov_len = sizeof(buf);
255         memset (&smsg, 0x00, sizeof(struct msghdr));
256         smsg.msg_iov = &iov;
257         smsg.msg_iovlen = 1;
258         smsg.msg_control = cred_msg;
259         smsg.msg_controllen = sizeof(cred_msg);
260
261         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
262                 if (errno != EINTR)
263                         info(udev_monitor->udev, "unable to receive message");
264                 return NULL;
265         }
266
267         if (udev_monitor->sun.sun_family != 0) {
268                 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
269                 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
270
271                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
272                         info(udev_monitor->udev, "no sender credentials received, message ignored");
273                         return NULL;
274                 }
275
276                 if (cred->uid != 0) {
277                         info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
278                         return NULL;
279                 }
280         }
281
282         /* skip header */
283         bufpos = strlen(buf) + 1;
284         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
285                 info(udev_monitor->udev, "invalid message length");
286                 return NULL;
287         }
288
289         /* check message header */
290         if (strstr(buf, "@/") == NULL) {
291                 info(udev_monitor->udev, "unrecognized message header");
292                 return NULL;
293         }
294
295         udev_device = device_new(udev_monitor->udev);
296         if (udev_device == NULL) {
297                 return NULL;
298         }
299
300         while (bufpos < sizeof(buf)) {
301                 char *key;
302                 size_t keylen;
303
304                 key = &buf[bufpos];
305                 keylen = strlen(key);
306                 if (keylen == 0)
307                         break;
308                 bufpos += keylen + 1;
309
310                 if (strncmp(key, "DEVPATH=", 8) == 0) {
311                         char path[UTIL_PATH_SIZE];
312
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);
316                         devpath_set = 1;
317                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
318                         udev_device_set_subsystem(udev_device, &key[10]);
319                         subsystem_set = 1;
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];
326                         char *slink;
327                         char *next;
328
329                         util_strlcpy(devlinks, &key[9], sizeof(devlinks));
330                         slink = devlinks;
331                         next = strchr(slink, ' ');
332                         while (next != NULL) {
333                                 next[0] = '\0';
334                                 udev_device_add_devlink(udev_device, slink);
335                                 slink = &next[1];
336                                 next = strchr(slink, ' ');
337                         }
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]);
344                         action_set = 1;
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 */
359                         continue;
360                 } else {
361                         udev_device_add_property_from_string(udev_device, key);
362                 }
363         }
364         if (!devpath_set || !subsystem_set || !action_set) {
365                 info(udev_monitor->udev, "missing values, skip\n");
366                 udev_device_unref(udev_device);
367                 return NULL;
368         }
369         if (maj > 0)
370                 udev_device_set_devnum(udev_device, makedev(maj, min));
371         udev_device_set_info_loaded(udev_device);
372         return udev_device;
373 }
374
375 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
376 {
377         const char *buf;
378         ssize_t len;
379         ssize_t count;
380
381         len = udev_device_get_properties_monitor_buf(udev_device, &buf);
382         if (len < 32)
383                 return -1;
384         count = sendto(udev_monitor->sock,
385                        buf, len, 0,
386                        (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
387         info(udev_monitor->udev, "passed %zi bytes to monitor %p, \n", count, udev_monitor);
388         return count;
389 }