chiark / gitweb /
move udev_device_db to libudev
[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 <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <linux/netlink.h>
31
32 #include "libudev.h"
33 #include "libudev-private.h"
34
35 struct udev_monitor {
36         struct udev *udev;
37         int refcount;
38         int sock;
39         struct sockaddr_nl snl;
40         struct sockaddr_un sun;
41         socklen_t addrlen;
42 };
43
44 /**
45  * udev_monitor_new_from_socket:
46  * @udev: udev library context
47  * @socket_path: unix socket path
48  *
49  * Create new udev monitor, setup and connect to a specified socket. The
50  * path to a socket can point to an existing socket file, or it will be
51  * created if needed. If neccessary, the permissions adjustment as well as
52  * the later cleanup of the socket file, needs to be done by the caller.
53  * If the socket path starts with a '@' character, an abstract namespace
54  * socket will be used.
55  *
56  * The initial refcount is 1, and needs to be decremented to
57  * release the ressources of the udev monitor.
58  *
59  * Returns: a new udev monitor, or #NULL, in case of an error
60  **/
61 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
62 {
63         struct udev_monitor *udev_monitor;
64         struct stat statbuf;
65
66         if (udev == NULL)
67                 return NULL;
68         if (socket_path == NULL)
69                 return NULL;
70         udev_monitor = malloc(sizeof(struct udev_monitor));
71         if (udev_monitor == NULL)
72                 return NULL;
73         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
74         udev_monitor->refcount = 1;
75         udev_monitor->udev = udev;
76
77         udev_monitor->sun.sun_family = AF_LOCAL;
78         if (socket_path[0] == '@') {
79                 /* translate leading '@' to abstract namespace */
80                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
81                 udev_monitor->sun.sun_path[0] = '\0';
82                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
83         } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
84                 /* existing socket file */
85                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
86                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
87         } else {
88                 /* no socket file, assume abstract namespace socket */
89                 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
90                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
91         }
92         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
93         if (udev_monitor->sock == -1) {
94                 err(udev, "error getting socket: %m\n");
95                 free(udev_monitor);
96                 return NULL;
97         }
98         info(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
99         return udev_monitor;
100 }
101
102 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
103 {
104         struct udev_monitor *udev_monitor;
105
106         if (udev == NULL)
107                 return NULL;
108         udev_monitor = malloc(sizeof(struct udev_monitor));
109         if (udev_monitor == NULL)
110                 return NULL;
111         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
112         udev_monitor->refcount = 1;
113         udev_monitor->udev = udev;
114
115         udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
116         if (udev_monitor->sock == -1) {
117                 err(udev, "error getting socket: %m\n");
118                 free(udev_monitor);
119                 return NULL;
120         }
121
122         udev_monitor->snl.nl_family = AF_NETLINK;
123         udev_monitor->snl.nl_pid = getpid();
124         udev_monitor->snl.nl_groups = 1;
125
126         info(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
127         return udev_monitor;
128 }
129
130 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
131 {
132         int err;
133         const int on = 1;
134
135         if (udev_monitor->snl.nl_family != 0) {
136                 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
137                 if (err < 0) {
138                         err(udev_monitor->udev, "bind failed: %m\n");
139                         return err;
140                 }
141                 info(udev_monitor->udev, "monitor %p listening on netlink\n", udev_monitor);
142         } else if (udev_monitor->sun.sun_family != 0) {
143                 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
144                 if (err < 0) {
145                         err(udev_monitor->udev, "bind failed: %m\n");
146                         return err;
147                 }
148                 /* enable receiving of the sender credentials */
149                 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
150                 info(udev_monitor->udev, "monitor %p listening on socket\n", udev_monitor);
151         }
152         return 0;
153 }
154
155 /**
156  * udev_monitor_ref:
157  * @udev_monitor: udev monitor
158  *
159  * Take a reference of a udev monitor.
160  *
161  * Returns: the passed udev monitor
162  **/
163 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
164 {
165         if (udev_monitor == NULL)
166                 return NULL;
167         udev_monitor->refcount++;
168         return udev_monitor;
169 }
170
171 /**
172  * udev_monitor_unref:
173  * @udev_monitor: udev monitor
174  *
175  * Drop a reference ofa udev monitor. If the refcount reaches zero,
176  * the bound socket will be closed, and the ressources of the monitor
177  * will be released.
178  *
179  **/
180 void udev_monitor_unref(struct udev_monitor *udev_monitor)
181 {
182         if (udev_monitor == NULL)
183                 return;
184         udev_monitor->refcount--;
185         if (udev_monitor->refcount > 0)
186                 return;
187         if (udev_monitor->sock >= 0)
188                 close(udev_monitor->sock);
189         info(udev_monitor->udev, "monitor %p released\n", udev_monitor);
190         free(udev_monitor);
191 }
192
193 /**
194  * udev_monitor_get_udev:
195  * @udev_monitor: udev monitor
196  *
197  * Retrieve the udev library context the monitor was created with.
198  *
199  * Returns: the udev library context
200  **/
201 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
202 {
203         if (udev_monitor == NULL)
204                 return NULL;
205         return udev_monitor->udev;
206 }
207
208 /**
209  * udev_monitor_get_fd:
210  * @udev_monitor: udev monitor
211  *
212  * Retrieve the socket file descriptor associated with the monitor.
213  *
214  * Returns: the socket file descriptor
215  **/
216 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
217 {
218         if (udev_monitor == NULL)
219                 return -1;
220         return udev_monitor->sock;
221 }
222
223 /**
224  * udev_monitor_receive_device:
225  * @udev_monitor: udev monitor
226  *
227  * Receive data from the udev monitor socket, allocate a new udev
228  * device, fill in the received data, and return the device.
229  *
230  * Only socket connections with uid=0 are accepted. The caller
231  * needs to make sure, that there is data to read from the socket,
232  * the call will block until the socket becomes readable.
233  *
234  * The initial refcount is 1, and needs to be decremented to
235  * release the ressources of the udev device.
236  *
237  * Returns: a new udev device, or #NULL, in case of an error
238  **/
239 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
240 {
241         struct udev_device *udev_device;
242         struct msghdr smsg;
243         struct iovec iov;
244         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
245         char buf[4096];
246         size_t bufpos;
247         int devpath_set = 0;
248         int subsystem_set = 0;
249         int action_set = 0;
250         int maj = 0;
251         int min = 0;
252
253         if (udev_monitor == NULL)
254                 return NULL;
255         memset(buf, 0x00, sizeof(buf));
256         iov.iov_base = &buf;
257         iov.iov_len = sizeof(buf);
258         memset (&smsg, 0x00, sizeof(struct msghdr));
259         smsg.msg_iov = &iov;
260         smsg.msg_iovlen = 1;
261         smsg.msg_control = cred_msg;
262         smsg.msg_controllen = sizeof(cred_msg);
263
264         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
265                 if (errno != EINTR)
266                         info(udev_monitor->udev, "unable to receive message");
267                 return NULL;
268         }
269
270         if (udev_monitor->sun.sun_family != 0) {
271                 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
272                 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
273
274                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
275                         info(udev_monitor->udev, "no sender credentials received, message ignored");
276                         return NULL;
277                 }
278
279                 if (cred->uid != 0) {
280                         info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
281                         return NULL;
282                 }
283         }
284
285         /* skip header */
286         bufpos = strlen(buf) + 1;
287         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
288                 info(udev_monitor->udev, "invalid message length");
289                 return NULL;
290         }
291
292         /* check message header */
293         if (strstr(buf, "@/") == NULL) {
294                 info(udev_monitor->udev, "unrecognized message header");
295                 return NULL;
296         }
297
298         udev_device = device_new(udev_monitor->udev);
299         if (udev_device == NULL) {
300                 return NULL;
301         }
302
303         while (bufpos < sizeof(buf)) {
304                 char *key;
305                 size_t keylen;
306
307                 key = &buf[bufpos];
308                 keylen = strlen(key);
309                 if (keylen == 0)
310                         break;
311                 bufpos += keylen + 1;
312
313                 if (strncmp(key, "DEVPATH=", 8) == 0) {
314                         char path[UTIL_PATH_SIZE];
315
316                         util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
317                         util_strlcat(path, &key[8], sizeof(path));
318                         udev_device_set_syspath(udev_device, path);
319                         devpath_set = 1;
320                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
321                         udev_device_set_subsystem(udev_device, &key[10]);
322                         subsystem_set = 1;
323                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
324                         udev_device_set_devnode(udev_device, &key[8]);
325                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
326                         char devlinks[UTIL_PATH_SIZE];
327                         char *slink;
328                         char *next;
329
330                         util_strlcpy(devlinks, &key[9], sizeof(devlinks));
331                         slink = devlinks;
332                         next = strchr(slink, ' ');
333                         while (next != NULL) {
334                                 next[0] = '\0';
335                                 udev_device_add_devlink(udev_device, slink);
336                                 slink = &next[1];
337                                 next = strchr(slink, ' ');
338                         }
339                         if (slink[0] != '\0')
340                                 udev_device_add_devlink(udev_device, slink);
341                 } else if (strncmp(key, "DRIVER=", 7) == 0) {
342                         udev_device_set_driver(udev_device, &key[7]);
343                 } else if (strncmp(key, "ACTION=", 7) == 0) {
344                         udev_device_set_action(udev_device, &key[7]);
345                         action_set = 1;
346                 } else if (strncmp(key, "MAJOR=", 6) == 0) {
347                         maj = strtoull(&key[6], NULL, 10);
348                 } else if (strncmp(key, "MINOR=", 6) == 0) {
349                         min = strtoull(&key[6], NULL, 10);
350                 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
351                         udev_device_set_devpath_old(udev_device, &key[12]);
352                 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
353                         udev_device_set_physdevpath(udev_device, &key[12]);
354                 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
355                         udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
356                 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
357                         udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
358                 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
359                         /* skip deprecated values */
360                         continue;
361                 } else {
362                         udev_device_add_property_from_string(udev_device, key);
363                 }
364         }
365         if (!devpath_set || !subsystem_set || !action_set) {
366                 info(udev_monitor->udev, "missing values, skip\n");
367                 udev_device_unref(udev_device);
368                 return NULL;
369         }
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 *action;
378         struct udev_list_entry *list_entry;
379         char buf[4096];
380         size_t bufpos;
381         ssize_t count;
382
383         action = udev_device_get_action(udev_device);
384         if (action == NULL)
385                 return -EINVAL;
386         bufpos = snprintf(buf, sizeof(buf), "%s@%s", action, udev_device_get_devpath(udev_device));
387         bufpos++;
388         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
389                 bufpos += snprintf(&buf[bufpos], sizeof(buf) - bufpos, "%s=%s",
390                                    udev_list_entry_get_name(list_entry),
391                                    udev_list_entry_get_value(list_entry));
392                 bufpos++;
393         }
394         count = sendto(udev_monitor->sock,
395                        &buf, bufpos, 0,
396                        (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
397         info(udev_monitor->udev, "passed %zi bytes to monitor %p, \n", count, udev_monitor);
398         return count;
399 }