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