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 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
56 arg_syspath
= argv
[optind
++];
58 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
64 int builtin_main(int argc
, char *argv
[], void *userdata
) {
65 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
66 UdevBuiltinCommand cmd
;
69 log_set_max_level(LOG_DEBUG
);
71 r
= parse_argv(argc
, argv
);
77 cmd
= udev_builtin_lookup(arg_command
);
79 log_error("Unknown command '%s'", arg_command
);
84 r
= find_device(arg_syspath
, "/sys", &dev
);
86 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
90 r
= udev_builtin_run(dev
, cmd
, arg_command
, true);
92 log_debug_errno(r
, "Builtin command '%s' fails: %m", arg_command
);