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