chiark / gitweb /
b2f76c3129bf2216b333566fb53b9f12723b184a
[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_nl snl_peer;
33         struct sockaddr_un sun;
34         socklen_t addrlen;
35 };
36
37 /**
38  * udev_monitor_new_from_socket:
39  * @udev: udev library context
40  * @socket_path: unix socket path
41  *
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.
48  *
49  * The initial refcount is 1, and needs to be decremented to
50  * release the resources of the udev monitor.
51  *
52  * Returns: a new udev monitor, or #NULL, in case of an error
53  **/
54 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
55 {
56         struct udev_monitor *udev_monitor;
57         struct stat statbuf;
58
59         if (udev == NULL)
60                 return NULL;
61         if (socket_path == NULL)
62                 return NULL;
63         udev_monitor = calloc(1, sizeof(struct udev_monitor));
64         if (udev_monitor == NULL)
65                 return NULL;
66         udev_monitor->refcount = 1;
67         udev_monitor->udev = udev;
68
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);
79         } else {
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;
83         }
84         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
85         if (udev_monitor->sock == -1) {
86                 err(udev, "error getting socket: %m\n");
87                 free(udev_monitor);
88                 return NULL;
89         }
90         dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
91         return udev_monitor;
92 }
93
94 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, unsigned int group)
95 {
96         struct udev_monitor *udev_monitor;
97
98         if (udev == NULL)
99                 return NULL;
100         udev_monitor = calloc(1, sizeof(struct udev_monitor));
101         if (udev_monitor == NULL)
102                 return NULL;
103         udev_monitor->refcount = 1;
104         udev_monitor->udev = udev;
105
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");
109                 free(udev_monitor);
110                 return NULL;
111         }
112
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;
117
118         dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
119         return udev_monitor;
120 }
121
122 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
123 {
124         int err;
125         const int on = 1;
126
127         if (udev_monitor->snl.nl_family != 0) {
128                 err = bind(udev_monitor->sock,
129                            (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
130                 if (err < 0) {
131                         err(udev_monitor->udev, "bind failed: %m\n");
132                         return err;
133                 }
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);
138                 if (err < 0) {
139                         err(udev_monitor->udev, "bind failed: %m\n");
140                         return err;
141                 }
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);
145         }
146         return 0;
147 }
148
149 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
150 {
151         if (udev_monitor == NULL)
152                 return -1;
153         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
154 }
155
156 /**
157  * udev_monitor_ref:
158  * @udev_monitor: udev monitor
159  *
160  * Take a reference of a udev monitor.
161  *
162  * Returns: the passed udev monitor
163  **/
164 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
165 {
166         if (udev_monitor == NULL)
167                 return NULL;
168         udev_monitor->refcount++;
169         return udev_monitor;
170 }
171
172 /**
173  * udev_monitor_unref:
174  * @udev_monitor: udev monitor
175  *
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
178  * will be released.
179  *
180  **/
181 void udev_monitor_unref(struct udev_monitor *udev_monitor)
182 {
183         if (udev_monitor == NULL)
184                 return;
185         udev_monitor->refcount--;
186         if (udev_monitor->refcount > 0)
187                 return;
188         if (udev_monitor->sock >= 0)
189                 close(udev_monitor->sock);
190         dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
191         free(udev_monitor);
192 }
193
194 /**
195  * udev_monitor_get_udev:
196  * @udev_monitor: udev monitor
197  *
198  * Retrieve the udev library context the monitor was created with.
199  *
200  * Returns: the udev library context
201  **/
202 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
203 {
204         if (udev_monitor == NULL)
205                 return NULL;
206         return udev_monitor->udev;
207 }
208
209 /**
210  * udev_monitor_get_fd:
211  * @udev_monitor: udev monitor
212  *
213  * Retrieve the socket file descriptor associated with the monitor.
214  *
215  * Returns: the socket file descriptor
216  **/
217 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
218 {
219         if (udev_monitor == NULL)
220                 return -1;
221         return udev_monitor->sock;
222 }
223
224 /**
225  * udev_monitor_receive_device:
226  * @udev_monitor: udev monitor
227  *
228  * Receive data from the udev monitor socket, allocate a new udev
229  * device, fill in the received data, and return the device.
230  *
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.
234  *
235  * The initial refcount is 1, and needs to be decremented to
236  * release the resources of the udev device.
237  *
238  * Returns: a new udev device, or #NULL, in case of an error
239  **/
240 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
241 {
242         struct udev_device *udev_device;
243         struct msghdr smsg;
244         struct iovec iov;
245         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
246         char buf[4096];
247         size_t bufpos;
248         int devpath_set = 0;
249         int subsystem_set = 0;
250         int action_set = 0;
251         int maj = 0;
252         int min = 0;
253
254         if (udev_monitor == NULL)
255                 return NULL;
256         memset(buf, 0x00, sizeof(buf));
257         iov.iov_base = &buf;
258         iov.iov_len = sizeof(buf);
259         memset (&smsg, 0x00, sizeof(struct msghdr));
260         smsg.msg_iov = &iov;
261         smsg.msg_iovlen = 1;
262         smsg.msg_control = cred_msg;
263         smsg.msg_controllen = sizeof(cred_msg);
264
265         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
266                 if (errno != EINTR)
267                         info(udev_monitor->udev, "unable to receive message");
268                 return NULL;
269         }
270
271         if (udev_monitor->sun.sun_family != 0) {
272                 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
273                 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
274
275                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
276                         info(udev_monitor->udev, "no sender credentials received, message ignored");
277                         return NULL;
278                 }
279
280                 if (cred->uid != 0) {
281                         info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
282                         return NULL;
283                 }
284         }
285
286         /* skip header */
287         bufpos = strlen(buf) + 1;
288         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
289                 info(udev_monitor->udev, "invalid message length");
290                 return NULL;
291         }
292
293         /* check message header */
294         if (strstr(buf, "@/") == NULL) {
295                 info(udev_monitor->udev, "unrecognized message header");
296                 return NULL;
297         }
298
299         udev_device = device_new(udev_monitor->udev);
300         if (udev_device == NULL) {
301                 return NULL;
302         }
303
304         while (bufpos < sizeof(buf)) {
305                 char *key;
306                 size_t keylen;
307
308                 key = &buf[bufpos];
309                 keylen = strlen(key);
310                 if (keylen == 0)
311                         break;
312                 bufpos += keylen + 1;
313
314                 if (strncmp(key, "DEVPATH=", 8) == 0) {
315                         char path[UTIL_PATH_SIZE];
316
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);
320                         devpath_set = 1;
321                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
322                         udev_device_set_subsystem(udev_device, &key[10]);
323                         subsystem_set = 1;
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];
330                         char *slink;
331                         char *next;
332
333                         util_strlcpy(devlinks, &key[9], sizeof(devlinks));
334                         slink = devlinks;
335                         next = strchr(slink, ' ');
336                         while (next != NULL) {
337                                 next[0] = '\0';
338                                 udev_device_add_devlink(udev_device, slink);
339                                 slink = &next[1];
340                                 next = strchr(slink, ' ');
341                         }
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]);
348                         action_set = 1;
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 */
363                         continue;
364                 } else {
365                         udev_device_add_property_from_string(udev_device, key);
366                 }
367         }
368         if (!devpath_set || !subsystem_set || !action_set) {
369                 info(udev_monitor->udev, "missing values, skip\n");
370                 udev_device_unref(udev_device);
371                 return NULL;
372         }
373         if (maj > 0)
374                 udev_device_set_devnum(udev_device, makedev(maj, min));
375         udev_device_set_info_loaded(udev_device);
376         return udev_device;
377 }
378
379 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
380 {
381         const char *buf;
382         ssize_t len;
383         ssize_t count;
384
385         len = udev_device_get_properties_monitor_buf(udev_device, &buf);
386         if (len < 32)
387                 return -1;
388         if (udev_monitor->sun.sun_family != 0) {
389                 count = sendto(udev_monitor->sock,
390                                buf, len, 0,
391                                (struct sockaddr *)&udev_monitor->sun,
392                                udev_monitor->addrlen);
393         } else {
394                 /* no destination besides the muticast group, we will always get -1 ECONNREFUSED */
395                 count = sendto(udev_monitor->sock,
396                                buf, len, 0,
397                                (struct sockaddr *)&udev_monitor->snl_peer,
398                                sizeof(struct sockaddr_nl));
399         }
400         info(udev_monitor->udev, "passed %zi bytes to monitor %p, \n", count, udev_monitor);
401         return count;
402 }