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