]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-device.c
treewide: use log_*_errno whenever %m is in the format string
[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
572016d1 164int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
905555dc 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) {
56f64d95 538 log_debug_errno(errno, "no db file to read %s: %m", 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
ff49bc32 580 log_debug("device %p filled with db file data", 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 644 struct udev_device *udev_device;
912541b0 645
aa0e72ea
TG
646 if (udev == NULL) {
647 errno = EINVAL;
912541b0 648 return NULL;
aa0e72ea 649 }
912541b0 650
955d98c9 651 udev_device = new0(struct udev_device, 1);
aa0e72ea
TG
652 if (udev_device == NULL) {
653 errno = ENOMEM;
912541b0 654 return NULL;
aa0e72ea 655 }
912541b0
KS
656 udev_device->refcount = 1;
657 udev_device->udev = udev;
658 udev_list_init(udev, &udev_device->devlinks_list, true);
659 udev_list_init(udev, &udev_device->properties_list, true);
660 udev_list_init(udev, &udev_device->sysattr_value_list, true);
661 udev_list_init(udev, &udev_device->sysattr_list, false);
662 udev_list_init(udev, &udev_device->tags_list, true);
663 udev_device->watch_handle = -1;
41b848b0 664
912541b0 665 return udev_device;
eb1f0e66
KS
666}
667
668/**
8753fadf 669 * udev_device_new_from_syspath:
eb1f0e66 670 * @udev: udev library context
8753fadf 671 * @syspath: sys device path including sys directory
eb1f0e66 672 *
8753fadf 673 * Create new udev device, and fill in information from the sys
214a6c79 674 * device and the udev database entry. The syspath is the absolute
8753fadf 675 * path to the device, including the sys mount point.
eb1f0e66
KS
676 *
677 * The initial refcount is 1, and needs to be decremented to
be7de409 678 * release the resources of the udev device.
eb1f0e66
KS
679 *
680 * Returns: a new udev device, or #NULL, if it does not exist
681 **/
54cf0b7f 682_public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
eb1f0e66 683{
912541b0
KS
684 const char *subdir;
685 char path[UTIL_PATH_SIZE];
686 char *pos;
687 struct stat statbuf;
688 struct udev_device *udev_device;
689
aa0e72ea
TG
690 if (udev == NULL) {
691 errno = EINVAL;
912541b0 692 return NULL;
aa0e72ea
TG
693 }
694
695 if (syspath == NULL) {
696 errno = EINVAL;
912541b0 697 return NULL;
aa0e72ea 698 }
912541b0
KS
699
700 /* path starts in sys */
4cb72937 701 if (!startswith(syspath, "/sys")) {
ff49bc32 702 log_debug("not in sys :%s", syspath);
aa0e72ea 703 errno = EINVAL;
912541b0
KS
704 return NULL;
705 }
706
707 /* path is not a root directory */
4cb72937 708 subdir = syspath + strlen("/sys");
912541b0 709 pos = strrchr(subdir, '/');
aa0e72ea
TG
710 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
711 errno = EINVAL;
912541b0 712 return NULL;
aa0e72ea 713 }
912541b0
KS
714
715 /* resolve possible symlink to real path */
d5a89d7d 716 strscpy(path, sizeof(path), syspath);
912541b0
KS
717 util_resolve_sys_link(udev, path, sizeof(path));
718
4cb72937 719 if (startswith(path + strlen("/sys"), "/devices/")) {
912541b0
KS
720 char file[UTIL_PATH_SIZE];
721
722 /* all "devices" require a "uevent" file */
d5a89d7d 723 strscpyl(file, sizeof(file), path, "/uevent", NULL);
baa30fbc 724 if (stat(file, &statbuf) != 0)
912541b0 725 return NULL;
912541b0
KS
726 } else {
727 /* everything else just needs to be a directory */
baa30fbc 728 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
912541b0 729 return NULL;
912541b0
KS
730 }
731
732 udev_device = udev_device_new(udev);
733 if (udev_device == NULL)
734 return NULL;
735
736 udev_device_set_syspath(udev_device, path);
ff49bc32 737 log_debug("device %p has devpath '%s'", udev_device, udev_device_get_devpath(udev_device));
912541b0
KS
738
739 return udev_device;
eb1f0e66
KS
740}
741
1e511322
KS
742/**
743 * udev_device_new_from_devnum:
744 * @udev: udev library context
745 * @type: char or block device
746 * @devnum: device major/minor number
747 *
748 * Create new udev device, and fill in information from the sys
dbba7e40
KS
749 * device and the udev database entry. The device is looked-up
750 * by its major/minor number and type. Character and block device
751 * numbers are not unique across the two types.
1e511322
KS
752 *
753 * The initial refcount is 1, and needs to be decremented to
754 * release the resources of the udev device.
755 *
756 * Returns: a new udev device, or #NULL, if it does not exist
757 **/
54cf0b7f 758_public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
4c9dff47 759{
912541b0
KS
760 char path[UTIL_PATH_SIZE];
761 const char *type_str;
03198b93 762
912541b0
KS
763 if (type == 'b')
764 type_str = "block";
765 else if (type == 'c')
766 type_str = "char";
aa0e72ea
TG
767 else {
768 errno = EINVAL;
912541b0 769 return NULL;
aa0e72ea 770 }
4c9dff47 771
912541b0 772 /* use /sys/dev/{block,char}/<maj>:<min> link */
4cb72937 773 snprintf(path, sizeof(path), "/sys/dev/%s/%u:%u",
6ada823a 774 type_str, major(devnum), minor(devnum));
912541b0 775 return udev_device_new_from_syspath(udev, path);
4c9dff47
KS
776}
777
dbf61afb
KS
778/**
779 * udev_device_new_from_device_id:
780 * @udev: udev library context
781 * @id: text string identifying a kernel device
782 *
783 * Create new udev device, and fill in information from the sys
784 * device and the udev database entry. The device is looked-up
785 * by a special string:
786 * b8:2 - block device major:minor
787 * c128:1 - char device major:minor
788 * n3 - network device ifindex
789 * +sound:card29 - kernel driver core subsystem:device name
790 *
791 * The initial refcount is 1, and needs to be decremented to
792 * release the resources of the udev device.
793 *
794 * Returns: a new udev device, or #NULL, if it does not exist
795 **/
67410e9f 796_public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id)
cad40a5f 797{
912541b0
KS
798 char type;
799 int maj, min;
800 char subsys[UTIL_PATH_SIZE];
801 char *sysname;
802
803 switch(id[0]) {
804 case 'b':
805 case 'c':
806 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
807 return NULL;
808 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
809 case 'n': {
810 int sk;
811 struct ifreq ifr;
812 struct udev_device *dev;
813 int ifindex;
814
815 ifindex = strtoul(&id[1], NULL, 10);
aa0e72ea
TG
816 if (ifindex <= 0) {
817 errno = EINVAL;
912541b0 818 return NULL;
aa0e72ea 819 }
912541b0
KS
820
821 sk = socket(PF_INET, SOCK_DGRAM, 0);
822 if (sk < 0)
823 return NULL;
29804cc1 824 memzero(&ifr, sizeof(struct ifreq));
912541b0
KS
825 ifr.ifr_ifindex = ifindex;
826 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
827 close(sk);
828 return NULL;
829 }
830 close(sk);
831
832 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
833 if (dev == NULL)
834 return NULL;
835 if (udev_device_get_ifindex(dev) == ifindex)
836 return dev;
aa0e72ea
TG
837
838 /* this is racy, so we may end up with the wrong device */
912541b0 839 udev_device_unref(dev);
aa0e72ea 840 errno = ENODEV;
912541b0
KS
841 return NULL;
842 }
843 case '+':
d5a89d7d 844 strscpy(subsys, sizeof(subsys), &id[1]);
912541b0 845 sysname = strchr(subsys, ':');
aa0e72ea
TG
846 if (sysname == NULL) {
847 errno = EINVAL;
912541b0 848 return NULL;
aa0e72ea 849 }
912541b0
KS
850 sysname[0] = '\0';
851 sysname = &sysname[1];
852 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
853 default:
aa0e72ea 854 errno = EINVAL;
912541b0
KS
855 return NULL;
856 }
cad40a5f
KS
857}
858
1e511322
KS
859/**
860 * udev_device_new_from_subsystem_sysname:
861 * @udev: udev library context
214a6c79 862 * @subsystem: the subsystem of the device
1e511322
KS
863 * @sysname: the name of the device
864 *
dbba7e40
KS
865 * Create new udev device, and fill in information from the sys device
866 * and the udev database entry. The device is looked up by the subsystem
867 * and name string of the device, like "mem" / "zero", or "block" / "sda".
1e511322
KS
868 *
869 * The initial refcount is 1, and needs to be decremented to
870 * release the resources of the udev device.
871 *
872 * Returns: a new udev device, or #NULL, if it does not exist
873 **/
54cf0b7f 874_public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
90d80c2e 875{
6ada823a 876 char path[UTIL_PATH_SIZE];
912541b0
KS
877 struct stat statbuf;
878
33502ffe 879 if (streq(subsystem, "subsystem")) {
d5a89d7d 880 strscpyl(path, sizeof(path), "/sys/subsystem/", sysname, NULL);
6ada823a 881 if (stat(path, &statbuf) == 0)
912541b0
KS
882 goto found;
883
d5a89d7d 884 strscpyl(path, sizeof(path), "/sys/bus/", sysname, NULL);
6ada823a 885 if (stat(path, &statbuf) == 0)
912541b0
KS
886 goto found;
887
d5a89d7d 888 strscpyl(path, sizeof(path), "/sys/class/", sysname, NULL);
6ada823a 889 if (stat(path, &statbuf) == 0)
912541b0
KS
890 goto found;
891 goto out;
892 }
893
33502ffe 894 if (streq(subsystem, "module")) {
d5a89d7d 895 strscpyl(path, sizeof(path), "/sys/module/", sysname, NULL);
6ada823a 896 if (stat(path, &statbuf) == 0)
912541b0
KS
897 goto found;
898 goto out;
899 }
900
33502ffe 901 if (streq(subsystem, "drivers")) {
912541b0
KS
902 char subsys[UTIL_NAME_SIZE];
903 char *driver;
904
d5a89d7d 905 strscpy(subsys, sizeof(subsys), sysname);
912541b0
KS
906 driver = strchr(subsys, ':');
907 if (driver != NULL) {
908 driver[0] = '\0';
909 driver = &driver[1];
910
d5a89d7d 911 strscpyl(path, sizeof(path), "/sys/subsystem/", subsys, "/drivers/", driver, NULL);
6ada823a 912 if (stat(path, &statbuf) == 0)
912541b0
KS
913 goto found;
914
d5a89d7d 915 strscpyl(path, sizeof(path), "/sys/bus/", subsys, "/drivers/", driver, NULL);
6ada823a 916 if (stat(path, &statbuf) == 0)
912541b0 917 goto found;
aa0e72ea
TG
918 } else
919 errno = EINVAL;
920
912541b0
KS
921 goto out;
922 }
923
d5a89d7d 924 strscpyl(path, sizeof(path), "/sys/subsystem/", subsystem, "/devices/", sysname, NULL);
6ada823a 925 if (stat(path, &statbuf) == 0)
912541b0
KS
926 goto found;
927
d5a89d7d 928 strscpyl(path, sizeof(path), "/sys/bus/", subsystem, "/devices/", sysname, NULL);
6ada823a 929 if (stat(path, &statbuf) == 0)
912541b0
KS
930 goto found;
931
d5a89d7d 932 strscpyl(path, sizeof(path), "/sys/class/", subsystem, "/", sysname, NULL);
6ada823a 933 if (stat(path, &statbuf) == 0)
912541b0 934 goto found;
90d80c2e 935out:
912541b0 936 return NULL;
90d80c2e 937found:
6ada823a 938 return udev_device_new_from_syspath(udev, path);
90d80c2e
KS
939}
940
fc8d61c5
KS
941/**
942 * udev_device_new_from_environment
943 * @udev: udev library context
944 *
945 * Create new udev device, and fill in information from the
946 * current process environment. This only works reliable if
947 * the process is called from a udev rule. It is usually used
948 * for tools executed from IMPORT= rules.
949 *
950 * The initial refcount is 1, and needs to be decremented to
951 * release the resources of the udev device.
952 *
953 * Returns: a new udev device, or #NULL, if it does not exist
954 **/
54cf0b7f 955_public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
fc8d61c5 956{
912541b0
KS
957 int i;
958 struct udev_device *udev_device;
fc8d61c5 959
912541b0
KS
960 udev_device = udev_device_new(udev);
961 if (udev_device == NULL)
962 return NULL;
963 udev_device_set_info_loaded(udev_device);
fc8d61c5 964
912541b0
KS
965 for (i = 0; environ[i] != NULL; i++)
966 udev_device_add_property_from_string_parse(udev_device, environ[i]);
fc8d61c5 967
912541b0 968 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
ff49bc32 969 log_debug("missing values, invalid device");
912541b0
KS
970 udev_device_unref(udev_device);
971 udev_device = NULL;
972 }
fc8d61c5 973
912541b0 974 return udev_device;
fc8d61c5
KS
975}
976
b2d9e4f2 977static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
4ad3a37f 978{
912541b0
KS
979 struct udev_device *udev_device_parent = NULL;
980 char path[UTIL_PATH_SIZE];
981 const char *subdir;
4ad3a37f 982
d5a89d7d 983 strscpy(path, sizeof(path), udev_device->syspath);
4cb72937 984 subdir = path + strlen("/sys/");
912541b0
KS
985 for (;;) {
986 char *pos;
b95f8a76 987
912541b0
KS
988 pos = strrchr(subdir, '/');
989 if (pos == NULL || pos < &subdir[2])
990 break;
991 pos[0] = '\0';
992 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
993 if (udev_device_parent != NULL)
994 return udev_device_parent;
995 }
aa0e72ea
TG
996
997 errno = ENOENT;
912541b0 998 return NULL;
4ad3a37f
KS
999}
1000
1e511322
KS
1001/**
1002 * udev_device_get_parent:
1003 * @udev_device: the device to start searching from
1004 *
1005 * Find the next parent device, and fill in information from the sys
1006 * device and the udev database entry.
1007 *
572ce4f7
ZJS
1008 * Returned device is not referenced. It is attached to the child
1009 * device, and will be cleaned up when the child device is cleaned up.
1e511322 1010 *
214a6c79 1011 * It is not necessarily just the upper level directory, empty or not
1e511322
KS
1012 * recognized sys directories are ignored.
1013 *
1014 * It can be called as many times as needed, without caring about
1015 * references.
1016 *
1017 * Returns: a new udev device, or #NULL, if it no parent exist.
1018 **/
54cf0b7f 1019_public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
b2d9e4f2 1020{
aa0e72ea
TG
1021 if (udev_device == NULL) {
1022 errno = EINVAL;
912541b0 1023 return NULL;
aa0e72ea 1024 }
912541b0
KS
1025 if (!udev_device->parent_set) {
1026 udev_device->parent_set = true;
1027 udev_device->parent_device = device_new_from_parent(udev_device);
1028 }
912541b0 1029 return udev_device->parent_device;
b2d9e4f2
KS
1030}
1031
1e511322
KS
1032/**
1033 * udev_device_get_parent_with_subsystem_devtype:
1034 * @udev_device: udev device to start searching from
214a6c79 1035 * @subsystem: the subsystem of the device
1e511322
KS
1036 * @devtype: the type (DEVTYPE) of the device
1037 *
1038 * Find the next parent device, with a matching subsystem and devtype
1039 * value, and fill in information from the sys device and the udev
1040 * database entry.
1041 *
79ef2e97 1042 * If devtype is #NULL, only subsystem is checked, and any devtype will
54d4f54e
MP
1043 * match.
1044 *
572ce4f7
ZJS
1045 * Returned device is not referenced. It is attached to the child
1046 * device, and will be cleaned up when the child device is cleaned up.
1e511322
KS
1047 *
1048 * It can be called as many times as needed, without caring about
1049 * references.
1050 *
8d6bc73a 1051 * Returns: a new udev device, or #NULL if no matching parent exists.
1e511322 1052 **/
54cf0b7f 1053_public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
bf8b2ae1 1054{
912541b0 1055 struct udev_device *parent;
bf8b2ae1 1056
aa0e72ea
TG
1057 if (subsystem == NULL) {
1058 errno = EINVAL;
912541b0 1059 return NULL;
aa0e72ea 1060 }
883012d4 1061
912541b0
KS
1062 parent = udev_device_get_parent(udev_device);
1063 while (parent != NULL) {
1064 const char *parent_subsystem;
1065 const char *parent_devtype;
bf8b2ae1 1066
912541b0 1067 parent_subsystem = udev_device_get_subsystem(parent);
33502ffe 1068 if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) {
912541b0
KS
1069 if (devtype == NULL)
1070 break;
1071 parent_devtype = udev_device_get_devtype(parent);
33502ffe 1072 if (parent_devtype != NULL && streq(parent_devtype, devtype))
912541b0
KS
1073 break;
1074 }
1075 parent = udev_device_get_parent(parent);
1076 }
aa0e72ea
TG
1077
1078 if (!parent)
1079 errno = ENOENT;
1080
912541b0 1081 return parent;
bf8b2ae1
MH
1082}
1083
eb1f0e66
KS
1084/**
1085 * udev_device_get_udev:
7d8787b3 1086 * @udev_device: udev device
eb1f0e66
KS
1087 *
1088 * Retrieve the udev library context the device was created with.
1089 *
1090 * Returns: the udev library context
1091 **/
54cf0b7f 1092_public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
eb1f0e66 1093{
912541b0
KS
1094 if (udev_device == NULL)
1095 return NULL;
1096 return udev_device->udev;
eb1f0e66
KS
1097}
1098
1099/**
1100 * udev_device_ref:
1101 * @udev_device: udev device
1102 *
1103 * Take a reference of a udev device.
1104 *
1105 * Returns: the passed udev device
1106 **/
54cf0b7f 1107_public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
eb1f0e66 1108{
912541b0
KS
1109 if (udev_device == NULL)
1110 return NULL;
1111 udev_device->refcount++;
1112 return udev_device;
eb1f0e66
KS
1113}
1114
1115/**
1116 * udev_device_unref:
1117 * @udev_device: udev device
1118 *
1119 * Drop a reference of a udev device. If the refcount reaches zero,
be7de409 1120 * the resources of the device will be released.
eb1f0e66 1121 *
725d7e6c 1122 * Returns: #NULL
eb1f0e66 1123 **/
20bbd54f 1124_public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
eb1f0e66 1125{
912541b0 1126 if (udev_device == NULL)
20bbd54f 1127 return NULL;
912541b0
KS
1128 udev_device->refcount--;
1129 if (udev_device->refcount > 0)
725d7e6c 1130 return NULL;
912541b0
KS
1131 if (udev_device->parent_device != NULL)
1132 udev_device_unref(udev_device->parent_device);
1133 free(udev_device->syspath);
1134 free(udev_device->sysname);
1135 free(udev_device->devnode);
1136 free(udev_device->subsystem);
1137 free(udev_device->devtype);
1138 udev_list_cleanup(&udev_device->devlinks_list);
1139 udev_list_cleanup(&udev_device->properties_list);
1140 udev_list_cleanup(&udev_device->sysattr_value_list);
1141 udev_list_cleanup(&udev_device->sysattr_list);
1142 udev_list_cleanup(&udev_device->tags_list);
1143 free(udev_device->action);
1144 free(udev_device->driver);
1145 free(udev_device->devpath_old);
1146 free(udev_device->id_filename);
1147 free(udev_device->envp);
1148 free(udev_device->monitor_buf);
912541b0 1149 free(udev_device);
20bbd54f 1150 return NULL;
eb1f0e66
KS
1151}
1152
1153/**
1154 * udev_device_get_devpath:
1155 * @udev_device: udev device
1156 *
11d543c1
KS
1157 * Retrieve the kernel devpath value of the udev device. The path
1158 * does not contain the sys mount point, and starts with a '/'.
eb1f0e66 1159 *
11d543c1 1160 * Returns: the devpath of the udev device
eb1f0e66 1161 **/
54cf0b7f 1162_public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
eb1f0e66 1163{
912541b0
KS
1164 if (udev_device == NULL)
1165 return NULL;
1166 return udev_device->devpath;
eb1f0e66
KS
1167}
1168
11d543c1
KS
1169/**
1170 * udev_device_get_syspath:
1171 * @udev_device: udev device
1172 *
1173 * Retrieve the sys path of the udev device. The path is an
1174 * absolute path and starts with the sys mount point.
1175 *
1176 * Returns: the sys path of the udev device
1177 **/
54cf0b7f 1178_public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
11d543c1 1179{
912541b0
KS
1180 if (udev_device == NULL)
1181 return NULL;
1182 return udev_device->syspath;
11d543c1
KS
1183}
1184
1e511322
KS
1185/**
1186 * udev_device_get_sysname:
1187 * @udev_device: udev device
1188 *
21dbe43a
KS
1189 * Get the kernel device name in /sys.
1190 *
1191 * Returns: the name string of the device device
1e511322 1192 **/
54cf0b7f 1193_public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
4ad3a37f 1194{
912541b0
KS
1195 if (udev_device == NULL)
1196 return NULL;
1197 return udev_device->sysname;
4ad3a37f
KS
1198}
1199
1e511322
KS
1200/**
1201 * udev_device_get_sysnum:
1202 * @udev_device: udev device
1203 *
21dbe43a
KS
1204 * Get the instance number of the device.
1205 *
c5315881 1206 * Returns: the trailing number string of the device name
1e511322 1207 **/
54cf0b7f 1208_public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
517814e7 1209{
912541b0
KS
1210 if (udev_device == NULL)
1211 return NULL;
1212 return udev_device->sysnum;
517814e7
KS
1213}
1214
eb1f0e66 1215/**
fb762bb9 1216 * udev_device_get_devnode:
eb1f0e66
KS
1217 * @udev_device: udev device
1218 *
1219 * Retrieve the device node file name belonging to the udev device.
ba6929f6 1220 * The path is an absolute path, and starts with the device directory.
eb1f0e66
KS
1221 *
1222 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1223 **/
54cf0b7f 1224_public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
eb1f0e66 1225{
912541b0
KS
1226 if (udev_device == NULL)
1227 return NULL;
1228 if (udev_device->devnode != NULL)
1229 return udev_device->devnode;
1230 if (!udev_device->info_loaded)
1231 udev_device_read_uevent_file(udev_device);
1232 return udev_device->devnode;
eb1f0e66
KS
1233}
1234
eb1f0e66 1235/**
0de33a61 1236 * udev_device_get_devlinks_list_entry:
eb1f0e66 1237 * @udev_device: udev device
eb1f0e66 1238 *
bf7ad0ea
KS
1239 * Retrieve the list of device links pointing to the device file of
1240 * the udev device. The next list entry can be retrieved with
bb061708 1241 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
bf7ad0ea 1242 * The devlink path can be retrieved from the list entry by
e345e267 1243 * udev_list_entry_get_name(). The path is an absolute path, and starts with
bf7ad0ea 1244 * the device directory.
eb1f0e66 1245 *
bf7ad0ea 1246 * Returns: the first entry of the device node link list
eb1f0e66 1247 **/
54cf0b7f 1248_public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
eb1f0e66 1249{
912541b0
KS
1250 if (udev_device == NULL)
1251 return NULL;
1252 if (!udev_device->info_loaded)
1253 udev_device_read_db(udev_device, NULL);
1254 return udev_list_get_entry(&udev_device->devlinks_list);
eb1f0e66
KS
1255}
1256
979ff016
KS
1257void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1258{
912541b0
KS
1259 udev_device->devlinks_uptodate = false;
1260 udev_list_cleanup(&udev_device->devlinks_list);
979ff016
KS
1261}
1262
eb1f0e66 1263/**
0de33a61 1264 * udev_device_get_properties_list_entry:
eb1f0e66 1265 * @udev_device: udev device
eb1f0e66 1266 *
bf7ad0ea 1267 * Retrieve the list of key/value device properties of the udev
bb061708 1268 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
bf7ad0ea 1269 * which returns #NULL if no more entries exist. The property name
bb061708
KS
1270 * can be retrieved from the list entry by udev_list_entry_get_name(),
1271 * the property value by udev_list_entry_get_value().
eb1f0e66 1272 *
bf7ad0ea 1273 * Returns: the first entry of the property list
eb1f0e66 1274 **/
54cf0b7f 1275_public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
eb1f0e66 1276{
912541b0
KS
1277 if (udev_device == NULL)
1278 return NULL;
1279 if (!udev_device->info_loaded) {
1280 udev_device_read_uevent_file(udev_device);
1281 udev_device_read_db(udev_device, NULL);
1282 }
1283 if (!udev_device->devlinks_uptodate) {
1284 char symlinks[UTIL_PATH_SIZE];
1285 struct udev_list_entry *list_entry;
1286
1287 udev_device->devlinks_uptodate = true;
1288 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1289 if (list_entry != NULL) {
1290 char *s;
1291 size_t l;
1292
1293 s = symlinks;
d5a89d7d 1294 l = strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
912541b0 1295 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
d5a89d7d 1296 l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
912541b0
KS
1297 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1298 }
1299 }
1300 if (!udev_device->tags_uptodate) {
1301 udev_device->tags_uptodate = true;
1302 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1303 char tags[UTIL_PATH_SIZE];
1304 struct udev_list_entry *list_entry;
1305 char *s;
1306 size_t l;
1307
1308 s = tags;
d5a89d7d 1309 l = strpcpyl(&s, sizeof(tags), ":", NULL);
912541b0 1310 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
d5a89d7d 1311 l = strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
912541b0
KS
1312 udev_device_add_property(udev_device, "TAGS", tags);
1313 }
1314 }
1315 return udev_list_get_entry(&udev_device->properties_list);
eb1f0e66 1316}
11d543c1 1317
1e511322
KS
1318/**
1319 * udev_device_get_action:
1320 * @udev_device: udev device
1321 *
1322 * This is only valid if the device was received through a monitor. Devices read from
1323 * sys do not have an action string. Usual actions are: add, remove, change, online,
1324 * offline.
1325 *
1326 * Returns: the kernel action value, or #NULL if there is no action value available.
1327 **/
54cf0b7f 1328_public_ const char *udev_device_get_action(struct udev_device *udev_device)
c4f5f942 1329{
912541b0
KS
1330 if (udev_device == NULL)
1331 return NULL;
1332 return udev_device->action;
c4f5f942
KS
1333}
1334
9c6a11b1
KS
1335/**
1336 * udev_device_get_usec_since_initialized:
1337 * @udev_device: udev device
1338 *
1339 * Return the number of microseconds passed since udev set up the
1340 * device for the first time.
1341 *
1342 * This is only implemented for devices with need to store properties
1343 * in the udev database. All other devices return 0 here.
1344 *
1345 * Returns: the number of microseconds since the device was first seen.
1346 **/
54cf0b7f 1347_public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
9c6a11b1 1348{
40fe8b11 1349 usec_t now_ts;
9c6a11b1 1350
912541b0
KS
1351 if (udev_device == NULL)
1352 return 0;
1353 if (!udev_device->info_loaded)
1354 udev_device_read_db(udev_device, NULL);
1355 if (udev_device->usec_initialized == 0)
1356 return 0;
40fe8b11 1357 now_ts = now(CLOCK_MONOTONIC);
33502ffe 1358 if (now_ts == 0)
912541b0 1359 return 0;
33502ffe 1360 return now_ts - udev_device->usec_initialized;
9c6a11b1
KS
1361}
1362
40fe8b11 1363usec_t udev_device_get_usec_initialized(struct udev_device *udev_device)
9c6a11b1 1364{
912541b0 1365 return udev_device->usec_initialized;
9c6a11b1
KS
1366}
1367
40fe8b11 1368void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized)
9c6a11b1 1369{
912541b0 1370 char num[32];
a20a57a7 1371
912541b0 1372 udev_device->usec_initialized = usec_initialized;
de0671ee 1373 snprintf(num, sizeof(num), USEC_FMT, usec_initialized);
912541b0 1374 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
9c6a11b1
KS
1375}
1376
1e511322
KS
1377/**
1378 * udev_device_get_sysattr_value:
1379 * @udev_device: udev device
1380 * @sysattr: attribute name
1381 *
456719b6 1382 * The retrieved value is cached in the device. Repeated calls will return the same
1e511322
KS
1383 * value and not open the attribute again.
1384 *
1385 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1386 **/
54cf0b7f 1387_public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
93b0f384 1388{
912541b0
KS
1389 struct udev_list_entry *list_entry;
1390 char path[UTIL_PATH_SIZE];
1391 char value[4096];
1392 struct stat statbuf;
1393 int fd;
1394 ssize_t size;
1395 const char *val = NULL;
1396
1397 if (udev_device == NULL)
1398 return NULL;
1399 if (sysattr == NULL)
1400 return NULL;
1401
1402 /* look for possibly already cached result */
1403 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1404 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
baa30fbc 1405 if (list_entry != NULL)
912541b0 1406 return udev_list_entry_get_value(list_entry);
912541b0 1407
d5a89d7d 1408 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
912541b0 1409 if (lstat(path, &statbuf) != 0) {
912541b0
KS
1410 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1411 goto out;
1412 }
1413
1414 if (S_ISLNK(statbuf.st_mode)) {
912541b0
KS
1415 /*
1416 * Some core links return only the last element of the target path,
1417 * these are just values, the paths should not be exposed.
1418 */
33502ffe
KS
1419 if (streq(sysattr, "driver") ||
1420 streq(sysattr, "subsystem") ||
1421 streq(sysattr, "module")) {
912541b0
KS
1422 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1423 udev_device->syspath, value, sizeof(value)) < 0)
1424 return NULL;
912541b0
KS
1425 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1426 val = udev_list_entry_get_value(list_entry);
1427 goto out;
1428 }
1429
912541b0
KS
1430 goto out;
1431 }
1432
1433 /* skip directories */
1434 if (S_ISDIR(statbuf.st_mode))
1435 goto out;
1436
1437 /* skip non-readable files */
1438 if ((statbuf.st_mode & S_IRUSR) == 0)
1439 goto out;
1440
1441 /* read attribute value */
1442 fd = open(path, O_RDONLY|O_CLOEXEC);
baa30fbc 1443 if (fd < 0)
912541b0 1444 goto out;
912541b0
KS
1445 size = read(fd, value, sizeof(value));
1446 close(fd);
1447 if (size < 0)
1448 goto out;
1449 if (size == sizeof(value))
1450 goto out;
1451
1452 /* got a valid value, store it in cache and return it */
1453 value[size] = '\0';
1454 util_remove_trailing_chars(value, '\n');
912541b0
KS
1455 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1456 val = udev_list_entry_get_value(list_entry);
93b0f384 1457out:
912541b0 1458 return val;
93b0f384 1459}
517814e7 1460
946f1825
HR
1461/**
1462 * udev_device_set_sysattr_value:
1463 * @udev_device: udev device
1464 * @sysattr: attribute name
1465 * @value: new value to be set
1466 *
1467 * Update the contents of the sys attribute and the cached value of the device.
1468 *
1469 * Returns: Negative error code on failure or 0 on success.
1470 **/
1471_public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, char *value)
1472{
1473 struct udev_device *dev;
1474 char path[UTIL_PATH_SIZE];
1475 struct stat statbuf;
1476 int fd;
1477 ssize_t size, value_len;
1478 int ret = 0;
1479
1480 if (udev_device == NULL)
1481 return -EINVAL;
1482 dev = udev_device;
1483 if (sysattr == NULL)
1484 return -EINVAL;
1485 if (value == NULL)
1486 value_len = 0;
1487 else
1488 value_len = strlen(value);
53726349 1489
946f1825
HR
1490 strscpyl(path, sizeof(path), udev_device_get_syspath(dev), "/", sysattr, NULL);
1491 if (lstat(path, &statbuf) != 0) {
1492 udev_list_entry_add(&dev->sysattr_value_list, sysattr, NULL);
1493 ret = -ENXIO;
1494 goto out;
1495 }
1496
1497 if (S_ISLNK(statbuf.st_mode)) {
53726349 1498 ret = -EINVAL;
946f1825
HR
1499 goto out;
1500 }
1501
1502 /* skip directories */
1503 if (S_ISDIR(statbuf.st_mode)) {
1504 ret = -EISDIR;
1505 goto out;
1506 }
1507
1508 /* skip non-readable files */
1509 if ((statbuf.st_mode & S_IRUSR) == 0) {
1510 ret = -EACCES;
1511 goto out;
1512 }
1513
1514 /* Value is limited to 4k */
1515 if (value_len > 4096) {
1516 ret = -EINVAL;
1517 goto out;
1518 }
1519 util_remove_trailing_chars(value, '\n');
1520
1521 /* write attribute value */
1522 fd = open(path, O_WRONLY|O_CLOEXEC);
1523 if (fd < 0) {
1524 ret = -errno;
1525 goto out;
1526 }
1527 size = write(fd, value, value_len);
1528 close(fd);
1529 if (size < 0) {
1530 ret = -errno;
1531 goto out;
1532 }
1533 if (size < value_len) {
1534 ret = -EIO;
1535 goto out;
1536 }
1537
1538 /* wrote a valid value, store it in cache and return it */
1539 udev_list_entry_add(&dev->sysattr_value_list, sysattr, value);
1540out:
1541 if (dev != udev_device)
1542 udev_device_unref(dev);
1543 return ret;
1544}
1545
20bee04c 1546static int udev_device_sysattr_list_read(struct udev_device *udev_device)
f180ad25 1547{
912541b0
KS
1548 struct dirent *dent;
1549 DIR *dir;
1550 int num = 0;
f180ad25 1551
912541b0 1552 if (udev_device == NULL)
994e0234 1553 return -EINVAL;
912541b0
KS
1554 if (udev_device->sysattr_list_read)
1555 return 0;
f180ad25 1556
912541b0 1557 dir = opendir(udev_device_get_syspath(udev_device));
baa30fbc 1558 if (!dir)
994e0234 1559 return -errno;
f180ad25 1560
912541b0
KS
1561 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1562 char path[UTIL_PATH_SIZE];
1563 struct stat statbuf;
f180ad25 1564
912541b0
KS
1565 /* only handle symlinks and regular files */
1566 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1567 continue;
f180ad25 1568
d5a89d7d 1569 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
912541b0
KS
1570 if (lstat(path, &statbuf) != 0)
1571 continue;
1572 if ((statbuf.st_mode & S_IRUSR) == 0)
1573 continue;
f180ad25 1574
912541b0
KS
1575 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1576 num++;
1577 }
f180ad25 1578
912541b0 1579 closedir(dir);
912541b0 1580 udev_device->sysattr_list_read = true;
f180ad25 1581
912541b0 1582 return num;
f180ad25
TE
1583}
1584
1585/**
1586 * udev_device_get_sysattr_list_entry:
1587 * @udev_device: udev device
1588 *
1589 * Retrieve the list of available sysattrs, with value being empty;
20bee04c
KS
1590 * This just return all available sysfs attributes for a particular
1591 * device without reading their values.
f180ad25
TE
1592 *
1593 * Returns: the first entry of the property list
1594 **/
54cf0b7f 1595_public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
f180ad25 1596{
912541b0
KS
1597 if (!udev_device->sysattr_list_read) {
1598 int ret;
1599 ret = udev_device_sysattr_list_read(udev_device);
1600 if (0 > ret)
1601 return NULL;
1602 }
f180ad25 1603
912541b0 1604 return udev_list_get_entry(&udev_device->sysattr_list);
f180ad25
TE
1605}
1606
8cd2e972 1607int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
11d543c1 1608{
912541b0
KS
1609 const char *pos;
1610 size_t len;
8753fadf 1611
912541b0
KS
1612 free(udev_device->syspath);
1613 udev_device->syspath = strdup(syspath);
1614 if (udev_device->syspath == NULL)
1615 return -ENOMEM;
4cb72937 1616 udev_device->devpath = udev_device->syspath + strlen("/sys");
912541b0 1617 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
517814e7 1618
912541b0
KS
1619 pos = strrchr(udev_device->syspath, '/');
1620 if (pos == NULL)
1621 return -EINVAL;
1622 udev_device->sysname = strdup(&pos[1]);
1623 if (udev_device->sysname == NULL)
1624 return -ENOMEM;
517814e7 1625
912541b0
KS
1626 /* some devices have '!' in their name, change that to '/' */
1627 len = 0;
1628 while (udev_device->sysname[len] != '\0') {
1629 if (udev_device->sysname[len] == '!')
1630 udev_device->sysname[len] = '/';
1631 len++;
1632 }
517814e7 1633
912541b0
KS
1634 /* trailing number */
1635 while (len > 0 && isdigit(udev_device->sysname[--len]))
1636 udev_device->sysnum = &udev_device->sysname[len];
babcf3cb 1637
912541b0
KS
1638 /* sysname is completely numeric */
1639 if (len == 0)
1640 udev_device->sysnum = NULL;
babcf3cb 1641
912541b0 1642 return 0;
11d543c1
KS
1643}
1644
b2661839 1645static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
11d543c1 1646{
912541b0
KS
1647 free(udev_device->devnode);
1648 if (devnode[0] != '/') {
4cb72937 1649 if (asprintf(&udev_device->devnode, "/dev/%s", devnode) < 0)
912541b0
KS
1650 udev_device->devnode = NULL;
1651 } else {
1652 udev_device->devnode = strdup(devnode);
1653 }
1654 if (udev_device->devnode == NULL)
1655 return -ENOMEM;
1656 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1657 return 0;
11d543c1
KS
1658}
1659
8a173387 1660int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
11d543c1 1661{
912541b0 1662 struct udev_list_entry *list_entry;
6c29f2b9 1663
912541b0
KS
1664 udev_device->devlinks_uptodate = false;
1665 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1666 if (list_entry == NULL)
1667 return -ENOMEM;
912541b0 1668 return 0;
11d543c1
KS
1669}
1670
4281da1f
KS
1671const char *udev_device_get_id_filename(struct udev_device *udev_device)
1672{
912541b0
KS
1673 if (udev_device->id_filename == NULL) {
1674 if (udev_device_get_subsystem(udev_device) == NULL)
1675 return NULL;
1676
1677 if (major(udev_device_get_devnum(udev_device)) > 0) {
1678 /* use dev_t -- b259:131072, c254:0 */
1679 if (asprintf(&udev_device->id_filename, "%c%u:%u",
33502ffe 1680 streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c',
912541b0
KS
1681 major(udev_device_get_devnum(udev_device)),
1682 minor(udev_device_get_devnum(udev_device))) < 0)
1683 udev_device->id_filename = NULL;
1684 } else if (udev_device_get_ifindex(udev_device) > 0) {
1685 /* use netdev ifindex -- n3 */
1686 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1687 udev_device->id_filename = NULL;
1688 } else {
1689 /*
1690 * use $subsys:$syname -- pci:0000:00:1f.2
1691 * sysname() has '!' translated, get it from devpath
1692 */
1693 const char *sysname;
1694 sysname = strrchr(udev_device->devpath, '/');
1695 if (sysname == NULL)
1696 return NULL;
1697 sysname = &sysname[1];
1698 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1699 udev_device->id_filename = NULL;
1700 }
1701 }
1702 return udev_device->id_filename;
4281da1f
KS
1703}
1704
48a0170b
KS
1705/**
1706 * udev_device_get_is_initialized:
1707 * @udev_device: udev device
1708 *
1709 * Check if udev has already handled the device and has set up
1710 * device node permissions and context, or has renamed a network
1711 * device.
1712 *
9c6a11b1 1713 * This is only implemented for devices with a device node
48a0170b
KS
1714 * or network interfaces. All other devices return 1 here.
1715 *
1716 * Returns: 1 if the device is set up. 0 otherwise.
1717 **/
54cf0b7f 1718_public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
48a0170b 1719{
912541b0
KS
1720 if (!udev_device->info_loaded)
1721 udev_device_read_db(udev_device, NULL);
1722 return udev_device->is_initialized;
48a0170b
KS
1723}
1724
1725void udev_device_set_is_initialized(struct udev_device *udev_device)
1726{
912541b0 1727 udev_device->is_initialized = true;
48a0170b
KS
1728}
1729
8e3ba377
DH
1730static bool is_valid_tag(const char *tag)
1731{
1732 return !strchr(tag, ':') && !strchr(tag, ' ');
1733}
1734
28460195
KS
1735int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1736{
8e3ba377 1737 if (!is_valid_tag(tag))
912541b0
KS
1738 return -EINVAL;
1739 udev_device->tags_uptodate = false;
1740 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1741 return 0;
1742 return -ENOMEM;
28460195
KS
1743}
1744
8e3ba377
DH
1745void udev_device_remove_tag(struct udev_device *udev_device, const char *tag)
1746{
1747 struct udev_list_entry *e;
1748
1749 if (!is_valid_tag(tag))
1750 return;
1751 e = udev_list_get_entry(&udev_device->tags_list);
1752 e = udev_list_entry_get_by_name(e, tag);
1753 if (e) {
1754 udev_device->tags_uptodate = false;
1755 udev_list_entry_delete(e);
1756 }
1757}
1758
28460195
KS
1759void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1760{
912541b0
KS
1761 udev_device->tags_uptodate = false;
1762 udev_list_cleanup(&udev_device->tags_list);
28460195
KS
1763}
1764
f712894d
KS
1765/**
1766 * udev_device_get_tags_list_entry:
1767 * @udev_device: udev device
1768 *
1769 * Retrieve the list of tags attached to the udev device. The next
bb061708 1770 * list entry can be retrieved with udev_list_entry_get_next(),
f712894d 1771 * which returns #NULL if no more entries exist. The tag string
bb061708 1772 * can be retrieved from the list entry by udev_list_entry_get_name().
f712894d
KS
1773 *
1774 * Returns: the first entry of the tag list
1775 **/
54cf0b7f 1776_public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
28460195 1777{
912541b0
KS
1778 if (udev_device == NULL)
1779 return NULL;
1780 if (!udev_device->info_loaded)
1781 udev_device_read_db(udev_device, NULL);
1782 return udev_list_get_entry(&udev_device->tags_list);
28460195
KS
1783}
1784
21dbe43a
KS
1785/**
1786 * udev_device_has_tag:
1787 * @udev_device: udev device
1788 * @tag: tag name
1789 *
1790 * Check if a given device has a certain tag associated.
1791 *
1792 * Returns: 1 if the tag is found. 0 otherwise.
1793 **/
54cf0b7f 1794_public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
28460195 1795{
912541b0 1796 struct udev_list_entry *list_entry;
28460195 1797
912541b0
KS
1798 if (udev_device == NULL)
1799 return false;
1800 if (!udev_device->info_loaded)
1801 udev_device_read_db(udev_device, NULL);
1802 list_entry = udev_device_get_tags_list_entry(udev_device);
1803 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1804 return true;
1805 return false;
28460195
KS
1806}
1807
912541b0
KS
1808#define ENVP_SIZE 128
1809#define MONITOR_BUF_SIZE 4096
6493e655 1810static int update_envp_monitor_buf(struct udev_device *udev_device)
979ff016 1811{
912541b0
KS
1812 struct udev_list_entry *list_entry;
1813 char *s;
1814 size_t l;
1815 unsigned int i;
1816
1817 /* monitor buffer of property strings */
1818 free(udev_device->monitor_buf);
1819 udev_device->monitor_buf_len = 0;
1820 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1821 if (udev_device->monitor_buf == NULL)
1822 return -ENOMEM;
1823
1824 /* envp array, strings will point into monitor buffer */
1825 if (udev_device->envp == NULL)
1826 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1827 if (udev_device->envp == NULL)
1828 return -ENOMEM;
1829
1830 i = 0;
1831 s = udev_device->monitor_buf;
1832 l = MONITOR_BUF_SIZE;
1833 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1834 const char *key;
1835
1836 key = udev_list_entry_get_name(list_entry);
1837 /* skip private variables */
1838 if (key[0] == '.')
1839 continue;
1840
1841 /* add string to envp array */
1842 udev_device->envp[i++] = s;
1843 if (i+1 >= ENVP_SIZE)
1844 return -EINVAL;
1845
1846 /* add property string to monitor buffer */
d5a89d7d 1847 l = strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
912541b0
KS
1848 if (l == 0)
1849 return -EINVAL;
d5a89d7d 1850 /* advance past the trailing '\0' that strpcpyl() guarantees */
912541b0
KS
1851 s++;
1852 l--;
1853 }
1854 udev_device->envp[i] = NULL;
1855 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1856 udev_device->envp_uptodate = true;
912541b0 1857 return 0;
6493e655
KS
1858}
1859
1860char **udev_device_get_properties_envp(struct udev_device *udev_device)
1861{
912541b0
KS
1862 if (!udev_device->envp_uptodate)
1863 if (update_envp_monitor_buf(udev_device) != 0)
1864 return NULL;
1865 return udev_device->envp;
6493e655
KS
1866}
1867
1868ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1869{
912541b0
KS
1870 if (!udev_device->envp_uptodate)
1871 if (update_envp_monitor_buf(udev_device) != 0)
1872 return -EINVAL;
1873 *buf = udev_device->monitor_buf;
1874 return udev_device->monitor_buf_len;
c2654402
KS
1875}
1876
8cd2e972 1877int udev_device_set_action(struct udev_device *udev_device, const char *action)
c4f5f942 1878{
912541b0
KS
1879 free(udev_device->action);
1880 udev_device->action = strdup(action);
1881 if (udev_device->action == NULL)
1882 return -ENOMEM;
1883 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1884 return 0;
c4f5f942
KS
1885}
1886
8cd2e972 1887int udev_device_get_devlink_priority(struct udev_device *udev_device)
6bd1c78a 1888{
912541b0
KS
1889 if (!udev_device->info_loaded)
1890 udev_device_read_db(udev_device, NULL);
1891 return udev_device->devlink_priority;
6bd1c78a
KS
1892}
1893
8cd2e972 1894int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
6bd1c78a 1895{
912541b0
KS
1896 udev_device->devlink_priority = prio;
1897 return 0;
6bd1c78a
KS
1898}
1899
d7ce7539
SJR
1900int udev_device_get_watch_handle(struct udev_device *udev_device)
1901{
912541b0
KS
1902 if (!udev_device->info_loaded)
1903 udev_device_read_db(udev_device, NULL);
1904 return udev_device->watch_handle;
d7ce7539
SJR
1905}
1906
1907int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1908{
912541b0
KS
1909 udev_device->watch_handle = handle;
1910 return 0;
d7ce7539 1911}
fc416258 1912
9ead6627
KS
1913bool udev_device_get_db_persist(struct udev_device *udev_device)
1914{
912541b0 1915 return udev_device->db_persist;
9ead6627
KS
1916}
1917
1918void udev_device_set_db_persist(struct udev_device *udev_device)
1919{
912541b0 1920 udev_device->db_persist = true;
9ead6627 1921}