2020-11-09 05:25:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2004-02-13 04:48:07 +01:00
|
|
|
/*
|
2018-06-12 17:15:23 +02:00
|
|
|
* Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
|
2004-02-13 04:48:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2007-03-21 11:55:26 +01:00
|
|
|
#include <getopt.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-04-20 01:53:03 +02:00
|
|
|
#include <sys/signalfd.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <unistd.h>
|
2004-02-13 04:48:07 +01:00
|
|
|
|
2018-11-01 14:52:39 +01:00
|
|
|
#include "sd-device.h"
|
|
|
|
|
|
|
|
#include "device-private.h"
|
|
|
|
#include "device-util.h"
|
2020-08-26 17:22:06 +02:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "strxcpyx.h"
|
2018-08-29 09:12:22 +02:00
|
|
|
#include "udev-builtin.h"
|
2019-04-25 01:21:11 +02:00
|
|
|
#include "udev-event.h"
|
2018-08-21 04:17:01 +02:00
|
|
|
#include "udevadm.h"
|
2004-02-13 04:48:07 +01:00
|
|
|
|
2018-08-21 09:24:23 +02:00
|
|
|
static const char *arg_action = "add";
|
2018-10-25 08:30:51 +02:00
|
|
|
static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
|
2020-12-14 08:45:12 +01:00
|
|
|
static char arg_syspath[UDEV_PATH_SIZE] = {};
|
2018-08-21 09:24:23 +02:00
|
|
|
|
|
|
|
static int help(void) {
|
2015-01-05 13:19:55 +01:00
|
|
|
|
2017-12-05 15:30:10 +01:00
|
|
|
printf("%s test [OPTIONS] DEVPATH\n\n"
|
|
|
|
"Test an event run.\n\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -h --help Show this help\n"
|
2017-12-05 15:30:10 +01:00
|
|
|
" -V --version Show package version\n"
|
2019-06-28 21:03:06 +02:00
|
|
|
" -a --action=ACTION|help Set action string\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -N --resolve-names=early|late|never When to resolve names\n"
|
|
|
|
, program_invocation_short_name);
|
|
|
|
|
2018-08-21 09:24:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-08-21 09:24:23 +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
|
|
|
{ "action", required_argument, NULL, 'a' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{ "resolve-names", required_argument, NULL, 'N' },
|
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:24:23 +02:00
|
|
|
int c;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2017-12-05 15:30:10 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
|
2013-12-18 03:48:14 +01:00
|
|
|
switch (c) {
|
2019-03-09 02:20:42 +01:00
|
|
|
case 'a': {
|
|
|
|
DeviceAction a;
|
|
|
|
|
2019-06-28 21:03:06 +02:00
|
|
|
if (streq(optarg, "help")) {
|
|
|
|
dump_device_action_table();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 02:20:42 +01:00
|
|
|
a = device_action_from_string(optarg);
|
|
|
|
if (a < 0)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Invalid action '%s'", optarg);
|
|
|
|
|
2018-08-21 09:24:23 +02:00
|
|
|
arg_action = optarg;
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2019-03-09 02:20:42 +01:00
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'N':
|
2018-10-25 08:30:51 +02:00
|
|
|
arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_resolve_name_timing < 0)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"--resolve-names= must be early, late or never");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
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:24:23 +02:00
|
|
|
return help();
|
2013-12-18 03:48:14 +01:00
|
|
|
case '?':
|
2018-08-21 09:24:23 +02:00
|
|
|
return -EINVAL;
|
2013-12-18 03:48:14 +01:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!argv[optind])
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"syspath parameter missing.");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-08-21 09:24:23 +02:00
|
|
|
/* add /sys if needed */
|
2020-08-26 17:22:06 +02:00
|
|
|
if (!path_startswith(argv[optind], "/sys"))
|
2018-08-21 09:24:23 +02:00
|
|
|
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) {
|
2018-12-14 10:24:40 +01:00
|
|
|
_cleanup_(udev_rules_freep) UdevRules *rules = NULL;
|
2018-12-14 10:27:24 +01:00
|
|
|
_cleanup_(udev_event_freep) UdevEvent *event = NULL;
|
2018-11-01 14:52:39 +01:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
|
|
|
const char *cmd, *key, *value;
|
2018-08-21 09:24:23 +02:00
|
|
|
sigset_t mask, sigmask_orig;
|
2018-10-16 23:11:33 +02:00
|
|
|
void *val;
|
2018-08-21 09:24:23 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
log_set_max_level(LOG_DEBUG);
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
|
2012-04-08 16:06:20 +02:00
|
|
|
printf("This program is for debugging only, it does not run any program\n"
|
2012-01-10 01:34:15 +01:00
|
|
|
"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"
|
|
|
|
"\n");
|
|
|
|
|
2018-10-12 18:52:11 +02:00
|
|
|
assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-08-22 12:57:32 +02:00
|
|
|
udev_builtin_init();
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-06-23 17:58:33 +02:00
|
|
|
r = udev_rules_load(&rules, arg_resolve_name_timing);
|
2018-12-14 10:20:43 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Failed to read udev rules: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:52:39 +01:00
|
|
|
r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
|
2012-01-10 01:34:15 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-03-15 17:10:17 +01:00
|
|
|
/* don't read info from the db */
|
2018-11-01 14:52:39 +01:00
|
|
|
device_seal(dev);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-12-08 14:37:12 +01:00
|
|
|
event = udev_event_new(dev, 0, NULL, LOG_DEBUG);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-12-21 08:12:45 +01:00
|
|
|
assert_se(sigfillset(&mask) >= 0);
|
|
|
|
assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-04-09 11:14:25 +02:00
|
|
|
udev_event_execute_rules(event, 60 * USEC_PER_SEC, SIGKILL, NULL, rules);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-11-01 14:52:39 +01:00
|
|
|
FOREACH_DEVICE_PROPERTY(dev, key, value)
|
|
|
|
printf("%s=%s\n", key, value);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list) {
|
2020-12-14 08:45:12 +01:00
|
|
|
char program[UDEV_PATH_SIZE];
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-06-18 06:48:45 +02:00
|
|
|
(void) udev_event_apply_format(event, cmd, program, sizeof(program), false);
|
2014-05-14 00:34:49 +02:00
|
|
|
printf("run: '%s'\n", program);
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2018-08-21 09:24:23 +02:00
|
|
|
|
|
|
|
r = 0;
|
2011-04-20 01:53:03 +02:00
|
|
|
out:
|
2018-08-22 12:57:32 +02:00
|
|
|
udev_builtin_exit();
|
2018-08-21 09:24:23 +02:00
|
|
|
return r;
|
2004-02-13 04:48:07 +01:00
|
|
|
}
|