chiark / gitweb /
b031353c5dc0e70393c7173c6dda32220a471d54
[elogind.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 /// Additional includes needed by elogind
16 #include "alloc-util.h"
17
18 int udev_parse_config(void) {
19         _cleanup_free_ char *val = NULL;
20         const char *log;
21         size_t n;
22         int r;
23
24         r = parse_env_file(NULL, "/etc/udev/udev.conf", NEWLINE, "udev_log", &val, NULL);
25         if (r == -ENOENT || !val)
26                 return 0;
27         if (r < 0)
28                 return r;
29
30         /* unquote */
31         n = strlen(val);
32         if (n >= 2 &&
33             ((val[0] == '"' && val[n-1] == '"') ||
34              (val[0] == '\'' && val[n-1] == '\''))) {
35                 val[n - 1] = '\0';
36                 log = val + 1;
37         } else
38                 log = val;
39
40         /* we set the udev log level here explicitly, this is supposed
41          * to regulate the code in libudev/ and udev/. */
42         r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
43         if (r < 0)
44                 log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
45
46         return 0;
47 }
48
49 int udev_device_new_from_stat_rdev(struct udev *udev, const struct stat *st, struct udev_device **ret) {
50         struct udev_device *nd;
51         char type;
52
53         assert(udev);
54         assert(st);
55         assert(ret);
56
57         if (S_ISBLK(st->st_mode))
58                 type = 'b';
59         else if (S_ISCHR(st->st_mode))
60                 type = 'c';
61         else
62                 return -ENOTTY;
63
64         nd = udev_device_new_from_devnum(udev, type, st->st_rdev);
65         if (!nd)
66                 return -errno;
67
68         *ret = nd;
69         return 0;
70 }