Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <errno.h>
4 :
5 : #include "alloc-util.h"
6 : #include "device-util.h"
7 : #include "env-file.h"
8 : #include "log.h"
9 : #include "parse-util.h"
10 : #include "string-table.h"
11 : #include "string-util.h"
12 : #include "udev-util.h"
13 :
14 : static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = {
15 : [RESOLVE_NAME_NEVER] = "never",
16 : [RESOLVE_NAME_LATE] = "late",
17 : [RESOLVE_NAME_EARLY] = "early",
18 : };
19 :
20 0 : DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);
21 :
22 8 : int udev_parse_config_full(
23 : unsigned *ret_children_max,
24 : usec_t *ret_exec_delay_usec,
25 : usec_t *ret_event_timeout_usec,
26 : ResolveNameTiming *ret_resolve_name_timing) {
27 :
28 8 : _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL;
29 : int r;
30 :
31 8 : r = parse_env_file(NULL, "/etc/udev/udev.conf",
32 : "udev_log", &log_val,
33 : "children_max", &children_max,
34 : "exec_delay", &exec_delay,
35 : "event_timeout", &event_timeout,
36 : "resolve_names", &resolve_names);
37 8 : if (r == -ENOENT)
38 0 : return 0;
39 8 : if (r < 0)
40 0 : return r;
41 :
42 8 : if (log_val) {
43 : const char *log;
44 : size_t n;
45 :
46 : /* unquote */
47 0 : n = strlen(log_val);
48 0 : if (n >= 2 &&
49 0 : ((log_val[0] == '"' && log_val[n-1] == '"') ||
50 0 : (log_val[0] == '\'' && log_val[n-1] == '\''))) {
51 0 : log_val[n - 1] = '\0';
52 0 : log = log_val + 1;
53 : } else
54 0 : log = log_val;
55 :
56 : /* we set the udev log level here explicitly, this is supposed
57 : * to regulate the code in libudev/ and udev/. */
58 0 : r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
59 0 : if (r < 0)
60 0 : log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
61 : "failed to set udev log level '%s', ignoring: %m", log);
62 : }
63 :
64 8 : if (ret_children_max && children_max) {
65 0 : r = safe_atou(children_max, ret_children_max);
66 0 : if (r < 0)
67 0 : log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
68 : "failed to set parse children_max=%s, ignoring: %m", children_max);
69 : }
70 :
71 8 : if (ret_exec_delay_usec && exec_delay) {
72 0 : r = parse_sec(exec_delay, ret_exec_delay_usec);
73 0 : if (r < 0)
74 0 : log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
75 : "failed to set parse exec_delay=%s, ignoring: %m", exec_delay);
76 : }
77 :
78 8 : if (ret_event_timeout_usec && event_timeout) {
79 0 : r = parse_sec(event_timeout, ret_event_timeout_usec);
80 0 : if (r < 0)
81 0 : log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
82 : "failed to set parse event_timeout=%s, ignoring: %m", event_timeout);
83 : }
84 :
85 8 : if (ret_resolve_name_timing && resolve_names) {
86 : ResolveNameTiming t;
87 :
88 0 : t = resolve_name_timing_from_string(resolve_names);
89 0 : if (t < 0)
90 0 : log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
91 : "failed to set parse resolve_names=%s, ignoring.", resolve_names);
92 : else
93 0 : *ret_resolve_name_timing = t;
94 : }
95 :
96 8 : return 0;
97 : }
98 :
99 : struct DeviceMonitorData {
100 : const char *sysname;
101 : sd_device *device;
102 : };
103 :
104 0 : static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
105 0 : struct DeviceMonitorData *data = userdata;
106 : const char *sysname;
107 :
108 0 : assert(device);
109 0 : assert(data);
110 0 : assert(data->sysname);
111 0 : assert(!data->device);
112 :
113 0 : if (sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) {
114 0 : data->device = sd_device_ref(device);
115 0 : return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
116 : }
117 :
118 0 : return 0;
119 : }
120 :
121 0 : static int device_timeout_handler(sd_event_source *s, uint64_t usec, void *userdata) {
122 0 : return sd_event_exit(sd_event_source_get_event(s), -ETIMEDOUT);
123 : }
124 :
125 0 : int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret) {
126 0 : _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
127 0 : _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL;
128 0 : _cleanup_(sd_event_unrefp) sd_event *event = NULL;
129 0 : struct DeviceMonitorData data = {};
130 : int r;
131 :
132 0 : assert(device);
133 :
134 0 : if (sd_device_get_is_initialized(device) > 0) {
135 0 : if (ret)
136 0 : *ret = sd_device_ref(device);
137 0 : return 0;
138 : }
139 :
140 0 : assert_se(sd_device_get_sysname(device, &data.sysname) >= 0);
141 :
142 : /* Wait until the device is initialized, so that we can get access to the ID_PATH property */
143 :
144 0 : r = sd_event_new(&event);
145 0 : if (r < 0)
146 0 : return log_error_errno(r, "Failed to get default event: %m");
147 :
148 0 : r = sd_device_monitor_new(&monitor);
149 0 : if (r < 0)
150 0 : return log_error_errno(r, "Failed to acquire monitor: %m");
151 :
152 0 : if (!subsystem) {
153 0 : r = sd_device_get_subsystem(device, &subsystem);
154 0 : if (r < 0 && r != -ENOENT)
155 0 : return log_device_error_errno(device, r, "Failed to get subsystem: %m");
156 : }
157 :
158 0 : if (subsystem) {
159 0 : r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
160 0 : if (r < 0)
161 0 : return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
162 : }
163 :
164 0 : r = sd_device_monitor_attach_event(monitor, event);
165 0 : if (r < 0)
166 0 : return log_error_errno(r, "Failed to attach event to device monitor: %m");
167 :
168 0 : r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
169 0 : if (r < 0)
170 0 : return log_error_errno(r, "Failed to start device monitor: %m");
171 :
172 0 : if (timeout != USEC_INFINITY) {
173 0 : r = sd_event_add_time(event, &timeout_source,
174 0 : CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + timeout, 0,
175 : device_timeout_handler, NULL);
176 0 : if (r < 0)
177 0 : return log_error_errno(r, "Failed to add timeout event source: %m");
178 : }
179 :
180 : /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized
181 : * yet. */
182 0 : if (sd_device_get_is_initialized(device) > 0) {
183 0 : if (ret)
184 0 : *ret = sd_device_ref(device);
185 0 : return 0;
186 : }
187 :
188 0 : r = sd_event_loop(event);
189 0 : if (r < 0)
190 0 : return log_error_errno(r, "Failed to wait for device to be initialized: %m");
191 :
192 0 : if (ret)
193 0 : *ret = TAKE_PTR(data.device);
194 0 : return 0;
195 : }
196 :
197 611 : int device_is_renaming(sd_device *dev) {
198 : int r;
199 :
200 611 : assert(dev);
201 :
202 611 : r = sd_device_get_property_value(dev, "ID_RENAMING", NULL);
203 611 : if (r < 0 && r != -ENOENT)
204 0 : return r;
205 :
206 611 : return r >= 0;
207 : }
208 :
209 0 : bool device_for_action(sd_device *dev, DeviceAction action) {
210 : DeviceAction a;
211 :
212 0 : assert(dev);
213 :
214 0 : if (device_get_action(dev, &a) < 0)
215 0 : return false;
216 :
217 0 : return a == action;
218 : }
|