chiark / gitweb /
libudev: switch API from devpath to syspath
[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 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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <linux/netlink.h>
33
34 #include "libudev.h"
35 #include "libudev-private.h"
36
37 struct udev_monitor {
38         struct udev *udev;
39         int refcount;
40         int sock;
41         struct sockaddr_nl snl;
42         struct sockaddr_un sun;
43         socklen_t addrlen;
44 };
45
46 /**
47  * udev_monitor_new_from_socket:
48  * @udev: udev library context
49  * @socket_path: unix socket path
50  *
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.
57  *
58  * The initial refcount is 1, and needs to be decremented to
59  * release the ressources of the udev monitor.
60  *
61  * Returns: a new udev monitor, or #NULL, in case of an error
62  **/
63 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
64 {
65         struct udev_monitor *udev_monitor;
66
67         if (udev == NULL)
68                 return NULL;
69         if (socket_path == NULL)
70                 return NULL;
71         udev_monitor = malloc(sizeof(struct udev_monitor));
72         if (udev_monitor == NULL)
73                 return NULL;
74         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
75         udev_monitor->refcount = 1;
76         udev_monitor->udev = udev;
77
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);
81
82         /* translate leading '@' to abstract namespace */
83         if (udev_monitor->sun.sun_path[0] == '@')
84                 udev_monitor->sun.sun_path[0] = '\0';
85
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));
89                 free(udev_monitor);
90                 return NULL;
91         }
92         info(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
93         return udev_monitor;
94 }
95
96 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
97 {
98         struct udev_monitor *udev_monitor;
99
100         if (udev == NULL)
101                 return NULL;
102         udev_monitor = malloc(sizeof(struct udev_monitor));
103         if (udev_monitor == NULL)
104                 return NULL;
105         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
106         udev_monitor->refcount = 1;
107         udev_monitor->udev = udev;
108
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));
112                 free(udev_monitor);
113                 return NULL;
114         }
115
116         udev_monitor->snl.nl_family = AF_NETLINK;
117         udev_monitor->snl.nl_pid = getpid();
118         udev_monitor->snl.nl_groups = 1;
119
120         info(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
121         return udev_monitor;
122 }
123
124 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
125 {
126         int err;
127         const int on = 1;
128
129         if (udev_monitor->snl.nl_family != 0) {
130                 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
131                 if (err < 0) {
132                         err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
133                         return err;
134                 }
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);
138                 if (err < 0) {
139                         err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
140                         return err;
141                 }
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);
145         }
146         return 0;
147 }
148
149 /**
150  * udev_monitor_ref:
151  * @udev_monitor: udev monitor
152  *
153  * Take a reference of a udev monitor.
154  *
155  * Returns: the passed udev monitor
156  **/
157 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
158 {
159         if (udev_monitor == NULL)
160                 return NULL;
161         udev_monitor->refcount++;
162         return udev_monitor;
163 }
164
165 /**
166  * udev_monitor_unref:
167  * @udev_monitor: udev monitor
168  *
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
171  * will be released.
172  *
173  **/
174 void udev_monitor_unref(struct udev_monitor *udev_monitor)
175 {
176         if (udev_monitor == NULL)
177                 return;
178         udev_monitor->refcount--;
179         if (udev_monitor->refcount > 0)
180                 return;
181         if (udev_monitor->sock >= 0)
182                 close(udev_monitor->sock);
183         info(udev_monitor->udev, "monitor %p released\n", udev_monitor);
184         free(udev_monitor);
185 }
186
187 /**
188  * udev_monitor_get_udev:
189  * @udev_monitor: udev monitor
190  *
191  * Retrieve the udev library context the monitor was created with.
192  *
193  * Returns: the udev library context
194  **/
195 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
196 {
197         if (udev_monitor == NULL)
198                 return NULL;
199         return udev_monitor->udev;
200 }
201
202 /**
203  * udev_monitor_get_fd:
204  * @udev_monitor: udev monitor
205  *
206  * Retrieve the socket file descriptor associated with the monitor.
207  *
208  * Returns: the socket file descriptor
209  **/
210 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
211 {
212         if (udev_monitor == NULL)
213                 return -1;
214         return udev_monitor->sock;
215 }
216
217 /**
218  * udev_monitor_receive_device:
219  * @udev_monitor: udev monitor
220  *
221  * Receive data from the udev monitor socket, allocate a new udev
222  * device, fill in the received data, and return the device.
223  *
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.
227  *
228  * The initial refcount is 1, and needs to be decremented to
229  * release the ressources of the udev device.
230  *
231  * Returns: a new udev device, or #NULL, in case of an error
232  **/
233 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
234 {
235         struct udev_device *udev_device;
236         struct msghdr smsg;
237         struct iovec iov;
238         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
239         char buf[4096];
240         size_t bufpos;
241         int maj = 0;
242         int min = 0;
243
244         if (udev_monitor == NULL)
245                 return NULL;
246         memset(buf, 0x00, sizeof(buf));
247         iov.iov_base = &buf;
248         iov.iov_len = sizeof(buf);
249         memset (&smsg, 0x00, sizeof(struct msghdr));
250         smsg.msg_iov = &iov;
251         smsg.msg_iovlen = 1;
252         smsg.msg_control = cred_msg;
253         smsg.msg_controllen = sizeof(cred_msg);
254
255         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
256                 if (errno != EINTR)
257                         info(udev_monitor->udev, "unable to receive message");
258                 return NULL;
259         }
260
261         if (udev_monitor->sun.sun_family != 0) {
262                 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
263                 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
264
265                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
266                         info(udev_monitor->udev, "no sender credentials received, message ignored");
267                         return NULL;
268                 }
269
270                 if (cred->uid != 0) {
271                         info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
272                         return NULL;
273                 }
274         }
275
276         /* skip header */
277         bufpos = strlen(buf) + 1;
278         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
279                 info(udev_monitor->udev, "invalid message length");
280                 return NULL;
281         }
282
283         /* check message header */
284         if (strstr(buf, "@/") == NULL) {
285                 info(udev_monitor->udev, "unrecognized message header");
286                 return NULL;
287         }
288
289         udev_device = device_init(udev_monitor->udev);
290         if (udev_device == NULL) {
291                 return NULL;
292         }
293
294         while (bufpos < sizeof(buf)) {
295                 char *key;
296                 size_t keylen;
297
298                 key = &buf[bufpos];
299                 keylen = strlen(key);
300                 if (keylen == 0)
301                         break;
302                 bufpos += keylen + 1;
303
304                 if (strncmp(key, "DEVPATH=", 8) == 0) {
305                         char path[UTIL_PATH_SIZE];
306
307                         util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
308                         util_strlcat(path, &key[8], sizeof(path));
309                         device_set_syspath(udev_device, path);
310                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
311                         device_set_subsystem(udev_device, &key[10]);
312                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
313                         device_set_devname(udev_device, &key[8]);
314                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
315                         char *slink = &key[9];
316                         char *next = strchr(slink, ' ');
317
318                         while (next != NULL) {
319                                 next[0] = '\0';
320                                 device_add_devlink(udev_device, slink);
321                                 slink = &next[1];
322                                 next = strchr(slink, ' ');
323                         }
324                         if (slink[0] != '\0')
325                                 device_add_devlink(udev_device, slink);
326                 } else if (strncmp(key, "DRIVER=", 7) == 0) {
327                         device_set_driver(udev_device, &key[7]);
328                 } else if (strncmp(key, "ACTION=", 7) == 0) {
329                         device_set_action(udev_device, &key[7]);
330                 } else if (strncmp(key, "MAJOR=", 6) == 0) {
331                         maj = strtoull(&key[6], NULL, 10);
332                 } else if (strncmp(key, "MINOR=", 6) == 0) {
333                         min = strtoull(&key[6], NULL, 10);
334                 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
335                         device_set_devpath_old(udev_device, &key[12]);
336                 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
337                         device_set_physdevpath(udev_device, &key[12]);
338                 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
339                         device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
340                 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
341                         device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
342                 }
343                 if (strncmp(key, "PHYSDEV", 7) == 0)
344                         continue;
345                 device_add_property_from_string(udev_device, key);
346         }
347         device_set_devnum(udev_device, makedev(maj, min));
348
349         return udev_device;
350 }