1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
12 #include <sys/signalfd.h>
15 #include "sd-device.h"
17 #include "device-private.h"
18 #include "device-util.h"
19 #include "path-util.h"
20 #include "string-util.h"
22 #include "udev-builtin.h"
23 #include "udev-event.h"
24 #include "udevadm-util.h"
27 static sd_device_action_t arg_action
= SD_DEVICE_ADD
;
28 static ResolveNameTiming arg_resolve_name_timing
= RESOLVE_NAME_EARLY
;
29 static const char *arg_syspath
= NULL
;
31 static int help(void) {
33 printf("%s test [OPTIONS] DEVPATH\n\n"
34 "Test an event run.\n\n"
35 " -h --help Show this help\n"
36 " -V --version Show package version\n"
37 " -a --action=ACTION|help Set action string\n"
38 " -N --resolve-names=early|late|never When to resolve names\n",
39 program_invocation_short_name
);
44 static int parse_argv(int argc
, char *argv
[]) {
45 static const struct option options
[] = {
46 { "action", required_argument
, NULL
, 'a' },
47 { "resolve-names", required_argument
, NULL
, 'N' },
48 { "version", no_argument
, NULL
, 'V' },
49 { "help", no_argument
, NULL
, 'h' },
55 while ((c
= getopt_long(argc
, argv
, "a:N:Vh", options
, NULL
)) >= 0)
58 r
= parse_device_action(optarg
, &arg_action
);
60 return log_error_errno(r
, "Invalid action '%s'", optarg
);
65 arg_resolve_name_timing
= resolve_name_timing_from_string(optarg
);
66 if (arg_resolve_name_timing
< 0)
67 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
68 "--resolve-names= must be early, late or never");
71 return print_version();
80 arg_syspath
= argv
[optind
];
82 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "syspath parameter missing.");
87 int test_main(int argc
, char *argv
[], void *userdata
) {
88 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
89 _cleanup_(udev_event_freep
) UdevEvent
*event
= NULL
;
90 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
92 sigset_t mask
, sigmask_orig
;
96 log_set_max_level(LOG_DEBUG
);
98 r
= parse_argv(argc
, argv
);
102 printf("This program is for debugging only, it does not run any program\n"
103 "specified by a RUN key. It may show incorrect results, because\n"
104 "some values may be different, or not available at a simulation run.\n"
107 assert_se(sigprocmask(SIG_SETMASK
, NULL
, &sigmask_orig
) >= 0);
111 r
= udev_rules_load(&rules
, arg_resolve_name_timing
);
113 log_error_errno(r
, "Failed to read udev rules: %m");
117 r
= find_device_with_action(arg_syspath
, arg_action
, &dev
);
119 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
123 /* don't read info from the db */
126 event
= udev_event_new(dev
, 0, NULL
, LOG_DEBUG
);
128 assert_se(sigfillset(&mask
) >= 0);
129 assert_se(sigprocmask(SIG_SETMASK
, &mask
, &sigmask_orig
) >= 0);
131 udev_event_execute_rules(event
, -1, 60 * USEC_PER_SEC
, SIGKILL
, NULL
, rules
);
133 FOREACH_DEVICE_PROPERTY(dev
, key
, value
)
134 printf("%s=%s\n", key
, value
);
136 ORDERED_HASHMAP_FOREACH_KEY(val
, cmd
, event
->run_list
) {
137 char program
[UDEV_PATH_SIZE
];
140 (void) udev_event_apply_format(event
, cmd
, program
, sizeof(program
), false, &truncated
);
142 log_warning("The command '%s' is truncated while substituting into '%s'.", program
, cmd
);
143 printf("run: '%s'\n", program
);