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