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