chiark / gitweb /
e72b2066232885c6dc8addbda93b7b8b406bd7d2
[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
65         if (udev == NULL)
66                 return NULL;
67         if (socket_path == NULL)
68                 return NULL;
69         udev_monitor = malloc(sizeof(struct udev_monitor));
70         if (udev_monitor == NULL)
71                 return NULL;
72         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
73         udev_monitor->refcount = 1;
74         udev_monitor->udev = udev;
75
76         udev_monitor->sun.sun_family = AF_LOCAL;
77         strcpy(udev_monitor->sun.sun_path, socket_path);
78         udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->sun.sun_path);
79
80         /* translate leading '@' to abstract namespace */
81         if (udev_monitor->sun.sun_path[0] == '@')
82                 udev_monitor->sun.sun_path[0] = '\0';
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         info(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)
95 {
96         struct udev_monitor *udev_monitor;
97
98         if (udev == NULL)
99                 return NULL;
100         udev_monitor = malloc(sizeof(struct udev_monitor));
101         if (udev_monitor == NULL)
102                 return NULL;
103         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
104         udev_monitor->refcount = 1;
105         udev_monitor->udev = udev;
106
107         udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
108         if (udev_monitor->sock == -1) {
109                 err(udev, "error getting socket: %m\n");
110                 free(udev_monitor);
111                 return NULL;
112         }
113
114         udev_monitor->snl.nl_family = AF_NETLINK;
115         udev_monitor->snl.nl_pid = getpid();
116         udev_monitor->snl.nl_groups = 1;
117
118         info(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT\n", udev_monitor);
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, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
129                 if (err < 0) {
130                         err(udev_monitor->udev, "bind failed: %m\n");
131                         return err;
132                 }
133                 info(udev_monitor->udev, "monitor %p listening on netlink\n", udev_monitor);
134         } else if (udev_monitor->sun.sun_family != 0) {
135                 err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
136                 if (err < 0) {
137                         err(udev_monitor->udev, "bind failed: %m\n");
138                         return err;
139                 }
140                 /* enable receiving of the sender credentials */
141                 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
142                 info(udev_monitor->udev, "monitor %p listening on socket\n", udev_monitor);
143         }
144         return 0;
145 }
146
147 /**
148  * udev_monitor_ref:
149  * @udev_monitor: udev monitor
150  *
151  * Take a reference of a udev monitor.
152  *
153  * Returns: the passed udev monitor
154  **/
155 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
156 {
157         if (udev_monitor == NULL)
158                 return NULL;
159         udev_monitor->refcount++;
160         return udev_monitor;
161 }
162
163 /**
164  * udev_monitor_unref:
165  * @udev_monitor: udev monitor
166  *
167  * Drop a reference ofa udev monitor. If the refcount reaches zero,
168  * the bound socket will be closed, and the ressources of the monitor
169  * will be released.
170  *
171  **/
172 void udev_monitor_unref(struct udev_monitor *udev_monitor)
173 {
174         if (udev_monitor == NULL)
175                 return;
176         udev_monitor->refcount--;
177         if (udev_monitor->refcount > 0)
178                 return;
179         if (udev_monitor->sock >= 0)
180                 close(udev_monitor->sock);
181         info(udev_monitor->udev, "monitor %p released\n", udev_monitor);
182         free(udev_monitor);
183 }
184
185 /**
186  * udev_monitor_get_udev:
187  * @udev_monitor: udev monitor
188  *
189  * Retrieve the udev library context the monitor was created with.
190  *
191  * Returns: the udev library context
192  **/
193 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
194 {
195         if (udev_monitor == NULL)
196                 return NULL;
197         return udev_monitor->udev;
198 }
199
200 /**
201  * udev_monitor_get_fd:
202  * @udev_monitor: udev monitor
203  *
204  * Retrieve the socket file descriptor associated with the monitor.
205  *
206  * Returns: the socket file descriptor
207  **/
208 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
209 {
210         if (udev_monitor == NULL)
211                 return -1;
212         return udev_monitor->sock;
213 }
214
215 /**
216  * udev_monitor_receive_device:
217  * @udev_monitor: udev monitor
218  *
219  * Receive data from the udev monitor socket, allocate a new udev
220  * device, fill in the received data, and return the device.
221  *
222  * Only socket connections with uid=0 are accepted. The caller
223  * needs to make sure, that there is data to read from the socket,
224  * the call will block until the socket becomes readable.
225  *
226  * The initial refcount is 1, and needs to be decremented to
227  * release the ressources of the udev device.
228  *
229  * Returns: a new udev device, or #NULL, in case of an error
230  **/
231 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
232 {
233         struct udev_device *udev_device;
234         struct msghdr smsg;
235         struct iovec iov;
236         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
237         char buf[4096];
238         size_t bufpos;
239         int maj = 0;
240         int min = 0;
241
242         if (udev_monitor == NULL)
243                 return NULL;
244         memset(buf, 0x00, sizeof(buf));
245         iov.iov_base = &buf;
246         iov.iov_len = sizeof(buf);
247         memset (&smsg, 0x00, sizeof(struct msghdr));
248         smsg.msg_iov = &iov;
249         smsg.msg_iovlen = 1;
250         smsg.msg_control = cred_msg;
251         smsg.msg_controllen = sizeof(cred_msg);
252
253         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
254                 if (errno != EINTR)
255                         info(udev_monitor->udev, "unable to receive message");
256                 return NULL;
257         }
258
259         if (udev_monitor->sun.sun_family != 0) {
260                 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg);
261                 struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg);
262
263                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
264                         info(udev_monitor->udev, "no sender credentials received, message ignored");
265                         return NULL;
266                 }
267
268                 if (cred->uid != 0) {
269                         info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
270                         return NULL;
271                 }
272         }
273
274         /* skip header */
275         bufpos = strlen(buf) + 1;
276         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
277                 info(udev_monitor->udev, "invalid message length");
278                 return NULL;
279         }
280
281         /* check message header */
282         if (strstr(buf, "@/") == NULL) {
283                 info(udev_monitor->udev, "unrecognized message header");
284                 return NULL;
285         }
286
287         udev_device = device_init(udev_monitor->udev);
288         if (udev_device == NULL) {
289                 return NULL;
290         }
291
292         while (bufpos < sizeof(buf)) {
293                 char *key;
294                 size_t keylen;
295
296                 key = &buf[bufpos];
297                 keylen = strlen(key);
298                 if (keylen == 0)
299                         break;
300                 bufpos += keylen + 1;
301
302                 if (strncmp(key, "DEVPATH=", 8) == 0) {
303                         char path[UTIL_PATH_SIZE];
304
305                         util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
306                         util_strlcat(path, &key[8], sizeof(path));
307                         device_set_syspath(udev_device, path);
308                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
309                         device_set_subsystem(udev_device, &key[10]);
310                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
311                         device_set_devnode(udev_device, &key[8]);
312                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
313                         char *slink = &key[9];
314                         char *next = strchr(slink, ' ');
315
316                         while (next != NULL) {
317                                 next[0] = '\0';
318                                 device_add_devlink(udev_device, slink);
319                                 slink = &next[1];
320                                 next = strchr(slink, ' ');
321                         }
322                         if (slink[0] != '\0')
323                                 device_add_devlink(udev_device, slink);
324                 } else if (strncmp(key, "DRIVER=", 7) == 0) {
325                         device_set_driver(udev_device, &key[7]);
326                 } else if (strncmp(key, "ACTION=", 7) == 0) {
327                         device_set_action(udev_device, &key[7]);
328                 } else if (strncmp(key, "MAJOR=", 6) == 0) {
329                         maj = strtoull(&key[6], NULL, 10);
330                 } else if (strncmp(key, "MINOR=", 6) == 0) {
331                         min = strtoull(&key[6], NULL, 10);
332                 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
333                         device_set_devpath_old(udev_device, &key[12]);
334                 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
335                         device_set_physdevpath(udev_device, &key[12]);
336                 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
337                         device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
338                 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
339                         device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
340                 }
341                 if (strncmp(key, "PHYSDEV", 7) == 0)
342                         continue;
343                 device_add_property_from_string(udev_device, key);
344         }
345         device_set_devnum(udev_device, makedev(maj, min));
346
347         device_set_info_loaded(udev_device);
348         return udev_device;
349 }