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