]>
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> |
517814e7 | 28 | #include <ctype.h> |
eb1f0e66 KS |
29 | #include <sys/stat.h> |
30 | ||
31 | #include "libudev.h" | |
32 | #include "libudev-private.h" | |
eb1f0e66 | 33 | |
11d543c1 KS |
34 | struct udev_device { |
35 | int refcount; | |
36 | struct udev *udev; | |
b2d9e4f2 | 37 | struct udev_device *parent_device; |
31f4b036 | 38 | int parent_set; |
11d543c1 | 39 | char *syspath; |
4ad3a37f | 40 | const char *devpath; |
517814e7 KS |
41 | char *sysname; |
42 | const char *sysnum; | |
99214844 | 43 | char *devnode; |
11d543c1 | 44 | char *subsystem; |
5c5cad79 | 45 | int subsystem_set; |
517814e7 | 46 | struct udev_list_node devlinks_list; |
bd85566c | 47 | int devlinks_uptodate; |
8cd2e972 | 48 | struct udev_list_node properties_list; |
979ff016 KS |
49 | char *envp[128]; |
50 | int envp_uptodate; | |
51 | char *driver; | |
5c5cad79 | 52 | int driver_set; |
979ff016 | 53 | dev_t devnum; |
c4f5f942 | 54 | char *action; |
99214844 | 55 | int event_timeout; |
c4f5f942 KS |
56 | char *devpath_old; |
57 | char *physdevpath; | |
58 | int timeout; | |
6bd1c78a KS |
59 | unsigned long long int seqnum; |
60 | int num_fake_partitions; | |
e88a82b5 | 61 | int devlink_priority; |
6bd1c78a | 62 | int ignore_remove; |
8cd2e972 | 63 | struct udev_list_node attr_list; |
99214844 | 64 | int info_loaded; |
11d543c1 KS |
65 | }; |
66 | ||
9a997ecf | 67 | static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len) |
e88a82b5 KS |
68 | { |
69 | size_t start; | |
70 | ||
71 | /* translate to location of db file */ | |
9a997ecf | 72 | util_strlcpy(filename, udev_get_dev_path(udev), len); |
3eb46ec6 | 73 | start = util_strlcat(filename, "/.udev/db/", len); |
9a997ecf | 74 | util_strlcat(filename, devpath, len); |
7a01f11a | 75 | return util_path_encode(&filename[start], len - start); |
e88a82b5 KS |
76 | } |
77 | ||
aa8734ff | 78 | int udev_device_read_db(struct udev_device *udev_device) |
e88a82b5 KS |
79 | { |
80 | struct stat stats; | |
3eb46ec6 KS |
81 | char filename[UTIL_PATH_SIZE]; |
82 | char line[UTIL_LINE_SIZE]; | |
e88a82b5 | 83 | FILE *f; |
e88a82b5 | 84 | |
9a997ecf | 85 | devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename)); |
e88a82b5 KS |
86 | |
87 | if (lstat(filename, &stats) != 0) { | |
659353f5 | 88 | info(udev_device->udev, "no db file to read %s: %m\n", filename); |
e88a82b5 KS |
89 | return -1; |
90 | } | |
91 | if ((stats.st_mode & S_IFMT) == S_IFLNK) { | |
3eb46ec6 | 92 | char target[UTIL_PATH_SIZE]; |
517814e7 | 93 | char devnode[UTIL_PATH_SIZE]; |
e88a82b5 | 94 | int target_len; |
1e75cda3 | 95 | char *next; |
e88a82b5 | 96 | |
e88a82b5 KS |
97 | target_len = readlink(filename, target, sizeof(target)); |
98 | if (target_len > 0) | |
99 | target[target_len] = '\0'; | |
100 | else { | |
659353f5 | 101 | info(udev_device->udev, "error reading db link %s: %m\n", filename); |
e88a82b5 KS |
102 | return -1; |
103 | } | |
1e75cda3 KS |
104 | |
105 | next = strchr(target, ' '); | |
106 | if (next != NULL) { | |
107 | next[0] = '\0'; | |
108 | next = &next[1]; | |
109 | } | |
517814e7 KS |
110 | util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode)); |
111 | util_strlcat(devnode, "/", sizeof(devnode)); | |
112 | util_strlcat(devnode, target, sizeof(devnode)); | |
113 | udev_device_set_devnode(udev_device, devnode); | |
1e75cda3 | 114 | while (next != NULL) { |
517814e7 | 115 | char devlink[UTIL_PATH_SIZE]; |
1e75cda3 KS |
116 | const char *lnk; |
117 | ||
118 | lnk = next; | |
119 | next = strchr(next, ' '); | |
120 | if (next != NULL) { | |
121 | next[0] = '\0'; | |
122 | next = &next[1]; | |
123 | } | |
517814e7 KS |
124 | util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink)); |
125 | util_strlcat(devlink, "/", sizeof(devlink)); | |
126 | util_strlcat(devlink, lnk, sizeof(devlink)); | |
127 | udev_device_add_devlink(udev_device, devlink); | |
1e75cda3 | 128 | } |
99214844 | 129 | info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode); |
e88a82b5 KS |
130 | return 0; |
131 | } | |
132 | ||
133 | f = fopen(filename, "r"); | |
134 | if (f == NULL) { | |
659353f5 | 135 | info(udev_device->udev, "error reading db file %s: %m\n", filename); |
e88a82b5 KS |
136 | return -1; |
137 | } | |
138 | while (fgets(line, sizeof(line), f)) { | |
139 | ssize_t len; | |
140 | const char *val; | |
e88a82b5 KS |
141 | |
142 | len = strlen(line); | |
143 | if (len < 4) | |
144 | break; | |
145 | line[len-1] = '\0'; | |
146 | val = &line[2]; | |
e88a82b5 KS |
147 | switch(line[0]) { |
148 | case 'N': | |
517814e7 KS |
149 | util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename)); |
150 | util_strlcat(filename, "/", sizeof(filename)); | |
151 | util_strlcat(filename, val, sizeof(filename)); | |
152 | udev_device_set_devnode(udev_device, filename); | |
e88a82b5 | 153 | break; |
e88a82b5 | 154 | case 'S': |
3eb46ec6 KS |
155 | util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename)); |
156 | util_strlcat(filename, "/", sizeof(filename)); | |
157 | util_strlcat(filename, val, sizeof(filename)); | |
8cd2e972 | 158 | udev_device_add_devlink(udev_device, filename); |
e88a82b5 KS |
159 | break; |
160 | case 'L': | |
8cd2e972 | 161 | udev_device_set_devlink_priority(udev_device, atoi(val)); |
e88a82b5 KS |
162 | break; |
163 | case 'T': | |
8cd2e972 | 164 | udev_device_set_event_timeout(udev_device, atoi(val)); |
e88a82b5 KS |
165 | break; |
166 | case 'A': | |
8cd2e972 | 167 | udev_device_set_num_fake_partitions(udev_device, atoi(val)); |
e88a82b5 KS |
168 | break; |
169 | case 'R': | |
8cd2e972 | 170 | udev_device_set_ignore_remove(udev_device, atoi(val)); |
e88a82b5 KS |
171 | break; |
172 | case 'E': | |
8cd2e972 | 173 | udev_device_add_property_from_string(udev_device, val); |
e88a82b5 KS |
174 | break; |
175 | } | |
176 | } | |
177 | fclose(f); | |
178 | ||
cd42b50d | 179 | info(udev_device->udev, "device %p filled with db file data\n", udev_device); |
04f5d75f | 180 | return 0; |
e88a82b5 KS |
181 | } |
182 | ||
bd85566c | 183 | int udev_device_read_uevent_file(struct udev_device *udev_device) |
99214844 KS |
184 | { |
185 | char filename[UTIL_PATH_SIZE]; | |
186 | FILE *f; | |
187 | char line[UTIL_LINE_SIZE]; | |
188 | int maj = 0; | |
189 | int min = 0; | |
190 | ||
191 | util_strlcpy(filename, udev_device->syspath, sizeof(filename)); | |
192 | util_strlcat(filename, "/uevent", sizeof(filename)); | |
193 | f = fopen(filename, "r"); | |
194 | if (f == NULL) | |
195 | return -1; | |
196 | ||
197 | while (fgets(line, sizeof(line), f)) { | |
198 | char *pos; | |
199 | ||
200 | pos = strchr(line, '\n'); | |
201 | if (pos == NULL) | |
202 | continue; | |
203 | pos[0] = '\0'; | |
204 | ||
205 | if (strncmp(line, "MAJOR=", 6) == 0) | |
206 | maj = strtoull(&line[6], NULL, 10); | |
207 | else if (strncmp(line, "MINOR=", 6) == 0) | |
208 | min = strtoull(&line[6], NULL, 10); | |
209 | ||
8cd2e972 | 210 | udev_device_add_property_from_string(udev_device, line); |
99214844 KS |
211 | } |
212 | ||
213 | udev_device->devnum = makedev(maj, min); | |
214 | ||
215 | fclose(f); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static void device_load_info(struct udev_device *device) | |
220 | { | |
517814e7 | 221 | device->info_loaded = 1; |
bd85566c | 222 | udev_device_read_uevent_file(device); |
aa8734ff | 223 | udev_device_read_db(device); |
99214844 KS |
224 | } |
225 | ||
8cd2e972 | 226 | void udev_device_set_info_loaded(struct udev_device *device) |
99214844 KS |
227 | { |
228 | device->info_loaded = 1; | |
229 | } | |
230 | ||
e0083e8e | 231 | struct udev_device *device_new(struct udev *udev) |
eb1f0e66 KS |
232 | { |
233 | struct udev_device *udev_device; | |
ebacd6ec | 234 | struct udev_list_entry *list_entry; |
eb1f0e66 | 235 | |
ba6929f6 KS |
236 | if (udev == NULL) |
237 | return NULL; | |
238 | ||
eb1f0e66 KS |
239 | udev_device = malloc(sizeof(struct udev_device)); |
240 | if (udev_device == NULL) | |
241 | return NULL; | |
242 | memset(udev_device, 0x00, sizeof(struct udev_device)); | |
243 | udev_device->refcount = 1; | |
244 | udev_device->udev = udev; | |
517814e7 | 245 | udev_list_init(&udev_device->devlinks_list); |
8cd2e972 KS |
246 | udev_list_init(&udev_device->properties_list); |
247 | udev_list_init(&udev_device->attr_list); | |
979ff016 | 248 | udev_device->event_timeout = -1; |
ebacd6ec KS |
249 | /* copy global properties */ |
250 | udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev)) | |
251 | udev_device_add_property(udev_device, | |
252 | udev_list_entry_get_name(list_entry), | |
253 | udev_list_entry_get_value(list_entry)); | |
7d563a17 | 254 | info(udev_device->udev, "udev_device: %p created\n", udev_device); |
eb1f0e66 KS |
255 | return udev_device; |
256 | } | |
257 | ||
258 | /** | |
8753fadf | 259 | * udev_device_new_from_syspath: |
eb1f0e66 | 260 | * @udev: udev library context |
8753fadf | 261 | * @syspath: sys device path including sys directory |
eb1f0e66 | 262 | * |
8753fadf KS |
263 | * Create new udev device, and fill in information from the sys |
264 | * device and the udev database entry. The sypath is the absolute | |
265 | * path to the device, including the sys mount point. | |
eb1f0e66 KS |
266 | * |
267 | * The initial refcount is 1, and needs to be decremented to | |
268 | * release the ressources of the udev device. | |
269 | * | |
270 | * Returns: a new udev device, or #NULL, if it does not exist | |
271 | **/ | |
8753fadf | 272 | struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) |
eb1f0e66 | 273 | { |
b95f8a76 KS |
274 | size_t len; |
275 | const char *subdir; | |
3eb46ec6 | 276 | char path[UTIL_PATH_SIZE]; |
62b9dfb6 | 277 | char *pos; |
eb1f0e66 KS |
278 | struct stat statbuf; |
279 | struct udev_device *udev_device; | |
eb1f0e66 | 280 | |
ba6929f6 KS |
281 | if (udev == NULL) |
282 | return NULL; | |
8753fadf | 283 | if (syspath == NULL) |
ba6929f6 KS |
284 | return NULL; |
285 | ||
b95f8a76 KS |
286 | /* path starts in sys */ |
287 | len = strlen(udev_get_sys_path(udev)); | |
288 | if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) { | |
289 | info(udev, "not in sys :%s\n", syspath); | |
eb1f0e66 | 290 | return NULL; |
4ad3a37f | 291 | } |
eb1f0e66 | 292 | |
b95f8a76 KS |
293 | /* path is not a root directory */ |
294 | subdir = &syspath[len+1]; | |
295 | pos = strrchr(subdir, '/'); | |
296 | if (pos == NULL || pos < &subdir[2]) { | |
bc8184ed | 297 | info(udev, "not a subdir :%s\n", syspath); |
eb1f0e66 | 298 | return NULL; |
b95f8a76 | 299 | } |
eb1f0e66 | 300 | |
ba6929f6 | 301 | /* resolve possible symlink to real path */ |
8753fadf | 302 | util_strlcpy(path, syspath, sizeof(path)); |
b21b95d7 | 303 | util_resolve_sys_link(udev, path, sizeof(path)); |
b95f8a76 | 304 | |
62b9dfb6 KS |
305 | /* try to resolve the silly block layout if needed */ |
306 | if (strncmp(&path[len], "/block/", 7) == 0) { | |
307 | char block[UTIL_PATH_SIZE]; | |
308 | char part[UTIL_PATH_SIZE]; | |
309 | ||
310 | util_strlcpy(block, path, sizeof(block)); | |
311 | pos = strrchr(block, '/'); | |
312 | if (pos == NULL) | |
313 | return NULL; | |
314 | util_strlcpy(part, pos, sizeof(part)); | |
315 | pos[0] = '\0'; | |
316 | if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) { | |
317 | util_strlcpy(path, block, sizeof(path)); | |
318 | util_strlcat(path, part, sizeof(path)); | |
319 | } | |
320 | } | |
321 | ||
b95f8a76 KS |
322 | /* path exists in sys */ |
323 | if (strncmp(&syspath[len], "/devices/", 9) == 0 || | |
324 | strncmp(&syspath[len], "/class/", 7) == 0 || | |
325 | strncmp(&syspath[len], "/block/", 7) == 0) { | |
326 | char file[UTIL_PATH_SIZE]; | |
327 | ||
328 | /* all "devices" require a "uevent" file */ | |
329 | util_strlcpy(file, path, sizeof(file)); | |
330 | util_strlcat(file, "/uevent", sizeof(file)); | |
331 | if (stat(file, &statbuf) != 0) { | |
332 | info(udev, "not a device: %s\n", syspath); | |
333 | return NULL; | |
334 | } | |
335 | } else { | |
336 | /* everything else just needs to be a directory */ | |
337 | if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) { | |
338 | info(udev, "directory not found: %s\n", syspath); | |
339 | return NULL; | |
340 | } | |
341 | } | |
342 | ||
e0083e8e | 343 | udev_device = device_new(udev); |
b95f8a76 KS |
344 | if (udev_device == NULL) |
345 | return NULL; | |
346 | ||
8cd2e972 | 347 | udev_device_set_syspath(udev_device, path); |
7d563a17 | 348 | info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device)); |
eb1f0e66 | 349 | |
eb1f0e66 KS |
350 | return udev_device; |
351 | } | |
352 | ||
4c9dff47 KS |
353 | struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum) |
354 | { | |
355 | char path[UTIL_PATH_SIZE]; | |
03198b93 | 356 | const char *type_str; |
438d4c3c | 357 | struct udev_enumerate *udev_enumerate; |
0de33a61 | 358 | struct udev_list_entry *list_entry; |
bf7ad0ea | 359 | struct udev_device *device = NULL; |
03198b93 KS |
360 | |
361 | if (type == 'b') | |
362 | type_str = "block"; | |
363 | else if (type == 'c') | |
364 | type_str = "char"; | |
365 | else | |
366 | return NULL; | |
4c9dff47 | 367 | |
b95f8a76 | 368 | /* /sys/dev/{block,char}/<maj>:<min> link */ |
03198b93 KS |
369 | snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev), |
370 | type_str, major(devnum), minor(devnum)); | |
bf7ad0ea KS |
371 | if (util_resolve_sys_link(udev, path, sizeof(path)) == 0) |
372 | return udev_device_new_from_syspath(udev, path); | |
4c9dff47 | 373 | |
438d4c3c KS |
374 | udev_enumerate = udev_enumerate_new(udev); |
375 | if (udev_enumerate == NULL) | |
376 | return NULL; | |
377 | ||
bc8184ed KS |
378 | /* fallback to search sys devices for the major/minor */ |
379 | if (type == 'b') | |
c97f839e | 380 | udev_enumerate_add_match_subsystem(udev_enumerate, "block"); |
bc8184ed | 381 | else if (type == 'c') |
c97f839e KS |
382 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); |
383 | udev_enumerate_scan_devices(udev_enumerate); | |
438d4c3c | 384 | udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { |
bf7ad0ea KS |
385 | struct udev_device *device_loop; |
386 | ||
0de33a61 | 387 | device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry)); |
bf7ad0ea KS |
388 | if (device_loop != NULL) { |
389 | if (udev_device_get_devnum(device_loop) == devnum) { | |
c97f839e | 390 | if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0) |
bc8184ed | 391 | continue; |
c97f839e | 392 | if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0) |
bc8184ed | 393 | continue; |
bf7ad0ea KS |
394 | device = device_loop; |
395 | break; | |
396 | } | |
397 | udev_device_unref(device_loop); | |
398 | } | |
bf7ad0ea | 399 | } |
438d4c3c | 400 | udev_enumerate_unref(udev_enumerate); |
bf7ad0ea | 401 | return device; |
4c9dff47 KS |
402 | } |
403 | ||
90d80c2e KS |
404 | struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname) |
405 | { | |
406 | size_t sys_path_len; | |
407 | char path_full[UTIL_PATH_SIZE]; | |
408 | char *path; | |
409 | struct stat statbuf; | |
410 | ||
411 | sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full)); | |
412 | path = &path_full[sys_path_len]; | |
413 | ||
414 | if (strcmp(subsystem, "subsystem") == 0) { | |
415 | util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len); | |
416 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
417 | if (stat(path_full, &statbuf) == 0) | |
418 | goto found; | |
419 | ||
420 | util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len); | |
421 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
422 | if (stat(path_full, &statbuf) == 0) | |
423 | goto found; | |
424 | ||
425 | util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len); | |
426 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
427 | if (stat(path_full, &statbuf) == 0) | |
428 | goto found; | |
429 | goto out; | |
430 | } | |
431 | ||
432 | if (strcmp(subsystem, "module") == 0) { | |
433 | util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len); | |
434 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
435 | if (stat(path_full, &statbuf) == 0) | |
436 | goto found; | |
437 | goto out; | |
438 | } | |
439 | ||
440 | if (strcmp(subsystem, "drivers") == 0) { | |
441 | char subsys[UTIL_NAME_SIZE]; | |
442 | char *driver; | |
443 | ||
444 | util_strlcpy(subsys, sysname, sizeof(subsys)); | |
445 | driver = strchr(subsys, ':'); | |
446 | if (driver != NULL) { | |
447 | driver[0] = '\0'; | |
448 | driver = &driver[1]; | |
449 | util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len); | |
450 | util_strlcat(path, subsys, sizeof(path_full) - sys_path_len); | |
451 | util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len); | |
452 | util_strlcat(path, driver, sizeof(path_full) - sys_path_len); | |
453 | if (stat(path_full, &statbuf) == 0) | |
454 | goto found; | |
455 | ||
456 | util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len); | |
457 | util_strlcat(path, subsys, sizeof(path_full) - sys_path_len); | |
458 | util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len); | |
459 | util_strlcat(path, driver, sizeof(path_full) - sys_path_len); | |
460 | if (stat(path_full, &statbuf) == 0) | |
461 | goto found; | |
462 | } | |
463 | goto out; | |
464 | } | |
465 | ||
466 | util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len); | |
467 | util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len); | |
468 | util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len); | |
469 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
470 | if (stat(path_full, &statbuf) == 0) | |
471 | goto found; | |
472 | ||
473 | util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len); | |
474 | util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len); | |
475 | util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len); | |
476 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
477 | if (stat(path_full, &statbuf) == 0) | |
478 | goto found; | |
479 | ||
480 | util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len); | |
481 | util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len); | |
482 | util_strlcat(path, "/", sizeof(path_full) - sys_path_len); | |
483 | util_strlcat(path, sysname, sizeof(path_full) - sys_path_len); | |
484 | if (stat(path_full, &statbuf) == 0) | |
485 | goto found; | |
486 | out: | |
487 | return NULL; | |
488 | found: | |
489 | return udev_device_new_from_syspath(udev, path_full); | |
490 | } | |
491 | ||
b2d9e4f2 | 492 | static struct udev_device *device_new_from_parent(struct udev_device *udev_device) |
4ad3a37f KS |
493 | { |
494 | struct udev_device *udev_device_parent = NULL; | |
495 | char path[UTIL_PATH_SIZE]; | |
b95f8a76 | 496 | const char *subdir; |
4ad3a37f | 497 | |
b95f8a76 KS |
498 | /* follow "device" link in deprecated sys layout */ |
499 | if (strncmp(udev_device->devpath, "/class/", 7) == 0 || | |
500 | strncmp(udev_device->devpath, "/block/", 7) == 0) { | |
501 | util_strlcpy(path, udev_device->syspath, sizeof(path)); | |
502 | util_strlcat(path, "/device", sizeof(path)); | |
503 | if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) | |
504 | udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); | |
505 | return udev_device_parent; | |
506 | } | |
4ad3a37f | 507 | |
8753fadf | 508 | util_strlcpy(path, udev_device->syspath, sizeof(path)); |
b95f8a76 | 509 | subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1]; |
4ad3a37f | 510 | while (1) { |
b95f8a76 KS |
511 | char *pos; |
512 | ||
513 | pos = strrchr(subdir, '/'); | |
514 | if (pos == NULL || pos < &subdir[2]) | |
4ad3a37f KS |
515 | break; |
516 | pos[0] = '\0'; | |
8753fadf | 517 | udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path); |
4ad3a37f | 518 | if (udev_device_parent != NULL) |
0518da3b KS |
519 | return udev_device_parent; |
520 | } | |
0518da3b | 521 | return NULL; |
4ad3a37f KS |
522 | } |
523 | ||
b2d9e4f2 KS |
524 | struct udev_device *udev_device_get_parent(struct udev_device *udev_device) |
525 | { | |
b95f8a76 KS |
526 | if (udev_device == NULL) |
527 | return NULL; | |
31f4b036 KS |
528 | if (!udev_device->parent_set) { |
529 | udev_device->parent_set = 1; | |
530 | udev_device->parent_device = device_new_from_parent(udev_device); | |
0518da3b | 531 | } |
31f4b036 KS |
532 | if (udev_device->parent_device != NULL) |
533 | info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device); | |
b2d9e4f2 KS |
534 | return udev_device->parent_device; |
535 | } | |
536 | ||
98f10a9e KS |
537 | struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem) |
538 | { | |
539 | struct udev_device *parent; | |
540 | ||
541 | parent = udev_device_get_parent(udev_device); | |
542 | while (parent != NULL) { | |
543 | const char *parent_subsystem; | |
544 | ||
545 | parent_subsystem = udev_device_get_subsystem(parent); | |
546 | if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) | |
547 | break; | |
548 | parent = udev_device_get_parent(parent); | |
549 | } | |
550 | return parent; | |
551 | } | |
552 | ||
eb1f0e66 KS |
553 | /** |
554 | * udev_device_get_udev: | |
7d8787b3 | 555 | * @udev_device: udev device |
eb1f0e66 KS |
556 | * |
557 | * Retrieve the udev library context the device was created with. | |
558 | * | |
559 | * Returns: the udev library context | |
560 | **/ | |
561 | struct udev *udev_device_get_udev(struct udev_device *udev_device) | |
562 | { | |
ba6929f6 KS |
563 | if (udev_device == NULL) |
564 | return NULL; | |
eb1f0e66 KS |
565 | return udev_device->udev; |
566 | } | |
567 | ||
568 | /** | |
569 | * udev_device_ref: | |
570 | * @udev_device: udev device | |
571 | * | |
572 | * Take a reference of a udev device. | |
573 | * | |
574 | * Returns: the passed udev device | |
575 | **/ | |
576 | struct udev_device *udev_device_ref(struct udev_device *udev_device) | |
577 | { | |
ba6929f6 KS |
578 | if (udev_device == NULL) |
579 | return NULL; | |
eb1f0e66 KS |
580 | udev_device->refcount++; |
581 | return udev_device; | |
582 | } | |
583 | ||
584 | /** | |
585 | * udev_device_unref: | |
586 | * @udev_device: udev device | |
587 | * | |
588 | * Drop a reference of a udev device. If the refcount reaches zero, | |
589 | * the ressources of the device will be released. | |
590 | * | |
591 | **/ | |
592 | void udev_device_unref(struct udev_device *udev_device) | |
593 | { | |
979ff016 KS |
594 | unsigned int i; |
595 | ||
ba6929f6 KS |
596 | if (udev_device == NULL) |
597 | return; | |
eb1f0e66 KS |
598 | udev_device->refcount--; |
599 | if (udev_device->refcount > 0) | |
600 | return; | |
b2d9e4f2 KS |
601 | if (udev_device->parent_device != NULL) |
602 | udev_device_unref(udev_device->parent_device); | |
11d543c1 | 603 | free(udev_device->syspath); |
517814e7 | 604 | free(udev_device->sysname); |
99214844 | 605 | free(udev_device->devnode); |
ba6929f6 | 606 | free(udev_device->subsystem); |
517814e7 | 607 | udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list); |
8cd2e972 | 608 | udev_list_cleanup(udev_device->udev, &udev_device->properties_list); |
1c7047ea KS |
609 | free(udev_device->action); |
610 | free(udev_device->driver); | |
611 | free(udev_device->devpath_old); | |
612 | free(udev_device->physdevpath); | |
8cd2e972 | 613 | udev_list_cleanup(udev_device->udev, &udev_device->attr_list); |
979ff016 KS |
614 | for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++) |
615 | free(udev_device->envp[i]); | |
7d563a17 | 616 | info(udev_device->udev, "udev_device: %p released\n", udev_device); |
eb1f0e66 KS |
617 | free(udev_device); |
618 | } | |
619 | ||
620 | /** | |
621 | * udev_device_get_devpath: | |
622 | * @udev_device: udev device | |
623 | * | |
11d543c1 KS |
624 | * Retrieve the kernel devpath value of the udev device. The path |
625 | * does not contain the sys mount point, and starts with a '/'. | |
eb1f0e66 | 626 | * |
11d543c1 | 627 | * Returns: the devpath of the udev device |
eb1f0e66 KS |
628 | **/ |
629 | const char *udev_device_get_devpath(struct udev_device *udev_device) | |
630 | { | |
ba6929f6 KS |
631 | if (udev_device == NULL) |
632 | return NULL; | |
633 | return udev_device->devpath; | |
eb1f0e66 KS |
634 | } |
635 | ||
11d543c1 KS |
636 | /** |
637 | * udev_device_get_syspath: | |
638 | * @udev_device: udev device | |
639 | * | |
640 | * Retrieve the sys path of the udev device. The path is an | |
641 | * absolute path and starts with the sys mount point. | |
642 | * | |
643 | * Returns: the sys path of the udev device | |
644 | **/ | |
645 | const char *udev_device_get_syspath(struct udev_device *udev_device) | |
646 | { | |
647 | if (udev_device == NULL) | |
648 | return NULL; | |
649 | return udev_device->syspath; | |
650 | } | |
651 | ||
4ad3a37f KS |
652 | const char *udev_device_get_sysname(struct udev_device *udev_device) |
653 | { | |
654 | if (udev_device == NULL) | |
655 | return NULL; | |
656 | return udev_device->sysname; | |
657 | } | |
658 | ||
517814e7 KS |
659 | const char *udev_device_get_sysnum(struct udev_device *udev_device) |
660 | { | |
661 | if (udev_device == NULL) | |
662 | return NULL; | |
663 | return udev_device->sysnum; | |
664 | } | |
665 | ||
eb1f0e66 | 666 | /** |
fb762bb9 | 667 | * udev_device_get_devnode: |
eb1f0e66 KS |
668 | * @udev_device: udev device |
669 | * | |
670 | * Retrieve the device node file name belonging to the udev device. | |
ba6929f6 | 671 | * The path is an absolute path, and starts with the device directory. |
eb1f0e66 KS |
672 | * |
673 | * Returns: the device node file name of the udev device, or #NULL if no device node exists | |
674 | **/ | |
fb762bb9 | 675 | const char *udev_device_get_devnode(struct udev_device *udev_device) |
eb1f0e66 | 676 | { |
ba6929f6 | 677 | if (udev_device == NULL) |
eb1f0e66 | 678 | return NULL; |
99214844 KS |
679 | if (!udev_device->info_loaded) |
680 | device_load_info(udev_device); | |
681 | return udev_device->devnode; | |
eb1f0e66 KS |
682 | } |
683 | ||
684 | /** | |
685 | * udev_device_get_subsystem: | |
686 | * @udev_device: udev device | |
687 | * | |
688 | * Retrieve the subsystem string of the udev device. The string does not | |
689 | * contain any "/". | |
690 | * | |
691 | * Returns: the subsystem name of the udev device, or #NULL if it can not be determined | |
692 | **/ | |
693 | const char *udev_device_get_subsystem(struct udev_device *udev_device) | |
694 | { | |
17fcfb59 | 695 | char subsystem[UTIL_NAME_SIZE]; |
ba6929f6 KS |
696 | |
697 | if (udev_device == NULL) | |
698 | return NULL; | |
5c5cad79 KS |
699 | if (!udev_device->subsystem_set) { |
700 | udev_device->subsystem_set = 1; | |
701 | /* read "subsytem" link */ | |
702 | if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) { | |
703 | udev_device_set_subsystem(udev_device, subsystem); | |
704 | return udev_device->subsystem; | |
705 | } | |
706 | /* implicit names */ | |
707 | if (strncmp(udev_device->devpath, "/module/", 8) == 0) { | |
708 | udev_device_set_subsystem(udev_device, "module"); | |
709 | return udev_device->subsystem; | |
710 | } | |
711 | if (strstr(udev_device->devpath, "/drivers/") != NULL) { | |
712 | udev_device_set_subsystem(udev_device, "drivers"); | |
713 | return udev_device->subsystem; | |
714 | } | |
715 | if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 || | |
716 | strncmp(udev_device->devpath, "/class/", 7) == 0 || | |
717 | strncmp(udev_device->devpath, "/bus/", 5) == 0) { | |
718 | udev_device_set_subsystem(udev_device, "subsystem"); | |
719 | return udev_device->subsystem; | |
720 | } | |
0518da3b | 721 | } |
5c5cad79 | 722 | return udev_device->subsystem; |
eb1f0e66 KS |
723 | } |
724 | ||
725 | /** | |
0de33a61 | 726 | * udev_device_get_devlinks_list_entry: |
eb1f0e66 | 727 | * @udev_device: udev device |
eb1f0e66 | 728 | * |
bf7ad0ea KS |
729 | * Retrieve the list of device links pointing to the device file of |
730 | * the udev device. The next list entry can be retrieved with | |
e345e267 | 731 | * udev_list_entry_next(), which returns #NULL if no more entries exist. |
bf7ad0ea | 732 | * The devlink path can be retrieved from the list entry by |
e345e267 | 733 | * udev_list_entry_get_name(). The path is an absolute path, and starts with |
bf7ad0ea | 734 | * the device directory. |
eb1f0e66 | 735 | * |
bf7ad0ea | 736 | * Returns: the first entry of the device node link list |
eb1f0e66 | 737 | **/ |
0de33a61 | 738 | struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 739 | { |
99214844 KS |
740 | if (udev_device == NULL) |
741 | return NULL; | |
742 | if (!udev_device->info_loaded) | |
743 | device_load_info(udev_device); | |
517814e7 | 744 | return udev_list_get_entry(&udev_device->devlinks_list); |
eb1f0e66 KS |
745 | } |
746 | ||
979ff016 KS |
747 | void udev_device_cleanup_devlinks_list(struct udev_device *udev_device) |
748 | { | |
bd85566c | 749 | udev_device->devlinks_uptodate = 0; |
979ff016 KS |
750 | udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list); |
751 | } | |
752 | ||
eb1f0e66 | 753 | /** |
0de33a61 | 754 | * udev_device_get_properties_list_entry: |
eb1f0e66 | 755 | * @udev_device: udev device |
eb1f0e66 | 756 | * |
bf7ad0ea | 757 | * Retrieve the list of key/value device properties of the udev |
e345e267 | 758 | * device. The next list entry can be retrieved with udev_list_entry_next(), |
bf7ad0ea KS |
759 | * which returns #NULL if no more entries exist. The property name |
760 | * can be retrieved from the list entry by udev_list_get_name(), | |
761 | * the property value by udev_list_get_value(). | |
eb1f0e66 | 762 | * |
bf7ad0ea | 763 | * Returns: the first entry of the property list |
eb1f0e66 | 764 | **/ |
0de33a61 | 765 | struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device) |
eb1f0e66 | 766 | { |
99214844 KS |
767 | if (udev_device == NULL) |
768 | return NULL; | |
769 | if (!udev_device->info_loaded) | |
770 | device_load_info(udev_device); | |
bd85566c KS |
771 | if (!udev_device->devlinks_uptodate) { |
772 | char symlinks[UTIL_PATH_SIZE]; | |
773 | struct udev_list_entry *list_entry; | |
774 | ||
775 | udev_device->devlinks_uptodate = 1; | |
776 | list_entry = udev_device_get_devlinks_list_entry(udev_device); | |
777 | if (list_entry != NULL) { | |
778 | util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks)); | |
779 | udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) { | |
780 | util_strlcat(symlinks, " ", sizeof(symlinks)); | |
781 | util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks)); | |
782 | } | |
783 | udev_device_add_property(udev_device, "DEVLINKS", symlinks); | |
784 | } | |
785 | } | |
8cd2e972 | 786 | return udev_list_get_entry(&udev_device->properties_list); |
eb1f0e66 | 787 | } |
11d543c1 | 788 | |
c4f5f942 KS |
789 | const char *udev_device_get_driver(struct udev_device *udev_device) |
790 | { | |
17fcfb59 | 791 | char driver[UTIL_NAME_SIZE]; |
95d90c4f | 792 | |
c4f5f942 KS |
793 | if (udev_device == NULL) |
794 | return NULL; | |
5c5cad79 KS |
795 | if (!udev_device->driver_set) { |
796 | udev_device->driver_set = 1; | |
797 | if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0) | |
798 | udev_device->driver = strdup(driver); | |
799 | } | |
c4f5f942 KS |
800 | return udev_device->driver; |
801 | } | |
802 | ||
803 | dev_t udev_device_get_devnum(struct udev_device *udev_device) | |
804 | { | |
805 | if (udev_device == NULL) | |
806 | return makedev(0, 0); | |
99214844 KS |
807 | if (!udev_device->info_loaded) |
808 | device_load_info(udev_device); | |
c4f5f942 KS |
809 | return udev_device->devnum; |
810 | } | |
811 | ||
812 | const char *udev_device_get_action(struct udev_device *udev_device) | |
813 | { | |
814 | if (udev_device == NULL) | |
815 | return NULL; | |
816 | return udev_device->action; | |
817 | } | |
818 | ||
37372bbc KS |
819 | unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) |
820 | { | |
821 | if (udev_device == NULL) | |
822 | return 0; | |
823 | return udev_device->seqnum; | |
824 | } | |
825 | ||
93b0f384 KS |
826 | const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr) |
827 | { | |
0de33a61 | 828 | struct udev_list_entry *list_entry; |
93b0f384 KS |
829 | char path[UTIL_PATH_SIZE]; |
830 | char value[UTIL_NAME_SIZE]; | |
831 | struct stat statbuf; | |
832 | int fd; | |
833 | ssize_t size; | |
834 | const char *val = NULL; | |
835 | ||
517814e7 KS |
836 | if (udev_device == NULL) |
837 | return NULL; | |
838 | if (attr == NULL) | |
839 | return NULL; | |
840 | ||
0518da3b | 841 | /* look for possibly already cached result */ |
8cd2e972 | 842 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) { |
0de33a61 | 843 | if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) { |
517814e7 KS |
844 | info(udev_device->udev, "got '%s' (%s) from cache\n", |
845 | attr, udev_list_entry_get_value(list_entry)); | |
0de33a61 | 846 | return udev_list_entry_get_value(list_entry); |
0518da3b KS |
847 | } |
848 | } | |
849 | ||
93b0f384 KS |
850 | util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path)); |
851 | util_strlcat(path, "/", sizeof(path)); | |
852 | util_strlcat(path, attr, sizeof(path)); | |
853 | ||
854 | if (lstat(path, &statbuf) != 0) { | |
5c5cad79 KS |
855 | info(udev_device->udev, "no attribute '%s', keep negative entry\n", path); |
856 | udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, NULL, 0, 0); | |
93b0f384 KS |
857 | goto out; |
858 | } | |
859 | ||
860 | if (S_ISLNK(statbuf.st_mode)) { | |
861 | /* links return the last element of the target path */ | |
862 | char target[UTIL_NAME_SIZE]; | |
863 | int len; | |
864 | char *pos; | |
865 | ||
866 | len = readlink(path, target, sizeof(target)); | |
867 | if (len > 0) { | |
868 | target[len] = '\0'; | |
869 | pos = strrchr(target, '/'); | |
870 | if (pos != NULL) { | |
871 | pos = &pos[1]; | |
872 | info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos); | |
8cd2e972 | 873 | list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0); |
0de33a61 | 874 | val = udev_list_entry_get_value(list_entry); |
93b0f384 KS |
875 | } |
876 | } | |
877 | goto out; | |
878 | } | |
879 | ||
880 | /* skip directories */ | |
881 | if (S_ISDIR(statbuf.st_mode)) | |
882 | goto out; | |
883 | ||
884 | /* skip non-readable files */ | |
885 | if ((statbuf.st_mode & S_IRUSR) == 0) | |
886 | goto out; | |
887 | ||
888 | /* read attribute value */ | |
889 | fd = open(path, O_RDONLY); | |
890 | if (fd < 0) { | |
891 | info(udev_device->udev, "attribute '%s' can not be opened\n", path); | |
892 | goto out; | |
893 | } | |
894 | size = read(fd, value, sizeof(value)); | |
895 | close(fd); | |
896 | if (size < 0) | |
897 | goto out; | |
898 | if (size == sizeof(value)) | |
899 | goto out; | |
900 | ||
0518da3b | 901 | /* got a valid value, store it in cache and return it */ |
93b0f384 KS |
902 | value[size] = '\0'; |
903 | util_remove_trailing_chars(value, '\n'); | |
904 | info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value); | |
8cd2e972 | 905 | list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0); |
0de33a61 | 906 | val = udev_list_entry_get_value(list_entry); |
93b0f384 KS |
907 | out: |
908 | return val; | |
909 | } | |
517814e7 | 910 | |
8cd2e972 | 911 | int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath) |
11d543c1 | 912 | { |
8753fadf | 913 | const char *pos; |
517814e7 | 914 | size_t len; |
8753fadf | 915 | |
8cd2e972 | 916 | free(udev_device->syspath); |
8753fadf KS |
917 | udev_device->syspath = strdup(syspath); |
918 | if (udev_device->syspath == NULL) | |
11d543c1 KS |
919 | return -ENOMEM; |
920 | udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))]; | |
9a997ecf | 921 | udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath); |
517814e7 | 922 | |
8753fadf KS |
923 | pos = strrchr(udev_device->syspath, '/'); |
924 | if (pos == NULL) | |
925 | return -EINVAL; | |
517814e7 KS |
926 | udev_device->sysname = strdup(&pos[1]); |
927 | if (udev_device->sysname == NULL) | |
928 | return -ENOMEM; | |
929 | ||
930 | /* some devices have '!' in their name, change that to '/' */ | |
931 | len = 0; | |
932 | while (udev_device->sysname[len] != '\0') { | |
933 | if (udev_device->sysname[len] == '!') | |
934 | udev_device->sysname[len] = '/'; | |
935 | len++; | |
936 | } | |
937 | ||
938 | /* trailing number */ | |
939 | while (isdigit(udev_device->sysname[--len])) | |
940 | udev_device->sysnum = &udev_device->sysname[len]; | |
11d543c1 KS |
941 | return 0; |
942 | } | |
943 | ||
8cd2e972 | 944 | int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem) |
11d543c1 | 945 | { |
979ff016 | 946 | free(udev_device->subsystem); |
11d543c1 KS |
947 | udev_device->subsystem = strdup(subsystem); |
948 | if (udev_device->subsystem == NULL) | |
9a997ecf | 949 | return -ENOMEM; |
5c5cad79 | 950 | udev_device->subsystem_set = 1; |
9a997ecf | 951 | udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem); |
11d543c1 KS |
952 | return 0; |
953 | } | |
954 | ||
8cd2e972 | 955 | int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode) |
11d543c1 | 956 | { |
517814e7 | 957 | free(udev_device->devnode); |
99214844 | 958 | udev_device->devnode = strdup(devnode); |
bd85566c KS |
959 | if (devnode == NULL) |
960 | return 0; | |
99214844 | 961 | if (udev_device->devnode == NULL) |
11d543c1 | 962 | return -ENOMEM; |
979ff016 | 963 | udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode); |
11d543c1 KS |
964 | return 0; |
965 | } | |
966 | ||
8cd2e972 | 967 | int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink) |
11d543c1 | 968 | { |
bd85566c | 969 | udev_device->devlinks_uptodate = 0; |
517814e7 | 970 | if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL) |
11d543c1 KS |
971 | return -ENOMEM; |
972 | return 0; | |
973 | } | |
974 | ||
517814e7 | 975 | struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value) |
11d543c1 | 976 | { |
979ff016 | 977 | udev_device->envp_uptodate = 0; |
ebacd6ec KS |
978 | if (value == NULL) { |
979 | struct udev_list_entry *list_entry; | |
980 | ||
981 | list_entry = udev_device_get_properties_list_entry(udev_device); | |
982 | list_entry = udev_list_entry_get_by_name(list_entry, key); | |
983 | if (list_entry != NULL) | |
984 | udev_list_entry_remove(list_entry); | |
985 | return NULL; | |
986 | } | |
517814e7 | 987 | return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0); |
11d543c1 | 988 | } |
c4f5f942 | 989 | |
517814e7 | 990 | struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property) |
0518da3b KS |
991 | { |
992 | char name[UTIL_PATH_SIZE]; | |
993 | char *val; | |
994 | ||
995 | strncpy(name, property, sizeof(name)); | |
996 | val = strchr(name, '='); | |
997 | if (val == NULL) | |
517814e7 | 998 | return NULL; |
0518da3b KS |
999 | val[0] = '\0'; |
1000 | val = &val[1]; | |
1001 | if (val[0] == '\0') | |
1002 | val = NULL; | |
517814e7 | 1003 | return udev_device_add_property(udev_device, name, val); |
0518da3b KS |
1004 | } |
1005 | ||
979ff016 KS |
1006 | char **udev_device_get_properties_envp(struct udev_device *udev_device) |
1007 | { | |
1008 | if (!udev_device->envp_uptodate) { | |
1009 | unsigned int i; | |
1010 | struct udev_list_entry *list_entry; | |
1011 | ||
1012 | for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++) | |
1013 | free(udev_device->envp[i]); | |
1014 | i = 0; | |
1015 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { | |
1016 | asprintf(&udev_device->envp[i++], "%s=%s", | |
1017 | udev_list_entry_get_name(list_entry), | |
1018 | udev_list_entry_get_value(list_entry)); | |
1019 | if (i+1 >= ARRAY_SIZE(udev_device->envp)) | |
1020 | break; | |
1021 | } | |
1022 | udev_device->envp[i] = NULL; | |
1023 | info(udev_device->udev, "constructed envp from %u properties\n", i); | |
1024 | udev_device->envp_uptodate = 1; | |
1025 | } | |
1026 | return udev_device->envp; | |
1027 | } | |
1028 | ||
8cd2e972 | 1029 | int udev_device_set_action(struct udev_device *udev_device, const char *action) |
c4f5f942 | 1030 | { |
517814e7 | 1031 | free(udev_device->action); |
c4f5f942 KS |
1032 | udev_device->action = strdup(action); |
1033 | if (udev_device->action == NULL) | |
1034 | return -ENOMEM; | |
31f4b036 | 1035 | udev_device_add_property(udev_device, "ACTION", udev_device->action); |
c4f5f942 KS |
1036 | return 0; |
1037 | } | |
1038 | ||
8cd2e972 | 1039 | int udev_device_set_driver(struct udev_device *udev_device, const char *driver) |
c4f5f942 | 1040 | { |
979ff016 | 1041 | free(udev_device->driver); |
c4f5f942 KS |
1042 | udev_device->driver = strdup(driver); |
1043 | if (udev_device->driver == NULL) | |
1044 | return -ENOMEM; | |
5c5cad79 | 1045 | udev_device->driver_set = 1; |
31f4b036 | 1046 | udev_device_add_property(udev_device, "DRIVER", udev_device->driver); |
c4f5f942 KS |
1047 | return 0; |
1048 | } | |
1049 | ||
8cd2e972 | 1050 | const char *udev_device_get_devpath_old(struct udev_device *udev_device) |
c4f5f942 | 1051 | { |
c4f5f942 KS |
1052 | return udev_device->devpath_old; |
1053 | } | |
1054 | ||
8cd2e972 | 1055 | int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old) |
c4f5f942 KS |
1056 | { |
1057 | udev_device->devpath_old = strdup(devpath_old); | |
1058 | if (udev_device->devpath_old == NULL) | |
1059 | return -ENOMEM; | |
31f4b036 | 1060 | udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old); |
c4f5f942 KS |
1061 | return 0; |
1062 | } | |
1063 | ||
8cd2e972 | 1064 | const char *udev_device_get_physdevpath(struct udev_device *udev_device) |
c4f5f942 | 1065 | { |
c4f5f942 KS |
1066 | return udev_device->physdevpath; |
1067 | } | |
1068 | ||
8cd2e972 | 1069 | int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath) |
c4f5f942 KS |
1070 | { |
1071 | udev_device->physdevpath = strdup(physdevpath); | |
1072 | if (udev_device->physdevpath == NULL) | |
1073 | return -ENOMEM; | |
1074 | return 0; | |
1075 | } | |
1076 | ||
8cd2e972 | 1077 | int udev_device_get_timeout(struct udev_device *udev_device) |
c4f5f942 | 1078 | { |
c4f5f942 KS |
1079 | return udev_device->timeout; |
1080 | } | |
1081 | ||
8cd2e972 | 1082 | int udev_device_set_timeout(struct udev_device *udev_device, int timeout) |
c4f5f942 KS |
1083 | { |
1084 | udev_device->timeout = timeout; | |
1085 | return 0; | |
1086 | } | |
8cd2e972 | 1087 | int udev_device_get_event_timeout(struct udev_device *udev_device) |
99214844 KS |
1088 | { |
1089 | if (!udev_device->info_loaded) | |
1090 | device_load_info(udev_device); | |
1091 | return udev_device->event_timeout; | |
1092 | } | |
1093 | ||
8cd2e972 | 1094 | int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout) |
99214844 KS |
1095 | { |
1096 | udev_device->event_timeout = event_timeout; | |
1097 | return 0; | |
1098 | } | |
c4f5f942 | 1099 | |
8cd2e972 | 1100 | int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum) |
37372bbc | 1101 | { |
31f4b036 KS |
1102 | char num[32]; |
1103 | ||
37372bbc | 1104 | udev_device->seqnum = seqnum; |
31f4b036 KS |
1105 | snprintf(num, sizeof(num), "%llu", seqnum); |
1106 | udev_device_add_property(udev_device, "SEQNUM", num); | |
37372bbc KS |
1107 | return 0; |
1108 | } | |
1109 | ||
8cd2e972 | 1110 | int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum) |
c4f5f942 | 1111 | { |
31f4b036 KS |
1112 | char num[32]; |
1113 | ||
c4f5f942 | 1114 | udev_device->devnum = devnum; |
31f4b036 KS |
1115 | |
1116 | snprintf(num, sizeof(num), "%u", major(devnum)); | |
1117 | udev_device_add_property(udev_device, "MAJOR", num); | |
1118 | snprintf(num, sizeof(num), "%u", minor(devnum)); | |
1119 | udev_device_add_property(udev_device, "MINOR", num); | |
c4f5f942 KS |
1120 | return 0; |
1121 | } | |
6bd1c78a | 1122 | |
8cd2e972 | 1123 | int udev_device_get_num_fake_partitions(struct udev_device *udev_device) |
6bd1c78a | 1124 | { |
99214844 KS |
1125 | if (!udev_device->info_loaded) |
1126 | device_load_info(udev_device); | |
6bd1c78a KS |
1127 | return udev_device->num_fake_partitions; |
1128 | } | |
1129 | ||
8cd2e972 | 1130 | int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num) |
6bd1c78a KS |
1131 | { |
1132 | udev_device->num_fake_partitions = num; | |
e88a82b5 | 1133 | return 0; |
6bd1c78a KS |
1134 | } |
1135 | ||
8cd2e972 | 1136 | int udev_device_get_devlink_priority(struct udev_device *udev_device) |
6bd1c78a | 1137 | { |
99214844 KS |
1138 | if (!udev_device->info_loaded) |
1139 | device_load_info(udev_device); | |
e88a82b5 | 1140 | return udev_device->devlink_priority; |
6bd1c78a KS |
1141 | } |
1142 | ||
8cd2e972 | 1143 | int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio) |
6bd1c78a | 1144 | { |
e88a82b5 KS |
1145 | udev_device->devlink_priority = prio; |
1146 | return 0; | |
6bd1c78a KS |
1147 | } |
1148 | ||
8cd2e972 | 1149 | int udev_device_get_ignore_remove(struct udev_device *udev_device) |
6bd1c78a | 1150 | { |
99214844 KS |
1151 | if (!udev_device->info_loaded) |
1152 | device_load_info(udev_device); | |
6bd1c78a KS |
1153 | return udev_device->ignore_remove; |
1154 | } | |
1155 | ||
8cd2e972 | 1156 | int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore) |
6bd1c78a | 1157 | { |
e88a82b5 KS |
1158 | udev_device->ignore_remove = ignore; |
1159 | return 0; | |
6bd1c78a | 1160 | } |