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