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