Debian Codegraph @

src/udev/udev-format.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <sys/sysmacros.h>
4
5 #include "alloc-util.h"
6 #include "device-util.h"
7 #include "errno-util.h"
8 #include "parse-util.h"
9 #include "string-util.h"
10 #include "strxcpyx.h"
11 #include "udev-event.h"
12 #include "udev-format.h"
13 #include "udev-util.h"
14 #include "udev-worker.h"
15
16 typedef enum {
17 FORMAT_SUBST_DEVNODE,
18 FORMAT_SUBST_ATTR,
19 FORMAT_SUBST_ENV,
20 FORMAT_SUBST_KERNEL,
21 FORMAT_SUBST_KERNEL_NUMBER,
22 FORMAT_SUBST_DRIVER,
23 FORMAT_SUBST_DEVPATH,
24 FORMAT_SUBST_ID,
25 FORMAT_SUBST_MAJOR,
26 FORMAT_SUBST_MINOR,
27 FORMAT_SUBST_RESULT,
28 FORMAT_SUBST_PARENT,
29 FORMAT_SUBST_NAME,
30 FORMAT_SUBST_LINKS,
31 FORMAT_SUBST_ROOT,
32 FORMAT_SUBST_SYS,
33 _FORMAT_SUBST_TYPE_MAX,
34 _FORMAT_SUBST_TYPE_INVALID = -EINVAL,
35 } FormatSubstitutionType;
36
37 struct subst_map_entry {
38 const char *name;
39 const char fmt;
40 FormatSubstitutionType type;
41 };
42
43 static const struct subst_map_entry map[] = {
44 { .name = "devnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE },
45 { .name = "tempnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE }, /* deprecated */
46 { .name = "attr", .fmt = 's', .type = FORMAT_SUBST_ATTR },
47 { .name = "sysfs", .fmt = 's', .type = FORMAT_SUBST_ATTR }, /* deprecated */
48 { .name = "env", .fmt = 'E', .type = FORMAT_SUBST_ENV },
49 { .name = "kernel", .fmt = 'k', .type = FORMAT_SUBST_KERNEL },
50 { .name = "number", .fmt = 'n', .type = FORMAT_SUBST_KERNEL_NUMBER },
51 { .name = "driver", .fmt = 'd', .type = FORMAT_SUBST_DRIVER },
52 { .name = "devpath", .fmt = 'p', .type = FORMAT_SUBST_DEVPATH },
53 { .name = "id", .fmt = 'b', .type = FORMAT_SUBST_ID },
54 { .name = "major", .fmt = 'M', .type = FORMAT_SUBST_MAJOR },
55 { .name = "minor", .fmt = 'm', .type = FORMAT_SUBST_MINOR },
56 { .name = "result", .fmt = 'c', .type = FORMAT_SUBST_RESULT },
57 { .name = "parent", .fmt = 'P', .type = FORMAT_SUBST_PARENT },
58 { .name = "name", .fmt = 'D', .type = FORMAT_SUBST_NAME },
59 { .name = "links", .fmt = 'L', .type = FORMAT_SUBST_LINKS },
60 { .name = "root", .fmt = 'r', .type = FORMAT_SUBST_ROOT },
61 { .name = "sys", .fmt = 'S', .type = FORMAT_SUBST_SYS },
62 };
63
64 static const char* format_type_to_string(FormatSubstitutionType t) {
65 FOREACH_ELEMENT(entry, map)
66 if (entry->type == t)
67 return entry->name;
68 return NULL;
69 }
70
71 static char format_type_to_char(FormatSubstitutionType t) {
72 FOREACH_ELEMENT(entry, map)
73 if (entry->type == t)
74 return entry->fmt;
75 return '\0';
76 }
77
78 static int get_subst_type(const char **str, bool strict, FormatSubstitutionType *ret_type, char ret_attr[static UDEV_PATH_SIZE]) {
79 const char *p = *str, *q = NULL;
80 size_t i;
81
82 assert(str);
83 assert(*str);
84 assert(ret_type);
85 assert(ret_attr);
86
87 if (*p == '$') {
88 p++;
89 if (*p == '$') {
90 *str = p;
91 return 0;
92 }
93 for (i = 0; i < ELEMENTSOF(map); i++)
94 if ((q = startswith(p, map[i].name)))
95 break;
96 } else if (*p == '%') {
97 p++;
98 if (*p == '%') {
99 *str = p;
100 return 0;
101 }
102
103 for (i = 0; i < ELEMENTSOF(map); i++)
104 if (*p == map[i].fmt) {
105 q = p + 1;
106 break;
107 }
108 } else
109 return 0;
110 if (!q)
111 /* When 'strict' flag is set, then '$' and '%' must be escaped. */
112 return strict ? -EINVAL : 0;
113
114 if (*q == '{') {
115 const char *start, *end;
116 size_t len;
117
118 start = q + 1;
119 end = strchr(start, '}');
120 if (!end)
121 return -EINVAL;
122
123 len = end - start;
124 if (len == 0 || len >= UDEV_PATH_SIZE)
125 return -EINVAL;
126
127 strnscpy(ret_attr, UDEV_PATH_SIZE, start, len);
128 q = end + 1;
129 } else
130 *ret_attr = '\0';
131
132 *str = q;
133 *ret_type = map[i].type;
134 return 1;
135 }
136
137 static int safe_atou_optional_plus(const char *s, unsigned *ret) {
138 const char *p;
139 int r;
140
141 assert(s);
142 assert(ret);
143
144 /* Returns 1 if plus, 0 if no plus, negative on error */
145
146 p = endswith(s, "+");
147 if (p)
148 s = strndupa_safe(s, p - s);
149
150 r = safe_atou(s, ret);
151 if (r < 0)
152 return r;
153
154 return !!p;
155 }
156
157 static ssize_t udev_event_subst_format(
158 UdevEvent *event,
159 FormatSubstitutionType type,
160 const char *attr,
161 char *dest,
162 size_t l,
163 bool *ret_truncated) {
164
165 sd_device *parent, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
166 const char *val = NULL;
167 bool truncated = false;
168 char *s = dest;
169 int r;
170
171 switch (type) {
172 case FORMAT_SUBST_DEVPATH:
173 r = sd_device_get_devpath(dev, &val);
174 if (r < 0)
175 return r;
176 strpcpy_full(&s, l, val, &truncated);
177 break;
178 case FORMAT_SUBST_KERNEL:
179 r = sd_device_get_sysname(dev, &val);
180 if (r < 0)
181 return r;
182 strpcpy_full(&s, l, val, &truncated);
183 break;
184 case FORMAT_SUBST_KERNEL_NUMBER:
185 r = sd_device_get_sysnum(dev, &val);
186 if (r == -ENOENT)
187 goto null_terminate;
188 if (r < 0)
189 return r;
190 strpcpy_full(&s, l, val, &truncated);
191 break;
192 case FORMAT_SUBST_ID:
193 if (!event->dev_parent)
194 goto null_terminate;
195 r = sd_device_get_sysname(event->dev_parent, &val);
196 if (r < 0)
197 return r;
198 strpcpy_full(&s, l, val, &truncated);
199 break;
200 case FORMAT_SUBST_DRIVER:
201 if (!event->dev_parent)
202 goto null_terminate;
203 r = sd_device_get_driver(event->dev_parent, &val);
204 if (r == -ENOENT)
205 goto null_terminate;
206 if (r < 0)
207 return r;
208 strpcpy_full(&s, l, val, &truncated);
209 break;
210 case FORMAT_SUBST_MAJOR:
211 case FORMAT_SUBST_MINOR: {
212 dev_t devnum;
213
214 r = sd_device_get_devnum(dev, &devnum);
215 if (r < 0 && r != -ENOENT)
216 return r;
217 strpcpyf_full(&s, l, &truncated, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum));
218 break;
219 }
220 case FORMAT_SUBST_RESULT: {
221 unsigned index = 0; /* 0 means whole string */
222 bool has_plus;
223
224 if (!event->program_result)
225 goto null_terminate;
226
227 if (!isempty(attr)) {
228 r = safe_atou_optional_plus(attr, &index);
229 if (r < 0)
230 return r;
231
232 has_plus = r;
233 }
234
235 if (index == 0)
236 strpcpy_full(&s, l, event->program_result, &truncated);
237 else {
238 const char *start, *p;
239 unsigned i;
240
241 p = skip_leading_chars(event->program_result, NULL);
242
243 for (i = 1; i < index; i++) {
244 while (*p && !strchr(WHITESPACE, *p))
245 p++;
246 p = skip_leading_chars(p, NULL);
247 if (*p == '\0')
248 break;
249 }
250 if (i != index) {
251 log_device_debug(dev, "requested part of result string not found");
252 goto null_terminate;
253 }
254
255 start = p;
256 /* %c{2+} copies the whole string from the second part on */
257 if (has_plus)
258 strpcpy_full(&s, l, start, &truncated);
259 else {
260 while (*p && !strchr(WHITESPACE, *p))
261 p++;
262 strnpcpy_full(&s, l, start, p - start, &truncated);
263 }
264 }
265 break;
266 }
267 case FORMAT_SUBST_ATTR: {
268 char vbuf[UDEV_NAME_SIZE];
269 int count;
270 bool t;
271
272 if (isempty(attr))
273 return -EINVAL;
274
275 /* try to read the value specified by "[dmi/id]product_name" */
276 if (udev_resolve_subsys_kernel(attr, vbuf, sizeof(vbuf), true) == 0)
277 val = vbuf;
278
279 /* try to read the attribute the device */
280 if (!val)
281 (void) sd_device_get_sysattr_value(dev, attr, &val);
282
283 /* try to read the attribute of the parent device, other matches have selected */
284 if (!val && event->dev_parent && event->dev_parent != dev)
285 (void) sd_device_get_sysattr_value(event->dev_parent, attr, &val);
286
287 if (!val)
288 goto null_terminate;
289
290 /* strip trailing whitespace, and replace unwanted characters */
291 if (val != vbuf)
292 strscpy_full(vbuf, sizeof(vbuf), val, &truncated);
293 delete_trailing_chars(vbuf, NULL);
294 count = udev_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
295 if (count > 0)
296 log_device_debug(dev, "%i character(s) replaced", count);
297 strpcpy_full(&s, l, vbuf, &t);
298 truncated = truncated || t;
299 break;
300 }
301 case FORMAT_SUBST_PARENT:
302 r = sd_device_get_parent(dev, &parent);
303 if (r == -ENOENT)
304 goto null_terminate;
305 if (r < 0)
306 return r;
307 r = sd_device_get_devname(parent, &val);
308 if (r == -ENOENT)
309 goto null_terminate;
310 if (r < 0)
311 return r;
312 strpcpy_full(&s, l, val + STRLEN("/dev/"), &truncated);
313 break;
314 case FORMAT_SUBST_DEVNODE:
315 r = sd_device_get_devname(dev, &val);
316 if (r == -ENOENT)
317 goto null_terminate;
318 if (r < 0)
319 return r;
320 strpcpy_full(&s, l, val, &truncated);
321 break;
322 case FORMAT_SUBST_NAME:
323 if (event->name)
324 strpcpy_full(&s, l, event->name, &truncated);
325 else if (sd_device_get_devname(dev, &val) >= 0)
326 strpcpy_full(&s, l, val + STRLEN("/dev/"), &truncated);
327 else {
328 r = sd_device_get_sysname(dev, &val);
329 if (r < 0)
330 return r;
331 strpcpy_full(&s, l, val, &truncated);
332 }
333 break;
334 case FORMAT_SUBST_LINKS:
335 FOREACH_DEVICE_DEVLINK(dev, link) {
336 if (s == dest)
337 strpcpy_full(&s, l, link + STRLEN("/dev/"), &truncated);
338 else
339 strpcpyl_full(&s, l, &truncated, " ", link + STRLEN("/dev/"), NULL);
340 if (truncated)
341 break;
342 }
343 if (s == dest)
344 goto null_terminate;
345 break;
346 case FORMAT_SUBST_ROOT:
347 strpcpy_full(&s, l, "/dev", &truncated);
348 break;
349 case FORMAT_SUBST_SYS:
350 strpcpy_full(&s, l, "/sys", &truncated);
351 break;
352 case FORMAT_SUBST_ENV:
353 if (isempty(attr))
354 return -EINVAL;
355 r = device_get_property_value_with_fallback(dev, attr, event->worker ? event->worker->properties : NULL, &val);
356 if (r == -ENOENT)
357 goto null_terminate;
358 if (r < 0)
359 return r;
360 strpcpy_full(&s, l, val, &truncated);
361 break;
362 default:
363 assert_not_reached();
364 }
365
366 if (ret_truncated)
367 *ret_truncated = truncated;
368
369 return s - dest;
370
371 null_terminate:
372 if (ret_truncated)
373 *ret_truncated = truncated;
374
375 *s = '\0';
376 return 0;
377 }
378
379 size_t udev_event_apply_format(
380 UdevEvent *event,
381 const char *src,
382 char *dest,
383 size_t size,
384 bool replace_whitespace,
385 bool *ret_truncated) {
386
387 bool truncated = false;
388 const char *s = ASSERT_PTR(src);
389 int r;
390
391 assert(event);
392 assert(event->dev);
393 assert(dest);
394 assert(size > 0);
395
396 while (*s) {
397 FormatSubstitutionType type;
398 char attr[UDEV_PATH_SIZE];
399 ssize_t subst_len;
400 bool t;
401
402 r = get_subst_type(&s, false, &type, attr);
403 if (r < 0) {
404 log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src);
405 break;
406 } else if (r == 0) {
407 if (size < 2) {
408 /* need space for this char and the terminating NUL */
409 truncated = true;
410 break;
411 }
412 *dest++ = *s++;
413 size--;
414 continue;
415 }
416
417 subst_len = udev_event_subst_format(event, type, attr, dest, size, &t);
418 if (subst_len < 0) {
419 log_device_warning_errno(event->dev, subst_len,
420 "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
421 format_type_to_string(type), format_type_to_char(type));
422 break;
423 }
424
425 truncated = truncated || t;
426
427 /* FORMAT_SUBST_RESULT handles spaces itself */
428 if (replace_whitespace && type != FORMAT_SUBST_RESULT)
429 /* udev_replace_whitespace can replace in-place,
430 * and does nothing if subst_len == 0 */
431 subst_len = udev_replace_whitespace(dest, dest, subst_len);
432
433 dest += subst_len;
434 size -= subst_len;
435 }
436
437 assert(size >= 1);
438
439 if (ret_truncated)
440 *ret_truncated = truncated;
441
442 *dest = '\0';
443 return size;
444 }
445
446 int udev_check_format(const char *value, size_t *offset, const char **hint) {
447 FormatSubstitutionType type;
448 const char *s = value;
449 char attr[UDEV_PATH_SIZE];
450 int r;
451
452 while (*s) {
453 r = get_subst_type(&s, true, &type, attr);
454 if (r < 0) {
455 if (offset)
456 *offset = s - value;
457 if (hint)
458 *hint = "invalid substitution type";
459 return r;
460 } else if (r == 0) {
461 s++;
462 continue;
463 }
464
465 if (IN_SET(type, FORMAT_SUBST_ATTR, FORMAT_SUBST_ENV) && isempty(attr)) {
466 if (offset)
467 *offset = s - value;
468 if (hint)
469 *hint = "attribute value missing";
470 return -EINVAL;
471 }
472
473 if (type == FORMAT_SUBST_RESULT && !isempty(attr)) {
474 unsigned i;
475
476 r = safe_atou_optional_plus(attr, &i);
477 if (r < 0) {
478 if (offset)
479 *offset = s - value;
480 if (hint)
481 *hint = "attribute value not a valid number";
482 return r;
483 }
484 }
485 }
486
487 return 0;
488 }
489
490 int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value) {
491 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
492 _cleanup_free_ char *temp = NULL;
493 char *subsys, *sysname, *attr;
494 const char *val;
495 int r;
496
497 assert(string);
498 assert(result);
499
500 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
501
502 if (string[0] != '[')
503 return -EINVAL;
504
505 temp = strdup(string);
506 if (!temp)
507 return -ENOMEM;
508
509 subsys = &temp[1];
510
511 sysname = strchr(subsys, '/');
512 if (!sysname)
513 return -EINVAL;
514 sysname[0] = '\0';
515 sysname = &sysname[1];
516
517 attr = strchr(sysname, ']');
518 if (!attr)
519 return -EINVAL;
520 attr[0] = '\0';
521 attr = &attr[1];
522 if (attr[0] == '/')
523 attr = &attr[1];
524 if (attr[0] == '\0')
525 attr = NULL;
526
527 if (read_value && !attr)
528 return -EINVAL;
529
530 r = sd_device_new_from_subsystem_sysname(&dev, subsys, sysname);
531 if (r < 0)
532 return r;
533
534 if (read_value) {
535 r = sd_device_get_sysattr_value(dev, attr, &val);
536 if (r < 0 && !ERRNO_IS_PRIVILEGE(r) && r != -ENOENT)
537 return r;
538 if (r >= 0)
539 strscpy(result, maxsize, val);
540 else
541 result[0] = '\0';
542 log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
543 } else {
544 r = sd_device_get_syspath(dev, &val);
545 if (r < 0)
546 return r;
547
548 strscpyl(result, maxsize, val, attr ? "/" : NULL, attr ?: NULL, NULL);
549 log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, strempty(attr), result);
550 }
551 return 0;
552 }