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