2020-11-09 05:25:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-08-05 02:00:30 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-08-05 02:00:30 +02:00
|
|
|
|
2018-10-22 06:47:06 +02:00
|
|
|
#include "log.h"
|
2018-08-29 09:12:22 +02:00
|
|
|
#include "udev-builtin.h"
|
2018-08-21 04:17:01 +02:00
|
|
|
#include "udevadm.h"
|
2018-10-22 06:47:06 +02:00
|
|
|
#include "udevadm-util.h"
|
2011-08-05 02:00:30 +02:00
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
static const char *arg_command = NULL;
|
2018-10-22 06:47:06 +02:00
|
|
|
static const char *arg_syspath = NULL;
|
2018-08-21 09:08:54 +02:00
|
|
|
|
|
|
|
static int help(void) {
|
2017-12-05 15:30:10 +01:00
|
|
|
printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
"Test a built-in command.\n\n"
|
|
|
|
" -h --help Print this message\n"
|
2017-12-05 15:30:10 +01:00
|
|
|
" -V --version Print version of the program\n\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
"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;
|
2011-08-05 02:00:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
2012-01-10 01:34:15 +01:00
|
|
|
static const struct option options[] = {
|
2017-12-05 15:30:10 +01:00
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
int c;
|
2018-08-21 04:17:01 +02:00
|
|
|
|
2017-12-05 15:30:10 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
|
2013-12-18 03:48:14 +01:00
|
|
|
switch (c) {
|
2017-12-05 15:30:10 +01:00
|
|
|
case 'V':
|
2018-09-21 10:45:26 +02:00
|
|
|
return print_version();
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
2018-08-21 09:08:54 +02:00
|
|
|
return help();
|
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
arg_command = argv[optind++];
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!arg_command)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Command missing.");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-10-22 06:47:06 +02:00
|
|
|
arg_syspath = argv[optind++];
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!arg_syspath)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"syspath missing.");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int builtin_main(int argc, char *argv[], void *userdata) {
|
2018-10-13 19:01:58 +02:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
2019-04-25 01:21:11 +02:00
|
|
|
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();
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
cmd = udev_builtin_lookup(arg_command);
|
2018-10-13 20:09:13 +02:00
|
|
|
if (cmd < 0) {
|
2018-08-21 09:08:54 +02:00
|
|
|
log_error("Unknown command '%s'", arg_command);
|
|
|
|
r = -EINVAL;
|
|
|
|
goto finish;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 06:47:06 +02:00
|
|
|
r = find_device(arg_syspath, "/sys", &dev);
|
2018-10-13 19:01:58 +02:00
|
|
|
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;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 19:01:58 +02:00
|
|
|
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;
|
2011-08-05 02:00:30 +02:00
|
|
|
}
|