]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test-builtin.c
udevadm: show only version number for '--version' option
[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 "path-util.h"
10 #include "string-util.h"
11 #include "udev.h"
12 #include "udevadm.h"
13
14 static const char *arg_command = NULL;
15 static char arg_syspath[UTIL_PATH_SIZE] = {};
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 const char *s;
38 int c;
39
40 while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
41 switch (c) {
42 case 'V':
43 return print_version();
44 case 'h':
45 return help();
46 case '?':
47 return -EINVAL;
48 default:
49 assert_not_reached("Unknown option");
50 }
51
52 arg_command = argv[optind++];
53 if (!arg_command) {
54 log_error("Command missing.");
55 return -EINVAL;
56 }
57
58 s = argv[optind++];
59 if (!s) {
60 log_error("syspath missing.");
61 return -EINVAL;
62 }
63
64 /* add /sys if needed */
65 if (!path_startswith(s, "/sys"))
66 strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", s, NULL);
67 else
68 strscpy(arg_syspath, sizeof(arg_syspath), s);
69
70 return 1;
71 }
72
73 int builtin_main(int argc, char *argv[], void *userdata) {
74 _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
75 enum udev_builtin_cmd cmd;
76 int r;
77
78 log_set_max_level(LOG_DEBUG);
79
80 r = parse_argv(argc, argv);
81 if (r <= 0)
82 return r;
83
84 udev_builtin_init();
85
86 cmd = udev_builtin_lookup(arg_command);
87 if (cmd >= UDEV_BUILTIN_MAX) {
88 log_error("Unknown command '%s'", arg_command);
89 r = -EINVAL;
90 goto finish;
91 }
92
93 dev = udev_device_new_from_syspath(NULL, arg_syspath);
94 if (!dev) {
95 r = log_error_errno(errno, "Failed to open device '%s'", arg_syspath);
96 goto finish;
97 }
98
99 r = udev_builtin_run(dev, cmd, arg_command, true);
100 if (r < 0)
101 log_debug("error executing '%s', exit code %i", arg_command, r);
102
103 finish:
104 udev_builtin_exit();
105 return r;
106 }