2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2004-02-13 04:48:07 +01:00
|
|
|
/*
|
2005-07-22 18:35:58 +02:00
|
|
|
* Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
|
2012-11-12 19:36:23 +01:00
|
|
|
* Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org>
|
2004-02-13 04:48:07 +01:00
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2004-02-13 04:48:07 +01:00
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2013-12-18 03:49:07 +01:00
|
|
|
#include "udev-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "udev.h"
|
2017-12-05 15:30:10 +01:00
|
|
|
#include "udevadm-util.h"
|
2004-02-13 04:48:07 +01:00
|
|
|
|
2015-01-05 13:19:55 +01:00
|
|
|
static void help(void) {
|
|
|
|
|
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"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -a --action=ACTION Set action string\n"
|
|
|
|
" -N --resolve-names=early|late|never When to resolve names\n"
|
|
|
|
, program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int adm_test(struct udev *udev, int argc, char *argv[]) {
|
2012-01-10 01:34:15 +01:00
|
|
|
int resolve_names = 1;
|
|
|
|
char filename[UTIL_PATH_SIZE];
|
|
|
|
const char *action = "add";
|
|
|
|
const char *syspath = NULL;
|
|
|
|
struct udev_list_entry *entry;
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_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;
|
2012-01-10 01:34:15 +01:00
|
|
|
sigset_t mask, sigmask_orig;
|
2013-12-18 03:48:14 +01:00
|
|
|
int rc = 0, c;
|
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
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-04-03 18:15:33 +02:00
|
|
|
log_debug("version %s", PACKAGE_VERSION);
|
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) {
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'a':
|
|
|
|
action = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
2013-02-13 18:13:22 +01:00
|
|
|
if (streq (optarg, "early")) {
|
2012-01-10 01:34:15 +01:00
|
|
|
resolve_names = 1;
|
2013-02-13 18:13:22 +01:00
|
|
|
} else if (streq (optarg, "late")) {
|
2012-01-10 01:34:15 +01:00
|
|
|
resolve_names = 0;
|
2013-02-13 18:13:22 +01:00
|
|
|
} else if (streq (optarg, "never")) {
|
2012-01-10 01:34:15 +01:00
|
|
|
resolve_names = -1;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "resolve-names must be early, late or never\n");
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("resolve-names must be early, late or never");
|
2012-01-10 01:34:15 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2017-12-05 15:30:10 +01:00
|
|
|
case 'V':
|
|
|
|
print_version();
|
|
|
|
exit(EXIT_SUCCESS);
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
2015-01-05 13:19:55 +01:00
|
|
|
help();
|
2012-01-10 01:34:15 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2013-12-18 03:48:14 +01:00
|
|
|
case '?':
|
2012-01-10 01:34:15 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2013-12-18 03:48:14 +01:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 03:48:14 +01:00
|
|
|
syspath = argv[optind];
|
2012-01-10 01:34:15 +01:00
|
|
|
if (syspath == NULL) {
|
|
|
|
fprintf(stderr, "syspath parameter missing\n");
|
|
|
|
rc = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
|
|
|
|
|
|
|
|
udev_builtin_init(udev);
|
|
|
|
|
|
|
|
rules = udev_rules_new(udev, resolve_names);
|
|
|
|
if (rules == NULL) {
|
|
|
|
fprintf(stderr, "error reading rules\n");
|
|
|
|
rc = 3;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add /sys if needed */
|
2012-04-16 20:27:44 +02:00
|
|
|
if (!startswith(syspath, "/sys"))
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
|
2012-01-10 01:34:15 +01:00
|
|
|
else
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpy(filename, sizeof(filename), syspath);
|
2017-11-09 11:12:47 +01:00
|
|
|
delete_trailing_chars(filename, "/");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2015-03-15 17:10:17 +01:00
|
|
|
dev = udev_device_new_from_synthetic_event(udev, filename, action);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (dev == NULL) {
|
|
|
|
fprintf(stderr, "unable to open device '%s'\n", filename);
|
|
|
|
rc = 4;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-03-15 17:10:17 +01:00
|
|
|
/* don't read info from the db */
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_device_set_info_loaded(dev);
|
|
|
|
|
|
|
|
event = udev_event_new(dev);
|
|
|
|
|
|
|
|
sigfillset(&mask);
|
|
|
|
sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
|
|
|
|
|
2014-11-13 20:35:06 +01:00
|
|
|
udev_event_execute_rules(event,
|
|
|
|
60 * USEC_PER_SEC, 20 * USEC_PER_SEC,
|
|
|
|
NULL,
|
2015-06-02 17:07:21 +02:00
|
|
|
rules);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
|
|
|
|
printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
|
|
|
|
|
2014-05-14 00:34:49 +02:00
|
|
|
udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
|
|
|
|
char program[UTIL_PATH_SIZE];
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2017-01-03 20:37:59 +01:00
|
|
|
udev_event_apply_format(event, udev_list_entry_get_name(entry), 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
|
|
|
}
|
2011-04-20 01:53:03 +02:00
|
|
|
out:
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_builtin_exit(udev);
|
|
|
|
return rc;
|
2004-02-13 04:48:07 +01:00
|
|
|
}
|
2011-07-14 01:53:23 +02:00
|
|
|
|
|
|
|
const struct udevadm_cmd udevadm_test = {
|
2012-01-10 01:34:15 +01:00
|
|
|
.name = "test",
|
|
|
|
.cmd = adm_test,
|
2015-01-05 13:19:55 +01:00
|
|
|
.help = "Test an event run",
|
2012-01-10 01:34:15 +01:00
|
|
|
.debug = true,
|
2011-07-14 01:53:23 +02:00
|
|
|
};
|