1 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 #include "udev-builtin.h"
12 #include "udevadm-util.h"
14 static sd_device_action_t arg_action
= SD_DEVICE_ADD
;
15 static const char *arg_command
= NULL
;
16 static const char *arg_syspath
= NULL
;
18 static int help(void) {
19 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
20 "Test a built-in command.\n\n"
21 " -h --help Print this message\n"
22 " -V --version Print version of the program\n\n"
23 " -a --action=ACTION|help Set action string\n"
25 program_invocation_short_name
);
32 static int parse_argv(int argc
, char *argv
[]) {
33 static const struct option options
[] = {
34 { "action", required_argument
, NULL
, 'a' },
35 { "version", no_argument
, NULL
, 'V' },
36 { "help", no_argument
, NULL
, 'h' },
42 while ((c
= getopt_long(argc
, argv
, "a:Vh", options
, NULL
)) >= 0)
45 r
= parse_device_action(optarg
, &arg_action
);
47 return log_error_errno(r
, "Invalid action '%s'", optarg
);
52 return print_version();
61 arg_command
= argv
[optind
++];
63 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
66 arg_syspath
= argv
[optind
++];
68 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
69 "device is missing.");
74 int builtin_main(int argc
, char *argv
[], void *userdata
) {
75 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
76 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
77 UdevBuiltinCommand cmd
;
80 log_set_max_level(LOG_DEBUG
);
82 r
= parse_argv(argc
, argv
);
88 cmd
= udev_builtin_lookup(arg_command
);
90 log_error("Unknown command '%s'", arg_command
);
95 r
= find_device_with_action(arg_syspath
, arg_action
, &dev
);
97 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
101 r
= udev_builtin_run(dev
, &rtnl
, cmd
, arg_command
, true);
103 log_debug_errno(r
, "Builtin command '%s' fails: %m", arg_command
);