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