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