1 /* SPDX-License-Identifier: GPL-2.0+ */
10 #include "udev-builtin.h"
12 #include "udevadm-util.h"
14 static const char *arg_command
= NULL
;
15 static const char *arg_syspath
= NULL
;
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"
23 , program_invocation_short_name
);
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' },
39 while ((c
= getopt_long(argc
, argv
, "Vh", options
, NULL
)) >= 0)
42 return print_version();
48 assert_not_reached("Unknown option");
51 arg_command
= argv
[optind
++];
53 log_error("Command missing.");
57 arg_syspath
= argv
[optind
++];
59 log_error("syspath missing.");
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
;
71 log_set_max_level(LOG_DEBUG
);
73 r
= parse_argv(argc
, argv
);
79 cmd
= udev_builtin_lookup(arg_command
);
81 log_error("Unknown command '%s'", arg_command
);
86 r
= find_device(arg_syspath
, "/sys", &dev
);
88 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
92 r
= udev_builtin_run(dev
, cmd
, arg_command
, true);
94 log_debug_errno(r
, "Builtin command '%s' fails: %m", arg_command
);