]>
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 | * | |
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 | ||
ba6929f6 KS |
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> | |
1c7047ea | 30 | #include <linux/netlink.h> |
ba6929f6 KS |
31 | |
32 | #include "libudev.h" | |
33 | #include "libudev-private.h" | |
ba6929f6 KS |
34 | |
35 | struct udev_monitor { | |
36 | struct udev *udev; | |
37 | int refcount; | |
d59f11e1 | 38 | int sock; |
1c7047ea KS |
39 | struct sockaddr_nl snl; |
40 | struct sockaddr_un sun; | |
d59f11e1 | 41 | socklen_t addrlen; |
ba6929f6 KS |
42 | }; |
43 | ||
7d8787b3 KS |
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 | **/ | |
ba6929f6 KS |
61 | struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path) |
62 | { | |
63 | struct udev_monitor *udev_monitor; | |
ba6929f6 KS |
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 | ||
1c7047ea KS |
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); | |
ba6929f6 KS |
79 | |
80 | /* translate leading '@' to abstract namespace */ | |
1c7047ea KS |
81 | if (udev_monitor->sun.sun_path[0] == '@') |
82 | udev_monitor->sun.sun_path[0] = '\0'; | |
ba6929f6 | 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 | } | |
c4f5f942 | 90 | info(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path); |
d59f11e1 KS |
91 | return udev_monitor; |
92 | } | |
ba6929f6 | 93 | |
1c7047ea KS |
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) { | |
659353f5 | 109 | err(udev, "error getting socket: %m\n"); |
1c7047ea KS |
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 | ||
d59f11e1 KS |
122 | int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) |
123 | { | |
124 | int err; | |
125 | const int on = 1; | |
126 | ||
1c7047ea KS |
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) { | |
659353f5 | 130 | err(udev_monitor->udev, "bind failed: %m\n"); |
1c7047ea KS |
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) { | |
659353f5 | 137 | err(udev_monitor->udev, "bind failed: %m\n"); |
1c7047ea KS |
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); | |
ba6929f6 | 143 | } |
d59f11e1 | 144 | return 0; |
ba6929f6 KS |
145 | } |
146 | ||
7d8787b3 KS |
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 | **/ | |
ba6929f6 KS |
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 | ||
7d8787b3 KS |
163 | /** |
164 | * udev_monitor_unref: | |
165 | * @udev_monitor: udev monitor | |
166 | * | |
d59f11e1 | 167 | * Drop a reference ofa udev monitor. If the refcount reaches zero, |
7d8787b3 KS |
168 | * the bound socket will be closed, and the ressources of the monitor |
169 | * will be released. | |
170 | * | |
171 | **/ | |
ba6929f6 KS |
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; | |
d59f11e1 KS |
179 | if (udev_monitor->sock >= 0) |
180 | close(udev_monitor->sock); | |
c4f5f942 | 181 | info(udev_monitor->udev, "monitor %p released\n", udev_monitor); |
ba6929f6 KS |
182 | free(udev_monitor); |
183 | } | |
184 | ||
7d8787b3 KS |
185 | /** |
186 | * udev_monitor_get_udev: | |
187 | * @udev_monitor: udev monitor | |
188 | * | |
b98fd840 | 189 | * Retrieve the udev library context the monitor was created with. |
7d8787b3 KS |
190 | * |
191 | * Returns: the udev library context | |
192 | **/ | |
ba6929f6 KS |
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 | ||
7d8787b3 KS |
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 | **/ | |
ba6929f6 KS |
208 | int udev_monitor_get_fd(struct udev_monitor *udev_monitor) |
209 | { | |
210 | if (udev_monitor == NULL) | |
211 | return -1; | |
d59f11e1 | 212 | return udev_monitor->sock; |
ba6929f6 KS |
213 | } |
214 | ||
7d8787b3 | 215 | /** |
d59f11e1 | 216 | * udev_monitor_receive_device: |
7d8787b3 KS |
217 | * @udev_monitor: udev monitor |
218 | * | |
d59f11e1 | 219 | * Receive data from the udev monitor socket, allocate a new udev |
b98fd840 | 220 | * device, fill in the received data, and return the device. |
7d8787b3 KS |
221 | * |
222 | * Only socket connections with uid=0 are accepted. The caller | |
b98fd840 | 223 | * needs to make sure, that there is data to read from the socket, |
7d8787b3 KS |
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 | **/ | |
d59f11e1 | 231 | struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor) |
ba6929f6 KS |
232 | { |
233 | struct udev_device *udev_device; | |
234 | struct msghdr smsg; | |
ba6929f6 | 235 | struct iovec iov; |
ba6929f6 KS |
236 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; |
237 | char buf[4096]; | |
238 | size_t bufpos; | |
37372bbc KS |
239 | int maj = 0; |
240 | int min = 0; | |
ba6929f6 KS |
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 | ||
d59f11e1 | 253 | if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) { |
ba6929f6 | 254 | if (errno != EINTR) |
7d563a17 | 255 | info(udev_monitor->udev, "unable to receive message"); |
ba6929f6 KS |
256 | return NULL; |
257 | } | |
ba6929f6 | 258 | |
1c7047ea KS |
259 | if (udev_monitor->sun.sun_family != 0) { |
260 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&smsg); | |
261 | struct ucred *cred = (struct ucred *)CMSG_DATA (cmsg); | |
ba6929f6 | 262 | |
1c7047ea KS |
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 | } | |
ba6929f6 KS |
272 | } |
273 | ||
274 | /* skip header */ | |
275 | bufpos = strlen(buf) + 1; | |
276 | if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) { | |
7d563a17 | 277 | info(udev_monitor->udev, "invalid message length"); |
ba6929f6 KS |
278 | return NULL; |
279 | } | |
280 | ||
281 | /* check message header */ | |
282 | if (strstr(buf, "@/") == NULL) { | |
7d563a17 | 283 | info(udev_monitor->udev, "unrecognized message header"); |
ba6929f6 KS |
284 | return NULL; |
285 | } | |
286 | ||
e0083e8e | 287 | udev_device = device_new(udev_monitor->udev); |
ba6929f6 KS |
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) { | |
8753fadf KS |
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)); | |
8cd2e972 | 307 | udev_device_set_syspath(udev_device, path); |
ba6929f6 | 308 | } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) { |
8cd2e972 | 309 | udev_device_set_subsystem(udev_device, &key[10]); |
ba6929f6 | 310 | } else if (strncmp(key, "DEVNAME=", 8) == 0) { |
8cd2e972 | 311 | udev_device_set_devnode(udev_device, &key[8]); |
ba6929f6 KS |
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'; | |
8cd2e972 | 318 | udev_device_add_devlink(udev_device, slink); |
ba6929f6 KS |
319 | slink = &next[1]; |
320 | next = strchr(slink, ' '); | |
321 | } | |
322 | if (slink[0] != '\0') | |
8cd2e972 | 323 | udev_device_add_devlink(udev_device, slink); |
37372bbc | 324 | } else if (strncmp(key, "DRIVER=", 7) == 0) { |
8cd2e972 | 325 | udev_device_set_driver(udev_device, &key[7]); |
37372bbc | 326 | } else if (strncmp(key, "ACTION=", 7) == 0) { |
8cd2e972 | 327 | udev_device_set_action(udev_device, &key[7]); |
37372bbc KS |
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) { | |
8cd2e972 | 333 | udev_device_set_devpath_old(udev_device, &key[12]); |
37372bbc | 334 | } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) { |
8cd2e972 | 335 | udev_device_set_physdevpath(udev_device, &key[12]); |
37372bbc | 336 | } else if (strncmp(key, "SEQNUM=", 7) == 0) { |
8cd2e972 | 337 | udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10)); |
37372bbc | 338 | } else if (strncmp(key, "TIMEOUT=", 8) == 0) { |
8cd2e972 | 339 | udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10)); |
ba6929f6 | 340 | } |
8753fadf KS |
341 | if (strncmp(key, "PHYSDEV", 7) == 0) |
342 | continue; | |
8cd2e972 | 343 | udev_device_add_property_from_string(udev_device, key); |
ba6929f6 | 344 | } |
8cd2e972 | 345 | udev_device_set_devnum(udev_device, makedev(maj, min)); |
ba6929f6 | 346 | |
8cd2e972 | 347 | udev_device_set_info_loaded(udev_device); |
ba6929f6 KS |
348 | return udev_device; |
349 | } |