1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

udevadm-test: modernize code

This commit is contained in:
Yu Watanabe 2018-08-21 16:24:23 +09:00
parent 8450abf423
commit 89e94ad333

View File

@ -15,9 +15,12 @@
#include "string-util.h"
#include "udev.h"
#include "udevadm.h"
#include "udevadm-util.h"
static void help(void) {
static const char *arg_action = "add";
static int arg_resolve_names = 1;
static char arg_syspath[UTIL_PATH_SIZE] = {};
static int help(void) {
printf("%s test [OPTIONS] DEVPATH\n\n"
"Test an event run.\n\n"
@ -26,20 +29,11 @@ static void help(void) {
" -a --action=ACTION Set action string\n"
" -N --resolve-names=early|late|never When to resolve names\n"
, program_invocation_short_name);
return 0;
}
int test_main(int argc, char *argv[], void *userdata) {
int resolve_names = 1;
char filename[UTIL_PATH_SIZE];
const char *action = "add";
const char *syspath = NULL;
struct udev_list_entry *entry;
_cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(udev_event_unrefp) struct udev_event *event = NULL;
sigset_t mask, sigmask_orig;
int rc = 0, c;
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "action", required_argument, NULL, 'a' },
{ "resolve-names", required_argument, NULL, 'N' },
@ -48,46 +42,63 @@ int test_main(int argc, char *argv[], void *userdata) {
{}
};
log_set_max_level(LOG_DEBUG);
log_debug("version %s", PACKAGE_VERSION);
int c;
while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
switch (c) {
case 'a':
action = optarg;
arg_action = optarg;
break;
case 'N':
if (streq (optarg, "early")) {
resolve_names = 1;
arg_resolve_names = 1;
} else if (streq (optarg, "late")) {
resolve_names = 0;
arg_resolve_names = 0;
} else if (streq (optarg, "never")) {
resolve_names = -1;
arg_resolve_names = -1;
} else {
fprintf(stderr, "resolve-names must be early, late or never\n");
log_error("resolve-names must be early, late or never");
exit(EXIT_FAILURE);
return -EINVAL;
}
break;
case 'V':
print_version();
exit(EXIT_SUCCESS);
return version();
case 'h':
help();
exit(EXIT_SUCCESS);
return help();
case '?':
exit(EXIT_FAILURE);
return -EINVAL;
default:
assert_not_reached("Unknown option");
}
syspath = argv[optind];
if (syspath == NULL) {
fprintf(stderr, "syspath parameter missing\n");
rc = 2;
goto out;
if (!argv[optind]) {
log_error("syspath parameter missing.");
return -EINVAL;
}
/* add /sys if needed */
if (!startswith(argv[optind], "/sys"))
strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL);
else
strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]);
return 1;
}
int test_main(int argc, char *argv[], void *userdata) {
_cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(udev_event_unrefp) struct udev_event *event = NULL;
struct udev_list_entry *entry;
sigset_t mask, sigmask_orig;
int r;
log_set_max_level(LOG_DEBUG);
r = parse_argv(argc, argv);
if (r <= 0)
return r;
printf("This program is for debugging only, it does not run any program\n"
"specified by a RUN key. It may show incorrect results, because\n"
"some values may be different, or not available at a simulation run.\n"
@ -97,24 +108,16 @@ int test_main(int argc, char *argv[], void *userdata) {
udev_builtin_init();
rules = udev_rules_new(resolve_names);
if (rules == NULL) {
fprintf(stderr, "error reading rules\n");
rc = 3;
rules = udev_rules_new(arg_resolve_names);
if (!rules) {
log_error("Failed to read udev rules.");
r = -ENOMEM;
goto out;
}
/* add /sys if needed */
if (!startswith(syspath, "/sys"))
strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
else
strscpy(filename, sizeof(filename), syspath);
delete_trailing_chars(filename, "/");
dev = udev_device_new_from_synthetic_event(NULL, filename, action);
dev = udev_device_new_from_synthetic_event(NULL, arg_syspath, arg_action);
if (dev == NULL) {
fprintf(stderr, "unable to open device '%s'\n", filename);
rc = 4;
r = log_error_errno(errno, "Failed to open device '%s': %m", arg_syspath);
goto out;
}
@ -140,7 +143,9 @@ int test_main(int argc, char *argv[], void *userdata) {
udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program), false);
printf("run: '%s'\n", program);
}
r = 0;
out:
udev_builtin_exit();
return rc;
return r;
}