]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test-builtin.c
7bf7b08a4487324edc8528335a09723d6a798d04
[thirdparty/systemd.git] / src / udev / udevadm-test-builtin.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "log.h"
10 #include "udev-builtin.h"
11 #include "udevadm.h"
12 #include "udevadm-util.h"
13
14 static const char *arg_command = NULL;
15 static const char *arg_syspath = NULL;
16
17 static int help(void) {
18 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
19 "Test a built-in command.\n\n"
20 " -h --help Print this message\n"
21 " -V --version Print version of the program\n\n"
22 "Commands:\n"
23 , program_invocation_short_name);
24
25 udev_builtin_list();
26
27 return 0;
28 }
29
30 static int parse_argv(int argc, char *argv[]) {
31 static const struct option options[] = {
32 { "version", no_argument, NULL, 'V' },
33 { "help", no_argument, NULL, 'h' },
34 {}
35 };
36
37 int c;
38
39 while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
40 switch (c) {
41 case 'V':
42 return print_version();
43 case 'h':
44 return help();
45 case '?':
46 return -EINVAL;
47 default:
48 assert_not_reached("Unknown option");
49 }
50
51 arg_command = argv[optind++];
52 if (!arg_command) {
53 log_error("Command missing.");
54 return -EINVAL;
55 }
56
57 arg_syspath = argv[optind++];
58 if (!arg_syspath) {
59 log_error("syspath missing.");
60 return -EINVAL;
61 }
62
63 return 1;
64 }
65
66 int builtin_main(int argc, char *argv[], void *userdata) {
67 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
68 enum udev_builtin_cmd cmd;
69 int r;
70
71 log_set_max_level(LOG_DEBUG);
72
73 r = parse_argv(argc, argv);
74 if (r <= 0)
75 return r;
76
77 udev_builtin_init();
78
79 cmd = udev_builtin_lookup(arg_command);
80 if (cmd < 0) {
81 log_error("Unknown command '%s'", arg_command);
82 r = -EINVAL;
83 goto finish;
84 }
85
86 r = find_device(arg_syspath, "/sys", &dev);
87 if (r < 0) {
88 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
89 goto finish;
90 }
91
92 r = udev_builtin_run(dev, cmd, arg_command, true);
93 if (r < 0)
94 log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
95
96 finish:
97 udev_builtin_exit();
98 return r;
99 }