]>
Commit | Line | Data |
---|---|---|
eb1f0e66 KS |
1 | /* |
2 | * libudev - interface to udev device information | |
3 | * | |
fbb31cd6 | 4 | * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org> |
eb1f0e66 | 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. | |
eb1f0e66 KS |
10 | */ |
11 | ||
eb1f0e66 KS |
12 | #include <stdio.h> |
13 | #include <stdlib.h> | |
14 | #include <stddef.h> | |
15 | #include <unistd.h> | |
fc8d61c5 | 16 | #include <stdbool.h> |
eb1f0e66 KS |
17 | #include <errno.h> |
18 | #include <string.h> | |
19 | #include <dirent.h> | |
93b0f384 | 20 | #include <fcntl.h> |
517814e7 | 21 | #include <ctype.h> |
3e0a2c9a | 22 | #include <net/if.h> |
eb1f0e66 | 23 | #include <sys/stat.h> |
3e0a2c9a KS |
24 | #include <sys/ioctl.h> |
25 | #include <sys/socket.h> | |
26 | #include <linux/sockios.h> | |
eb1f0e66 KS |
27 | |
28 | #include "libudev.h" | |
29 | #include "libudev-private.h" | |
eb1f0e66 | 30 | |
ce1d6d7f KS |
31 | /** |
32 | * SECTION:libudev-device | |
33 | * @short_description: kernel sys devices | |
34 | * | |
35 | * Representation of kernel sys devices. Devices are uniquely identified | |
36 | * by their syspath, every device has exactly one path in the kernel sys | |
37 | * filesystem. Devices usually belong to a kernel subsystem, and and have | |
214a6c79 | 38 | * a unique name inside that subsystem. |
ce1d6d7f KS |
39 | */ |
40 | ||
1e511322 KS |
41 | /** |
42 | * udev_device: | |
43 | * | |
ce1d6d7f | 44 | * Opaque object representing one kernel sys device. |
1e511322 | 45 | */ |
11d543c1 | 46 | struct udev_device { |
912541b0 KS |
47 | struct udev *udev; |
48 | struct udev_device *parent_device; | |
49 | char *syspath; | |
50 | const char *devpath; | |
51 | char *sysname; | |
52 | const char *sysnum; | |
53 | char *devnode; | |
54 | mode_t devnode_mode; | |
55 | char *subsystem; | |
56 | char *devtype; | |
57 | char *driver; | |
58 | char *action; | |
59 | char *devpath_old; | |
60 | char *id_filename; | |
61 | char **envp; | |
62 | char *monitor_buf; | |
63 | size_t monitor_buf_len; | |
64 | struct udev_list devlinks_list; | |
65 | struct udev_list properties_list; | |
66 | struct udev_list sysattr_value_list; | |
67 | struct udev_list sysattr_list; | |
68 | struct udev_list tags_list; | |
69 | unsigned long long int seqnum; | |
70 | unsigned long long int usec_initialized; | |
71 | int devlink_priority; | |
72 | int refcount; | |
73 | dev_t devnum; | |
74 | int ifindex; | |
75 | int watch_handle; | |
76 | int maj, min; | |
77 | bool parent_set; | |
78 | bool subsystem_set; | |
79 | bool devtype_set; | |
80 | bool devlinks_uptodate; | |
81 | bool envp_uptodate; | |
82 | bool tags_uptodate; | |
83 | bool driver_set; | |
84 | bool info_loaded; | |
85 | bool db_loaded; | |
86 | bool uevent_loaded; | |
87 | bool is_initialized; | |
88 | bool sysattr_list_read; | |
89 | bool db_persist; | |
11d543c1 KS |
90 | }; |
91 | ||
905555dc | 92 | /** |
a3321f68 | 93 | * udev_device_get_seqnum: |
905555dc KS |
94 | * @udev_device: udev device |
95 | * | |
96 | * This is only valid if the device was received through a monitor. Devices read from | |
97 | * sys do not have a sequence number. | |
98 | * | |
99 | * Returns: the kernel event sequence number, or 0 if there is no sequence number available. | |
100 | **/ | |
54cf0b7f | 101 | _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) |
905555dc | 102 | { |
912541b0 KS |
103 | if (udev_device == NULL) |
104 | return 0; | |
105 | return udev_device->seqnum; | |
905555dc KS |
106 | } |
107 | ||
108 | static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum) | |
109 | { | |
912541b0 | 110 | char num[32]; |
905555dc | 111 | |
912541b0 KS |
112 | udev_device->seqnum = seqnum; |
113 | snprintf(num, sizeof(num), "%llu", seqnum); | |
114 | udev_device_add_property(udev_device, "SEQNUM", num); | |
115 | return 0; | |
905555dc KS |
116 | } |
117 | ||
118 | int udev_device_get_ifindex(struct udev_device *udev_device) | |
119 | { | |
912541b0 KS |
120 | if (!udev_device->info_loaded) |
121 | udev_device_read_uevent_file(udev_device); | |
122 | return udev_device->ifindex; | |
905555dc KS |
123 | } |
124 | ||
125 | static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex) | |
126 | { | |
912541b0 | 127 | char num[32]; |
905555dc | 128 | |
912541b0 KS |
129 | udev_device->ifindex = ifindex; |
130 | snprintf(num, sizeof(num), "%u", ifindex); | |
131 | udev_device_add_property(udev_device, "IFINDEX", num); | |
132 | return 0; | |
905555dc KS |
133 | } |
134 | ||
135 | /** | |
136 | * udev_device_get_devnum: | |
137 | * @udev_device: udev device | |
138 | * | |
21dbe43a KS |
139 | * Get the device major/minor number. |
140 | * | |
141 | * Returns: the dev_t number. | |
905555dc | 142 | **/ |
54cf0b7f | 143 | _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device) |
905555dc | 144 | { |
912541b0 KS |
145 | if (udev_device == NULL) |
146 | return makedev(0, 0); | |
147 | if (!udev_device->info_loaded) | |
148 | udev_device_read_uevent_file(udev_device); | |
149 | return udev_device->devnum; | |
905555dc KS |
150 | } |
151 | ||
152 | static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum) | |
153 | { | |
912541b0 | 154 | char num[32]; |
905555dc | 155 | |
912541b0 | 156 | udev_device->devnum = devnum; |
905555dc | 157 | |
912541b0 KS |
158 | snprintf(num, sizeof(num), "%u", major(devnum)); |
159 | udev_device_add_property(udev_device, "MAJOR", num); | |
160 | snprintf(num, sizeof(num), "%u", minor(devnum)); | |
161 | udev_device_add_property(udev_device, "MINOR", num); | |
162 | return 0; | |
905555dc KS |
163 | } |
164 | ||
905555dc KS |
165 | const char *udev_device_get_devpath_old(struct udev_device *udev_device) |
166 | { | |
912541b0 | 167 | return udev_device->devpath_old; |
905555dc KS |
168 | } |
169 | ||
170 | static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old) | |
171 | { | |
912541b0 | 172 | const char *pos; |
905555dc | 173 | |
912541b0 KS |
174 | free(udev_device->devpath_old); |
175 | udev_device->devpath_old = strdup(devpath_old); | |
176 | if (udev_device->devpath_old == NULL) | |
177 | return -ENOMEM; | |
178 | udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old); | |
905555dc | 179 | |
912541b0 KS |
180 | pos = strrchr(udev_device->devpath_old, '/'); |
181 | if (pos == NULL) | |
182 | return -EINVAL; | |
183 | return 0; | |
905555dc KS |
184 | } |
185 | ||
186 | /** | |
187 | * udev_device_get_driver: | |
188 | * @udev_device: udev device | |
189 | * | |
21dbe43a KS |
190 | * Get the kernel driver name. |
191 | * | |
192 | * Returns: the driver name string, or #NULL if there is no driver attached. | |
905555dc | 193 | **/ |
54cf0b7f | 194 | _public_ const char *udev_device_get_driver(struct udev_device *udev_device) |
905555dc | 195 | { |
912541b0 | 196 | char driver[UTIL_NAME_SIZE]; |
905555dc | 197 | |
912541b0 KS |
198 | if (udev_device == NULL) |
199 | return NULL; | |
200 | if (!udev_device->driver_set) { | |
201 | udev_device->driver_set = true; | |
202 | if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0) | |
203 | udev_device->driver = strdup(driver); | |
204 | } | |
205 | return udev_device->driver; | |
905555dc KS |
206 | } |
207 | ||
208 | static int udev_device_set_driver(struct udev_device *udev_device, const char *driver) | |
209 | { | |
912541b0 KS |
210 | free(udev_device->driver); |
211 | udev_device->driver = strdup(driver); | |
212 | if (udev_device->driver == NULL) | |
213 | return -ENOMEM; | |
214 | udev_device->driver_set = true; | |
215 | udev_device_add_property(udev_device, "DRIVER", udev_device->driver); | |
216 | return 0; | |
905555dc KS |
217 | } |
218 | ||
219 | /** | |
220 | * udev_device_get_devtype: | |
221 | * @udev_device: udev device | |
222 | * | |
223 | * Retrieve the devtype string of the udev device. | |
224 | * | |
225 | * Returns: the devtype name of the udev device, or #NULL if it can not be determined | |
226 | **/ | |
54cf0b7f | 227 | _public_ const char *udev_device_get_devtype(struct udev_device *udev_device) |
905555dc | 228 | { |
912541b0 KS |
229 | if (udev_device == NULL) |
230 | return NULL; | |
231 | if (!udev_device->devtype_set) { | |
232 | udev_device->devtype_set = true; | |
233 | udev_device_read_uevent_file(udev_device); | |
234 | } | |
235 | return udev_device->devtype; | |
905555dc KS |
236 | } |
237 | ||
238 | static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype) | |
239 | { | |
912541b0 KS |
240 | free(udev_device->devtype); |
241 | udev_device->devtype = strdup(devtype); | |
242 | if (udev_device->devtype == NULL) | |
243 | return -ENOMEM; | |
244 | udev_device->devtype_set = true; | |
245 | udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype); | |
246 | return 0; | |
905555dc KS |
247 | } |
248 | ||
249 | static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem) | |
250 | { | |
912541b0 KS |
251 | free(udev_device->subsystem); |
252 | udev_device->subsystem = strdup(subsystem); | |
253 | if (udev_device->subsystem == NULL) | |
254 | return -ENOMEM; | |
255 | udev_device->subsystem_set = true; | |
256 | udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem); | |
257 | return 0; | |
905555dc KS |
258 | } |
259 | ||
260 | /** | |
261 | * udev_device_get_subsystem: | |
262 | * @udev_device: udev device | |
263 | * | |
264 | * Retrieve the subsystem string of the udev device. The string does not | |
265 | * contain any "/". | |
266 | * | |
267 | * Returns: the subsystem name of the udev device, or #NULL if it can not be determined | |
268 | **/ | |
54cf0b7f | 269 | _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device) |
905555dc | 270 | { |
912541b0 KS |
271 | char subsystem[UTIL_NAME_SIZE]; |
272 | ||
273 | if (udev_device == NULL) | |
274 | return NULL; | |
275 | if (!udev_device->subsystem_set) { | |
276 | udev_device->subsystem_set = true; | |
277 | /* read "subsystem" link */ | |
278 | if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) { | |
279 | udev_device_set_subsystem(udev_device, subsystem); | |
280 | return udev_device->subsystem; | |
281 | } | |
282 | /* implicit names */ | |
33502ffe | 283 | if (startswith(udev_device->devpath, "/module/")) { |
912541b0 KS |
284 | udev_device_set_subsystem(udev_device, "module"); |
285 | return udev_device->subsystem; | |
286 | } | |
287 | if (strstr(udev_device->devpath, "/drivers/") != NULL) { | |
288 | udev_device_set_subsystem(udev_device, "drivers"); | |
289 | return udev_device->subsystem; | |
290 | } | |
33502ffe KS |
291 | if (startswith(udev_device->devpath, "/subsystem/") || |
292 | startswith(udev_device->devpath, "/class/") || | |
293 | startswith(udev_device->devpath, "/bus/")) { | |
912541b0 KS |
294 | udev_device_set_subsystem(udev_device, "subsystem"); |
295 | return udev_device->subsystem; | |
296 | } | |
297 | } | |
298 | return udev_device->subsystem; | |
905555dc KS |
299 | } |
300 | ||
301 | mode_t udev_device_get_devnode_mode(struct udev_device *udev_device) | |
302 | { | |
912541b0 KS |
303 | if (!udev_device->info_loaded) |
304 | udev_device_read_uevent_file(udev_device); | |
305 | return udev_device->devnode_mode; | |
905555dc KS |
306 | } |
307 | ||
308 | static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode) | |
309 | { | |
912541b0 | 310 | char num[32]; |
905555dc | 311 | |
912541b0 KS |
312 | udev_device->devnode_mode = mode; |
313 | snprintf(num, sizeof(num), "%#o", mode); | |
314 | udev_device_add_property(udev_device, "DEVMODE", num); | |
315 | return 0; | |
905555dc KS |
316 | } |
317 | ||
fc8d61c5 KS |
318 | struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value) |
319 | { | |
912541b0 KS |
320 | udev_device->envp_uptodate = false; |
321 | if (value == NULL) { | |
322 | struct udev_list_entry *list_entry; | |
fc8d61c5 | 323 | |
912541b0 KS |
324 | list_entry = udev_device_get_properties_list_entry(udev_device); |
325 | list_entry = udev_list_entry_get_by_name(list_entry, key); | |
326 | if (list_entry != NULL) | |
327 | udev_list_entry_delete(list_entry); | |
328 | return NULL; | |
329 | } | |
330 | return udev_list_entry_add(&udev_device->properties_list, key, value); | |
fc8d61c5 KS |
331 | } |
332 | ||
333 | static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property) | |
334 | { | |
912541b0 KS |
335 | char name[UTIL_LINE_SIZE]; |
336 | char *val; | |
fc8d61c5 | 337 | |
912541b0 KS |
338 | util_strscpy(name, sizeof(name), property); |
339 | val = strchr(name, '='); | |
340 | if (val == NULL) | |
341 | return NULL; | |
342 | val[0] = '\0'; | |
343 | val = &val[1]; | |
344 | if (val[0] == '\0') | |
345 | val = NULL; | |
346 | return udev_device_add_property(udev_device, name, val); | |
fc8d61c5 KS |
347 | } |
348 | ||
349 | /* | |
350 | * parse property string, and if needed, update internal values accordingly | |
351 | * | |
352 | * udev_device_add_property_from_string_parse_finish() needs to be | |
353 | * called after adding properties, and its return value checked | |
354 | * | |
355 | * udev_device_set_info_loaded() needs to be set, to avoid trying | |
356 | * to use a device without a DEVPATH set | |
357 | */ | |
358 | void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property) | |
359 | { | |
33502ffe | 360 | if (startswith(property, "DEVPATH=")) { |
912541b0 KS |
361 | char path[UTIL_PATH_SIZE]; |
362 | ||
4cb72937 | 363 | util_strscpyl(path, sizeof(path), "/sys", &property[8], NULL); |
912541b0 | 364 | udev_device_set_syspath(udev_device, path); |
33502ffe | 365 | } else if (startswith(property, "SUBSYSTEM=")) { |
912541b0 | 366 | udev_device_set_subsystem(udev_device, &property[10]); |
33502ffe | 367 | } else if (startswith(property, "DEVTYPE=")) { |
912541b0 | 368 | udev_device_set_devtype(udev_device, &property[8]); |
33502ffe | 369 | } else if (startswith(property, "DEVNAME=")) { |
912541b0 | 370 | udev_device_set_devnode(udev_device, &property[8]); |
33502ffe | 371 | } else if (startswith(property, "DEVLINKS=")) { |
912541b0 KS |
372 | char devlinks[UTIL_PATH_SIZE]; |
373 | char *slink; | |
374 | char *next; | |
375 | ||
376 | util_strscpy(devlinks, sizeof(devlinks), &property[9]); | |
377 | slink = devlinks; | |
378 | next = strchr(slink, ' '); | |
379 | while (next != NULL) { | |
380 | next[0] = '\0'; | |
381 | udev_device_add_devlink(udev_device, slink, 0); | |
382 | slink = &next[1]; | |
383 | next = strchr(slink, ' '); | |
384 | } | |
385 | if (slink[0] != '\0') | |
386 | udev_device_add_devlink(udev_device, slink, 0); | |
33502ffe | 387 | } else if (startswith(property, "TAGS=")) { |
912541b0 KS |
388 | char tags[UTIL_PATH_SIZE]; |
389 | char *next; | |
390 | ||
391 | util_strscpy(tags, sizeof(tags), &property[5]); | |
392 | next = strchr(tags, ':'); | |
393 | if (next != NULL) { | |
394 | next++; | |
395 | while (next[0] != '\0') { | |
396 | char *tag; | |
397 | ||
398 | tag = next; | |
399 | next = strchr(tag, ':'); | |
400 | if (next == NULL) | |
401 | break; | |
402 | next[0] = '\0'; | |
403 | next++; | |
404 | udev_device_add_tag(udev_device, tag); | |
405 | } | |
406 | } | |
33502ffe | 407 | } else if (startswith(property, "USEC_INITIALIZED=")) { |
912541b0 | 408 | udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10)); |
33502ffe | 409 | } else if (startswith(property, "DRIVER=")) { |
912541b0 | 410 | udev_device_set_driver(udev_device, &property[7]); |
33502ffe | 411 | } else if (startswith(property, "ACTION=")) { |
912541b0 | 412 | udev_device_set_action(udev_device, &property[7]); |
33502ffe | 413 | } else if (startswith(property, "MAJOR=")) { |
912541b0 | 414 | udev_device->maj = strtoull(&property[6], NULL, 10); |
33502ffe | 415 | } else if (startswith(property, "MINOR=")) { |
912541b0 | 416 | udev_device->min = strtoull(&property[6], NULL, 10); |
33502ffe | 417 | } else if (startswith(property, "DEVPATH_OLD=")) { |
912541b0 | 418 | udev_device_set_devpath_old(udev_device, &property[12]); |
33502ffe | 419 | } else if (startswith(property, "SEQNUM=")) { |
912541b0 | 420 | udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10)); |
33502ffe | 421 | } else if (startswith(property, "IFINDEX=")) { |
912541b0 | 422 | udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10)); |
33502ffe | 423 | } else if (startswith(property, "DEVMODE=")) { |
912541b0 KS |
424 | udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8)); |
425 | } else { | |
426 | udev_device_add_property_from_string(udev_device, property); | |
427 | } | |
fc8d61c5 KS |
428 | } |
429 | ||
430 | int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device) | |
431 | { | |
912541b0 KS |
432 | if (udev_device->maj > 0) |
433 | udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min)); | |
434 | udev_device->maj = 0; | |
435 | udev_device->min = 0; | |
fc8d61c5 | 436 | |
912541b0 KS |
437 | if (udev_device->devpath == NULL || udev_device->subsystem == NULL) |
438 | return -EINVAL; | |
439 | return 0; | |
fc8d61c5 KS |
440 | } |
441 | ||
442 | /** | |
443 | * udev_device_get_property_value: | |
444 | * @udev_device: udev device | |
445 | * @key: property name | |
446 | * | |
21dbe43a KS |
447 | * Get the value of a given property. |
448 | * | |
449 | * Returns: the property string, or #NULL if there is no such property. | |
fc8d61c5 | 450 | **/ |
54cf0b7f | 451 | _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key) |
fc8d61c5 | 452 | { |
912541b0 | 453 | struct udev_list_entry *list_entry; |
fc8d61c5 | 454 | |
912541b0 KS |
455 | if (udev_device == NULL) |
456 | return NULL; | |
457 | if (key == NULL) | |
458 | return NULL; | |
fc8d61c5 | 459 | |
912541b0 KS |
460 | list_entry = udev_device_get_properties_list_entry(udev_device); |
461 | list_entry = udev_list_entry_get_by_name(list_entry, key); | |
462 | return udev_list_entry_get_value(list_entry); | |
fc8d61c5 KS |
463 | } |
464 | ||
5f59fa09 | 465 | int udev_device_read_db(struct udev_device *udev_device, const char *dbfile) |
e88a82b5 | 466 | { |
912541b0 KS |
467 | char filename[UTIL_PATH_SIZE]; |
468 | char line[UTIL_LINE_SIZE]; | |
469 | FILE *f; | |
470 | ||
471 | /* providing a database file will always force-load it */ | |
472 | if (dbfile == NULL) { | |
473 | const char *id; | |
474 | ||
475 | if (udev_device->db_loaded) | |
476 | return 0; | |
477 | udev_device->db_loaded = true; | |
478 | ||
479 | id = udev_device_get_id_filename(udev_device); | |
480 | if (id == NULL) | |
481 | return -1; | |
4cb72937 | 482 | util_strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL); |
912541b0 KS |
483 | dbfile = filename; |
484 | } | |
485 | ||
486 | f = fopen(dbfile, "re"); | |
487 | if (f == NULL) { | |
c8f8394a | 488 | udev_dbg(udev_device->udev, "no db file to read %s: %m\n", dbfile); |
912541b0 KS |
489 | return -1; |
490 | } | |
491 | udev_device->is_initialized = true; | |
492 | ||
493 | while (fgets(line, sizeof(line), f)) { | |
494 | ssize_t len; | |
495 | const char *val; | |
496 | struct udev_list_entry *entry; | |
497 | ||
498 | len = strlen(line); | |
499 | if (len < 4) | |
500 | break; | |
501 | line[len-1] = '\0'; | |
502 | val = &line[2]; | |
503 | switch(line[0]) { | |
504 | case 'S': | |
4cb72937 | 505 | util_strscpyl(filename, sizeof(filename), "/dev/", val, NULL); |
912541b0 KS |
506 | udev_device_add_devlink(udev_device, filename, 0); |
507 | break; | |
508 | case 'L': | |
509 | udev_device_set_devlink_priority(udev_device, atoi(val)); | |
510 | break; | |
511 | case 'E': | |
512 | entry = udev_device_add_property_from_string(udev_device, val); | |
513 | udev_list_entry_set_num(entry, true); | |
514 | break; | |
515 | case 'G': | |
516 | udev_device_add_tag(udev_device, val); | |
517 | break; | |
518 | case 'W': | |
519 | udev_device_set_watch_handle(udev_device, atoi(val)); | |
520 | break; | |
521 | case 'I': | |
522 | udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10)); | |
523 | break; | |
524 | } | |
525 | } | |
526 | fclose(f); | |
527 | ||
c8f8394a | 528 | udev_dbg(udev_device->udev, "device %p filled with db file data\n", udev_device); |
912541b0 | 529 | return 0; |
e88a82b5 KS |
530 | } |
531 | ||
bd85566c | 532 | int udev_device_read_uevent_file(struct udev_device *udev_device) |
99214844 | 533 | { |
912541b0 KS |
534 | char filename[UTIL_PATH_SIZE]; |
535 | FILE *f; | |
536 | char line[UTIL_LINE_SIZE]; | |
537 | int maj = 0; | |
538 | int min = 0; | |
539 | ||
540 | if (udev_device->uevent_loaded) | |
541 | return 0; | |
542 | ||
543 | util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL); | |
544 | f = fopen(filename, "re"); | |
545 | if (f == NULL) | |
546 | return -1; | |
547 | udev_device->uevent_loaded = true; | |
548 | ||
549 | while (fgets(line, sizeof(line), f)) { | |
550 | char *pos; | |
551 | ||
552 | pos = strchr(line, '\n'); | |
553 | if (pos == NULL) | |
554 | continue; | |
555 | pos[0] = '\0'; | |
556 | ||
33502ffe | 557 | if (startswith(line, "DEVTYPE=")) { |
912541b0 | 558 | udev_device_set_devtype(udev_device, &line[8]); |
d2b795f2 KS |
559 | continue; |
560 | } | |
33502ffe | 561 | if (startswith(line, "IFINDEX=")) { |
d2b795f2 KS |
562 | udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10)); |
563 | continue; | |
564 | } | |
33502ffe | 565 | if (startswith(line, "DEVNAME=")) { |
d2b795f2 KS |
566 | udev_device_set_devnode(udev_device, &line[8]); |
567 | continue; | |
568 | } | |
569 | ||
33502ffe | 570 | if (startswith(line, "MAJOR=")) |
912541b0 | 571 | maj = strtoull(&line[6], NULL, 10); |
33502ffe | 572 | else if (startswith(line, "MINOR=")) |
912541b0 | 573 | min = strtoull(&line[6], NULL, 10); |
33502ffe | 574 | else if (startswith(line, "DEVMODE=")) |
912541b0 KS |
575 | udev_device->devnode_mode = strtoul(&line[8], NULL, 8); |
576 | ||
577 | udev_device_add_property_from_string(udev_device, line); | |
578 | } | |
579 | ||
580 | udev_device->devnum = makedev(maj, min); | |
581 | fclose(f); | |
582 | return 0; | |
99214844 KS |
583 | } |
584 | ||
8cd2e972 | 585 | void udev_device_set_info_loaded(struct udev_device *device) |
99214844 | 586 | { |
912541b0 | 587 | device->info_loaded = true; |
99214844 KS |
588 | } |
589 | ||
a5710160 | 590 | struct udev_device *udev_device_new(struct udev *udev) |
eb1f0e66 | 591 | { |
912541b0 KS |
592 | struct udev_device *udev_device; |
593 | struct udev_list_entry *list_entry; | |
594 | ||
595 | if (udev == NULL) | |
596 | return NULL; | |
597 | ||
598 | udev_device = calloc(1, sizeof(struct udev_device)); | |
599 | if (udev_device == NULL) | |
600 | return NULL; | |
601 | udev_device->refcount = 1; | |
602 | udev_device->udev = udev; | |
603 | udev_list_init(udev, &udev_device->devlinks_list, true); | |
604 | udev_list_init(udev, &udev_device->properties_list, true); | |
605 | udev_list_init(udev, &udev_device->sysattr_value_list, true); | |
606 | udev_list_init(udev, &udev_device->sysattr_list, false); | |
607 | udev_list_init(udev, &udev_device->tags_list, true); | |
608 | udev_device->watch_handle = -1; | |
609 | /* copy global properties */ | |
610 | udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev)) | |
611 | udev_device_add_property(udev_device, | |
612 | udev_list_entry_get_name(list_entry), | |
613 | udev_list_entry_get_value(list_entry)); | |
912541b0 | 614 | return udev_device; |
eb1f0e66 KS |
615 | } |
616 | ||
617 | /** | |
8753fadf | 618 | * udev_device_new_from_syspath: |
eb1f0e66 | 619 | * @udev: udev library context |
8753fadf | 620 | * @syspath: sys device path including sys directory |
eb1f0e66 | 621 | * |
8753fadf | 622 | * Create new udev device, and fill in information from the sys |
214a6c79 | 623 | * device and the udev database entry. The syspath is the absolute |
8753fadf | 624 | * path to the device, including the sys mount point. |
eb1f0e66 KS |
625 | * |
626 | * The initial refcount is 1, and needs to be decremented to | |
be7de409 | 627 | * release the resources of the udev device. |
eb1f0e66 KS |
628 | * |
629 | * Returns: a new udev device, or #NULL, if it does not exist | |
630 | **/ | |
54cf0b7f | 631 | _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) |
eb1f0e66 | 632 | { |
912541b0 KS |
633 | const char *subdir; |
634 | char path[UTIL_PATH_SIZE]; | |
635 | char *pos; | |
636 | struct stat statbuf; | |
637 | struct udev_device *udev_device; | |
638 | ||
639 | if (udev == NULL) | |
640 | return NULL; | |
641 | if (syspath == NULL) | |
642 | return NULL; | |
643 | ||
644 | /* path starts in sys */ | |
4cb72937 | 645 | if (!startswith(syspath, "/sys")) { |
c8f8394a | 646 | udev_dbg(udev, "not in sys :%s\n", syspath); |
912541b0 KS |
647 | return NULL; |
648 | } | |
649 | ||
650 | /* path is not a root directory */ | |
4cb72937 | 651 | subdir = syspath + strlen("/sys"); |
912541b0 | 652 | pos = strrchr(subdir, '/'); |
baa30fbc | 653 | if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) |
912541b0 | 654 | return NULL; |
912541b0 KS |
655 | |
656 | /* resolve possible symlink to real path */ | |
657 | util_strscpy(path, sizeof(path), syspath); | |
658 | util_resolve_sys_link(udev, path, sizeof(path)); | |
659 | ||
4cb72937 | 660 | if (startswith(path + strlen("/sys"), "/devices/")) { |
912541b0 KS |
661 | char file[UTIL_PATH_SIZE]; |
662 | ||
663 | /* all "devices" require a "uevent" file */ | |
664 | util_strscpyl(file, sizeof(file), path, "/uevent", NULL); | |
baa30fbc | 665 | if (stat(file, &statbuf) != 0) |
912541b0 | 666 | return NULL; |
912541b0 KS |
667 | } else { |
668 | /* everything else just needs to be a directory */ | |
baa30fbc | 669 | if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) |
912541b0 | 670 | return NULL; |
912541b0 KS |
671 | } |
672 | ||
673 | udev_device = udev_device_new(udev); | |
674 | if (udev_device == NULL) | |
675 | return NULL; | |
676 | ||
677 | udev_device_set_syspath(udev_device, path); | |
c8f8394a | 678 | udev_dbg(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device)); |
912541b0 KS |
679 | |
680 | return udev_device; | |
eb1f0e66 KS |
681 | } |
682 | ||
1e511322 KS |
683 | /** |
684 | * udev_device_new_from_devnum: | |
685 | * @udev: udev library context | |
686 | * @type: char or block device | |
687 | * @devnum: device major/minor number | |
688 | * | |
689 | * Create new udev device, and fill in information from the sys | |
dbba7e40 KS |
690 | * device and the udev database entry. The device is looked-up |
691 | * by its major/minor number and type. Character and block device | |
692 | * numbers are not unique across the two types. | |
1e511322 KS |
693 | * |
694 | * The initial refcount is 1, and needs to be decremented to | |
695 | * release the resources of the udev device. | |
696 | * | |
697 | * Returns: a new udev device, or #NULL, if it does not exist | |
698 | **/ | |
54cf0b7f | 699 | _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum) |
4c9dff47 | 700 | { |
912541b0 KS |
701 | char path[UTIL_PATH_SIZE]; |
702 | const char *type_str; | |
03198b93 | 703 | |
912541b0 KS |
704 | if (type == 'b') |
705 | type_str = "block"; | |
706 | else if (type == 'c') | |
707 | type_str = "char"; | |
708 | else | |
709 | return NULL; | |
4c9dff47 | 710 | |
912541b0 | 711 | /* use /sys/dev/{block,char}/<maj>:<min> link */ |
4cb72937 | 712 | snprintf(path, sizeof(path), "/sys/dev/%s/%u:%u", |
6ada823a | 713 | type_str, major(devnum), minor(devnum)); |
912541b0 | 714 | return udev_device_new_from_syspath(udev, path); |
4c9dff47 KS |
715 | } |
716 | ||
dbf61afb KS |
717 | /** |
718 | * udev_device_new_from_device_id: | |
719 | * @udev: udev library context | |
720 | * @id: text string identifying a kernel device | |
721 | * | |
722 | * Create new udev device, and fill in information from the sys | |
723 | * device and the udev database entry. The device is looked-up | |
724 | * by a special string: | |
725 | * b8:2 - block device major:minor | |
726 | * c128:1 - char device major:minor | |
727 | * n3 - network device ifindex | |
728 | * +sound:card29 - kernel driver core subsystem:device name | |
729 | * | |
730 | * The initial refcount is 1, and needs to be decremented to | |
731 | * release the resources of the udev device. | |
732 | * | |
733 | * Returns: a new udev device, or #NULL, if it does not exist | |
734 | **/ | |
735 | _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, char *id) | |
cad40a5f | 736 | { |
912541b0 KS |
737 | char type; |
738 | int maj, min; | |
739 | char subsys[UTIL_PATH_SIZE]; | |
740 | char *sysname; | |
741 | ||
742 | switch(id[0]) { | |
743 | case 'b': | |
744 | case 'c': | |
745 | if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3) | |
746 | return NULL; | |
747 | return udev_device_new_from_devnum(udev, type, makedev(maj, min)); | |
748 | case 'n': { | |
749 | int sk; | |
750 | struct ifreq ifr; | |
751 | struct udev_device *dev; | |
752 | int ifindex; | |
753 | ||
754 | ifindex = strtoul(&id[1], NULL, 10); | |
755 | if (ifindex <= 0) | |
756 | return NULL; | |
757 | ||
758 | sk = socket(PF_INET, SOCK_DGRAM, 0); | |
759 | if (sk < 0) | |
760 | return NULL; | |
761 | memset(&ifr, 0x00, sizeof(struct ifreq)); | |
762 | ifr.ifr_ifindex = ifindex; | |
763 | if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) { | |
764 | close(sk); | |
765 | return NULL; | |
766 | } | |
767 | close(sk); | |
768 | ||
769 | dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name); | |
770 | if (dev == NULL) | |
771 | return NULL; | |
772 | if (udev_device_get_ifindex(dev) == ifindex) | |
773 | return dev; | |
774 | udev_device_unref(dev); | |
775 | return NULL; | |
776 | } | |
777 | case '+': | |
778 | util_strscpy(subsys, sizeof(subsys), &id[1]); | |
779 | sysname = strchr(subsys, ':'); | |
780 | if (sysname == NULL) | |
781 | return NULL; | |
782 | sysname[0] = '\0'; | |
783 | sysname = &sysname[1]; | |
784 | return udev_device_new_from_subsystem_sysname(udev, subsys, sysname); | |
785 | default: | |
786 | return NULL; | |
787 | } | |
cad40a5f KS |
788 | } |
789 | ||
1e511322 KS |
790 | /** |
791 | * udev_device_new_from_subsystem_sysname: | |
792 | * @udev: udev library context | |
214a6c79 | 793 | * @subsystem: the subsystem of the device |
1e511322 KS |
794 | * @sysname: the name of the device |
795 | * | |
dbba7e40 KS |
796 | * Create new udev device, and fill in information from the sys device |
797 | * and the udev database entry. The device is looked up by the subsystem | |
798 | * and name string of the device, like "mem" / "zero", or "block" / "sda". | |
1e511322 KS |
799 | * |
800 | * The initial refcount is 1, and needs to be decremented to | |
801 | * release the resources of the udev device. | |
802 | * | |
803 | * Returns: a new udev device, or #NULL, if it does not exist | |
804 | **/ | |
54cf0b7f | 805 | _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname) |
90d80c2e | 806 | { |
6ada823a | 807 | char path[UTIL_PATH_SIZE]; |
912541b0 KS |
808 | struct stat statbuf; |
809 | ||
33502ffe | 810 | if (streq(subsystem, "subsystem")) { |
4cb72937 | 811 | util_strscpyl(path, sizeof(path), "/sys/subsystem/", sysname, NULL); |
6ada823a | 812 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
813 | goto found; |
814 | ||
4cb72937 | 815 | util_strscpyl(path, sizeof(path), "/sys/bus/", sysname, NULL); |
6ada823a | 816 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
817 | goto found; |
818 | ||
4cb72937 | 819 | util_strscpyl(path, sizeof(path), "/sys/class/", sysname, NULL); |
6ada823a | 820 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
821 | goto found; |
822 | goto out; | |
823 | } | |
824 | ||
33502ffe | 825 | if (streq(subsystem, "module")) { |
4cb72937 | 826 | util_strscpyl(path, sizeof(path), "/sys/module/", sysname, NULL); |
6ada823a | 827 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
828 | goto found; |
829 | goto out; | |
830 | } | |
831 | ||
33502ffe | 832 | if (streq(subsystem, "drivers")) { |
912541b0 KS |
833 | char subsys[UTIL_NAME_SIZE]; |
834 | char *driver; | |
835 | ||
836 | util_strscpy(subsys, sizeof(subsys), sysname); | |
837 | driver = strchr(subsys, ':'); | |
838 | if (driver != NULL) { | |
839 | driver[0] = '\0'; | |
840 | driver = &driver[1]; | |
841 | ||
4cb72937 | 842 | util_strscpyl(path, sizeof(path), "/sys/subsystem/", subsys, "/drivers/", driver, NULL); |
6ada823a | 843 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
844 | goto found; |
845 | ||
4cb72937 | 846 | util_strscpyl(path, sizeof(path), "/sys/bus/", subsys, "/drivers/", driver, NULL); |
6ada823a | 847 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
848 | goto found; |
849 | } | |
850 | goto out; | |
851 | } | |
852 | ||
4cb72937 | 853 | util_strscpyl(path, sizeof(path), "/sys/subsystem/", subsystem, "/devices/", sysname, NULL); |
6ada823a | 854 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
855 | goto found; |
856 | ||
4cb72937 | 857 | util_strscpyl(path, sizeof(path), "/sys/bus/", subsystem, "/devices/", sysname, NULL); |
6ada823a | 858 | if (stat(path, &statbuf) == 0) |
912541b0 KS |
859 | goto found; |
860 | ||
4cb72937 | 861 | util_strscpyl(path, sizeof(path), "/sys/class/", subsystem, "/", sysname, NULL); |
6ada823a | 862 | if (stat(path, &statbuf) == 0) |
912541b0 | 863 | goto found; |
90d80c2e | 864 | out: |
912541b0 | 865 | return NULL; |
90d80c2e | 866 | found: |
6ada823a | 867 | return udev_device_new_from_syspath(udev, path); |
90d80c2e KS |
868 | } |
869 | ||
fc8d61c5 KS |
870 | /** |
871 | * udev_device_new_from_environment | |
872 | * @udev: udev library context | |
873 | * | |
874 | * Create new udev device, and fill in information from the | |
875 | * current process environment. This only works reliable if | |
876 | * the process is called from a udev rule. It is usually used | |
877 | * for tools executed from IMPORT= rules. | |
878 | * | |
879 | * The initial refcount is 1, and needs to be decremented to | |
880 | * release the resources of the udev device. | |
881 | * | |
882 | * Returns: a new udev device, or #NULL, if it does not exist | |
883 | **/ | |
54cf0b7f | 884 | _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev) |
fc8d61c5 | 885 | { |
912541b0 KS |
886 | int i; |
887 | struct udev_device *udev_device; | |
fc8d61c5 | 888 | |
912541b0 KS |
889 | udev_device = udev_device_new(udev); |
890 | if (udev_device == NULL) | |
891 | return NULL; | |
892 | udev_device_set_info_loaded(udev_device); | |
fc8d61c5 | 893 | |
912541b0 KS |
894 | for (i = 0; environ[i] != NULL; i++) |
895 | udev_device_add_property_from_string_parse(udev_device, environ[i]); | |
fc8d61c5 | 896 | |
912541b0 | 897 | if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) { |
c8f8394a | 898 | udev_dbg(udev, "missing values, invalid device\n"); |
912541b0 KS |
899 | udev_device_unref(udev_device); |
900 | udev_device = NULL; | |
901 | } | |
fc8d61c5 | 902 | |
912541b0 | 903 | return udev_device; |
fc8d61c5 KS |
904 | } |
905 | ||
b2d9e4f2 | 906 | static struct udev_device *device_new_from_parent(struct udev_device *udev_device) |
4ad3a37f | 907 | { |
912541b0 KS |
908 | struct udev_device *udev_device_parent = NULL; |
909 | char path[UTIL_PATH_SIZE]; | |
910 | const char *subdir; | |
4ad3a37f | 911 | |
912541b0 | 912 | util_strscpy(path, sizeof(path), udev_device->syspath); |
4cb72937 | 913 | subdir = path + strlen("/sys/"); |
912541b0 KS |
914 | for (;;) { |
915 | char *pos; | |
b95f8a76 | 916 | |
912541b0 KS |
917 | pos = strrchr(subdir, '/'); |
918 | if (pos == NULL || pos < &subdir[2]) | |
919 | break; | |
920 | pos[0] = '\0'; | |
921 | udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); | |
922 | if (udev_device_parent != NULL) | |
923 | return udev_device_parent; | |
924 | } | |
925 | return NULL; | |
4ad3a37f KS |
926 | } |
927 | ||
1e511322 KS |
928 | /** |
929 | * udev_device_get_parent: | |
930 | * @udev_device: the device to start searching from | |
931 | * | |
932 | * Find the next parent device, and fill in information from the sys | |
933 | * device and the udev database entry. | |
934 | * | |
935 | * The returned the device is not referenced. It is attached to the | |
936 | * child device, and will be cleaned up when the child device | |
937 | * is cleaned up. | |
938 | * | |
214a6c79 | 939 | * It is not necessarily just the upper level directory, empty or not |
1e511322 KS |
940 | * recognized sys directories are ignored. |
941 | * | |
942 | * It can be called as many times as needed, without caring about | |
943 | * references. | |
944 | * | |
945 | * Returns: a new udev device, or #NULL, if it no parent exist. | |
946 | **/ | |
54cf0b7f | 947 | _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device) |
b2d9e4f2 | 948 | { |
912541b0 KS |
949 | if (udev_device == NULL) |
950 | return NULL; | |
951 | if (!udev_device->parent_set) { | |
952 | udev_device->parent_set = true; | |
953 | udev_device->parent_device = device_new_from_parent(udev_device); | |
954 | } | |
912541b0 | 955 | return udev_device->parent_device; |
b2d9e4f2 KS |
956 | } |
957 | ||
1e511322 KS |
958 | /** |
959 | * udev_device_get_parent_with_subsystem_devtype: | |
960 | * @udev_device: udev device to start searching from | |
214a6c79 | 961 | * @subsystem: the subsystem of the device |
1e511322 KS |
962 | * @devtype: the type (DEVTYPE) of the device |
963 | * | |
964 | * Find the next parent device, with a matching subsystem and devtype | |
965 | * value, and fill in information from the sys device and the udev | |
966 | * database entry. | |
967 | * | |
79ef2e97 | 968 | * If devtype is #NULL, only subsystem is checked, and any devtype will |
54d4f54e MP |
969 | * match. |
970 | * | |
1e511322 KS |
971 | * The returned the device is not referenced. It is attached to the |
972 | * child device, and will be cleaned up when the child device | |
973 | * is cleaned up. | |
974 | * | |
975 | * It can be called as many times as needed, without caring about | |
976 | * references. | |
977 | * | |
8d6bc73a | 978 | * Returns: a new udev device, or #NULL if no matching parent exists. |
1e511322 | 979 | **/ |
54cf0b7f | 980 | _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype) |
bf8b2ae1 | 981 | { |
912541b0 | 982 | struct udev_device *parent; |
bf8b2ae1 | 983 | |
912541b0 KS |
984 | if (subsystem == NULL) |
985 | return NULL; | |
883012d4 | 986 | |
912541b0 KS |
987 | parent = udev_device_get_parent(udev_device); |
988 | while (parent != NULL) { | |
989 | const char *parent_subsystem; | |
990 | const char *parent_devtype; | |
bf8b2ae1 | 991 | |
912541b0 | 992 | parent_subsystem = udev_device_get_subsystem(parent); |
33502ffe | 993 | if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) { |
912541b0 KS |
994 | if (devtype == NULL) |
995 | break; | |
996 | parent_devtype = udev_device_get_devtype(parent); | |
33502ffe | 997 | if (parent_devtype != NULL && streq(parent_devtype, devtype)) |
912541b0 KS |
998 | break; |
999 | } | |
1000 | parent = udev_device_get_parent(parent); | |
1001 | } | |
1002 | return parent; | |
bf8b2ae1 MH |
1003 | } |
1004 | ||
eb1f0e66 KS |
1005 | /** |
1006 | * udev_device_get_udev: | |
7d8787b3 | 1007 | * @udev_device: udev device |
eb1f0e66 KS |
1008 | * |
1009 | * Retrieve the udev library context the device was created with. | |
1010 | * | |
1011 | * Returns: the udev library context | |
1012 | **/ | |
54cf0b7f | 1013 | _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device) |
eb1f0e66 | 1014 | { |
912541b0 KS |
1015 | if (udev_device == NULL) |
1016 | return NULL; | |
1017 | return udev_device->udev; | |
eb1f0e66 KS |
1018 | } |
1019 | ||
1020 | /** | |
1021 | * udev_device_ref: | |
1022 | * @udev_device: udev device | |
1023 | * | |
1024 | * Take a reference of a udev device. | |
1025 | * | |
1026 | * Returns: the passed udev device | |
1027 | **/ | |
54cf0b7f | 1028 | _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device) |
eb1f0e66 | 1029 | { |
912541b0 KS |
1030 | if (udev_device == NULL) |
1031 | return NULL; | |
1032 | udev_device->refcount++; | |
1033 | return udev_device; | |
eb1f0e66 KS |
1034 | } |
1035 | ||
1036 | /** | |
1037 | * udev_device_unref: | |
1038 | * @udev_device: udev device | |
1039 | * | |
1040 | * Drop a reference of a udev device. If the refcount reaches zero, | |
be7de409 | 1041 | * the resources of the device will be released. |
eb1f0e66 | 1042 | * |
c1959569 | 1043 | * Returns: the passed udev device if it has still an active reference, or #NULL otherwise. |
eb1f0e66 | 1044 | **/ |
20bbd54f | 1045 | _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device) |
eb1f0e66 | 1046 | { |
912541b0 | 1047 | if (udev_device == NULL) |
20bbd54f | 1048 | return NULL; |
912541b0 KS |
1049 | udev_device->refcount--; |
1050 | if (udev_device->refcount > 0) | |
20bbd54f | 1051 | return udev_device; |
912541b0 KS |
1052 | if (udev_device->parent_device != NULL) |
1053 | udev_device_unref(udev_device->parent_device); | |
1054 | free(udev_device->syspath); | |
1055 | free(udev_device->sysname); | |
1056 | free(udev_device->devnode); | |
1057 | free(udev_device->subsystem); | |
1058 | free(udev_device->devtype); | |
1059 | udev_list_cleanup(&udev_device->devlinks_list); | |
1060 | udev_list_cleanup(&udev_device->properties_list); | |
1061 | udev_list_cleanup(&udev_device->sysattr_value_list); | |
1062 | udev_list_cleanup(&udev_device->sysattr_list); | |
1063 | udev_list_cleanup(&udev_device->tags_list); | |
1064 | free(udev_device->action); | |
1065 | free(udev_device->driver); | |
1066 | free(udev_device->devpath_old); | |
1067 | free(udev_device->id_filename); | |
1068 | free(udev_device->envp); | |
1069 | free(udev_device->monitor_buf); | |
912541b0 | 1070 | free(udev_device); |
20bbd54f | 1071 | return NULL; |
eb1f0e66 KS |
1072 | } |
1073 | ||
1074 | /** | |
1075 | * udev_device_get_devpath: | |
1076 | * @udev_device: udev device | |
1077 | * | |
11d543c1 KS |
1078 | * Retrieve the kernel devpath value of the udev device. The path |
1079 | * does not contain the sys mount point, and starts with a '/'. | |
eb1f0e66 | 1080 | * |
11d543c1 | 1081 | * Returns: the devpath of the udev device |
eb1f0e66 | 1082 | **/ |
54cf0b7f | 1083 | _public_ const char *udev_device_get_devpath(struct udev_device *udev_device) |
eb1f0e66 | 1084 | { |
912541b0 KS |
1085 | if (udev_device == NULL) |
1086 | return NULL; | |
1087 | return udev_device->devpath; | |
eb1f0e66 KS |
1088 | } |
1089 | ||
11d543c1 KS |
1090 | /** |
1091 | * udev_device_get_syspath: | |
1092 | * @udev_device: udev device | |
1093 | * | |
1094 | * Retrieve the sys path of the udev device. The path is an | |
1095 | * absolute path and starts with the sys mount point. | |
1096 | * | |
1097 | * Returns: the sys path of the udev device | |
1098 | **/ | |
54cf0b7f | 1099 | _public_ const char *udev_device_get_syspath(struct udev_device *udev_device) |
11d543c1 | 1100 | { |
912541b0 KS |
1101 | if (udev_device == NULL) |
1102 | return NULL; | |
1103 | return udev_device->syspath; | |
11d543c1 KS |
1104 | } |
1105 | ||
1e511322 KS |
1106 | /** |
1107 | * udev_device_get_sysname: | |
1108 | * @udev_device: udev device | |
1109 | * | |
21dbe43a KS |
1110 | * Get the kernel device name in /sys. |
1111 | * | |
1112 | * Returns: the name string of the device device | |
1e511322 | 1113 | **/ |
54cf0b7f | 1114 | _public_ const char *udev_device_get_sysname(struct udev_device *udev_device) |
4ad3a37f | 1115 | { |
912541b0 KS |
1116 | if (udev_device == NULL) |
1117 | return NULL; | |
1118 | return udev_device->sysname; | |
4ad3a37f KS |
1119 | } |
1120 | ||
1e511322 KS |
1121 | /** |
1122 | * udev_device_get_sysnum: | |
1123 | * @udev_device: udev device | |
1124 | * | |
21dbe43a KS |
1125 | * Get the instance number of the device. |
1126 | * | |
1127 | * Returns: the trailing number string of of the device name | |
1e511322 | 1128 | **/ |
54cf0b7f | 1129 | _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device) |
517814e7 | 1130 | { |
912541b0 KS |
1131 | if (udev_device == NULL) |
1132 | return NULL; | |
1133 | return udev_device->sysnum; | |
517814e7 KS |
1134 | } |
1135 | ||
eb1f0e66 | 1136 | /** |
fb762bb9 | 1137 | * udev_device_get_devnode: |
eb1f0e66 KS |
1138 | * @udev_device: udev device |
1139 | * | |
1140 | * Retrieve the device node file name belonging to the udev device. | |
ba6929f6 | 1141 | * The path is an absolute path, and starts with the device directory. |
eb1f0e66 KS |
1142 | * |
1143 | * Returns: the device node file name of the udev device, or #NULL if no device node exists | |
1144 | **/ | |
54cf0b7f | 1145 | _public_ const char *udev_device_get_devnode(struct udev_device *udev_device) |
eb1f0e66 | 1146 | { |
912541b0 KS |
1147 | if (udev_device == NULL) |
1148 | return NULL; | |
1149 | if (udev_device->devnode != NULL) | |
1150 | return udev_device->devnode; | |
1151 | if (!udev_device->info_loaded) | |
1152 | udev_device_read_uevent_file(udev_device); | |
1153 | return udev_device->devnode; | |
eb1f0e66 KS |
1154 | } |
1155 | ||
eb1f0e66 | 1156 | /** |
0de33a61 | 1157 | * udev_device_get_devlinks_list_entry: |
eb1f0e66 | 1158 | * @udev_device: udev device |
eb1f0e66 | 1159 | * |
bf7ad0ea KS |
1160 | * Retrieve the list of device links pointing to the device file of |
1161 | * the udev device. The next list entry can be retrieved with | |
bb061708 | 1162 | * udev_list_entry_get_next(), which returns #NULL if no more entries exist. |
bf7ad0ea | 1163 | * The devlink path can be retrieved from the list entry by |
e345e267 | 1164 | * udev_list_entry_get_name(). The path is an absolute path, and starts with |
bf7ad0ea | 1165 | * the device directory. |
eb1f0e66 | 1166 | * |
bf7ad0ea | 1167 | * Returns: the first entry of the device node link list |
eb1f0e66 | 1168 | **/ |
54cf0b7f | 1169 | _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 1170 | { |
912541b0 KS |
1171 | if (udev_device == NULL) |
1172 | return NULL; | |
1173 | if (!udev_device->info_loaded) | |
1174 | udev_device_read_db(udev_device, NULL); | |
1175 | return udev_list_get_entry(&udev_device->devlinks_list); | |
eb1f0e66 KS |
1176 | } |
1177 | ||
979ff016 KS |
1178 | void udev_device_cleanup_devlinks_list(struct udev_device *udev_device) |
1179 | { | |
912541b0 KS |
1180 | udev_device->devlinks_uptodate = false; |
1181 | udev_list_cleanup(&udev_device->devlinks_list); | |
979ff016 KS |
1182 | } |
1183 | ||
eb1f0e66 | 1184 | /** |
0de33a61 | 1185 | * udev_device_get_properties_list_entry: |
eb1f0e66 | 1186 | * @udev_device: udev device |
eb1f0e66 | 1187 | * |
bf7ad0ea | 1188 | * Retrieve the list of key/value device properties of the udev |
bb061708 | 1189 | * device. The next list entry can be retrieved with udev_list_entry_get_next(), |
bf7ad0ea | 1190 | * which returns #NULL if no more entries exist. The property name |
bb061708 KS |
1191 | * can be retrieved from the list entry by udev_list_entry_get_name(), |
1192 | * the property value by udev_list_entry_get_value(). | |
eb1f0e66 | 1193 | * |
bf7ad0ea | 1194 | * Returns: the first entry of the property list |
eb1f0e66 | 1195 | **/ |
54cf0b7f | 1196 | _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 1197 | { |
912541b0 KS |
1198 | if (udev_device == NULL) |
1199 | return NULL; | |
1200 | if (!udev_device->info_loaded) { | |
1201 | udev_device_read_uevent_file(udev_device); | |
1202 | udev_device_read_db(udev_device, NULL); | |
1203 | } | |
1204 | if (!udev_device->devlinks_uptodate) { | |
1205 | char symlinks[UTIL_PATH_SIZE]; | |
1206 | struct udev_list_entry *list_entry; | |
1207 | ||
1208 | udev_device->devlinks_uptodate = true; | |
1209 | list_entry = udev_device_get_devlinks_list_entry(udev_device); | |
1210 | if (list_entry != NULL) { | |
1211 | char *s; | |
1212 | size_t l; | |
1213 | ||
1214 | s = symlinks; | |
1215 | l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL); | |
1216 | udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) | |
1217 | l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL); | |
1218 | udev_device_add_property(udev_device, "DEVLINKS", symlinks); | |
1219 | } | |
1220 | } | |
1221 | if (!udev_device->tags_uptodate) { | |
1222 | udev_device->tags_uptodate = true; | |
1223 | if (udev_device_get_tags_list_entry(udev_device) != NULL) { | |
1224 | char tags[UTIL_PATH_SIZE]; | |
1225 | struct udev_list_entry *list_entry; | |
1226 | char *s; | |
1227 | size_t l; | |
1228 | ||
1229 | s = tags; | |
1230 | l = util_strpcpyl(&s, sizeof(tags), ":", NULL); | |
1231 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) | |
1232 | l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL); | |
1233 | udev_device_add_property(udev_device, "TAGS", tags); | |
1234 | } | |
1235 | } | |
1236 | return udev_list_get_entry(&udev_device->properties_list); | |
eb1f0e66 | 1237 | } |
11d543c1 | 1238 | |
1e511322 KS |
1239 | /** |
1240 | * udev_device_get_action: | |
1241 | * @udev_device: udev device | |
1242 | * | |
1243 | * This is only valid if the device was received through a monitor. Devices read from | |
1244 | * sys do not have an action string. Usual actions are: add, remove, change, online, | |
1245 | * offline. | |
1246 | * | |
1247 | * Returns: the kernel action value, or #NULL if there is no action value available. | |
1248 | **/ | |
54cf0b7f | 1249 | _public_ const char *udev_device_get_action(struct udev_device *udev_device) |
c4f5f942 | 1250 | { |
912541b0 KS |
1251 | if (udev_device == NULL) |
1252 | return NULL; | |
1253 | return udev_device->action; | |
c4f5f942 KS |
1254 | } |
1255 | ||
9c6a11b1 KS |
1256 | /** |
1257 | * udev_device_get_usec_since_initialized: | |
1258 | * @udev_device: udev device | |
1259 | * | |
1260 | * Return the number of microseconds passed since udev set up the | |
1261 | * device for the first time. | |
1262 | * | |
1263 | * This is only implemented for devices with need to store properties | |
1264 | * in the udev database. All other devices return 0 here. | |
1265 | * | |
1266 | * Returns: the number of microseconds since the device was first seen. | |
1267 | **/ | |
54cf0b7f | 1268 | _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device) |
9c6a11b1 | 1269 | { |
33502ffe | 1270 | unsigned long long now_ts; |
9c6a11b1 | 1271 | |
912541b0 KS |
1272 | if (udev_device == NULL) |
1273 | return 0; | |
1274 | if (!udev_device->info_loaded) | |
1275 | udev_device_read_db(udev_device, NULL); | |
1276 | if (udev_device->usec_initialized == 0) | |
1277 | return 0; | |
33502ffe KS |
1278 | now_ts = now_usec(); |
1279 | if (now_ts == 0) | |
912541b0 | 1280 | return 0; |
33502ffe | 1281 | return now_ts - udev_device->usec_initialized; |
9c6a11b1 KS |
1282 | } |
1283 | ||
1284 | unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device) | |
1285 | { | |
912541b0 | 1286 | return udev_device->usec_initialized; |
9c6a11b1 KS |
1287 | } |
1288 | ||
1289 | void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized) | |
1290 | { | |
912541b0 | 1291 | char num[32]; |
a20a57a7 | 1292 | |
912541b0 KS |
1293 | udev_device->usec_initialized = usec_initialized; |
1294 | snprintf(num, sizeof(num), "%llu", usec_initialized); | |
1295 | udev_device_add_property(udev_device, "USEC_INITIALIZED", num); | |
9c6a11b1 KS |
1296 | } |
1297 | ||
1e511322 KS |
1298 | /** |
1299 | * udev_device_get_sysattr_value: | |
1300 | * @udev_device: udev device | |
1301 | * @sysattr: attribute name | |
1302 | * | |
456719b6 | 1303 | * The retrieved value is cached in the device. Repeated calls will return the same |
1e511322 KS |
1304 | * value and not open the attribute again. |
1305 | * | |
1306 | * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value. | |
1307 | **/ | |
54cf0b7f | 1308 | _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr) |
93b0f384 | 1309 | { |
912541b0 KS |
1310 | struct udev_list_entry *list_entry; |
1311 | char path[UTIL_PATH_SIZE]; | |
1312 | char value[4096]; | |
1313 | struct stat statbuf; | |
1314 | int fd; | |
1315 | ssize_t size; | |
1316 | const char *val = NULL; | |
1317 | ||
1318 | if (udev_device == NULL) | |
1319 | return NULL; | |
1320 | if (sysattr == NULL) | |
1321 | return NULL; | |
1322 | ||
1323 | /* look for possibly already cached result */ | |
1324 | list_entry = udev_list_get_entry(&udev_device->sysattr_value_list); | |
1325 | list_entry = udev_list_entry_get_by_name(list_entry, sysattr); | |
baa30fbc | 1326 | if (list_entry != NULL) |
912541b0 | 1327 | return udev_list_entry_get_value(list_entry); |
912541b0 KS |
1328 | |
1329 | util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL); | |
1330 | if (lstat(path, &statbuf) != 0) { | |
912541b0 KS |
1331 | udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL); |
1332 | goto out; | |
1333 | } | |
1334 | ||
1335 | if (S_ISLNK(statbuf.st_mode)) { | |
1336 | struct udev_device *dev; | |
1337 | ||
1338 | /* | |
1339 | * Some core links return only the last element of the target path, | |
1340 | * these are just values, the paths should not be exposed. | |
1341 | */ | |
33502ffe KS |
1342 | if (streq(sysattr, "driver") || |
1343 | streq(sysattr, "subsystem") || | |
1344 | streq(sysattr, "module")) { | |
912541b0 KS |
1345 | if (util_get_sys_core_link_value(udev_device->udev, sysattr, |
1346 | udev_device->syspath, value, sizeof(value)) < 0) | |
1347 | return NULL; | |
912541b0 KS |
1348 | list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value); |
1349 | val = udev_list_entry_get_value(list_entry); | |
1350 | goto out; | |
1351 | } | |
1352 | ||
1353 | /* resolve link to a device and return its syspath */ | |
1354 | util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL); | |
1355 | dev = udev_device_new_from_syspath(udev_device->udev, path); | |
1356 | if (dev != NULL) { | |
1357 | list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, | |
1358 | udev_device_get_syspath(dev)); | |
1359 | val = udev_list_entry_get_value(list_entry); | |
1360 | udev_device_unref(dev); | |
1361 | } | |
1362 | ||
1363 | goto out; | |
1364 | } | |
1365 | ||
1366 | /* skip directories */ | |
1367 | if (S_ISDIR(statbuf.st_mode)) | |
1368 | goto out; | |
1369 | ||
1370 | /* skip non-readable files */ | |
1371 | if ((statbuf.st_mode & S_IRUSR) == 0) | |
1372 | goto out; | |
1373 | ||
1374 | /* read attribute value */ | |
1375 | fd = open(path, O_RDONLY|O_CLOEXEC); | |
baa30fbc | 1376 | if (fd < 0) |
912541b0 | 1377 | goto out; |
912541b0 KS |
1378 | size = read(fd, value, sizeof(value)); |
1379 | close(fd); | |
1380 | if (size < 0) | |
1381 | goto out; | |
1382 | if (size == sizeof(value)) | |
1383 | goto out; | |
1384 | ||
1385 | /* got a valid value, store it in cache and return it */ | |
1386 | value[size] = '\0'; | |
1387 | util_remove_trailing_chars(value, '\n'); | |
912541b0 KS |
1388 | list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value); |
1389 | val = udev_list_entry_get_value(list_entry); | |
93b0f384 | 1390 | out: |
912541b0 | 1391 | return val; |
93b0f384 | 1392 | } |
517814e7 | 1393 | |
20bee04c | 1394 | static int udev_device_sysattr_list_read(struct udev_device *udev_device) |
f180ad25 | 1395 | { |
912541b0 KS |
1396 | struct dirent *dent; |
1397 | DIR *dir; | |
1398 | int num = 0; | |
f180ad25 | 1399 | |
912541b0 KS |
1400 | if (udev_device == NULL) |
1401 | return -1; | |
1402 | if (udev_device->sysattr_list_read) | |
1403 | return 0; | |
f180ad25 | 1404 | |
912541b0 | 1405 | dir = opendir(udev_device_get_syspath(udev_device)); |
baa30fbc | 1406 | if (!dir) |
912541b0 | 1407 | return -1; |
f180ad25 | 1408 | |
912541b0 KS |
1409 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { |
1410 | char path[UTIL_PATH_SIZE]; | |
1411 | struct stat statbuf; | |
f180ad25 | 1412 | |
912541b0 KS |
1413 | /* only handle symlinks and regular files */ |
1414 | if (dent->d_type != DT_LNK && dent->d_type != DT_REG) | |
1415 | continue; | |
f180ad25 | 1416 | |
912541b0 KS |
1417 | util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL); |
1418 | if (lstat(path, &statbuf) != 0) | |
1419 | continue; | |
1420 | if ((statbuf.st_mode & S_IRUSR) == 0) | |
1421 | continue; | |
f180ad25 | 1422 | |
912541b0 KS |
1423 | udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL); |
1424 | num++; | |
1425 | } | |
f180ad25 | 1426 | |
912541b0 | 1427 | closedir(dir); |
912541b0 | 1428 | udev_device->sysattr_list_read = true; |
f180ad25 | 1429 | |
912541b0 | 1430 | return num; |
f180ad25 TE |
1431 | } |
1432 | ||
1433 | /** | |
1434 | * udev_device_get_sysattr_list_entry: | |
1435 | * @udev_device: udev device | |
1436 | * | |
1437 | * Retrieve the list of available sysattrs, with value being empty; | |
20bee04c KS |
1438 | * This just return all available sysfs attributes for a particular |
1439 | * device without reading their values. | |
f180ad25 TE |
1440 | * |
1441 | * Returns: the first entry of the property list | |
1442 | **/ | |
54cf0b7f | 1443 | _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device) |
f180ad25 | 1444 | { |
912541b0 KS |
1445 | if (!udev_device->sysattr_list_read) { |
1446 | int ret; | |
1447 | ret = udev_device_sysattr_list_read(udev_device); | |
1448 | if (0 > ret) | |
1449 | return NULL; | |
1450 | } | |
f180ad25 | 1451 | |
912541b0 | 1452 | return udev_list_get_entry(&udev_device->sysattr_list); |
f180ad25 TE |
1453 | } |
1454 | ||
8cd2e972 | 1455 | int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath) |
11d543c1 | 1456 | { |
912541b0 KS |
1457 | const char *pos; |
1458 | size_t len; | |
8753fadf | 1459 | |
912541b0 KS |
1460 | free(udev_device->syspath); |
1461 | udev_device->syspath = strdup(syspath); | |
1462 | if (udev_device->syspath == NULL) | |
1463 | return -ENOMEM; | |
4cb72937 | 1464 | udev_device->devpath = udev_device->syspath + strlen("/sys"); |
912541b0 | 1465 | udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath); |
517814e7 | 1466 | |
912541b0 KS |
1467 | pos = strrchr(udev_device->syspath, '/'); |
1468 | if (pos == NULL) | |
1469 | return -EINVAL; | |
1470 | udev_device->sysname = strdup(&pos[1]); | |
1471 | if (udev_device->sysname == NULL) | |
1472 | return -ENOMEM; | |
517814e7 | 1473 | |
912541b0 KS |
1474 | /* some devices have '!' in their name, change that to '/' */ |
1475 | len = 0; | |
1476 | while (udev_device->sysname[len] != '\0') { | |
1477 | if (udev_device->sysname[len] == '!') | |
1478 | udev_device->sysname[len] = '/'; | |
1479 | len++; | |
1480 | } | |
517814e7 | 1481 | |
912541b0 KS |
1482 | /* trailing number */ |
1483 | while (len > 0 && isdigit(udev_device->sysname[--len])) | |
1484 | udev_device->sysnum = &udev_device->sysname[len]; | |
babcf3cb | 1485 | |
912541b0 KS |
1486 | /* sysname is completely numeric */ |
1487 | if (len == 0) | |
1488 | udev_device->sysnum = NULL; | |
babcf3cb | 1489 | |
912541b0 | 1490 | return 0; |
11d543c1 KS |
1491 | } |
1492 | ||
8cd2e972 | 1493 | int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode) |
11d543c1 | 1494 | { |
912541b0 KS |
1495 | free(udev_device->devnode); |
1496 | if (devnode[0] != '/') { | |
4cb72937 | 1497 | if (asprintf(&udev_device->devnode, "/dev/%s", devnode) < 0) |
912541b0 KS |
1498 | udev_device->devnode = NULL; |
1499 | } else { | |
1500 | udev_device->devnode = strdup(devnode); | |
1501 | } | |
1502 | if (udev_device->devnode == NULL) | |
1503 | return -ENOMEM; | |
1504 | udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode); | |
1505 | return 0; | |
11d543c1 KS |
1506 | } |
1507 | ||
6c29f2b9 | 1508 | int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique) |
11d543c1 | 1509 | { |
912541b0 | 1510 | struct udev_list_entry *list_entry; |
6c29f2b9 | 1511 | |
912541b0 KS |
1512 | udev_device->devlinks_uptodate = false; |
1513 | list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL); | |
1514 | if (list_entry == NULL) | |
1515 | return -ENOMEM; | |
1516 | if (unique) | |
1517 | udev_list_entry_set_num(list_entry, true); | |
1518 | return 0; | |
11d543c1 KS |
1519 | } |
1520 | ||
4281da1f KS |
1521 | const char *udev_device_get_id_filename(struct udev_device *udev_device) |
1522 | { | |
912541b0 KS |
1523 | if (udev_device->id_filename == NULL) { |
1524 | if (udev_device_get_subsystem(udev_device) == NULL) | |
1525 | return NULL; | |
1526 | ||
1527 | if (major(udev_device_get_devnum(udev_device)) > 0) { | |
1528 | /* use dev_t -- b259:131072, c254:0 */ | |
1529 | if (asprintf(&udev_device->id_filename, "%c%u:%u", | |
33502ffe | 1530 | streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c', |
912541b0 KS |
1531 | major(udev_device_get_devnum(udev_device)), |
1532 | minor(udev_device_get_devnum(udev_device))) < 0) | |
1533 | udev_device->id_filename = NULL; | |
1534 | } else if (udev_device_get_ifindex(udev_device) > 0) { | |
1535 | /* use netdev ifindex -- n3 */ | |
1536 | if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0) | |
1537 | udev_device->id_filename = NULL; | |
1538 | } else { | |
1539 | /* | |
1540 | * use $subsys:$syname -- pci:0000:00:1f.2 | |
1541 | * sysname() has '!' translated, get it from devpath | |
1542 | */ | |
1543 | const char *sysname; | |
1544 | sysname = strrchr(udev_device->devpath, '/'); | |
1545 | if (sysname == NULL) | |
1546 | return NULL; | |
1547 | sysname = &sysname[1]; | |
1548 | if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0) | |
1549 | udev_device->id_filename = NULL; | |
1550 | } | |
1551 | } | |
1552 | return udev_device->id_filename; | |
4281da1f KS |
1553 | } |
1554 | ||
48a0170b KS |
1555 | /** |
1556 | * udev_device_get_is_initialized: | |
1557 | * @udev_device: udev device | |
1558 | * | |
1559 | * Check if udev has already handled the device and has set up | |
1560 | * device node permissions and context, or has renamed a network | |
1561 | * device. | |
1562 | * | |
9c6a11b1 | 1563 | * This is only implemented for devices with a device node |
48a0170b KS |
1564 | * or network interfaces. All other devices return 1 here. |
1565 | * | |
1566 | * Returns: 1 if the device is set up. 0 otherwise. | |
1567 | **/ | |
54cf0b7f | 1568 | _public_ int udev_device_get_is_initialized(struct udev_device *udev_device) |
48a0170b | 1569 | { |
912541b0 KS |
1570 | if (!udev_device->info_loaded) |
1571 | udev_device_read_db(udev_device, NULL); | |
1572 | return udev_device->is_initialized; | |
48a0170b KS |
1573 | } |
1574 | ||
1575 | void udev_device_set_is_initialized(struct udev_device *udev_device) | |
1576 | { | |
912541b0 | 1577 | udev_device->is_initialized = true; |
48a0170b KS |
1578 | } |
1579 | ||
28460195 KS |
1580 | int udev_device_add_tag(struct udev_device *udev_device, const char *tag) |
1581 | { | |
912541b0 KS |
1582 | if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL) |
1583 | return -EINVAL; | |
1584 | udev_device->tags_uptodate = false; | |
1585 | if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL) | |
1586 | return 0; | |
1587 | return -ENOMEM; | |
28460195 KS |
1588 | } |
1589 | ||
1590 | void udev_device_cleanup_tags_list(struct udev_device *udev_device) | |
1591 | { | |
912541b0 KS |
1592 | udev_device->tags_uptodate = false; |
1593 | udev_list_cleanup(&udev_device->tags_list); | |
28460195 KS |
1594 | } |
1595 | ||
f712894d KS |
1596 | /** |
1597 | * udev_device_get_tags_list_entry: | |
1598 | * @udev_device: udev device | |
1599 | * | |
1600 | * Retrieve the list of tags attached to the udev device. The next | |
bb061708 | 1601 | * list entry can be retrieved with udev_list_entry_get_next(), |
f712894d | 1602 | * which returns #NULL if no more entries exist. The tag string |
bb061708 | 1603 | * can be retrieved from the list entry by udev_list_entry_get_name(). |
f712894d KS |
1604 | * |
1605 | * Returns: the first entry of the tag list | |
1606 | **/ | |
54cf0b7f | 1607 | _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device) |
28460195 | 1608 | { |
912541b0 KS |
1609 | if (udev_device == NULL) |
1610 | return NULL; | |
1611 | if (!udev_device->info_loaded) | |
1612 | udev_device_read_db(udev_device, NULL); | |
1613 | return udev_list_get_entry(&udev_device->tags_list); | |
28460195 KS |
1614 | } |
1615 | ||
21dbe43a KS |
1616 | /** |
1617 | * udev_device_has_tag: | |
1618 | * @udev_device: udev device | |
1619 | * @tag: tag name | |
1620 | * | |
1621 | * Check if a given device has a certain tag associated. | |
1622 | * | |
1623 | * Returns: 1 if the tag is found. 0 otherwise. | |
1624 | **/ | |
54cf0b7f | 1625 | _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag) |
28460195 | 1626 | { |
912541b0 | 1627 | struct udev_list_entry *list_entry; |
28460195 | 1628 | |
912541b0 KS |
1629 | if (udev_device == NULL) |
1630 | return false; | |
1631 | if (!udev_device->info_loaded) | |
1632 | udev_device_read_db(udev_device, NULL); | |
1633 | list_entry = udev_device_get_tags_list_entry(udev_device); | |
1634 | if (udev_list_entry_get_by_name(list_entry, tag) != NULL) | |
1635 | return true; | |
1636 | return false; | |
28460195 KS |
1637 | } |
1638 | ||
912541b0 KS |
1639 | #define ENVP_SIZE 128 |
1640 | #define MONITOR_BUF_SIZE 4096 | |
6493e655 | 1641 | static int update_envp_monitor_buf(struct udev_device *udev_device) |
979ff016 | 1642 | { |
912541b0 KS |
1643 | struct udev_list_entry *list_entry; |
1644 | char *s; | |
1645 | size_t l; | |
1646 | unsigned int i; | |
1647 | ||
1648 | /* monitor buffer of property strings */ | |
1649 | free(udev_device->monitor_buf); | |
1650 | udev_device->monitor_buf_len = 0; | |
1651 | udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE); | |
1652 | if (udev_device->monitor_buf == NULL) | |
1653 | return -ENOMEM; | |
1654 | ||
1655 | /* envp array, strings will point into monitor buffer */ | |
1656 | if (udev_device->envp == NULL) | |
1657 | udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE); | |
1658 | if (udev_device->envp == NULL) | |
1659 | return -ENOMEM; | |
1660 | ||
1661 | i = 0; | |
1662 | s = udev_device->monitor_buf; | |
1663 | l = MONITOR_BUF_SIZE; | |
1664 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { | |
1665 | const char *key; | |
1666 | ||
1667 | key = udev_list_entry_get_name(list_entry); | |
1668 | /* skip private variables */ | |
1669 | if (key[0] == '.') | |
1670 | continue; | |
1671 | ||
1672 | /* add string to envp array */ | |
1673 | udev_device->envp[i++] = s; | |
1674 | if (i+1 >= ENVP_SIZE) | |
1675 | return -EINVAL; | |
1676 | ||
1677 | /* add property string to monitor buffer */ | |
1678 | l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL); | |
1679 | if (l == 0) | |
1680 | return -EINVAL; | |
1681 | /* advance past the trailing '\0' that util_strpcpyl() guarantees */ | |
1682 | s++; | |
1683 | l--; | |
1684 | } | |
1685 | udev_device->envp[i] = NULL; | |
1686 | udev_device->monitor_buf_len = s - udev_device->monitor_buf; | |
1687 | udev_device->envp_uptodate = true; | |
912541b0 | 1688 | return 0; |
6493e655 KS |
1689 | } |
1690 | ||
1691 | char **udev_device_get_properties_envp(struct udev_device *udev_device) | |
1692 | { | |
912541b0 KS |
1693 | if (!udev_device->envp_uptodate) |
1694 | if (update_envp_monitor_buf(udev_device) != 0) | |
1695 | return NULL; | |
1696 | return udev_device->envp; | |
6493e655 KS |
1697 | } |
1698 | ||
1699 | ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf) | |
1700 | { | |
912541b0 KS |
1701 | if (!udev_device->envp_uptodate) |
1702 | if (update_envp_monitor_buf(udev_device) != 0) | |
1703 | return -EINVAL; | |
1704 | *buf = udev_device->monitor_buf; | |
1705 | return udev_device->monitor_buf_len; | |
c2654402 KS |
1706 | } |
1707 | ||
8cd2e972 | 1708 | int udev_device_set_action(struct udev_device *udev_device, const char *action) |
c4f5f942 | 1709 | { |
912541b0 KS |
1710 | free(udev_device->action); |
1711 | udev_device->action = strdup(action); | |
1712 | if (udev_device->action == NULL) | |
1713 | return -ENOMEM; | |
1714 | udev_device_add_property(udev_device, "ACTION", udev_device->action); | |
1715 | return 0; | |
c4f5f942 KS |
1716 | } |
1717 | ||
8cd2e972 | 1718 | int udev_device_get_devlink_priority(struct udev_device *udev_device) |
6bd1c78a | 1719 | { |
912541b0 KS |
1720 | if (!udev_device->info_loaded) |
1721 | udev_device_read_db(udev_device, NULL); | |
1722 | return udev_device->devlink_priority; | |
6bd1c78a KS |
1723 | } |
1724 | ||
8cd2e972 | 1725 | int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio) |
6bd1c78a | 1726 | { |
912541b0 KS |
1727 | udev_device->devlink_priority = prio; |
1728 | return 0; | |
6bd1c78a KS |
1729 | } |
1730 | ||
d7ce7539 SJR |
1731 | int udev_device_get_watch_handle(struct udev_device *udev_device) |
1732 | { | |
912541b0 KS |
1733 | if (!udev_device->info_loaded) |
1734 | udev_device_read_db(udev_device, NULL); | |
1735 | return udev_device->watch_handle; | |
d7ce7539 SJR |
1736 | } |
1737 | ||
1738 | int udev_device_set_watch_handle(struct udev_device *udev_device, int handle) | |
1739 | { | |
912541b0 KS |
1740 | udev_device->watch_handle = handle; |
1741 | return 0; | |
d7ce7539 | 1742 | } |
fc416258 | 1743 | |
9ead6627 KS |
1744 | bool udev_device_get_db_persist(struct udev_device *udev_device) |
1745 | { | |
912541b0 | 1746 | return udev_device->db_persist; |
9ead6627 KS |
1747 | } |
1748 | ||
1749 | void udev_device_set_db_persist(struct udev_device *udev_device) | |
1750 | { | |
912541b0 | 1751 | udev_device->db_persist = true; |
9ead6627 | 1752 | } |