]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/udev-util.c
udev: warn when we fail to parse udev.conf
[thirdparty/systemd.git] / src / shared / udev-util.c
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 DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);
21
22 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 _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL;
29 int r;
30
31 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 if (r == -ENOENT)
38 return 0;
39 if (r < 0)
40 return r;
41
42 if (log_val) {
43 const char *log;
44 size_t n;
45
46 /* unquote */
47 n = strlen(log_val);
48 if (n >= 2 &&
49 ((log_val[0] == '"' && log_val[n-1] == '"') ||
50 (log_val[0] == '\'' && log_val[n-1] == '\''))) {
51 log_val[n - 1] = '\0';
52 log = log_val + 1;
53 } else
54 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 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
59 if (r < 0)
60 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 if (ret_children_max && children_max) {
65 r = safe_atou(children_max, ret_children_max);
66 if (r < 0)
67 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 if (ret_exec_delay_usec && exec_delay) {
72 r = parse_sec(exec_delay, ret_exec_delay_usec);
73 if (r < 0)
74 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 if (ret_event_timeout_usec && event_timeout) {
79 r = parse_sec(event_timeout, ret_event_timeout_usec);
80 if (r < 0)
81 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 if (ret_resolve_name_timing && resolve_names) {
86 ResolveNameTiming t;
87
88 t = resolve_name_timing_from_string(resolve_names);
89 if (t < 0)
90 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 *ret_resolve_name_timing = t;
94 }
95
96 return 0;
97 }
98
99 struct DeviceMonitorData {
100 const char *sysname;
101 sd_device *device;
102 };
103
104 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
105 struct DeviceMonitorData *data = userdata;
106 const char *sysname;
107
108 assert(device);
109 assert(data);
110 assert(data->sysname);
111 assert(!data->device);
112
113 if (sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) {
114 data->device = sd_device_ref(device);
115 return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
116 }
117
118 return 0;
119 }
120
121 static int device_timeout_handler(sd_event_source *s, uint64_t usec, void *userdata) {
122 return sd_event_exit(sd_event_source_get_event(s), -ETIMEDOUT);
123 }
124
125 int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret) {
126 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
127 _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL;
128 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
129 struct DeviceMonitorData data = {};
130 int r;
131
132 assert(device);
133
134 if (sd_device_get_is_initialized(device) > 0) {
135 if (ret)
136 *ret = sd_device_ref(device);
137 return 0;
138 }
139
140 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 r = sd_event_new(&event);
145 if (r < 0)
146 return log_error_errno(r, "Failed to get default event: %m");
147
148 r = sd_device_monitor_new(&monitor);
149 if (r < 0)
150 return log_error_errno(r, "Failed to acquire monitor: %m");
151
152 if (!subsystem) {
153 r = sd_device_get_subsystem(device, &subsystem);
154 if (r < 0 && r != -ENOENT)
155 return log_device_error_errno(device, r, "Failed to get subsystem: %m");
156 }
157
158 if (subsystem) {
159 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
160 if (r < 0)
161 return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
162 }
163
164 r = sd_device_monitor_attach_event(monitor, event);
165 if (r < 0)
166 return log_error_errno(r, "Failed to attach event to device monitor: %m");
167
168 r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
169 if (r < 0)
170 return log_error_errno(r, "Failed to start device monitor: %m");
171
172 if (timeout != USEC_INFINITY) {
173 r = sd_event_add_time(event, &timeout_source,
174 CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + timeout, 0,
175 device_timeout_handler, NULL);
176 if (r < 0)
177 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 if (sd_device_get_is_initialized(device) > 0) {
183 if (ret)
184 *ret = sd_device_ref(device);
185 return 0;
186 }
187
188 r = sd_event_loop(event);
189 if (r < 0)
190 return log_error_errno(r, "Failed to wait for device to be initialized: %m");
191
192 if (ret)
193 *ret = TAKE_PTR(data.device);
194 return 0;
195 }
196
197 int device_is_renaming(sd_device *dev) {
198 int r;
199
200 assert(dev);
201
202 r = sd_device_get_property_value(dev, "ID_RENAMING", NULL);
203 if (r < 0 && r != -ENOENT)
204 return r;
205
206 return r >= 0;
207 }
208
209 bool device_for_action(sd_device *dev, DeviceAction action) {
210 DeviceAction a;
211
212 assert(dev);
213
214 if (device_get_action(dev, &a) < 0)
215 return false;
216
217 return a == action;
218 }