]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-test.c
Merge pull request #516 from utezduyar/consistent-get-callback-return
[thirdparty/systemd.git] / src / udev / udevadm-test.c
CommitLineData
bb051f66 1/*
e8d569b4 2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
1298001e 3 * Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org>
bb051f66 4 *
55e9959b
KS
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
bb051f66 9 *
55e9959b
KS
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bb051f66
GKH
17 */
18
19#include <stdlib.h>
bb051f66 20#include <stdio.h>
1aa1e248
KS
21#include <stddef.h>
22#include <unistd.h>
bb051f66 23#include <errno.h>
bb051f66 24#include <signal.h>
eff4a673 25#include <getopt.h>
2181d30a 26#include <sys/signalfd.h>
bb051f66
GKH
27
28#include "udev.h"
44433ebd 29#include "udev-util.h"
bb051f66 30
5ac0162c
LP
31static void help(void) {
32
33 printf("%s test OPTIONS <syspath>\n\n"
34 "Test an event run.\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -a --action=ACTION Set action string\n"
38 " -N --resolve-names=early|late|never When to resolve names\n"
39 , program_invocation_short_name);
40}
41
9ec6e95b 42static int adm_test(struct udev *udev, int argc, char *argv[]) {
912541b0
KS
43 int resolve_names = 1;
44 char filename[UTIL_PATH_SIZE];
45 const char *action = "add";
46 const char *syspath = NULL;
912541b0 47 struct udev_list_entry *entry;
44433ebd
ZJS
48 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
49 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
50 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
912541b0 51 sigset_t mask, sigmask_orig;
7643ac9a 52 int rc = 0, c;
912541b0
KS
53
54 static const struct option options[] = {
55 { "action", required_argument, NULL, 'a' },
56 { "resolve-names", required_argument, NULL, 'N' },
57 { "help", no_argument, NULL, 'h' },
58 {}
59 };
60
9f6445e3 61 log_debug("version %s", VERSION);
912541b0 62
7643ac9a
ZJS
63 while((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0)
64 switch (c) {
912541b0
KS
65 case 'a':
66 action = optarg;
67 break;
68 case 'N':
090be865 69 if (streq (optarg, "early")) {
912541b0 70 resolve_names = 1;
090be865 71 } else if (streq (optarg, "late")) {
912541b0 72 resolve_names = 0;
090be865 73 } else if (streq (optarg, "never")) {
912541b0
KS
74 resolve_names = -1;
75 } else {
76 fprintf(stderr, "resolve-names must be early, late or never\n");
9f6445e3 77 log_error("resolve-names must be early, late or never");
912541b0
KS
78 exit(EXIT_FAILURE);
79 }
80 break;
81 case 'h':
5ac0162c 82 help();
912541b0 83 exit(EXIT_SUCCESS);
7643ac9a 84 case '?':
912541b0 85 exit(EXIT_FAILURE);
7643ac9a
ZJS
86 default:
87 assert_not_reached("Unknown option");
912541b0 88 }
912541b0 89
7643ac9a 90 syspath = argv[optind];
912541b0
KS
91 if (syspath == NULL) {
92 fprintf(stderr, "syspath parameter missing\n");
93 rc = 2;
94 goto out;
95 }
96
baa30fbc 97 printf("This program is for debugging only, it does not run any program\n"
912541b0
KS
98 "specified by a RUN key. It may show incorrect results, because\n"
99 "some values may be different, or not available at a simulation run.\n"
100 "\n");
101
102 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
103
104 udev_builtin_init(udev);
105
106 rules = udev_rules_new(udev, resolve_names);
107 if (rules == NULL) {
108 fprintf(stderr, "error reading rules\n");
109 rc = 3;
110 goto out;
111 }
112
113 /* add /sys if needed */
33502ffe 114 if (!startswith(syspath, "/sys"))
d5a89d7d 115 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
912541b0 116 else
d5a89d7d 117 strscpy(filename, sizeof(filename), syspath);
912541b0
KS
118 util_remove_trailing_chars(filename, '/');
119
bf0e00ec 120 dev = udev_device_new_from_synthetic_event(udev, filename, action);
912541b0
KS
121 if (dev == NULL) {
122 fprintf(stderr, "unable to open device '%s'\n", filename);
123 rc = 4;
124 goto out;
125 }
126
bf0e00ec 127 /* don't read info from the db */
912541b0 128 udev_device_set_info_loaded(dev);
912541b0 129
912541b0
KS
130 event = udev_event_new(dev);
131
132 sigfillset(&mask);
133 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
912541b0 134
adeba500
KS
135 udev_event_execute_rules(event,
136 60 * USEC_PER_SEC, 20 * USEC_PER_SEC,
137 NULL,
8314de1d 138 rules);
912541b0
KS
139
140 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
141 printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
142
1ea97217
KS
143 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
144 char program[UTIL_PATH_SIZE];
912541b0 145
1ea97217
KS
146 udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program));
147 printf("run: '%s'\n", program);
912541b0 148 }
2181d30a 149out:
912541b0
KS
150 udev_builtin_exit(udev);
151 return rc;
bb051f66 152}
1985c76e
KS
153
154const struct udevadm_cmd udevadm_test = {
912541b0
KS
155 .name = "test",
156 .cmd = adm_test,
5ac0162c 157 .help = "Test an event run",
912541b0 158 .debug = true,
1985c76e 159};