Debian Codegraph @

src/basic/devnum-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <sys/sysmacros.h>
6
7 #include "alloc-util.h"
8 #include "chase.h"
9 #include "devnum-util.h"
10 #include "parse-util.h"
11 #include "path-util.h"
12 #include "stdio-util.h"
13 #include "string-util.h"
14
15 int parse_devnum(const char *s, dev_t *ret) {
16 const char *major;
17 unsigned x, y;
18 size_t n;
19 int r;
20
21 assert(ret);
22
23 n = strspn(s, DIGITS);
24 if (n == 0)
25 return -EINVAL;
26 if (n > DECIMAL_STR_MAX(dev_t))
27 return -EINVAL;
28 if (s[n] != ':')
29 return -EINVAL;
30
31 major = strndupa_safe(s, n);
32 r = safe_atou(major, &x);
33 if (r < 0)
34 return r;
35
36 r = safe_atou(s + n + 1, &y);
37 if (r < 0)
38 return r;
39
40 if (!DEVICE_MAJOR_VALID(x) || !DEVICE_MINOR_VALID(y))
41 return -ERANGE;
42
43 *ret = makedev(x, y);
44 return 0;
45 }
46
47 int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
48 const char *t;
49
50 /* Generates the /dev/{char|block}/MAJOR:MINOR path for a dev_t */
51
52 if (S_ISCHR(mode))
53 t = "char";
54 else if (S_ISBLK(mode))
55 t = "block";
56 else
57 return -ENODEV;
58
59 if (asprintf(ret, "/dev/%s/" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
60 return -ENOMEM;
61
62 return 0;
63 }
64
65 int device_path_make_inaccessible(mode_t mode, char **ret) {
66 const char *s;
67
68 assert(ret);
69
70 if (S_ISCHR(mode))
71 s = "/run/systemd/inaccessible/chr";
72 else if (S_ISBLK(mode))
73 s = "/run/systemd/inaccessible/blk";
74 else
75 return -ENODEV;
76
77 return strdup_to(ret, s);
78 }
79
80 int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
81 _cleanup_free_ char *p = NULL;
82 int r;
83
84 /* Finds the canonical path for a device, i.e. resolves the /dev/{char|block}/MAJOR:MINOR path to the end. */
85
86 assert(ret);
87
88 if (devnum_is_zero(devnum))
89 /* A special hack to make sure our 'inaccessible' device nodes work. They won't have symlinks in
90 * /dev/block/ and /dev/char/, hence we handle them specially here. */
91 return device_path_make_inaccessible(mode, ret);
92
93 r = device_path_make_major_minor(mode, devnum, &p);
94 if (r < 0)
95 return r;
96
97 return chase(p, NULL, 0, ret, NULL);
98 }
99
100 int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum) {
101 mode_t mode;
102 dev_t devnum;
103 int r;
104
105 /* Tries to extract the major/minor directly from the device path if we can. Handles /dev/block/ and /dev/char/
106 * paths, as well out synthetic inaccessible device nodes. Never goes to disk. Returns -ENODEV if the device
107 * path cannot be parsed like this. */
108
109 if (path_equal(path, "/run/systemd/inaccessible/chr")) {
110 mode = S_IFCHR;
111 devnum = makedev(0, 0);
112 } else if (path_equal(path, "/run/systemd/inaccessible/blk")) {
113 mode = S_IFBLK;
114 devnum = makedev(0, 0);
115 } else {
116 const char *w;
117
118 w = path_startswith(path, "/dev/block/");
119 if (w)
120 mode = S_IFBLK;
121 else {
122 w = path_startswith(path, "/dev/char/");
123 if (!w)
124 return -ENODEV;
125
126 mode = S_IFCHR;
127 }
128
129 r = parse_devnum(w, &devnum);
130 if (r < 0)
131 return r;
132 }
133
134 if (ret_mode)
135 *ret_mode = mode;
136 if (ret_devnum)
137 *ret_devnum = devnum;
138
139 return 0;
140 }
141
142 char* format_devnum(dev_t d, char buf[static DEVNUM_STR_MAX]) {
143 return ASSERT_PTR(snprintf_ok(buf, DEVNUM_STR_MAX, DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(d)));
144 }