]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/udev-util.c
Merge pull request #9301 from keszybz/man-drop-authorgroup
[thirdparty/systemd.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 int udev_parse_config(void) {
11 _cleanup_free_ char *val = NULL;
12 const char *log;
13 size_t n;
14 int r;
15
16 r = parse_env_file(NULL, "/etc/udev/udev.conf", NEWLINE, "udev_log", &val, NULL);
17 if (r == -ENOENT || !val)
18 return 0;
19 if (r < 0)
20 return r;
21
22 /* unquote */
23 n = strlen(val);
24 if (n >= 2 &&
25 ((val[0] == '"' && val[n-1] == '"') ||
26 (val[0] == '\'' && val[n-1] == '\''))) {
27 val[n - 1] = '\0';
28 log = val + 1;
29 } else
30 log = val;
31
32 /* we set the udev log level here explicitly, this is supposed
33 * to regulate the code in libudev/ and udev/. */
34 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
35 if (r < 0)
36 log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
37
38 return 0;
39 }
40
41 int udev_device_new_from_stat_rdev(struct udev *udev, const struct stat *st, struct udev_device **ret) {
42 struct udev_device *nd;
43 char type;
44
45 assert(udev);
46 assert(st);
47 assert(ret);
48
49 if (S_ISBLK(st->st_mode))
50 type = 'b';
51 else if (S_ISCHR(st->st_mode))
52 type = 'c';
53 else
54 return -ENOTTY;
55
56 nd = udev_device_new_from_devnum(udev, type, st->st_rdev);
57 if (!nd)
58 return -errno;
59
60 *ret = nd;
61 return 0;
62 }