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