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