1 /* SPDX-License-Identifier: GPL-2.0+ */
10 #include "string-util.h"
14 static const char *arg_command
= NULL
;
15 static char arg_syspath
[UTIL_PATH_SIZE
] = {};
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' },
40 while ((c
= getopt_long(argc
, argv
, "Vh", options
, NULL
)) >= 0)
43 return print_version();
49 assert_not_reached("Unknown option");
52 arg_command
= argv
[optind
++];
54 log_error("Command missing.");
60 log_error("syspath missing.");
64 /* add /sys if needed */
65 if (!path_startswith(s
, "/sys"))
66 strscpyl(arg_syspath
, sizeof(arg_syspath
), "/sys", s
, NULL
);
68 strscpy(arg_syspath
, sizeof(arg_syspath
), s
);
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
;
78 log_set_max_level(LOG_DEBUG
);
80 r
= parse_argv(argc
, argv
);
86 cmd
= udev_builtin_lookup(arg_command
);
87 if (cmd
>= UDEV_BUILTIN_MAX
) {
88 log_error("Unknown command '%s'", arg_command
);
93 dev
= udev_device_new_from_syspath(NULL
, arg_syspath
);
95 r
= log_error_errno(errno
, "Failed to open device '%s'", arg_syspath
);
99 r
= udev_builtin_run(dev
, cmd
, arg_command
, true);
101 log_debug("error executing '%s', exit code %i", arg_command
, r
);