1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-device.h"
5 
6 #include "device-private.h"
7 #include "hashmap.h"
8 #include "set.h"
9 #include "time-util.h"
10 
11 #define LATEST_UDEV_DATABASE_VERSION 1
12 
13 struct sd_device {
14         unsigned n_ref;
15 
16         /* The database version indicates the supported features by the udev database.
17          * This is saved and parsed in V field.
18          *
19          * 0: None of the following features are supported (systemd version <= 246).
20          * 1: The current tags (Q) and the database version (V) features are implemented (>= 247).
21          */
22         unsigned database_version;
23 
24         int watch_handle;
25 
26         sd_device *parent;
27 
28         OrderedHashmap *properties;
29         Iterator properties_iterator;
30         uint64_t properties_generation; /* changes whenever the properties are changed */
31         uint64_t properties_iterator_generation; /* generation when iteration was started */
32 
33         /* the subset of the properties that should be written to the db */
34         OrderedHashmap *properties_db;
35 
36         Hashmap *sysattr_values; /* cached sysattr values */
37 
38         Set *sysattrs; /* names of sysattrs */
39         Iterator sysattrs_iterator;
40 
41         Set *all_tags, *current_tags;
42         Iterator all_tags_iterator, current_tags_iterator;
43         uint64_t all_tags_iterator_generation, current_tags_iterator_generation; /* generation when iteration was started */
44         uint64_t tags_generation; /* changes whenever the tags are changed */
45 
46         Set *devlinks;
47         Iterator devlinks_iterator;
48         uint64_t devlinks_generation; /* changes whenever the devlinks are changed */
49         uint64_t devlinks_iterator_generation; /* generation when iteration was started */
50         int devlink_priority;
51 
52         int ifindex;
53         char *devtype;
54         char *devname;
55         dev_t devnum;
56 
57         char **properties_strv; /* the properties hashmap as a strv */
58         uint8_t *properties_nulstr; /* the same as a nulstr */
59         size_t properties_nulstr_len;
60 
61         char *syspath;
62         const char *devpath;
63         const char *sysnum;
64         char *sysname;
65 
66         char *subsystem;
67         char *driver_subsystem; /* only set for the 'drivers' subsystem */
68         char *driver;
69 
70         char *device_id;
71 
72         usec_t usec_initialized;
73 
74         mode_t devmode;
75         uid_t devuid;
76         gid_t devgid;
77 
78         uint64_t diskseq; /* Block device sequence number, monothonically incremented by the kernel on create/attach */
79 
80         /* only set when device is passed through netlink */
81         sd_device_action_t action;
82         uint64_t seqnum;
83 
84         bool parent_set:1; /* no need to try to reload parent */
85         bool sysattrs_read:1; /* don't try to re-read sysattrs once read */
86         bool property_tags_outdated:1; /* need to update TAGS= or CURRENT_TAGS= property */
87         bool property_devlinks_outdated:1; /* need to update DEVLINKS= property */
88         bool properties_buf_outdated:1; /* need to reread hashmap */
89         bool subsystem_set:1; /* don't reread subsystem */
90         bool driver_set:1; /* don't reread driver */
91         bool uevent_loaded:1; /* don't reread uevent */
92         bool db_loaded; /* don't reread db */
93 
94         bool is_initialized:1;
95         bool sealed:1; /* don't read more information from uevent/db */
96         bool db_persist:1; /* don't clean up the db when switching from initrd to real root */
97 };
98 
99 int device_new_aux(sd_device **ret);
100 int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db);
device_add_property_internal(sd_device * device,const char * key,const char * value)101 static inline int device_add_property_internal(sd_device *device, const char *key, const char *value) {
102         return device_add_property_aux(device, key, value, false);
103 }
104 
105 int device_set_syspath(sd_device *device, const char *_syspath, bool verify);
106 int device_set_ifindex(sd_device *device, const char *ifindex);
107 int device_set_devmode(sd_device *device, const char *devmode);
108 int device_set_devname(sd_device *device, const char *devname);
109 int device_set_devtype(sd_device *device, const char *devtype);
110 int device_set_devnum(sd_device *device, const char *major, const char *minor);
111 int device_set_subsystem(sd_device *device, const char *subsystem);
112 int device_set_diskseq(sd_device *device, const char *str);
113 int device_set_drivers_subsystem(sd_device *device);
114 int device_set_driver(sd_device *device, const char *driver);
115 int device_set_usec_initialized(sd_device *device, usec_t when);
116