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