]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/udev-util.c
udev: add helper udev_device_new_from_stat_rdev()
[thirdparty/systemd.git] / src / shared / udev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2017 Zbigniew Jędrzejewski-Szmek
6 ***/
7
8 #include <string.h>
9
10 #include "fileio.h"
11 #include "log.h"
12 #include "string-util.h"
13 #include "udev-util.h"
14
15 int udev_parse_config(void) {
16 _cleanup_free_ char *val = NULL;
17 const char *log;
18 size_t n;
19 int r;
20
21 r = parse_env_file(NULL, "/etc/udev/udev.conf", NEWLINE, "udev_log", &val, NULL);
22 if (r == -ENOENT || !val)
23 return 0;
24 if (r < 0)
25 return r;
26
27 /* unquote */
28 n = strlen(val);
29 if (n >= 2 &&
30 ((val[0] == '"' && val[n-1] == '"') ||
31 (val[0] == '\'' && val[n-1] == '\''))) {
32 val[n - 1] = '\0';
33 log = val + 1;
34 } else
35 log = val;
36
37 /* we set the udev log level here explicitly, this is supposed
38 * to regulate the code in libudev/ and udev/. */
39 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
40 if (r < 0)
41 log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
42
43 return 0;
44 }
45
46 int udev_device_new_from_stat_rdev(struct udev *udev, const struct stat *st, struct udev_device **ret) {
47 struct udev_device *nd;
48 char type;
49
50 assert(udev);
51 assert(st);
52 assert(ret);
53
54 if (S_ISBLK(st->st_mode))
55 type = 'b';
56 else if (S_ISCHR(st->st_mode))
57 type = 'c';
58 else
59 return -ENOTTY;
60
61 nd = udev_device_new_from_devnum(udev, type, st->st_rdev);
62 if (!nd)
63 return -errno;
64
65 *ret = nd;
66 return 0;
67 }