2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
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-08-29 09:12:22 +02:00
|
|
|
#include "libudev-private.h"
|
2017-08-09 19:03:39 +02:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-08-29 09:12:22 +02:00
|
|
|
#include "strxcpyx.h"
|
|
|
|
#include "udev-builtin.h"
|
2018-08-21 04:17:01 +02:00
|
|
|
#include "udevadm.h"
|
2011-08-05 02:00:30 +02:00
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
static const char *arg_command = NULL;
|
|
|
|
static char arg_syspath[UTIL_PATH_SIZE] = {};
|
|
|
|
|
|
|
|
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
|
|
|
const char *s;
|
|
|
|
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++];
|
|
|
|
if (!arg_command) {
|
|
|
|
log_error("Command missing.");
|
|
|
|
return -EINVAL;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
s = argv[optind++];
|
|
|
|
if (!s) {
|
|
|
|
log_error("syspath missing.");
|
|
|
|
return -EINVAL;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
/* add /sys if needed */
|
|
|
|
if (!path_startswith(s, "/sys"))
|
|
|
|
strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", s, NULL);
|
|
|
|
else
|
|
|
|
strscpy(arg_syspath, sizeof(arg_syspath), s);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int builtin_main(int argc, char *argv[], void *userdata) {
|
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
|
|
|
|
enum udev_builtin_cmd cmd;
|
|
|
|
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);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (cmd >= UDEV_BUILTIN_MAX) {
|
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-08-21 09:08:54 +02:00
|
|
|
dev = udev_device_new_from_syspath(NULL, arg_syspath);
|
|
|
|
if (!dev) {
|
|
|
|
r = log_error_errno(errno, "Failed to open device '%s'", arg_syspath);
|
|
|
|
goto finish;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 09:08:54 +02:00
|
|
|
r = udev_builtin_run(dev, cmd, arg_command, true);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug("error executing '%s', exit code %i", arg_command, r);
|
|
|
|
|
|
|
|
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
|
|
|
}
|