1 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 #include "device-private.h"
10 #include "device-util.h"
12 #include "udev-builtin.h"
14 #include "udevadm-util.h"
16 static sd_device_action_t arg_action
= SD_DEVICE_ADD
;
17 static const char *arg_command
= NULL
;
18 static const char *arg_syspath
= NULL
;
20 static int help(void) {
21 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
22 "Test a built-in command.\n\n"
23 " -h --help Print this message\n"
24 " -V --version Print version of the program\n\n"
25 " -a --action=ACTION|help Set action string\n"
27 program_invocation_short_name
);
34 static int parse_argv(int argc
, char *argv
[]) {
35 static const struct option options
[] = {
36 { "action", required_argument
, NULL
, 'a' },
37 { "version", no_argument
, NULL
, 'V' },
38 { "help", no_argument
, NULL
, 'h' },
44 while ((c
= getopt_long(argc
, argv
, "a:Vh", options
, NULL
)) >= 0)
47 r
= parse_device_action(optarg
, &arg_action
);
49 return log_error_errno(r
, "Invalid action '%s'", optarg
);
54 return print_version();
63 arg_command
= argv
[optind
++];
65 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
68 arg_syspath
= argv
[optind
++];
70 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
71 "device is missing.");
76 int builtin_main(int argc
, char *argv
[], void *userdata
) {
77 _cleanup_(udev_event_freep
) UdevEvent
*event
= NULL
;
78 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
79 UdevBuiltinCommand cmd
;
82 log_set_max_level(LOG_DEBUG
);
83 log_parse_environment();
85 r
= parse_argv(argc
, argv
);
91 cmd
= udev_builtin_lookup(arg_command
);
93 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown command '%s'", arg_command
);
97 r
= find_device_with_action(arg_syspath
, arg_action
, &dev
);
99 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
103 event
= udev_event_new(dev
, NULL
);
109 if (arg_action
!= SD_DEVICE_REMOVE
) {
110 /* For net_setup_link */
111 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
113 log_device_error_errno(dev
, r
, "Failed to clone device: %m");
118 r
= udev_builtin_run(event
, cmd
, arg_command
, true);
120 log_debug_errno(r
, "Builtin command '%s' fails: %m", arg_command
);