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