1 /* SPDX-License-Identifier: GPL-2.0+ */
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 "libudev-util.h"
20 #include "path-util.h"
21 #include "string-util.h"
23 #include "udev-builtin.h"
24 #include "udev-event.h"
27 static const char *arg_action
= "add";
28 static ResolveNameTiming arg_resolve_name_timing
= RESOLVE_NAME_EARLY
;
29 static char arg_syspath
[UTIL_PATH_SIZE
] = {};
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)
60 if (streq(optarg
, "help")) {
61 dump_device_action_table();
65 a
= device_action_from_string(optarg
);
67 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
68 "Invalid action '%s'", optarg
);
74 arg_resolve_name_timing
= resolve_name_timing_from_string(optarg
);
75 if (arg_resolve_name_timing
< 0)
76 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
77 "--resolve-names= must be early, late or never");
80 return print_version();
86 assert_not_reached("Unknown option");
90 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
91 "syspath parameter missing.");
93 /* add /sys if needed */
94 if (!path_startswith(argv
[optind
], "/sys"))
95 strscpyl(arg_syspath
, sizeof(arg_syspath
), "/sys", argv
[optind
], NULL
);
97 strscpy(arg_syspath
, sizeof(arg_syspath
), argv
[optind
]);
102 int test_main(int argc
, char *argv
[], void *userdata
) {
103 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
104 _cleanup_(udev_event_freep
) UdevEvent
*event
= NULL
;
105 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
106 const char *cmd
, *key
, *value
;
107 sigset_t mask
, sigmask_orig
;
111 log_set_max_level(LOG_DEBUG
);
113 r
= parse_argv(argc
, argv
);
117 printf("This program is for debugging only, it does not run any program\n"
118 "specified by a RUN key. It may show incorrect results, because\n"
119 "some values may be different, or not available at a simulation run.\n"
122 assert_se(sigprocmask(SIG_SETMASK
, NULL
, &sigmask_orig
) >= 0);
126 r
= udev_rules_load(&rules
, arg_resolve_name_timing
);
128 log_error_errno(r
, "Failed to read udev rules: %m");
132 r
= device_new_from_synthetic_event(&dev
, arg_syspath
, arg_action
);
134 log_error_errno(r
, "Failed to open device '%s': %m", arg_syspath
);
138 /* don't read info from the db */
141 event
= udev_event_new(dev
, 0, NULL
);
143 assert_se(sigfillset(&mask
) >= 0);
144 assert_se(sigprocmask(SIG_SETMASK
, &mask
, &sigmask_orig
) >= 0);
146 udev_event_execute_rules(event
, 60 * USEC_PER_SEC
, SIGKILL
, NULL
, rules
);
148 FOREACH_DEVICE_PROPERTY(dev
, key
, value
)
149 printf("%s=%s\n", key
, value
);
151 ORDERED_HASHMAP_FOREACH_KEY(val
, cmd
, event
->run_list
) {
152 char program
[UTIL_PATH_SIZE
];
154 (void) udev_event_apply_format(event
, cmd
, program
, sizeof(program
), false);
155 printf("run: '%s'\n", program
);