Systemd/src/udev/udevadm-test-builtin.c

98 lines
2.6 KiB
C
Raw Normal View History

2020-11-09 05:25:50 +01:00
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <errno.h>
#include <getopt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
#include "udev-builtin.h"
#include "udevadm.h"
#include "udevadm-util.h"
2018-08-21 09:08:54 +02:00
static const char *arg_command = NULL;
static const char *arg_syspath = NULL;
2018-08-21 09:08:54 +02:00
static int help(void) {
printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
"Test a built-in command.\n\n"
" -h --help Print this message\n"
" -V --version Print version of the program\n\n"
"Commands:\n"
, program_invocation_short_name);
2018-08-22 12:57:32 +02:00
udev_builtin_list();
2018-08-21 09:08:54 +02:00
return 0;
}
2018-08-21 09:08:54 +02:00
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{}
};
2018-08-21 09:08:54 +02:00
int c;
while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
switch (c) {
case 'V':
return print_version();
case 'h':
2018-08-21 09:08:54 +02:00
return help();
case '?':
return -EINVAL;
default:
assert_not_reached("Unknown option");
}
2018-08-21 09:08:54 +02:00
arg_command = argv[optind++];
if (!arg_command)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Command missing.");
arg_syspath = argv[optind++];
if (!arg_syspath)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"syspath missing.");
2018-08-21 09:08:54 +02:00
return 1;
}
int builtin_main(int argc, char *argv[], void *userdata) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
UdevBuiltinCommand cmd;
2018-08-21 09:08:54 +02:00
int r;
log_set_max_level(LOG_DEBUG);
r = parse_argv(argc, argv);
if (r <= 0)
return r;
2018-08-22 12:57:32 +02:00
udev_builtin_init();
2018-08-21 09:08:54 +02:00
cmd = udev_builtin_lookup(arg_command);
if (cmd < 0) {
2018-08-21 09:08:54 +02:00
log_error("Unknown command '%s'", arg_command);
r = -EINVAL;
goto finish;
}
r = find_device(arg_syspath, "/sys", &dev);
if (r < 0) {
log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
2018-08-21 09:08:54 +02:00
goto finish;
}
r = udev_builtin_run(dev, cmd, arg_command, true);
2018-08-21 09:08:54 +02:00
if (r < 0)
2018-10-23 11:26:06 +02:00
log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
2018-08-21 09:08:54 +02:00
finish:
2018-08-22 12:57:32 +02:00
udev_builtin_exit();
2018-08-21 09:08:54 +02:00
return r;
}