]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
be9b51f6 | 2 | |
034f35d7 | 3 | #include <ctype.h> |
87171e46 | 4 | #include <errno.h> |
e6c1a2bd | 5 | #include <fcntl.h> |
07630cea LP |
6 | #include <getopt.h> |
7 | #include <stddef.h> | |
8 | #include <stdio.h> | |
9 | #include <string.h> | |
492e76c9 | 10 | #include <sys/stat.h> |
07630cea | 11 | #include <unistd.h> |
be9b51f6 | 12 | |
13aca847 YW |
13 | #include "sd-device.h" |
14 | ||
15 | #include "device-enumerator-private.h" | |
16 | #include "device-private.h" | |
17 | #include "device-util.h" | |
8fb3f009 | 18 | #include "dirent-util.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
13aca847 | 20 | #include "libudev-private.h" |
07630cea | 21 | #include "string-util.h" |
3d05193e | 22 | #include "udevadm.h" |
d6170d27 | 23 | #include "udevadm-util.h" |
be9b51f6 | 24 | |
9ec6e95b | 25 | static bool skip_attribute(const char *name) { |
46512cd9 | 26 | static const char* const skip[] = { |
912541b0 KS |
27 | "uevent", |
28 | "dev", | |
29 | "modalias", | |
30 | "resource", | |
31 | "driver", | |
32 | "subsystem", | |
33 | "module", | |
34 | }; | |
35 | unsigned int i; | |
36 | ||
8fef0ff2 | 37 | for (i = 0; i < ELEMENTSOF(skip); i++) |
090be865 | 38 | if (streq(name, skip[i])) |
912541b0 KS |
39 | return true; |
40 | return false; | |
20bee04c KS |
41 | } |
42 | ||
13aca847 YW |
43 | static void print_all_attributes(sd_device *device, const char *key) { |
44 | const char *name, *value; | |
912541b0 | 45 | |
13aca847 | 46 | FOREACH_DEVICE_PROPERTY(device, name, value) { |
912541b0 KS |
47 | size_t len; |
48 | ||
912541b0 KS |
49 | if (skip_attribute(name)) |
50 | continue; | |
51 | ||
912541b0 KS |
52 | /* skip any values that look like a path */ |
53 | if (value[0] == '/') | |
54 | continue; | |
55 | ||
56 | /* skip nonprintable attributes */ | |
57 | len = strlen(value); | |
58 | while (len > 0 && isprint(value[len-1])) | |
59 | len--; | |
baa30fbc | 60 | if (len > 0) |
912541b0 | 61 | continue; |
912541b0 KS |
62 | |
63 | printf(" %s{%s}==\"%s\"\n", key, name, value); | |
64 | } | |
65 | printf("\n"); | |
be9b51f6 KS |
66 | } |
67 | ||
13aca847 YW |
68 | static int print_device_chain(sd_device *device) { |
69 | sd_device *child, *parent; | |
912541b0 KS |
70 | const char *str; |
71 | ||
72 | printf("\n" | |
73 | "Udevadm info starts with the device specified by the devpath and then\n" | |
74 | "walks up the chain of parent devices. It prints for every device\n" | |
75 | "found, all possible attributes in the udev rules key format.\n" | |
76 | "A rule to match, can be composed by the attributes of the device\n" | |
77 | "and the attributes from one single parent device.\n" | |
78 | "\n"); | |
79 | ||
13aca847 YW |
80 | (void) sd_device_get_devpath(device, &str); |
81 | printf(" looking at device '%s':\n", str); | |
82 | (void) sd_device_get_sysname(device, &str); | |
83 | printf(" KERNEL==\"%s\"\n", str); | |
84 | if (sd_device_get_subsystem(device, &str) < 0) | |
912541b0 KS |
85 | str = ""; |
86 | printf(" SUBSYSTEM==\"%s\"\n", str); | |
13aca847 | 87 | if (sd_device_get_driver(device, &str) < 0) |
912541b0 KS |
88 | str = ""; |
89 | printf(" DRIVER==\"%s\"\n", str); | |
90 | print_all_attributes(device, "ATTR"); | |
91 | ||
13aca847 YW |
92 | for (child = device; sd_device_get_parent(child, &parent) >= 0; child = parent) { |
93 | (void) sd_device_get_devpath(parent, &str); | |
94 | printf(" looking at parent device '%s':\n", str); | |
95 | (void) sd_device_get_sysname(parent, &str); | |
96 | printf(" KERNELS==\"%s\"\n", str); | |
97 | if (sd_device_get_subsystem(parent, &str) < 0) | |
912541b0 KS |
98 | str = ""; |
99 | printf(" SUBSYSTEMS==\"%s\"\n", str); | |
13aca847 | 100 | if (sd_device_get_driver(parent, &str) < 0) |
912541b0 KS |
101 | str = ""; |
102 | printf(" DRIVERS==\"%s\"\n", str); | |
13aca847 YW |
103 | print_all_attributes(parent, "ATTRS"); |
104 | } | |
912541b0 KS |
105 | |
106 | return 0; | |
1aa1e248 KS |
107 | } |
108 | ||
13aca847 YW |
109 | static void print_record(sd_device *device) { |
110 | const char *str, *val; | |
912541b0 | 111 | int i; |
912541b0 | 112 | |
13aca847 YW |
113 | (void) sd_device_get_devpath(device, &str); |
114 | printf("P: %s\n", str); | |
912541b0 | 115 | |
13aca847 | 116 | if (sd_device_get_devname(device, &str) >= 0) |
fbd0b64f | 117 | printf("N: %s\n", str + STRLEN("/dev/")); |
912541b0 | 118 | |
13aca847 | 119 | if (device_get_devlink_priority(device, &i) >= 0) |
912541b0 KS |
120 | printf("L: %i\n", i); |
121 | ||
13aca847 YW |
122 | FOREACH_DEVICE_DEVLINK(device, str) |
123 | printf("S: %s\n", str + STRLEN("/dev/")); | |
124 | ||
125 | FOREACH_DEVICE_PROPERTY(device, str, val) | |
126 | printf("E: %s=%s\n", str, val); | |
912541b0 | 127 | |
912541b0 | 128 | printf("\n"); |
31de3a2b KS |
129 | } |
130 | ||
9ec6e95b | 131 | static int stat_device(const char *name, bool export, const char *prefix) { |
912541b0 KS |
132 | struct stat statbuf; |
133 | ||
134 | if (stat(name, &statbuf) != 0) | |
755700bb | 135 | return -errno; |
912541b0 KS |
136 | |
137 | if (export) { | |
13aca847 | 138 | if (!prefix) |
912541b0 | 139 | prefix = "INFO_"; |
1fa2f38f ZJS |
140 | printf("%sMAJOR=%u\n" |
141 | "%sMINOR=%u\n", | |
912541b0 KS |
142 | prefix, major(statbuf.st_dev), |
143 | prefix, minor(statbuf.st_dev)); | |
144 | } else | |
1fa2f38f | 145 | printf("%u:%u\n", major(statbuf.st_dev), minor(statbuf.st_dev)); |
912541b0 | 146 | return 0; |
f338bac8 KS |
147 | } |
148 | ||
2024ed61 | 149 | static int export_devices(void) { |
13aca847 YW |
150 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
151 | sd_device *d; | |
152 | int r; | |
912541b0 | 153 | |
13aca847 YW |
154 | r = sd_device_enumerator_new(&e); |
155 | if (r < 0) | |
156 | return r; | |
755700bb | 157 | |
13aca847 YW |
158 | r = sd_device_enumerator_allow_uninitialized(e); |
159 | if (r < 0) | |
160 | return r; | |
912541b0 | 161 | |
13aca847 YW |
162 | r = device_enumerator_scan_devices(e); |
163 | if (r < 0) | |
164 | return r; | |
165 | ||
166 | FOREACH_DEVICE_AND_SUBSYSTEM(e, d) | |
167 | print_record(d); | |
755700bb | 168 | |
912541b0 | 169 | return 0; |
bf7ad0ea KS |
170 | } |
171 | ||
9ec6e95b | 172 | static void cleanup_dir(DIR *dir, mode_t mask, int depth) { |
912541b0 KS |
173 | struct dirent *dent; |
174 | ||
175 | if (depth <= 0) | |
176 | return; | |
177 | ||
8fb3f009 | 178 | FOREACH_DIRENT_ALL(dent, dir, break) { |
912541b0 KS |
179 | struct stat stats; |
180 | ||
181 | if (dent->d_name[0] == '.') | |
182 | continue; | |
183 | if (fstatat(dirfd(dir), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0) | |
184 | continue; | |
185 | if ((stats.st_mode & mask) != 0) | |
186 | continue; | |
187 | if (S_ISDIR(stats.st_mode)) { | |
13aca847 | 188 | _cleanup_closedir_ DIR *dir2 = NULL; |
912541b0 KS |
189 | |
190 | dir2 = fdopendir(openat(dirfd(dir), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)); | |
13aca847 | 191 | if (dir2) |
912541b0 | 192 | cleanup_dir(dir2, mask, depth-1); |
200c7fa6 LP |
193 | |
194 | (void) unlinkat(dirfd(dir), dent->d_name, AT_REMOVEDIR); | |
195 | } else | |
196 | (void) unlinkat(dirfd(dir), dent->d_name, 0); | |
912541b0 | 197 | } |
9ead6627 KS |
198 | } |
199 | ||
2024ed61 | 200 | static void cleanup_db(void) { |
755700bb | 201 | _cleanup_closedir_ DIR *dir1 = NULL, *dir2 = NULL, *dir3 = NULL, *dir4 = NULL, *dir5 = NULL; |
912541b0 | 202 | |
755700bb | 203 | (void) unlink("/run/udev/queue.bin"); |
912541b0 | 204 | |
755700bb | 205 | dir1 = opendir("/run/udev/data"); |
13aca847 | 206 | if (dir1) |
755700bb | 207 | cleanup_dir(dir1, S_ISVTX, 1); |
912541b0 | 208 | |
755700bb | 209 | dir2 = opendir("/run/udev/links"); |
13aca847 | 210 | if (dir2) |
755700bb | 211 | cleanup_dir(dir2, 0, 2); |
912541b0 | 212 | |
755700bb | 213 | dir3 = opendir("/run/udev/tags"); |
13aca847 | 214 | if (dir3) |
755700bb | 215 | cleanup_dir(dir3, 0, 2); |
912541b0 | 216 | |
755700bb | 217 | dir4 = opendir("/run/udev/static_node-tags"); |
13aca847 | 218 | if (dir4) |
755700bb | 219 | cleanup_dir(dir4, 0, 2); |
84b6ad70 | 220 | |
755700bb | 221 | dir5 = opendir("/run/udev/watch"); |
13aca847 | 222 | if (dir5) |
755700bb | 223 | cleanup_dir(dir5, 0, 1); |
9ead6627 KS |
224 | } |
225 | ||
ee4a776d | 226 | static int help(void) { |
5ac0162c LP |
227 | |
228 | printf("%s info [OPTIONS] [DEVPATH|FILE]\n\n" | |
229 | "Query sysfs or the udev database.\n\n" | |
230 | " -h --help Print this message\n" | |
73527992 | 231 | " -V --version Print version of the program\n" |
5ac0162c LP |
232 | " -q --query=TYPE Query device information:\n" |
233 | " name Name of device node\n" | |
234 | " symlink Pointing to node\n" | |
235 | " path sysfs device path\n" | |
236 | " property The device properties\n" | |
237 | " all All values\n" | |
238 | " -p --path=SYSPATH sysfs device path used for query or attribute walk\n" | |
239 | " -n --name=NAME Node or symlink name used for query or attribute walk\n" | |
240 | " -r --root Prepend dev directory to path names\n" | |
241 | " -a --attribute-walk Print all key matches walking along the chain\n" | |
242 | " of parent devices\n" | |
243 | " -d --device-id-of-file=FILE Print major:minor of device containing this file\n" | |
244 | " -x --export Export key/value pairs\n" | |
245 | " -P --export-prefix Export the key name with a prefix\n" | |
246 | " -e --export-db Export the content of the udev database\n" | |
247 | " -c --cleanup-db Clean up the udev database\n" | |
248 | , program_invocation_short_name); | |
ee4a776d YW |
249 | |
250 | return 0; | |
5ac0162c LP |
251 | } |
252 | ||
3d05193e | 253 | int info_main(int argc, char *argv[], void *userdata) { |
13aca847 | 254 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
912541b0 KS |
255 | bool root = 0; |
256 | bool export = 0; | |
257 | const char *export_prefix = NULL; | |
912541b0 | 258 | char name[UTIL_PATH_SIZE]; |
ee4a776d | 259 | int c, r; |
912541b0 KS |
260 | |
261 | static const struct option options[] = { | |
7643ac9a ZJS |
262 | { "name", required_argument, NULL, 'n' }, |
263 | { "path", required_argument, NULL, 'p' }, | |
264 | { "query", required_argument, NULL, 'q' }, | |
265 | { "attribute-walk", no_argument, NULL, 'a' }, | |
266 | { "cleanup-db", no_argument, NULL, 'c' }, | |
267 | { "export-db", no_argument, NULL, 'e' }, | |
268 | { "root", no_argument, NULL, 'r' }, | |
912541b0 | 269 | { "device-id-of-file", required_argument, NULL, 'd' }, |
7643ac9a ZJS |
270 | { "export", no_argument, NULL, 'x' }, |
271 | { "export-prefix", required_argument, NULL, 'P' }, | |
272 | { "version", no_argument, NULL, 'V' }, | |
273 | { "help", no_argument, NULL, 'h' }, | |
912541b0 KS |
274 | {} |
275 | }; | |
276 | ||
277 | enum action_type { | |
912541b0 KS |
278 | ACTION_QUERY, |
279 | ACTION_ATTRIBUTE_WALK, | |
912541b0 | 280 | ACTION_DEVICE_ID_FILE, |
4f5d327a | 281 | } action = ACTION_QUERY; |
912541b0 KS |
282 | |
283 | enum query_type { | |
912541b0 KS |
284 | QUERY_NAME, |
285 | QUERY_PATH, | |
286 | QUERY_SYMLINK, | |
287 | QUERY_PROPERTY, | |
288 | QUERY_ALL, | |
4f5d327a | 289 | } query = QUERY_ALL; |
912541b0 | 290 | |
7643ac9a ZJS |
291 | while ((c = getopt_long(argc, argv, "aced:n:p:q:rxP:RVh", options, NULL)) >= 0) |
292 | switch (c) { | |
ee4a776d YW |
293 | case 'n': |
294 | if (device) { | |
295 | log_error("device already specified"); | |
296 | return -EINVAL; | |
912541b0 | 297 | } |
4f5d327a | 298 | |
13aca847 YW |
299 | r = find_device(optarg, "/dev/", &device); |
300 | if (r < 0) | |
301 | return log_error_errno(r, "device node not found: %m"); | |
912541b0 KS |
302 | break; |
303 | case 'p': | |
ee4a776d YW |
304 | if (device) { |
305 | log_error("device already specified"); | |
306 | return -EINVAL; | |
912541b0 | 307 | } |
4f5d327a | 308 | |
13aca847 YW |
309 | r = find_device(optarg, "/sys", &device); |
310 | if (r < 0) | |
311 | return log_error_errno(r, "syspath not found: %m"); | |
912541b0 KS |
312 | break; |
313 | case 'q': | |
314 | action = ACTION_QUERY; | |
44433ebd | 315 | if (streq(optarg, "property") || streq(optarg, "env")) |
912541b0 | 316 | query = QUERY_PROPERTY; |
44433ebd | 317 | else if (streq(optarg, "name")) |
912541b0 | 318 | query = QUERY_NAME; |
44433ebd | 319 | else if (streq(optarg, "symlink")) |
912541b0 | 320 | query = QUERY_SYMLINK; |
44433ebd | 321 | else if (streq(optarg, "path")) |
912541b0 | 322 | query = QUERY_PATH; |
44433ebd | 323 | else if (streq(optarg, "all")) |
912541b0 | 324 | query = QUERY_ALL; |
44433ebd | 325 | else { |
ee4a776d YW |
326 | log_error("unknown query type"); |
327 | return -EINVAL; | |
912541b0 KS |
328 | } |
329 | break; | |
330 | case 'r': | |
912541b0 KS |
331 | root = true; |
332 | break; | |
912541b0 KS |
333 | case 'd': |
334 | action = ACTION_DEVICE_ID_FILE; | |
d5a89d7d | 335 | strscpy(name, sizeof(name), optarg); |
912541b0 KS |
336 | break; |
337 | case 'a': | |
338 | action = ACTION_ATTRIBUTE_WALK; | |
339 | break; | |
340 | case 'e': | |
ee4a776d | 341 | return export_devices(); |
912541b0 | 342 | case 'c': |
2024ed61 | 343 | cleanup_db(); |
44433ebd | 344 | return 0; |
912541b0 KS |
345 | case 'x': |
346 | export = true; | |
347 | break; | |
348 | case 'P': | |
349 | export_prefix = optarg; | |
350 | break; | |
351 | case 'V': | |
51b006e1 | 352 | return print_version(); |
912541b0 | 353 | case 'h': |
ee4a776d YW |
354 | return help(); |
355 | case '?': | |
356 | return -EINVAL; | |
912541b0 | 357 | default: |
ee4a776d | 358 | assert_not_reached("Unknown option"); |
912541b0 | 359 | } |
912541b0 KS |
360 | |
361 | switch (action) { | |
362 | case ACTION_QUERY: | |
4f5d327a KS |
363 | if (!device) { |
364 | if (!argv[optind]) { | |
5ac0162c | 365 | help(); |
ee4a776d | 366 | return -EINVAL; |
4f5d327a | 367 | } |
13aca847 YW |
368 | r = find_device(argv[optind], NULL, &device); |
369 | if (r < 0) | |
370 | return log_error_errno(r, "Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected: %m"); | |
912541b0 KS |
371 | } |
372 | ||
373 | switch(query) { | |
374 | case QUERY_NAME: { | |
13aca847 | 375 | const char *node; |
912541b0 | 376 | |
13aca847 YW |
377 | r = sd_device_get_devname(device, &node); |
378 | if (r < 0) | |
379 | return log_error_errno(r, "no device node found: %m"); | |
912541b0 | 380 | |
6ada823a | 381 | if (root) |
13aca847 | 382 | printf("%s\n", node); |
6ada823a | 383 | else |
13aca847 | 384 | printf("%s\n", node + STRLEN("/dev/")); |
912541b0 KS |
385 | break; |
386 | } | |
13aca847 YW |
387 | case QUERY_SYMLINK: { |
388 | const char *devlink; | |
389 | bool first = true; | |
390 | ||
391 | FOREACH_DEVICE_DEVLINK(device, devlink) { | |
392 | if (!first) | |
393 | printf(" "); | |
6ada823a | 394 | if (root) |
13aca847 | 395 | printf("%s", devlink); |
6ada823a | 396 | else |
13aca847 YW |
397 | printf("%s", devlink + STRLEN("/dev/")); |
398 | ||
399 | first = false; | |
912541b0 KS |
400 | } |
401 | printf("\n"); | |
402 | break; | |
13aca847 YW |
403 | } |
404 | case QUERY_PATH: { | |
405 | const char *devpath; | |
406 | ||
407 | r = sd_device_get_devpath(device, &devpath); | |
408 | if (r < 0) | |
409 | return log_error_errno(r, "Failed to get device path: %m"); | |
410 | ||
411 | printf("%s\n", devpath); | |
44433ebd | 412 | return 0; |
13aca847 YW |
413 | } |
414 | case QUERY_PROPERTY: { | |
415 | const char *key, *value; | |
416 | ||
417 | FOREACH_DEVICE_PROPERTY(device, key, value) | |
19a29798 | 418 | if (export) |
13aca847 | 419 | printf("%s%s='%s'\n", strempty(export_prefix), key, value); |
19a29798 | 420 | else |
13aca847 | 421 | printf("%s=%s\n", key, value); |
19a29798 | 422 | |
912541b0 | 423 | break; |
13aca847 | 424 | } |
912541b0 KS |
425 | case QUERY_ALL: |
426 | print_record(device); | |
427 | break; | |
428 | default: | |
bdd13f6b | 429 | assert_not_reached("unknown query type"); |
912541b0 KS |
430 | } |
431 | break; | |
432 | case ACTION_ATTRIBUTE_WALK: | |
4f5d327a | 433 | if (!device && argv[optind]) { |
13aca847 YW |
434 | r = find_device(argv[optind], NULL, &device); |
435 | if (r < 0) | |
436 | return log_error_errno(r, "Unknown device, absolute path in /dev/ or /sys expected: %m"); | |
4f5d327a KS |
437 | } |
438 | if (!device) { | |
ee4a776d YW |
439 | log_error("Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected."); |
440 | return -EINVAL; | |
912541b0 KS |
441 | } |
442 | print_device_chain(device); | |
443 | break; | |
444 | case ACTION_DEVICE_ID_FILE: | |
ee4a776d YW |
445 | r = stat_device(name, export, export_prefix); |
446 | if (r < 0) | |
447 | return r; | |
912541b0 | 448 | break; |
912541b0 | 449 | } |
87171e46 | 450 | |
44433ebd | 451 | return 0; |
87171e46 | 452 | } |