]>
Commit | Line | Data |
---|---|---|
eb1f0e66 KS |
1 | /* |
2 | * libudev - interface to udev device information | |
3 | * | |
4 | * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
eb1f0e66 KS |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <stddef.h> | |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <dirent.h> | |
93b0f384 | 27 | #include <fcntl.h> |
eb1f0e66 KS |
28 | #include <sys/stat.h> |
29 | ||
30 | #include "libudev.h" | |
31 | #include "libudev-private.h" | |
eb1f0e66 | 32 | |
11d543c1 KS |
33 | struct udev_device { |
34 | int refcount; | |
35 | struct udev *udev; | |
b2d9e4f2 | 36 | struct udev_device *parent_device; |
11d543c1 | 37 | char *syspath; |
4ad3a37f KS |
38 | const char *devpath; |
39 | const char *sysname; | |
99214844 | 40 | char *devnode; |
11d543c1 | 41 | char *subsystem; |
e345e267 KS |
42 | struct list_node devlink_list; |
43 | struct list_node properties_list; | |
c4f5f942 | 44 | char *action; |
99214844 | 45 | int event_timeout; |
c4f5f942 KS |
46 | char *driver; |
47 | char *devpath_old; | |
48 | char *physdevpath; | |
49 | int timeout; | |
50 | dev_t devnum; | |
6bd1c78a KS |
51 | unsigned long long int seqnum; |
52 | int num_fake_partitions; | |
e88a82b5 | 53 | int devlink_priority; |
6bd1c78a | 54 | int ignore_remove; |
e345e267 | 55 | struct list_node attr_list; |
99214844 | 56 | int info_loaded; |
11d543c1 KS |
57 | }; |
58 | ||
8753fadf | 59 | static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len) |
e88a82b5 KS |
60 | { |
61 | size_t start; | |
62 | ||
63 | /* translate to location of db file */ | |
8753fadf | 64 | util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len); |
3eb46ec6 | 65 | start = util_strlcat(filename, "/.udev/db/", len); |
8753fadf | 66 | util_strlcat(filename, udev_device->devpath, len); |
7a01f11a | 67 | return util_path_encode(&filename[start], len - start); |
e88a82b5 KS |
68 | } |
69 | ||
70 | static int device_read_db(struct udev_device *udev_device) | |
71 | { | |
72 | struct stat stats; | |
3eb46ec6 KS |
73 | char filename[UTIL_PATH_SIZE]; |
74 | char line[UTIL_LINE_SIZE]; | |
e88a82b5 | 75 | FILE *f; |
e88a82b5 | 76 | |
8753fadf | 77 | syspath_to_db_path(udev_device, filename, sizeof(filename)); |
e88a82b5 KS |
78 | |
79 | if (lstat(filename, &stats) != 0) { | |
659353f5 | 80 | info(udev_device->udev, "no db file to read %s: %m\n", filename); |
e88a82b5 KS |
81 | return -1; |
82 | } | |
83 | if ((stats.st_mode & S_IFMT) == S_IFLNK) { | |
3eb46ec6 | 84 | char target[UTIL_PATH_SIZE]; |
e88a82b5 | 85 | int target_len; |
1e75cda3 | 86 | char *next; |
e88a82b5 | 87 | |
e88a82b5 KS |
88 | target_len = readlink(filename, target, sizeof(target)); |
89 | if (target_len > 0) | |
90 | target[target_len] = '\0'; | |
91 | else { | |
659353f5 | 92 | info(udev_device->udev, "error reading db link %s: %m\n", filename); |
e88a82b5 KS |
93 | return -1; |
94 | } | |
1e75cda3 KS |
95 | |
96 | next = strchr(target, ' '); | |
97 | if (next != NULL) { | |
98 | next[0] = '\0'; | |
99 | next = &next[1]; | |
100 | } | |
99214844 | 101 | if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0) |
e88a82b5 | 102 | return -ENOMEM; |
1e75cda3 KS |
103 | while (next != NULL) { |
104 | char linkname[UTIL_PATH_SIZE]; | |
105 | const char *lnk; | |
106 | ||
107 | lnk = next; | |
108 | next = strchr(next, ' '); | |
109 | if (next != NULL) { | |
110 | next[0] = '\0'; | |
111 | next = &next[1]; | |
112 | } | |
113 | util_strlcpy(linkname, udev_get_dev_path(udev_device->udev), sizeof(linkname)); | |
114 | util_strlcat(linkname, "/", sizeof(linkname)); | |
31441f6a | 115 | util_strlcat(linkname, lnk, sizeof(linkname)); |
1e75cda3 KS |
116 | device_add_devlink(udev_device, linkname); |
117 | } | |
99214844 | 118 | info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode); |
e88a82b5 KS |
119 | return 0; |
120 | } | |
121 | ||
122 | f = fopen(filename, "r"); | |
123 | if (f == NULL) { | |
659353f5 | 124 | info(udev_device->udev, "error reading db file %s: %m\n", filename); |
e88a82b5 KS |
125 | return -1; |
126 | } | |
127 | while (fgets(line, sizeof(line), f)) { | |
128 | ssize_t len; | |
129 | const char *val; | |
e88a82b5 KS |
130 | |
131 | len = strlen(line); | |
132 | if (len < 4) | |
133 | break; | |
134 | line[len-1] = '\0'; | |
135 | val = &line[2]; | |
136 | ||
137 | switch(line[0]) { | |
138 | case 'N': | |
99214844 | 139 | asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val); |
e88a82b5 | 140 | break; |
e88a82b5 | 141 | case 'S': |
3eb46ec6 KS |
142 | util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename)); |
143 | util_strlcat(filename, "/", sizeof(filename)); | |
144 | util_strlcat(filename, val, sizeof(filename)); | |
e88a82b5 KS |
145 | device_add_devlink(udev_device, filename); |
146 | break; | |
147 | case 'L': | |
148 | device_set_devlink_priority(udev_device, atoi(val)); | |
149 | break; | |
150 | case 'T': | |
99214844 | 151 | device_set_event_timeout(udev_device, atoi(val)); |
e88a82b5 KS |
152 | break; |
153 | case 'A': | |
154 | device_set_num_fake_partitions(udev_device, atoi(val)); | |
155 | break; | |
156 | case 'R': | |
157 | device_set_ignore_remove(udev_device, atoi(val)); | |
158 | break; | |
159 | case 'E': | |
0518da3b | 160 | device_add_property_from_string(udev_device, val); |
e88a82b5 KS |
161 | break; |
162 | } | |
163 | } | |
164 | fclose(f); | |
165 | ||
cd42b50d | 166 | info(udev_device->udev, "device %p filled with db file data\n", udev_device); |
04f5d75f | 167 | return 0; |
e88a82b5 KS |
168 | } |
169 | ||
99214844 KS |
170 | static int device_read_uevent_file(struct udev_device *udev_device) |
171 | { | |
172 | char filename[UTIL_PATH_SIZE]; | |
173 | FILE *f; | |
174 | char line[UTIL_LINE_SIZE]; | |
175 | int maj = 0; | |
176 | int min = 0; | |
177 | ||
178 | util_strlcpy(filename, udev_device->syspath, sizeof(filename)); | |
179 | util_strlcat(filename, "/uevent", sizeof(filename)); | |
180 | f = fopen(filename, "r"); | |
181 | if (f == NULL) | |
182 | return -1; | |
183 | ||
184 | while (fgets(line, sizeof(line), f)) { | |
185 | char *pos; | |
186 | ||
187 | pos = strchr(line, '\n'); | |
188 | if (pos == NULL) | |
189 | continue; | |
190 | pos[0] = '\0'; | |
191 | ||
192 | if (strncmp(line, "MAJOR=", 6) == 0) | |
193 | maj = strtoull(&line[6], NULL, 10); | |
194 | else if (strncmp(line, "MINOR=", 6) == 0) | |
195 | min = strtoull(&line[6], NULL, 10); | |
196 | ||
197 | device_add_property_from_string(udev_device, line); | |
198 | } | |
199 | ||
200 | udev_device->devnum = makedev(maj, min); | |
201 | ||
202 | fclose(f); | |
203 | return 0; | |
204 | } | |
205 | ||
206 | static void device_load_info(struct udev_device *device) | |
207 | { | |
208 | device_read_uevent_file(device); | |
209 | device_read_db(device); | |
210 | device->info_loaded = 1; | |
211 | } | |
212 | ||
213 | void device_set_info_loaded(struct udev_device *device) | |
214 | { | |
215 | device->info_loaded = 1; | |
216 | } | |
217 | ||
e0083e8e | 218 | struct udev_device *device_new(struct udev *udev) |
eb1f0e66 KS |
219 | { |
220 | struct udev_device *udev_device; | |
221 | ||
ba6929f6 KS |
222 | if (udev == NULL) |
223 | return NULL; | |
224 | ||
eb1f0e66 KS |
225 | udev_device = malloc(sizeof(struct udev_device)); |
226 | if (udev_device == NULL) | |
227 | return NULL; | |
228 | memset(udev_device, 0x00, sizeof(struct udev_device)); | |
229 | udev_device->refcount = 1; | |
230 | udev_device->udev = udev; | |
e345e267 KS |
231 | list_init(&udev_device->devlink_list); |
232 | list_init(&udev_device->properties_list); | |
233 | list_init(&udev_device->attr_list); | |
7d563a17 | 234 | info(udev_device->udev, "udev_device: %p created\n", udev_device); |
eb1f0e66 KS |
235 | return udev_device; |
236 | } | |
237 | ||
238 | /** | |
8753fadf | 239 | * udev_device_new_from_syspath: |
eb1f0e66 | 240 | * @udev: udev library context |
8753fadf | 241 | * @syspath: sys device path including sys directory |
eb1f0e66 | 242 | * |
8753fadf KS |
243 | * Create new udev device, and fill in information from the sys |
244 | * device and the udev database entry. The sypath is the absolute | |
245 | * path to the device, including the sys mount point. | |
eb1f0e66 KS |
246 | * |
247 | * The initial refcount is 1, and needs to be decremented to | |
248 | * release the ressources of the udev device. | |
249 | * | |
250 | * Returns: a new udev device, or #NULL, if it does not exist | |
251 | **/ | |
8753fadf | 252 | struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) |
eb1f0e66 | 253 | { |
b95f8a76 KS |
254 | size_t len; |
255 | const char *subdir; | |
3eb46ec6 | 256 | char path[UTIL_PATH_SIZE]; |
62b9dfb6 | 257 | char *pos; |
eb1f0e66 KS |
258 | struct stat statbuf; |
259 | struct udev_device *udev_device; | |
eb1f0e66 | 260 | |
ba6929f6 KS |
261 | if (udev == NULL) |
262 | return NULL; | |
8753fadf | 263 | if (syspath == NULL) |
ba6929f6 KS |
264 | return NULL; |
265 | ||
b95f8a76 KS |
266 | /* path starts in sys */ |
267 | len = strlen(udev_get_sys_path(udev)); | |
268 | if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) { | |
269 | info(udev, "not in sys :%s\n", syspath); | |
eb1f0e66 | 270 | return NULL; |
4ad3a37f | 271 | } |
eb1f0e66 | 272 | |
b95f8a76 KS |
273 | /* path is not a root directory */ |
274 | subdir = &syspath[len+1]; | |
275 | pos = strrchr(subdir, '/'); | |
276 | if (pos == NULL || pos < &subdir[2]) { | |
bc8184ed | 277 | info(udev, "not a subdir :%s\n", syspath); |
eb1f0e66 | 278 | return NULL; |
b95f8a76 | 279 | } |
eb1f0e66 | 280 | |
ba6929f6 | 281 | /* resolve possible symlink to real path */ |
8753fadf | 282 | util_strlcpy(path, syspath, sizeof(path)); |
b21b95d7 | 283 | util_resolve_sys_link(udev, path, sizeof(path)); |
b95f8a76 | 284 | |
62b9dfb6 KS |
285 | /* try to resolve the silly block layout if needed */ |
286 | if (strncmp(&path[len], "/block/", 7) == 0) { | |
287 | char block[UTIL_PATH_SIZE]; | |
288 | char part[UTIL_PATH_SIZE]; | |
289 | ||
290 | util_strlcpy(block, path, sizeof(block)); | |
291 | pos = strrchr(block, '/'); | |
292 | if (pos == NULL) | |
293 | return NULL; | |
294 | util_strlcpy(part, pos, sizeof(part)); | |
295 | pos[0] = '\0'; | |
296 | if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) { | |
297 | util_strlcpy(path, block, sizeof(path)); | |
298 | util_strlcat(path, part, sizeof(path)); | |
299 | } | |
300 | } | |
301 | ||
b95f8a76 KS |
302 | /* path exists in sys */ |
303 | if (strncmp(&syspath[len], "/devices/", 9) == 0 || | |
304 | strncmp(&syspath[len], "/class/", 7) == 0 || | |
305 | strncmp(&syspath[len], "/block/", 7) == 0) { | |
306 | char file[UTIL_PATH_SIZE]; | |
307 | ||
308 | /* all "devices" require a "uevent" file */ | |
309 | util_strlcpy(file, path, sizeof(file)); | |
310 | util_strlcat(file, "/uevent", sizeof(file)); | |
311 | if (stat(file, &statbuf) != 0) { | |
312 | info(udev, "not a device: %s\n", syspath); | |
313 | return NULL; | |
314 | } | |
315 | } else { | |
316 | /* everything else just needs to be a directory */ | |
317 | if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) { | |
318 | info(udev, "directory not found: %s\n", syspath); | |
319 | return NULL; | |
320 | } | |
321 | } | |
322 | ||
e0083e8e | 323 | udev_device = device_new(udev); |
b95f8a76 KS |
324 | if (udev_device == NULL) |
325 | return NULL; | |
326 | ||
8753fadf | 327 | device_set_syspath(udev_device, path); |
7d563a17 | 328 | info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device)); |
eb1f0e66 | 329 | |
eb1f0e66 KS |
330 | return udev_device; |
331 | } | |
332 | ||
4c9dff47 KS |
333 | struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum) |
334 | { | |
335 | char path[UTIL_PATH_SIZE]; | |
03198b93 | 336 | const char *type_str; |
438d4c3c | 337 | struct udev_enumerate *udev_enumerate; |
0de33a61 | 338 | struct udev_list_entry *list_entry; |
bf7ad0ea | 339 | struct udev_device *device = NULL; |
03198b93 KS |
340 | |
341 | if (type == 'b') | |
342 | type_str = "block"; | |
343 | else if (type == 'c') | |
344 | type_str = "char"; | |
345 | else | |
346 | return NULL; | |
4c9dff47 | 347 | |
b95f8a76 | 348 | /* /sys/dev/{block,char}/<maj>:<min> link */ |
03198b93 KS |
349 | snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev), |
350 | type_str, major(devnum), minor(devnum)); | |
bf7ad0ea KS |
351 | if (util_resolve_sys_link(udev, path, sizeof(path)) == 0) |
352 | return udev_device_new_from_syspath(udev, path); | |
4c9dff47 | 353 | |
438d4c3c KS |
354 | udev_enumerate = udev_enumerate_new(udev); |
355 | if (udev_enumerate == NULL) | |
356 | return NULL; | |
357 | ||
bc8184ed KS |
358 | /* fallback to search sys devices for the major/minor */ |
359 | if (type == 'b') | |
c97f839e | 360 | udev_enumerate_add_match_subsystem(udev_enumerate, "block"); |
bc8184ed | 361 | else if (type == 'c') |
c97f839e KS |
362 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); |
363 | udev_enumerate_scan_devices(udev_enumerate); | |
438d4c3c | 364 | udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { |
bf7ad0ea KS |
365 | struct udev_device *device_loop; |
366 | ||
0de33a61 | 367 | device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry)); |
bf7ad0ea KS |
368 | if (device_loop != NULL) { |
369 | if (udev_device_get_devnum(device_loop) == devnum) { | |
c97f839e | 370 | if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0) |
bc8184ed | 371 | continue; |
c97f839e | 372 | if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0) |
bc8184ed | 373 | continue; |
bf7ad0ea KS |
374 | device = device_loop; |
375 | break; | |
376 | } | |
377 | udev_device_unref(device_loop); | |
378 | } | |
bf7ad0ea | 379 | } |
438d4c3c | 380 | udev_enumerate_unref(udev_enumerate); |
bf7ad0ea | 381 | return device; |
4c9dff47 KS |
382 | } |
383 | ||
b2d9e4f2 | 384 | static struct udev_device *device_new_from_parent(struct udev_device *udev_device) |
4ad3a37f KS |
385 | { |
386 | struct udev_device *udev_device_parent = NULL; | |
387 | char path[UTIL_PATH_SIZE]; | |
b95f8a76 | 388 | const char *subdir; |
4ad3a37f | 389 | |
b95f8a76 KS |
390 | /* follow "device" link in deprecated sys layout */ |
391 | if (strncmp(udev_device->devpath, "/class/", 7) == 0 || | |
392 | strncmp(udev_device->devpath, "/block/", 7) == 0) { | |
393 | util_strlcpy(path, udev_device->syspath, sizeof(path)); | |
394 | util_strlcat(path, "/device", sizeof(path)); | |
395 | if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) | |
396 | udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); | |
397 | return udev_device_parent; | |
398 | } | |
4ad3a37f | 399 | |
8753fadf | 400 | util_strlcpy(path, udev_device->syspath, sizeof(path)); |
b95f8a76 | 401 | subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1]; |
4ad3a37f | 402 | while (1) { |
b95f8a76 KS |
403 | char *pos; |
404 | ||
405 | pos = strrchr(subdir, '/'); | |
406 | if (pos == NULL || pos < &subdir[2]) | |
4ad3a37f KS |
407 | break; |
408 | pos[0] = '\0'; | |
8753fadf | 409 | udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); |
4ad3a37f | 410 | if (udev_device_parent != NULL) |
0518da3b KS |
411 | return udev_device_parent; |
412 | } | |
0518da3b | 413 | return NULL; |
4ad3a37f KS |
414 | } |
415 | ||
b2d9e4f2 KS |
416 | struct udev_device *udev_device_get_parent(struct udev_device *udev_device) |
417 | { | |
b95f8a76 KS |
418 | if (udev_device == NULL) |
419 | return NULL; | |
420 | ||
0518da3b KS |
421 | if (udev_device->parent_device != NULL) { |
422 | info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device); | |
423 | return udev_device->parent_device; | |
424 | } | |
425 | udev_device->parent_device = device_new_from_parent(udev_device); | |
b2d9e4f2 KS |
426 | return udev_device->parent_device; |
427 | } | |
428 | ||
98f10a9e KS |
429 | struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem) |
430 | { | |
431 | struct udev_device *parent; | |
432 | ||
433 | parent = udev_device_get_parent(udev_device); | |
434 | while (parent != NULL) { | |
435 | const char *parent_subsystem; | |
436 | ||
437 | parent_subsystem = udev_device_get_subsystem(parent); | |
438 | if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) | |
439 | break; | |
440 | parent = udev_device_get_parent(parent); | |
441 | } | |
442 | return parent; | |
443 | } | |
444 | ||
eb1f0e66 KS |
445 | /** |
446 | * udev_device_get_udev: | |
7d8787b3 | 447 | * @udev_device: udev device |
eb1f0e66 KS |
448 | * |
449 | * Retrieve the udev library context the device was created with. | |
450 | * | |
451 | * Returns: the udev library context | |
452 | **/ | |
453 | struct udev *udev_device_get_udev(struct udev_device *udev_device) | |
454 | { | |
ba6929f6 KS |
455 | if (udev_device == NULL) |
456 | return NULL; | |
eb1f0e66 KS |
457 | return udev_device->udev; |
458 | } | |
459 | ||
460 | /** | |
461 | * udev_device_ref: | |
462 | * @udev_device: udev device | |
463 | * | |
464 | * Take a reference of a udev device. | |
465 | * | |
466 | * Returns: the passed udev device | |
467 | **/ | |
468 | struct udev_device *udev_device_ref(struct udev_device *udev_device) | |
469 | { | |
ba6929f6 KS |
470 | if (udev_device == NULL) |
471 | return NULL; | |
eb1f0e66 KS |
472 | udev_device->refcount++; |
473 | return udev_device; | |
474 | } | |
475 | ||
476 | /** | |
477 | * udev_device_unref: | |
478 | * @udev_device: udev device | |
479 | * | |
480 | * Drop a reference of a udev device. If the refcount reaches zero, | |
481 | * the ressources of the device will be released. | |
482 | * | |
483 | **/ | |
484 | void udev_device_unref(struct udev_device *udev_device) | |
485 | { | |
ba6929f6 KS |
486 | if (udev_device == NULL) |
487 | return; | |
eb1f0e66 KS |
488 | udev_device->refcount--; |
489 | if (udev_device->refcount > 0) | |
490 | return; | |
b2d9e4f2 KS |
491 | if (udev_device->parent_device != NULL) |
492 | udev_device_unref(udev_device->parent_device); | |
11d543c1 | 493 | free(udev_device->syspath); |
99214844 | 494 | free(udev_device->devnode); |
ba6929f6 | 495 | free(udev_device->subsystem); |
99214844 | 496 | list_cleanup(udev_device->udev, &udev_device->devlink_list); |
bf7ad0ea | 497 | list_cleanup(udev_device->udev, &udev_device->properties_list); |
1c7047ea KS |
498 | free(udev_device->action); |
499 | free(udev_device->driver); | |
500 | free(udev_device->devpath_old); | |
501 | free(udev_device->physdevpath); | |
bf7ad0ea | 502 | list_cleanup(udev_device->udev, &udev_device->attr_list); |
7d563a17 | 503 | info(udev_device->udev, "udev_device: %p released\n", udev_device); |
eb1f0e66 KS |
504 | free(udev_device); |
505 | } | |
506 | ||
507 | /** | |
508 | * udev_device_get_devpath: | |
509 | * @udev_device: udev device | |
510 | * | |
11d543c1 KS |
511 | * Retrieve the kernel devpath value of the udev device. The path |
512 | * does not contain the sys mount point, and starts with a '/'. | |
eb1f0e66 | 513 | * |
11d543c1 | 514 | * Returns: the devpath of the udev device |
eb1f0e66 KS |
515 | **/ |
516 | const char *udev_device_get_devpath(struct udev_device *udev_device) | |
517 | { | |
ba6929f6 KS |
518 | if (udev_device == NULL) |
519 | return NULL; | |
520 | return udev_device->devpath; | |
eb1f0e66 KS |
521 | } |
522 | ||
11d543c1 KS |
523 | /** |
524 | * udev_device_get_syspath: | |
525 | * @udev_device: udev device | |
526 | * | |
527 | * Retrieve the sys path of the udev device. The path is an | |
528 | * absolute path and starts with the sys mount point. | |
529 | * | |
530 | * Returns: the sys path of the udev device | |
531 | **/ | |
532 | const char *udev_device_get_syspath(struct udev_device *udev_device) | |
533 | { | |
534 | if (udev_device == NULL) | |
535 | return NULL; | |
536 | return udev_device->syspath; | |
537 | } | |
538 | ||
4ad3a37f KS |
539 | const char *udev_device_get_sysname(struct udev_device *udev_device) |
540 | { | |
541 | if (udev_device == NULL) | |
542 | return NULL; | |
543 | return udev_device->sysname; | |
544 | } | |
545 | ||
eb1f0e66 | 546 | /** |
fb762bb9 | 547 | * udev_device_get_devnode: |
eb1f0e66 KS |
548 | * @udev_device: udev device |
549 | * | |
550 | * Retrieve the device node file name belonging to the udev device. | |
ba6929f6 | 551 | * The path is an absolute path, and starts with the device directory. |
eb1f0e66 KS |
552 | * |
553 | * Returns: the device node file name of the udev device, or #NULL if no device node exists | |
554 | **/ | |
fb762bb9 | 555 | const char *udev_device_get_devnode(struct udev_device *udev_device) |
eb1f0e66 | 556 | { |
ba6929f6 | 557 | if (udev_device == NULL) |
eb1f0e66 | 558 | return NULL; |
99214844 KS |
559 | if (!udev_device->info_loaded) |
560 | device_load_info(udev_device); | |
561 | return udev_device->devnode; | |
eb1f0e66 KS |
562 | } |
563 | ||
564 | /** | |
565 | * udev_device_get_subsystem: | |
566 | * @udev_device: udev device | |
567 | * | |
568 | * Retrieve the subsystem string of the udev device. The string does not | |
569 | * contain any "/". | |
570 | * | |
571 | * Returns: the subsystem name of the udev device, or #NULL if it can not be determined | |
572 | **/ | |
573 | const char *udev_device_get_subsystem(struct udev_device *udev_device) | |
574 | { | |
17fcfb59 | 575 | char subsystem[UTIL_NAME_SIZE]; |
ba6929f6 KS |
576 | |
577 | if (udev_device == NULL) | |
578 | return NULL; | |
579 | if (udev_device->subsystem != NULL) | |
580 | return udev_device->subsystem; | |
0518da3b KS |
581 | |
582 | /* read "subsytem" link */ | |
279595bd | 583 | if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) { |
0518da3b KS |
584 | udev_device->subsystem = strdup(subsystem); |
585 | return udev_device->subsystem; | |
586 | } | |
587 | ||
588 | /* implicit names */ | |
589 | if (strncmp(udev_device->devpath, "/module/", 8) == 0) { | |
590 | udev_device->subsystem = strdup("module"); | |
591 | return udev_device->subsystem; | |
592 | } | |
593 | if (strstr(udev_device->devpath, "/drivers/") != NULL) { | |
594 | udev_device->subsystem = strdup("drivers"); | |
595 | return udev_device->subsystem; | |
596 | } | |
597 | if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 || | |
598 | strncmp(udev_device->devpath, "/class/", 7) == 0 || | |
599 | strncmp(udev_device->devpath, "/bus/", 5) == 0) { | |
600 | udev_device->subsystem = strdup("subsystem"); | |
601 | return udev_device->subsystem; | |
602 | } | |
603 | return NULL; | |
eb1f0e66 KS |
604 | } |
605 | ||
606 | /** | |
0de33a61 | 607 | * udev_device_get_devlinks_list_entry: |
eb1f0e66 | 608 | * @udev_device: udev device |
eb1f0e66 | 609 | * |
bf7ad0ea KS |
610 | * Retrieve the list of device links pointing to the device file of |
611 | * the udev device. The next list entry can be retrieved with | |
e345e267 | 612 | * udev_list_entry_next(), which returns #NULL if no more entries exist. |
bf7ad0ea | 613 | * The devlink path can be retrieved from the list entry by |
e345e267 | 614 | * udev_list_entry_get_name(). The path is an absolute path, and starts with |
bf7ad0ea | 615 | * the device directory. |
eb1f0e66 | 616 | * |
bf7ad0ea | 617 | * Returns: the first entry of the device node link list |
eb1f0e66 | 618 | **/ |
0de33a61 | 619 | struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 620 | { |
99214844 KS |
621 | if (udev_device == NULL) |
622 | return NULL; | |
623 | if (!udev_device->info_loaded) | |
624 | device_load_info(udev_device); | |
625 | return list_get_entry(&udev_device->devlink_list); | |
eb1f0e66 KS |
626 | } |
627 | ||
628 | /** | |
0de33a61 | 629 | * udev_device_get_properties_list_entry: |
eb1f0e66 | 630 | * @udev_device: udev device |
eb1f0e66 | 631 | * |
bf7ad0ea | 632 | * Retrieve the list of key/value device properties of the udev |
e345e267 | 633 | * device. The next list entry can be retrieved with udev_list_entry_next(), |
bf7ad0ea KS |
634 | * which returns #NULL if no more entries exist. The property name |
635 | * can be retrieved from the list entry by udev_list_get_name(), | |
636 | * the property value by udev_list_get_value(). | |
eb1f0e66 | 637 | * |
bf7ad0ea | 638 | * Returns: the first entry of the property list |
eb1f0e66 | 639 | **/ |
0de33a61 | 640 | struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 641 | { |
99214844 KS |
642 | if (udev_device == NULL) |
643 | return NULL; | |
644 | if (!udev_device->info_loaded) | |
645 | device_load_info(udev_device); | |
bf7ad0ea | 646 | return list_get_entry(&udev_device->properties_list); |
eb1f0e66 | 647 | } |
11d543c1 | 648 | |
c4f5f942 KS |
649 | const char *udev_device_get_driver(struct udev_device *udev_device) |
650 | { | |
17fcfb59 | 651 | char driver[UTIL_NAME_SIZE]; |
95d90c4f | 652 | |
c4f5f942 KS |
653 | if (udev_device == NULL) |
654 | return NULL; | |
95d90c4f KS |
655 | if (udev_device->driver != NULL) |
656 | return udev_device->driver; | |
8753fadf | 657 | if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2) |
95d90c4f KS |
658 | return NULL; |
659 | udev_device->driver = strdup(driver); | |
c4f5f942 KS |
660 | return udev_device->driver; |
661 | } | |
662 | ||
663 | dev_t udev_device_get_devnum(struct udev_device *udev_device) | |
664 | { | |
665 | if (udev_device == NULL) | |
666 | return makedev(0, 0); | |
99214844 KS |
667 | if (!udev_device->info_loaded) |
668 | device_load_info(udev_device); | |
c4f5f942 KS |
669 | return udev_device->devnum; |
670 | } | |
671 | ||
672 | const char *udev_device_get_action(struct udev_device *udev_device) | |
673 | { | |
674 | if (udev_device == NULL) | |
675 | return NULL; | |
676 | return udev_device->action; | |
677 | } | |
678 | ||
37372bbc KS |
679 | unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) |
680 | { | |
681 | if (udev_device == NULL) | |
682 | return 0; | |
683 | return udev_device->seqnum; | |
684 | } | |
685 | ||
93b0f384 KS |
686 | const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr) |
687 | { | |
0de33a61 | 688 | struct udev_list_entry *list_entry; |
93b0f384 KS |
689 | char path[UTIL_PATH_SIZE]; |
690 | char value[UTIL_NAME_SIZE]; | |
691 | struct stat statbuf; | |
692 | int fd; | |
693 | ssize_t size; | |
694 | const char *val = NULL; | |
695 | ||
0518da3b | 696 | /* look for possibly already cached result */ |
0de33a61 KS |
697 | udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) { |
698 | if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) { | |
699 | info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry)); | |
700 | return udev_list_entry_get_value(list_entry); | |
0518da3b KS |
701 | } |
702 | } | |
703 | ||
93b0f384 KS |
704 | util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path)); |
705 | util_strlcat(path, "/", sizeof(path)); | |
706 | util_strlcat(path, attr, sizeof(path)); | |
707 | ||
708 | if (lstat(path, &statbuf) != 0) { | |
659353f5 | 709 | info(udev_device->udev, "stat '%s' failed: %m\n", path); |
93b0f384 KS |
710 | goto out; |
711 | } | |
712 | ||
713 | if (S_ISLNK(statbuf.st_mode)) { | |
714 | /* links return the last element of the target path */ | |
715 | char target[UTIL_NAME_SIZE]; | |
716 | int len; | |
717 | char *pos; | |
718 | ||
719 | len = readlink(path, target, sizeof(target)); | |
720 | if (len > 0) { | |
721 | target[len] = '\0'; | |
722 | pos = strrchr(target, '/'); | |
723 | if (pos != NULL) { | |
724 | pos = &pos[1]; | |
725 | info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos); | |
0de33a61 KS |
726 | list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0); |
727 | val = udev_list_entry_get_value(list_entry); | |
93b0f384 KS |
728 | } |
729 | } | |
730 | goto out; | |
731 | } | |
732 | ||
733 | /* skip directories */ | |
734 | if (S_ISDIR(statbuf.st_mode)) | |
735 | goto out; | |
736 | ||
737 | /* skip non-readable files */ | |
738 | if ((statbuf.st_mode & S_IRUSR) == 0) | |
739 | goto out; | |
740 | ||
741 | /* read attribute value */ | |
742 | fd = open(path, O_RDONLY); | |
743 | if (fd < 0) { | |
744 | info(udev_device->udev, "attribute '%s' can not be opened\n", path); | |
745 | goto out; | |
746 | } | |
747 | size = read(fd, value, sizeof(value)); | |
748 | close(fd); | |
749 | if (size < 0) | |
750 | goto out; | |
751 | if (size == sizeof(value)) | |
752 | goto out; | |
753 | ||
0518da3b | 754 | /* got a valid value, store it in cache and return it */ |
93b0f384 KS |
755 | value[size] = '\0'; |
756 | util_remove_trailing_chars(value, '\n'); | |
757 | info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value); | |
0de33a61 KS |
758 | list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0); |
759 | val = udev_list_entry_get_value(list_entry); | |
93b0f384 KS |
760 | out: |
761 | return val; | |
762 | } | |
8753fadf | 763 | int device_set_syspath(struct udev_device *udev_device, const char *syspath) |
11d543c1 | 764 | { |
8753fadf KS |
765 | const char *pos; |
766 | ||
767 | udev_device->syspath = strdup(syspath); | |
768 | if (udev_device->syspath == NULL) | |
11d543c1 KS |
769 | return -ENOMEM; |
770 | udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))]; | |
8753fadf KS |
771 | pos = strrchr(udev_device->syspath, '/'); |
772 | if (pos == NULL) | |
773 | return -EINVAL; | |
774 | udev_device->sysname = &pos[1]; | |
11d543c1 KS |
775 | return 0; |
776 | } | |
777 | ||
778 | int device_set_subsystem(struct udev_device *udev_device, const char *subsystem) | |
779 | { | |
780 | udev_device->subsystem = strdup(subsystem); | |
781 | if (udev_device->subsystem == NULL) | |
782 | return -1; | |
783 | return 0; | |
784 | } | |
785 | ||
99214844 | 786 | int device_set_devnode(struct udev_device *udev_device, const char *devnode) |
11d543c1 | 787 | { |
99214844 KS |
788 | udev_device->devnode = strdup(devnode); |
789 | if (udev_device->devnode == NULL) | |
11d543c1 KS |
790 | return -ENOMEM; |
791 | return 0; | |
792 | } | |
793 | ||
794 | int device_add_devlink(struct udev_device *udev_device, const char *devlink) | |
795 | { | |
0de33a61 | 796 | if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL) |
11d543c1 KS |
797 | return -ENOMEM; |
798 | return 0; | |
799 | } | |
800 | ||
0518da3b | 801 | int device_add_property(struct udev_device *udev_device, const char *key, const char *value) |
11d543c1 | 802 | { |
0de33a61 | 803 | if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL) |
11d543c1 KS |
804 | return -ENOMEM; |
805 | return 0; | |
806 | } | |
c4f5f942 | 807 | |
0518da3b KS |
808 | int device_add_property_from_string(struct udev_device *udev_device, const char *property) |
809 | { | |
810 | char name[UTIL_PATH_SIZE]; | |
811 | char *val; | |
812 | ||
813 | strncpy(name, property, sizeof(name)); | |
814 | val = strchr(name, '='); | |
815 | if (val == NULL) | |
816 | return -1; | |
817 | val[0] = '\0'; | |
818 | val = &val[1]; | |
819 | if (val[0] == '\0') | |
820 | val = NULL; | |
821 | device_add_property(udev_device, name, val); | |
822 | return 0; | |
823 | } | |
824 | ||
c4f5f942 KS |
825 | int device_set_action(struct udev_device *udev_device, const char *action) |
826 | { | |
827 | udev_device->action = strdup(action); | |
828 | if (udev_device->action == NULL) | |
829 | return -ENOMEM; | |
830 | return 0; | |
831 | } | |
832 | ||
833 | int device_set_driver(struct udev_device *udev_device, const char *driver) | |
834 | { | |
835 | udev_device->driver = strdup(driver); | |
836 | if (udev_device->driver == NULL) | |
837 | return -ENOMEM; | |
838 | return 0; | |
839 | } | |
840 | ||
841 | const char *device_get_devpath_old(struct udev_device *udev_device) | |
842 | { | |
c4f5f942 KS |
843 | return udev_device->devpath_old; |
844 | } | |
845 | ||
846 | int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old) | |
847 | { | |
848 | udev_device->devpath_old = strdup(devpath_old); | |
849 | if (udev_device->devpath_old == NULL) | |
850 | return -ENOMEM; | |
851 | return 0; | |
852 | } | |
853 | ||
854 | const char *device_get_physdevpath(struct udev_device *udev_device) | |
855 | { | |
c4f5f942 KS |
856 | return udev_device->physdevpath; |
857 | } | |
858 | ||
859 | int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath) | |
860 | { | |
861 | udev_device->physdevpath = strdup(physdevpath); | |
862 | if (udev_device->physdevpath == NULL) | |
863 | return -ENOMEM; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | int device_get_timeout(struct udev_device *udev_device) | |
868 | { | |
c4f5f942 KS |
869 | return udev_device->timeout; |
870 | } | |
871 | ||
872 | int device_set_timeout(struct udev_device *udev_device, int timeout) | |
873 | { | |
874 | udev_device->timeout = timeout; | |
875 | return 0; | |
876 | } | |
99214844 KS |
877 | int device_get_event_timeout(struct udev_device *udev_device) |
878 | { | |
879 | if (!udev_device->info_loaded) | |
880 | device_load_info(udev_device); | |
881 | return udev_device->event_timeout; | |
882 | } | |
883 | ||
884 | int device_set_event_timeout(struct udev_device *udev_device, int event_timeout) | |
885 | { | |
886 | udev_device->event_timeout = event_timeout; | |
887 | return 0; | |
888 | } | |
c4f5f942 | 889 | |
37372bbc KS |
890 | int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum) |
891 | { | |
892 | udev_device->seqnum = seqnum; | |
893 | return 0; | |
894 | } | |
895 | ||
c4f5f942 KS |
896 | int device_set_devnum(struct udev_device *udev_device, dev_t devnum) |
897 | { | |
898 | udev_device->devnum = devnum; | |
899 | return 0; | |
900 | } | |
6bd1c78a KS |
901 | |
902 | int device_get_num_fake_partitions(struct udev_device *udev_device) | |
903 | { | |
99214844 KS |
904 | if (!udev_device->info_loaded) |
905 | device_load_info(udev_device); | |
6bd1c78a KS |
906 | return udev_device->num_fake_partitions; |
907 | } | |
908 | ||
909 | int device_set_num_fake_partitions(struct udev_device *udev_device, int num) | |
910 | { | |
911 | udev_device->num_fake_partitions = num; | |
e88a82b5 | 912 | return 0; |
6bd1c78a KS |
913 | } |
914 | ||
e88a82b5 | 915 | int device_get_devlink_priority(struct udev_device *udev_device) |
6bd1c78a | 916 | { |
99214844 KS |
917 | if (!udev_device->info_loaded) |
918 | device_load_info(udev_device); | |
e88a82b5 | 919 | return udev_device->devlink_priority; |
6bd1c78a KS |
920 | } |
921 | ||
e88a82b5 | 922 | int device_set_devlink_priority(struct udev_device *udev_device, int prio) |
6bd1c78a | 923 | { |
e88a82b5 KS |
924 | udev_device->devlink_priority = prio; |
925 | return 0; | |
6bd1c78a KS |
926 | } |
927 | ||
928 | int device_get_ignore_remove(struct udev_device *udev_device) | |
929 | { | |
99214844 KS |
930 | if (!udev_device->info_loaded) |
931 | device_load_info(udev_device); | |
6bd1c78a KS |
932 | return udev_device->ignore_remove; |
933 | } | |
934 | ||
935 | int device_set_ignore_remove(struct udev_device *udev_device, int ignore) | |
936 | { | |
e88a82b5 KS |
937 | udev_device->ignore_remove = ignore; |
938 | return 0; | |
6bd1c78a | 939 | } |
e88a82b5 | 940 |