]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
libudev: private - make property_from_string_parse* static
[thirdparty/systemd.git] / src / libudev / libudev-device.c
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 ***/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <stdbool.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <net/if.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <linux/sockios.h>
35
36 #include "libudev.h"
37 #include "libudev-private.h"
38
39 static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode);
40
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
47 * filesystem. Devices usually belong to a kernel subsystem, and have
48 * a unique name inside that subsystem.
49 */
50
51 /**
52 * udev_device:
53 *
54 * Opaque object representing one kernel sys device.
55 */
56 struct udev_device {
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;
65 uid_t devnode_uid;
66 gid_t devnode_gid;
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;
82 usec_t usec_initialized;
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;
102 };
103
104 /**
105 * udev_device_get_seqnum:
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 **/
113 _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
114 {
115 if (udev_device == NULL)
116 return 0;
117 return udev_device->seqnum;
118 }
119
120 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
121 {
122 char num[32];
123
124 udev_device->seqnum = seqnum;
125 snprintf(num, sizeof(num), "%llu", seqnum);
126 udev_device_add_property(udev_device, "SEQNUM", num);
127 return 0;
128 }
129
130 int udev_device_get_ifindex(struct udev_device *udev_device)
131 {
132 if (!udev_device->info_loaded)
133 udev_device_read_uevent_file(udev_device);
134 return udev_device->ifindex;
135 }
136
137 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
138 {
139 char num[32];
140
141 udev_device->ifindex = ifindex;
142 snprintf(num, sizeof(num), "%d", ifindex);
143 udev_device_add_property(udev_device, "IFINDEX", num);
144 return 0;
145 }
146
147 /**
148 * udev_device_get_devnum:
149 * @udev_device: udev device
150 *
151 * Get the device major/minor number.
152 *
153 * Returns: the dev_t number.
154 **/
155 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
156 {
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;
162 }
163
164 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
165 {
166 char num[32];
167
168 udev_device->devnum = devnum;
169
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;
175 }
176
177 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
178 {
179 return udev_device->devpath_old;
180 }
181
182 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
183 {
184 const char *pos;
185
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);
191
192 pos = strrchr(udev_device->devpath_old, '/');
193 if (pos == NULL)
194 return -EINVAL;
195 return 0;
196 }
197
198 /**
199 * udev_device_get_driver:
200 * @udev_device: udev device
201 *
202 * Get the kernel driver name.
203 *
204 * Returns: the driver name string, or #NULL if there is no driver attached.
205 **/
206 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
207 {
208 char driver[UTIL_NAME_SIZE];
209
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;
218 }
219
220 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
221 {
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;
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 **/
239 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
240 {
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;
248 }
249
250 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
251 {
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;
259 }
260
261 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
262 {
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;
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 **/
281 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
282 {
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 */
295 if (startswith(udev_device->devpath, "/module/")) {
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 }
303 if (startswith(udev_device->devpath, "/subsystem/") ||
304 startswith(udev_device->devpath, "/class/") ||
305 startswith(udev_device->devpath, "/bus/")) {
306 udev_device_set_subsystem(udev_device, "subsystem");
307 return udev_device->subsystem;
308 }
309 }
310 return udev_device->subsystem;
311 }
312
313 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
314 {
315 if (!udev_device->info_loaded)
316 udev_device_read_uevent_file(udev_device);
317 return udev_device->devnode_mode;
318 }
319
320 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
321 {
322 char num[32];
323
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;
328 }
329
330 uid_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
337 static 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
347 gid_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
354 static 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
364 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
365 {
366 udev_device->envp_uptodate = false;
367 if (value == NULL) {
368 struct udev_list_entry *list_entry;
369
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);
377 }
378
379 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
380 {
381 char name[UTIL_LINE_SIZE];
382 char *val;
383
384 strscpy(name, sizeof(name), property);
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);
393 }
394
395 static int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
396 {
397 const char *pos;
398 size_t len;
399
400 free(udev_device->syspath);
401 udev_device->syspath = strdup(syspath);
402 if (udev_device->syspath == NULL)
403 return -ENOMEM;
404 udev_device->devpath = udev_device->syspath + strlen("/sys");
405 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
406
407 pos = strrchr(udev_device->syspath, '/');
408 if (pos == NULL)
409 return -EINVAL;
410 udev_device->sysname = strdup(&pos[1]);
411 if (udev_device->sysname == NULL)
412 return -ENOMEM;
413
414 /* some devices have '!' in their name, change that to '/' */
415 len = 0;
416 while (udev_device->sysname[len] != '\0') {
417 if (udev_device->sysname[len] == '!')
418 udev_device->sysname[len] = '/';
419 len++;
420 }
421
422 /* trailing number */
423 while (len > 0 && isdigit(udev_device->sysname[--len]))
424 udev_device->sysnum = &udev_device->sysname[len];
425
426 /* sysname is completely numeric */
427 if (len == 0)
428 udev_device->sysnum = NULL;
429
430 return 0;
431 }
432
433 /*
434 * parse property string, and if needed, update internal values accordingly
435 *
436 * udev_device_add_property_from_string_parse_finish() needs to be
437 * called after adding properties, and its return value checked
438 *
439 * udev_device_set_info_loaded() needs to be set, to avoid trying
440 * to use a device without a DEVPATH set
441 */
442 static void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
443 {
444 if (startswith(property, "DEVPATH=")) {
445 char path[UTIL_PATH_SIZE];
446
447 strscpyl(path, sizeof(path), "/sys", &property[8], NULL);
448 udev_device_set_syspath(udev_device, path);
449 } else if (startswith(property, "SUBSYSTEM=")) {
450 udev_device_set_subsystem(udev_device, &property[10]);
451 } else if (startswith(property, "DEVTYPE=")) {
452 udev_device_set_devtype(udev_device, &property[8]);
453 } else if (startswith(property, "DEVNAME=")) {
454 udev_device_set_devnode(udev_device, &property[8]);
455 } else if (startswith(property, "DEVLINKS=")) {
456 char devlinks[UTIL_PATH_SIZE];
457 char *slink;
458 char *next;
459
460 strscpy(devlinks, sizeof(devlinks), &property[9]);
461 slink = devlinks;
462 next = strchr(slink, ' ');
463 while (next != NULL) {
464 next[0] = '\0';
465 udev_device_add_devlink(udev_device, slink);
466 slink = &next[1];
467 next = strchr(slink, ' ');
468 }
469 if (slink[0] != '\0')
470 udev_device_add_devlink(udev_device, slink);
471 } else if (startswith(property, "TAGS=")) {
472 char tags[UTIL_PATH_SIZE];
473 char *next;
474
475 strscpy(tags, sizeof(tags), &property[5]);
476 next = strchr(tags, ':');
477 if (next != NULL) {
478 next++;
479 while (next[0] != '\0') {
480 char *tag;
481
482 tag = next;
483 next = strchr(tag, ':');
484 if (next == NULL)
485 break;
486 next[0] = '\0';
487 next++;
488 udev_device_add_tag(udev_device, tag);
489 }
490 }
491 } else if (startswith(property, "USEC_INITIALIZED=")) {
492 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
493 } else if (startswith(property, "DRIVER=")) {
494 udev_device_set_driver(udev_device, &property[7]);
495 } else if (startswith(property, "ACTION=")) {
496 udev_device_set_action(udev_device, &property[7]);
497 } else if (startswith(property, "MAJOR=")) {
498 udev_device->maj = strtoull(&property[6], NULL, 10);
499 } else if (startswith(property, "MINOR=")) {
500 udev_device->min = strtoull(&property[6], NULL, 10);
501 } else if (startswith(property, "DEVPATH_OLD=")) {
502 udev_device_set_devpath_old(udev_device, &property[12]);
503 } else if (startswith(property, "SEQNUM=")) {
504 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
505 } else if (startswith(property, "IFINDEX=")) {
506 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
507 } else if (startswith(property, "DEVMODE=")) {
508 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
509 } else if (startswith(property, "DEVUID=")) {
510 udev_device_set_devnode_uid(udev_device, strtoul(&property[7], NULL, 10));
511 } else if (startswith(property, "DEVGID=")) {
512 udev_device_set_devnode_gid(udev_device, strtoul(&property[7], NULL, 10));
513 } else {
514 udev_device_add_property_from_string(udev_device, property);
515 }
516 }
517
518 static int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
519 {
520 if (udev_device->maj > 0)
521 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
522 udev_device->maj = 0;
523 udev_device->min = 0;
524
525 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
526 return -EINVAL;
527 return 0;
528 }
529
530 /**
531 * udev_device_get_property_value:
532 * @udev_device: udev device
533 * @key: property name
534 *
535 * Get the value of a given property.
536 *
537 * Returns: the property string, or #NULL if there is no such property.
538 **/
539 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
540 {
541 struct udev_list_entry *list_entry;
542
543 if (udev_device == NULL)
544 return NULL;
545 if (key == NULL)
546 return NULL;
547
548 list_entry = udev_device_get_properties_list_entry(udev_device);
549 list_entry = udev_list_entry_get_by_name(list_entry, key);
550 return udev_list_entry_get_value(list_entry);
551 }
552
553 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
554 {
555 char filename[UTIL_PATH_SIZE];
556 char line[UTIL_LINE_SIZE];
557 FILE *f;
558
559 /* providing a database file will always force-load it */
560 if (dbfile == NULL) {
561 const char *id;
562
563 if (udev_device->db_loaded)
564 return 0;
565 udev_device->db_loaded = true;
566
567 id = udev_device_get_id_filename(udev_device);
568 if (id == NULL)
569 return -1;
570 strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
571 dbfile = filename;
572 }
573
574 f = fopen(dbfile, "re");
575 if (f == NULL)
576 return log_debug_errno(errno, "no db file to read %s: %m", dbfile);
577
578 /* devices with a database entry are initialized */
579 udev_device->is_initialized = true;
580
581 while (fgets(line, sizeof(line), f)) {
582 ssize_t len;
583 const char *val;
584 struct udev_list_entry *entry;
585
586 len = strlen(line);
587 if (len < 4)
588 break;
589 line[len-1] = '\0';
590 val = &line[2];
591 switch(line[0]) {
592 case 'S':
593 strscpyl(filename, sizeof(filename), "/dev/", val, NULL);
594 udev_device_add_devlink(udev_device, filename);
595 break;
596 case 'L':
597 udev_device_set_devlink_priority(udev_device, atoi(val));
598 break;
599 case 'E':
600 entry = udev_device_add_property_from_string(udev_device, val);
601 udev_list_entry_set_num(entry, true);
602 break;
603 case 'G':
604 udev_device_add_tag(udev_device, val);
605 break;
606 case 'W':
607 udev_device_set_watch_handle(udev_device, atoi(val));
608 break;
609 case 'I':
610 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
611 break;
612 }
613 }
614 fclose(f);
615
616 log_debug("device %p filled with db file data", udev_device);
617 return 0;
618 }
619
620 int udev_device_read_uevent_file(struct udev_device *udev_device)
621 {
622 char filename[UTIL_PATH_SIZE];
623 FILE *f;
624 char line[UTIL_LINE_SIZE];
625 int maj = 0;
626 int min = 0;
627
628 if (udev_device->uevent_loaded)
629 return 0;
630
631 strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
632 f = fopen(filename, "re");
633 if (f == NULL)
634 return -errno;
635 udev_device->uevent_loaded = true;
636
637 while (fgets(line, sizeof(line), f)) {
638 char *pos;
639
640 pos = strchr(line, '\n');
641 if (pos == NULL)
642 continue;
643 pos[0] = '\0';
644
645 if (startswith(line, "DEVTYPE=")) {
646 udev_device_set_devtype(udev_device, &line[8]);
647 continue;
648 }
649 if (startswith(line, "IFINDEX=")) {
650 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
651 continue;
652 }
653 if (startswith(line, "DEVNAME=")) {
654 udev_device_set_devnode(udev_device, &line[8]);
655 continue;
656 }
657
658 if (startswith(line, "MAJOR="))
659 maj = strtoull(&line[6], NULL, 10);
660 else if (startswith(line, "MINOR="))
661 min = strtoull(&line[6], NULL, 10);
662 else if (startswith(line, "DEVMODE="))
663 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
664
665 udev_device_add_property_from_string(udev_device, line);
666 }
667
668 udev_device->devnum = makedev(maj, min);
669 fclose(f);
670 return 0;
671 }
672
673 void udev_device_set_info_loaded(struct udev_device *device)
674 {
675 device->info_loaded = true;
676 }
677
678 static struct udev_device *udev_device_new(struct udev *udev)
679 {
680 struct udev_device *udev_device;
681
682 if (udev == NULL) {
683 errno = EINVAL;
684 return NULL;
685 }
686
687 udev_device = new0(struct udev_device, 1);
688 if (udev_device == NULL) {
689 errno = ENOMEM;
690 return NULL;
691 }
692 udev_device->refcount = 1;
693 udev_device->udev = udev;
694 udev_list_init(udev, &udev_device->devlinks_list, true);
695 udev_list_init(udev, &udev_device->properties_list, true);
696 udev_list_init(udev, &udev_device->sysattr_value_list, true);
697 udev_list_init(udev, &udev_device->sysattr_list, false);
698 udev_list_init(udev, &udev_device->tags_list, true);
699 udev_device->watch_handle = -1;
700
701 return udev_device;
702 }
703
704 /**
705 * udev_device_new_from_syspath:
706 * @udev: udev library context
707 * @syspath: sys device path including sys directory
708 *
709 * Create new udev device, and fill in information from the sys
710 * device and the udev database entry. The syspath is the absolute
711 * path to the device, including the sys mount point.
712 *
713 * The initial refcount is 1, and needs to be decremented to
714 * release the resources of the udev device.
715 *
716 * Returns: a new udev device, or #NULL, if it does not exist
717 **/
718 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
719 {
720 const char *subdir;
721 char path[UTIL_PATH_SIZE];
722 char *pos;
723 struct stat statbuf;
724 struct udev_device *udev_device;
725
726 if (udev == NULL) {
727 errno = EINVAL;
728 return NULL;
729 }
730
731 if (syspath == NULL) {
732 errno = EINVAL;
733 return NULL;
734 }
735
736 /* path starts in sys */
737 if (!startswith(syspath, "/sys")) {
738 log_debug("not in sys :%s", syspath);
739 errno = EINVAL;
740 return NULL;
741 }
742
743 /* path is not a root directory */
744 subdir = syspath + strlen("/sys");
745 pos = strrchr(subdir, '/');
746 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
747 errno = EINVAL;
748 return NULL;
749 }
750
751 /* resolve possible symlink to real path */
752 strscpy(path, sizeof(path), syspath);
753 util_resolve_sys_link(udev, path, sizeof(path));
754
755 if (startswith(path + strlen("/sys"), "/devices/")) {
756 char file[UTIL_PATH_SIZE];
757
758 /* all "devices" require a "uevent" file */
759 strscpyl(file, sizeof(file), path, "/uevent", NULL);
760 if (stat(file, &statbuf) != 0)
761 return NULL;
762 } else {
763 /* everything else just needs to be a directory */
764 if (stat(path, &statbuf) != 0)
765 return NULL;
766
767 if (!S_ISDIR(statbuf.st_mode)) {
768 errno = EISDIR;
769 return NULL;
770 }
771 }
772
773 udev_device = udev_device_new(udev);
774 if (udev_device == NULL)
775 return NULL;
776
777 udev_device_set_syspath(udev_device, path);
778 log_debug("device %p has devpath '%s'", udev_device, udev_device_get_devpath(udev_device));
779
780 return udev_device;
781 }
782
783 /**
784 * udev_device_new_from_devnum:
785 * @udev: udev library context
786 * @type: char or block device
787 * @devnum: device major/minor number
788 *
789 * Create new udev device, and fill in information from the sys
790 * device and the udev database entry. The device is looked-up
791 * by its major/minor number and type. Character and block device
792 * numbers are not unique across the two types.
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 **/
799 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
800 {
801 char path[UTIL_PATH_SIZE];
802 const char *type_str;
803
804 if (type == 'b')
805 type_str = "block";
806 else if (type == 'c')
807 type_str = "char";
808 else {
809 errno = EINVAL;
810 return NULL;
811 }
812
813 /* use /sys/dev/{block,char}/<maj>:<min> link */
814 snprintf(path, sizeof(path), "/sys/dev/%s/%u:%u",
815 type_str, major(devnum), minor(devnum));
816 return udev_device_new_from_syspath(udev, path);
817 }
818
819 /**
820 * udev_device_new_from_device_id:
821 * @udev: udev library context
822 * @id: text string identifying a kernel device
823 *
824 * Create new udev device, and fill in information from the sys
825 * device and the udev database entry. The device is looked-up
826 * by a special string:
827 * b8:2 - block device major:minor
828 * c128:1 - char device major:minor
829 * n3 - network device ifindex
830 * +sound:card29 - kernel driver core subsystem:device name
831 *
832 * The initial refcount is 1, and needs to be decremented to
833 * release the resources of the udev device.
834 *
835 * Returns: a new udev device, or #NULL, if it does not exist
836 **/
837 _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id)
838 {
839 char type;
840 int maj, min;
841 char subsys[UTIL_PATH_SIZE];
842 char *sysname;
843
844 switch(id[0]) {
845 case 'b':
846 case 'c':
847 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
848 return NULL;
849 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
850 case 'n': {
851 int sk;
852 struct ifreq ifr;
853 struct udev_device *dev;
854 int ifindex;
855
856 ifindex = strtoul(&id[1], NULL, 10);
857 if (ifindex <= 0) {
858 errno = EINVAL;
859 return NULL;
860 }
861
862 sk = socket(PF_INET, SOCK_DGRAM, 0);
863 if (sk < 0)
864 return NULL;
865 memzero(&ifr, sizeof(struct ifreq));
866 ifr.ifr_ifindex = ifindex;
867 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
868 close(sk);
869 return NULL;
870 }
871 close(sk);
872
873 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
874 if (dev == NULL)
875 return NULL;
876 if (udev_device_get_ifindex(dev) == ifindex)
877 return dev;
878
879 /* this is racy, so we may end up with the wrong device */
880 udev_device_unref(dev);
881 errno = ENODEV;
882 return NULL;
883 }
884 case '+':
885 strscpy(subsys, sizeof(subsys), &id[1]);
886 sysname = strchr(subsys, ':');
887 if (sysname == NULL) {
888 errno = EINVAL;
889 return NULL;
890 }
891 sysname[0] = '\0';
892 sysname = &sysname[1];
893 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
894 default:
895 errno = EINVAL;
896 return NULL;
897 }
898 }
899
900 /**
901 * udev_device_new_from_subsystem_sysname:
902 * @udev: udev library context
903 * @subsystem: the subsystem of the device
904 * @sysname: the name of the device
905 *
906 * Create new udev device, and fill in information from the sys device
907 * and the udev database entry. The device is looked up by the subsystem
908 * and name string of the device, like "mem" / "zero", or "block" / "sda".
909 *
910 * The initial refcount is 1, and needs to be decremented to
911 * release the resources of the udev device.
912 *
913 * Returns: a new udev device, or #NULL, if it does not exist
914 **/
915 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
916 {
917 char path[UTIL_PATH_SIZE];
918 struct stat statbuf;
919
920 if (streq(subsystem, "subsystem")) {
921 strscpyl(path, sizeof(path), "/sys/subsystem/", sysname, NULL);
922 if (stat(path, &statbuf) == 0)
923 goto found;
924
925 strscpyl(path, sizeof(path), "/sys/bus/", sysname, NULL);
926 if (stat(path, &statbuf) == 0)
927 goto found;
928
929 strscpyl(path, sizeof(path), "/sys/class/", sysname, NULL);
930 if (stat(path, &statbuf) == 0)
931 goto found;
932 goto out;
933 }
934
935 if (streq(subsystem, "module")) {
936 strscpyl(path, sizeof(path), "/sys/module/", sysname, NULL);
937 if (stat(path, &statbuf) == 0)
938 goto found;
939 goto out;
940 }
941
942 if (streq(subsystem, "drivers")) {
943 char subsys[UTIL_NAME_SIZE];
944 char *driver;
945
946 strscpy(subsys, sizeof(subsys), sysname);
947 driver = strchr(subsys, ':');
948 if (driver != NULL) {
949 driver[0] = '\0';
950 driver = &driver[1];
951
952 strscpyl(path, sizeof(path), "/sys/subsystem/", subsys, "/drivers/", driver, NULL);
953 if (stat(path, &statbuf) == 0)
954 goto found;
955
956 strscpyl(path, sizeof(path), "/sys/bus/", subsys, "/drivers/", driver, NULL);
957 if (stat(path, &statbuf) == 0)
958 goto found;
959 } else
960 errno = EINVAL;
961
962 goto out;
963 }
964
965 strscpyl(path, sizeof(path), "/sys/subsystem/", subsystem, "/devices/", sysname, NULL);
966 if (stat(path, &statbuf) == 0)
967 goto found;
968
969 strscpyl(path, sizeof(path), "/sys/bus/", subsystem, "/devices/", sysname, NULL);
970 if (stat(path, &statbuf) == 0)
971 goto found;
972
973 strscpyl(path, sizeof(path), "/sys/class/", subsystem, "/", sysname, NULL);
974 if (stat(path, &statbuf) == 0)
975 goto found;
976 out:
977 return NULL;
978 found:
979 return udev_device_new_from_syspath(udev, path);
980 }
981
982 /**
983 * udev_device_new_from_environment
984 * @udev: udev library context
985 *
986 * Create new udev device, and fill in information from the
987 * current process environment. This only works reliable if
988 * the process is called from a udev rule. It is usually used
989 * for tools executed from IMPORT= rules.
990 *
991 * The initial refcount is 1, and needs to be decremented to
992 * release the resources of the udev device.
993 *
994 * Returns: a new udev device, or #NULL, if it does not exist
995 **/
996 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
997 {
998 int i;
999 struct udev_device *udev_device;
1000
1001 udev_device = udev_device_new(udev);
1002 if (udev_device == NULL)
1003 return NULL;
1004 udev_device_set_info_loaded(udev_device);
1005
1006 for (i = 0; environ[i] != NULL; i++)
1007 udev_device_add_property_from_string_parse(udev_device, environ[i]);
1008
1009 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
1010 log_debug("missing values, invalid device");
1011 udev_device_unref(udev_device);
1012 udev_device = NULL;
1013 }
1014
1015 return udev_device;
1016 }
1017
1018 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
1019 {
1020 struct udev_device *udev_device_parent = NULL;
1021 char path[UTIL_PATH_SIZE];
1022 const char *subdir;
1023
1024 strscpy(path, sizeof(path), udev_device->syspath);
1025 subdir = path + strlen("/sys/");
1026 for (;;) {
1027 char *pos;
1028
1029 pos = strrchr(subdir, '/');
1030 if (pos == NULL || pos < &subdir[2])
1031 break;
1032 pos[0] = '\0';
1033 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
1034 if (udev_device_parent != NULL)
1035 return udev_device_parent;
1036 }
1037
1038 errno = ENOENT;
1039 return NULL;
1040 }
1041
1042 /**
1043 * udev_device_get_parent:
1044 * @udev_device: the device to start searching from
1045 *
1046 * Find the next parent device, and fill in information from the sys
1047 * device and the udev database entry.
1048 *
1049 * Returned device is not referenced. It is attached to the child
1050 * device, and will be cleaned up when the child device is cleaned up.
1051 *
1052 * It is not necessarily just the upper level directory, empty or not
1053 * recognized sys directories are ignored.
1054 *
1055 * It can be called as many times as needed, without caring about
1056 * references.
1057 *
1058 * Returns: a new udev device, or #NULL, if it no parent exist.
1059 **/
1060 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
1061 {
1062 if (udev_device == NULL) {
1063 errno = EINVAL;
1064 return NULL;
1065 }
1066 if (!udev_device->parent_set) {
1067 udev_device->parent_set = true;
1068 udev_device->parent_device = device_new_from_parent(udev_device);
1069 }
1070 return udev_device->parent_device;
1071 }
1072
1073 /**
1074 * udev_device_get_parent_with_subsystem_devtype:
1075 * @udev_device: udev device to start searching from
1076 * @subsystem: the subsystem of the device
1077 * @devtype: the type (DEVTYPE) of the device
1078 *
1079 * Find the next parent device, with a matching subsystem and devtype
1080 * value, and fill in information from the sys device and the udev
1081 * database entry.
1082 *
1083 * If devtype is #NULL, only subsystem is checked, and any devtype will
1084 * match.
1085 *
1086 * Returned device is not referenced. It is attached to the child
1087 * device, and will be cleaned up when the child device is cleaned up.
1088 *
1089 * It can be called as many times as needed, without caring about
1090 * references.
1091 *
1092 * Returns: a new udev device, or #NULL if no matching parent exists.
1093 **/
1094 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1095 {
1096 struct udev_device *parent;
1097
1098 if (subsystem == NULL) {
1099 errno = EINVAL;
1100 return NULL;
1101 }
1102
1103 parent = udev_device_get_parent(udev_device);
1104 while (parent != NULL) {
1105 const char *parent_subsystem;
1106 const char *parent_devtype;
1107
1108 parent_subsystem = udev_device_get_subsystem(parent);
1109 if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) {
1110 if (devtype == NULL)
1111 break;
1112 parent_devtype = udev_device_get_devtype(parent);
1113 if (parent_devtype != NULL && streq(parent_devtype, devtype))
1114 break;
1115 }
1116 parent = udev_device_get_parent(parent);
1117 }
1118
1119 if (!parent)
1120 errno = ENOENT;
1121
1122 return parent;
1123 }
1124
1125 /**
1126 * udev_device_get_udev:
1127 * @udev_device: udev device
1128 *
1129 * Retrieve the udev library context the device was created with.
1130 *
1131 * Returns: the udev library context
1132 **/
1133 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
1134 {
1135 if (udev_device == NULL)
1136 return NULL;
1137 return udev_device->udev;
1138 }
1139
1140 /**
1141 * udev_device_ref:
1142 * @udev_device: udev device
1143 *
1144 * Take a reference of a udev device.
1145 *
1146 * Returns: the passed udev device
1147 **/
1148 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
1149 {
1150 if (udev_device == NULL)
1151 return NULL;
1152 udev_device->refcount++;
1153 return udev_device;
1154 }
1155
1156 /**
1157 * udev_device_unref:
1158 * @udev_device: udev device
1159 *
1160 * Drop a reference of a udev device. If the refcount reaches zero,
1161 * the resources of the device will be released.
1162 *
1163 * Returns: #NULL
1164 **/
1165 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
1166 {
1167 if (udev_device == NULL)
1168 return NULL;
1169 udev_device->refcount--;
1170 if (udev_device->refcount > 0)
1171 return NULL;
1172 if (udev_device->parent_device != NULL)
1173 udev_device_unref(udev_device->parent_device);
1174 free(udev_device->syspath);
1175 free(udev_device->sysname);
1176 free(udev_device->devnode);
1177 free(udev_device->subsystem);
1178 free(udev_device->devtype);
1179 udev_list_cleanup(&udev_device->devlinks_list);
1180 udev_list_cleanup(&udev_device->properties_list);
1181 udev_list_cleanup(&udev_device->sysattr_value_list);
1182 udev_list_cleanup(&udev_device->sysattr_list);
1183 udev_list_cleanup(&udev_device->tags_list);
1184 free(udev_device->action);
1185 free(udev_device->driver);
1186 free(udev_device->devpath_old);
1187 free(udev_device->id_filename);
1188 free(udev_device->envp);
1189 free(udev_device->monitor_buf);
1190 free(udev_device);
1191 return NULL;
1192 }
1193
1194 /**
1195 * udev_device_get_devpath:
1196 * @udev_device: udev device
1197 *
1198 * Retrieve the kernel devpath value of the udev device. The path
1199 * does not contain the sys mount point, and starts with a '/'.
1200 *
1201 * Returns: the devpath of the udev device
1202 **/
1203 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
1204 {
1205 if (udev_device == NULL)
1206 return NULL;
1207 return udev_device->devpath;
1208 }
1209
1210 /**
1211 * udev_device_get_syspath:
1212 * @udev_device: udev device
1213 *
1214 * Retrieve the sys path of the udev device. The path is an
1215 * absolute path and starts with the sys mount point.
1216 *
1217 * Returns: the sys path of the udev device
1218 **/
1219 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
1220 {
1221 if (udev_device == NULL)
1222 return NULL;
1223 return udev_device->syspath;
1224 }
1225
1226 /**
1227 * udev_device_get_sysname:
1228 * @udev_device: udev device
1229 *
1230 * Get the kernel device name in /sys.
1231 *
1232 * Returns: the name string of the device device
1233 **/
1234 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
1235 {
1236 if (udev_device == NULL)
1237 return NULL;
1238 return udev_device->sysname;
1239 }
1240
1241 /**
1242 * udev_device_get_sysnum:
1243 * @udev_device: udev device
1244 *
1245 * Get the instance number of the device.
1246 *
1247 * Returns: the trailing number string of the device name
1248 **/
1249 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
1250 {
1251 if (udev_device == NULL)
1252 return NULL;
1253 return udev_device->sysnum;
1254 }
1255
1256 /**
1257 * udev_device_get_devnode:
1258 * @udev_device: udev device
1259 *
1260 * Retrieve the device node file name belonging to the udev device.
1261 * The path is an absolute path, and starts with the device directory.
1262 *
1263 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1264 **/
1265 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
1266 {
1267 if (udev_device == NULL)
1268 return NULL;
1269 if (udev_device->devnode != NULL)
1270 return udev_device->devnode;
1271 if (!udev_device->info_loaded)
1272 udev_device_read_uevent_file(udev_device);
1273 return udev_device->devnode;
1274 }
1275
1276 /**
1277 * udev_device_get_devlinks_list_entry:
1278 * @udev_device: udev device
1279 *
1280 * Retrieve the list of device links pointing to the device file of
1281 * the udev device. The next list entry can be retrieved with
1282 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1283 * The devlink path can be retrieved from the list entry by
1284 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1285 * the device directory.
1286 *
1287 * Returns: the first entry of the device node link list
1288 **/
1289 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1290 {
1291 if (udev_device == NULL)
1292 return NULL;
1293 if (!udev_device->info_loaded)
1294 udev_device_read_db(udev_device, NULL);
1295 return udev_list_get_entry(&udev_device->devlinks_list);
1296 }
1297
1298 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1299 {
1300 udev_device->devlinks_uptodate = false;
1301 udev_list_cleanup(&udev_device->devlinks_list);
1302 }
1303
1304 /**
1305 * udev_device_get_properties_list_entry:
1306 * @udev_device: udev device
1307 *
1308 * Retrieve the list of key/value device properties of the udev
1309 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1310 * which returns #NULL if no more entries exist. The property name
1311 * can be retrieved from the list entry by udev_list_entry_get_name(),
1312 * the property value by udev_list_entry_get_value().
1313 *
1314 * Returns: the first entry of the property list
1315 **/
1316 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1317 {
1318 if (udev_device == NULL)
1319 return NULL;
1320 if (!udev_device->info_loaded) {
1321 udev_device_read_uevent_file(udev_device);
1322 udev_device_read_db(udev_device, NULL);
1323 }
1324 if (!udev_device->devlinks_uptodate) {
1325 char symlinks[UTIL_PATH_SIZE];
1326 struct udev_list_entry *list_entry;
1327
1328 udev_device->devlinks_uptodate = true;
1329 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1330 if (list_entry != NULL) {
1331 char *s;
1332 size_t l;
1333
1334 s = symlinks;
1335 l = strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1336 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1337 l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1338 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1339 }
1340 }
1341 if (!udev_device->tags_uptodate) {
1342 udev_device->tags_uptodate = true;
1343 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1344 char tags[UTIL_PATH_SIZE];
1345 struct udev_list_entry *list_entry;
1346 char *s;
1347 size_t l;
1348
1349 s = tags;
1350 l = strpcpyl(&s, sizeof(tags), ":", NULL);
1351 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1352 l = strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1353 udev_device_add_property(udev_device, "TAGS", tags);
1354 }
1355 }
1356 return udev_list_get_entry(&udev_device->properties_list);
1357 }
1358
1359 /**
1360 * udev_device_get_action:
1361 * @udev_device: udev device
1362 *
1363 * This is only valid if the device was received through a monitor. Devices read from
1364 * sys do not have an action string. Usual actions are: add, remove, change, online,
1365 * offline.
1366 *
1367 * Returns: the kernel action value, or #NULL if there is no action value available.
1368 **/
1369 _public_ const char *udev_device_get_action(struct udev_device *udev_device)
1370 {
1371 if (udev_device == NULL)
1372 return NULL;
1373 return udev_device->action;
1374 }
1375
1376 /**
1377 * udev_device_get_usec_since_initialized:
1378 * @udev_device: udev device
1379 *
1380 * Return the number of microseconds passed since udev set up the
1381 * device for the first time.
1382 *
1383 * This is only implemented for devices with need to store properties
1384 * in the udev database. All other devices return 0 here.
1385 *
1386 * Returns: the number of microseconds since the device was first seen.
1387 **/
1388 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1389 {
1390 usec_t now_ts;
1391
1392 if (udev_device == NULL)
1393 return 0;
1394 if (!udev_device->info_loaded)
1395 udev_device_read_db(udev_device, NULL);
1396 if (udev_device->usec_initialized == 0)
1397 return 0;
1398 now_ts = now(CLOCK_MONOTONIC);
1399 if (now_ts == 0)
1400 return 0;
1401 return now_ts - udev_device->usec_initialized;
1402 }
1403
1404 usec_t udev_device_get_usec_initialized(struct udev_device *udev_device)
1405 {
1406 return udev_device->usec_initialized;
1407 }
1408
1409 void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized)
1410 {
1411 char num[32];
1412
1413 udev_device->usec_initialized = usec_initialized;
1414 snprintf(num, sizeof(num), USEC_FMT, usec_initialized);
1415 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1416 }
1417
1418 /**
1419 * udev_device_get_sysattr_value:
1420 * @udev_device: udev device
1421 * @sysattr: attribute name
1422 *
1423 * The retrieved value is cached in the device. Repeated calls will return the same
1424 * value and not open the attribute again.
1425 *
1426 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1427 **/
1428 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1429 {
1430 struct udev_list_entry *list_entry;
1431 char path[UTIL_PATH_SIZE];
1432 char value[4096];
1433 struct stat statbuf;
1434 int fd;
1435 ssize_t size;
1436 const char *val = NULL;
1437
1438 if (udev_device == NULL)
1439 return NULL;
1440 if (sysattr == NULL)
1441 return NULL;
1442
1443 /* look for possibly already cached result */
1444 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1445 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1446 if (list_entry != NULL)
1447 return udev_list_entry_get_value(list_entry);
1448
1449 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1450 if (lstat(path, &statbuf) != 0) {
1451 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1452 goto out;
1453 }
1454
1455 if (S_ISLNK(statbuf.st_mode)) {
1456 /*
1457 * Some core links return only the last element of the target path,
1458 * these are just values, the paths should not be exposed.
1459 */
1460 if (streq(sysattr, "driver") ||
1461 streq(sysattr, "subsystem") ||
1462 streq(sysattr, "module")) {
1463 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1464 udev_device->syspath, value, sizeof(value)) < 0)
1465 return NULL;
1466 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1467 val = udev_list_entry_get_value(list_entry);
1468 goto out;
1469 }
1470
1471 goto out;
1472 }
1473
1474 /* skip directories */
1475 if (S_ISDIR(statbuf.st_mode))
1476 goto out;
1477
1478 /* skip non-readable files */
1479 if ((statbuf.st_mode & S_IRUSR) == 0)
1480 goto out;
1481
1482 /* read attribute value */
1483 fd = open(path, O_RDONLY|O_CLOEXEC);
1484 if (fd < 0)
1485 goto out;
1486 size = read(fd, value, sizeof(value));
1487 close(fd);
1488 if (size < 0)
1489 goto out;
1490 if (size == sizeof(value))
1491 goto out;
1492
1493 /* got a valid value, store it in cache and return it */
1494 value[size] = '\0';
1495 util_remove_trailing_chars(value, '\n');
1496 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1497 val = udev_list_entry_get_value(list_entry);
1498 out:
1499 return val;
1500 }
1501
1502 /**
1503 * udev_device_set_sysattr_value:
1504 * @udev_device: udev device
1505 * @sysattr: attribute name
1506 * @value: new value to be set
1507 *
1508 * Update the contents of the sys attribute and the cached value of the device.
1509 *
1510 * Returns: Negative error code on failure or 0 on success.
1511 **/
1512 _public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, char *value)
1513 {
1514 struct udev_device *dev;
1515 char path[UTIL_PATH_SIZE];
1516 struct stat statbuf;
1517 int fd;
1518 ssize_t size, value_len;
1519 int ret = 0;
1520
1521 if (udev_device == NULL)
1522 return -EINVAL;
1523 dev = udev_device;
1524 if (sysattr == NULL)
1525 return -EINVAL;
1526 if (value == NULL)
1527 value_len = 0;
1528 else
1529 value_len = strlen(value);
1530
1531 strscpyl(path, sizeof(path), udev_device_get_syspath(dev), "/", sysattr, NULL);
1532 if (lstat(path, &statbuf) != 0) {
1533 udev_list_entry_add(&dev->sysattr_value_list, sysattr, NULL);
1534 ret = -ENXIO;
1535 goto out;
1536 }
1537
1538 if (S_ISLNK(statbuf.st_mode)) {
1539 ret = -EINVAL;
1540 goto out;
1541 }
1542
1543 /* skip directories */
1544 if (S_ISDIR(statbuf.st_mode)) {
1545 ret = -EISDIR;
1546 goto out;
1547 }
1548
1549 /* skip non-readable files */
1550 if ((statbuf.st_mode & S_IRUSR) == 0) {
1551 ret = -EACCES;
1552 goto out;
1553 }
1554
1555 /* Value is limited to 4k */
1556 if (value_len > 4096) {
1557 ret = -EINVAL;
1558 goto out;
1559 }
1560 util_remove_trailing_chars(value, '\n');
1561
1562 /* write attribute value */
1563 fd = open(path, O_WRONLY|O_CLOEXEC);
1564 if (fd < 0) {
1565 ret = -errno;
1566 goto out;
1567 }
1568 size = write(fd, value, value_len);
1569 close(fd);
1570 if (size < 0) {
1571 ret = -errno;
1572 goto out;
1573 }
1574 if (size < value_len) {
1575 ret = -EIO;
1576 goto out;
1577 }
1578
1579 /* wrote a valid value, store it in cache and return it */
1580 udev_list_entry_add(&dev->sysattr_value_list, sysattr, value);
1581 out:
1582 if (dev != udev_device)
1583 udev_device_unref(dev);
1584 return ret;
1585 }
1586
1587 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1588 {
1589 struct dirent *dent;
1590 DIR *dir;
1591 int num = 0;
1592
1593 if (udev_device == NULL)
1594 return -EINVAL;
1595 if (udev_device->sysattr_list_read)
1596 return 0;
1597
1598 dir = opendir(udev_device_get_syspath(udev_device));
1599 if (!dir)
1600 return -errno;
1601
1602 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1603 char path[UTIL_PATH_SIZE];
1604 struct stat statbuf;
1605
1606 /* only handle symlinks and regular files */
1607 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1608 continue;
1609
1610 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1611 if (lstat(path, &statbuf) != 0)
1612 continue;
1613 if ((statbuf.st_mode & S_IRUSR) == 0)
1614 continue;
1615
1616 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1617 num++;
1618 }
1619
1620 closedir(dir);
1621 udev_device->sysattr_list_read = true;
1622
1623 return num;
1624 }
1625
1626 /**
1627 * udev_device_get_sysattr_list_entry:
1628 * @udev_device: udev device
1629 *
1630 * Retrieve the list of available sysattrs, with value being empty;
1631 * This just return all available sysfs attributes for a particular
1632 * device without reading their values.
1633 *
1634 * Returns: the first entry of the property list
1635 **/
1636 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1637 {
1638 if (!udev_device->sysattr_list_read) {
1639 int ret;
1640 ret = udev_device_sysattr_list_read(udev_device);
1641 if (0 > ret)
1642 return NULL;
1643 }
1644
1645 return udev_list_get_entry(&udev_device->sysattr_list);
1646 }
1647
1648 static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1649 {
1650 free(udev_device->devnode);
1651 if (devnode[0] != '/') {
1652 if (asprintf(&udev_device->devnode, "/dev/%s", devnode) < 0)
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;
1661 }
1662
1663 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1664 {
1665 struct udev_list_entry *list_entry;
1666
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;
1671 return 0;
1672 }
1673
1674 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1675 {
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",
1683 streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c',
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 */
1689 if (asprintf(&udev_device->id_filename, "n%i", udev_device_get_ifindex(udev_device)) < 0)
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;
1706 }
1707
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 *
1716 * This is only implemented for devices with a device node
1717 * or network interfaces. All other devices return 1 here.
1718 *
1719 * Returns: 1 if the device is set up. 0 otherwise.
1720 **/
1721 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
1722 {
1723 if (!udev_device->info_loaded)
1724 udev_device_read_db(udev_device, NULL);
1725 return udev_device->is_initialized;
1726 }
1727
1728 void udev_device_set_is_initialized(struct udev_device *udev_device)
1729 {
1730 udev_device->is_initialized = true;
1731 }
1732
1733 static bool is_valid_tag(const char *tag)
1734 {
1735 return !strchr(tag, ':') && !strchr(tag, ' ');
1736 }
1737
1738 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1739 {
1740 if (!is_valid_tag(tag))
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;
1746 }
1747
1748 void 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
1762 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1763 {
1764 udev_device->tags_uptodate = false;
1765 udev_list_cleanup(&udev_device->tags_list);
1766 }
1767
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
1773 * list entry can be retrieved with udev_list_entry_get_next(),
1774 * which returns #NULL if no more entries exist. The tag string
1775 * can be retrieved from the list entry by udev_list_entry_get_name().
1776 *
1777 * Returns: the first entry of the tag list
1778 **/
1779 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1780 {
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);
1786 }
1787
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 **/
1797 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1798 {
1799 struct udev_list_entry *list_entry;
1800
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;
1809 }
1810
1811 #define ENVP_SIZE 128
1812 #define MONITOR_BUF_SIZE 4096
1813 static int update_envp_monitor_buf(struct udev_device *udev_device)
1814 {
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 */
1850 l = strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1851 if (l == 0)
1852 return -EINVAL;
1853 /* advance past the trailing '\0' that strpcpyl() guarantees */
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;
1860 return 0;
1861 }
1862
1863 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1864 {
1865 if (!udev_device->envp_uptodate)
1866 if (update_envp_monitor_buf(udev_device) != 0)
1867 return NULL;
1868 return udev_device->envp;
1869 }
1870
1871 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1872 {
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;
1878 }
1879
1880 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1881 {
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;
1888 }
1889
1890 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1891 {
1892 if (!udev_device->info_loaded)
1893 udev_device_read_db(udev_device, NULL);
1894 return udev_device->devlink_priority;
1895 }
1896
1897 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1898 {
1899 udev_device->devlink_priority = prio;
1900 return 0;
1901 }
1902
1903 int udev_device_get_watch_handle(struct udev_device *udev_device)
1904 {
1905 if (!udev_device->info_loaded)
1906 udev_device_read_db(udev_device, NULL);
1907 return udev_device->watch_handle;
1908 }
1909
1910 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1911 {
1912 udev_device->watch_handle = handle;
1913 return 0;
1914 }
1915
1916 bool udev_device_get_db_persist(struct udev_device *udev_device)
1917 {
1918 return udev_device->db_persist;
1919 }
1920
1921 void udev_device_set_db_persist(struct udev_device *udev_device)
1922 {
1923 udev_device->db_persist = true;
1924 }
1925
1926 int 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 = strjoina(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 }
1947
1948 struct udev_device *udev_device_shallow_clone(struct udev_device *old_device)
1949 {
1950 struct udev_device *device;
1951
1952 if (old_device == NULL)
1953 return NULL;
1954
1955 device = udev_device_new(old_device->udev);
1956 if (!device) {
1957 errno = ENOMEM;
1958
1959 return NULL;
1960 }
1961
1962 udev_device_set_syspath(device, udev_device_get_syspath(old_device));
1963 udev_device_set_subsystem(device, udev_device_get_subsystem(old_device));
1964 udev_device_set_devnum(device, udev_device_get_devnum(old_device));
1965
1966 return device;
1967 }
1968
1969 struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr, ssize_t buflen) {
1970 struct udev_device *device;
1971 ssize_t bufpos = 0;
1972
1973 if (nulstr == NULL || buflen <= 0) {
1974 errno = EINVAL;
1975
1976 return NULL;
1977 }
1978
1979 device = udev_device_new(udev);
1980 if (!device) {
1981 errno = ENOMEM;
1982
1983 return NULL;
1984 }
1985
1986 udev_device_set_info_loaded(device);
1987
1988 while (bufpos < buflen) {
1989 char *key;
1990 size_t keylen;
1991
1992 key = nulstr + bufpos;
1993 keylen = strlen(key);
1994 if (keylen == 0)
1995 break;
1996
1997 bufpos += keylen + 1;
1998 udev_device_add_property_from_string_parse(device, key);
1999 }
2000
2001 if (udev_device_add_property_from_string_parse_finish(device) < 0) {
2002 log_debug("missing values, invalid device");
2003
2004 udev_device_unref(device);
2005
2006 errno = EINVAL;
2007
2008 return NULL;
2009 }
2010
2011 return device;
2012 }