]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
udev: timeout - warn after a third of the timeout before killing
[thirdparty/systemd.git] / src / udev / udevadm-test.c
1 /*
2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org>
4 *
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.
9 *
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/>.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <getopt.h>
29 #include <sys/signalfd.h>
30
31 #include "udev.h"
32 #include "udev-util.h"
33
34 static int adm_test(struct udev *udev, int argc, char *argv[]) {
35 int resolve_names = 1;
36 char filename[UTIL_PATH_SIZE];
37 const char *action = "add";
38 const char *syspath = NULL;
39 struct udev_list_entry *entry;
40 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
41 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
42 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
43 sigset_t mask, sigmask_orig;
44 int rc = 0, c;
45
46 static const struct option options[] = {
47 { "action", required_argument, NULL, 'a' },
48 { "resolve-names", required_argument, NULL, 'N' },
49 { "help", no_argument, NULL, 'h' },
50 {}
51 };
52
53 log_debug("version %s", VERSION);
54
55 while((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0)
56 switch (c) {
57 case 'a':
58 action = optarg;
59 break;
60 case 'N':
61 if (streq (optarg, "early")) {
62 resolve_names = 1;
63 } else if (streq (optarg, "late")) {
64 resolve_names = 0;
65 } else if (streq (optarg, "never")) {
66 resolve_names = -1;
67 } else {
68 fprintf(stderr, "resolve-names must be early, late or never\n");
69 log_error("resolve-names must be early, late or never");
70 exit(EXIT_FAILURE);
71 }
72 break;
73 case 'h':
74 printf("Usage: udevadm test OPTIONS <syspath>\n"
75 " -a,--action=ACTION set action string\n"
76 " -N,--resolve-names=early|late|never when to resolve names\n"
77 " -h,--help print this help string\n"
78 "\n");
79 exit(EXIT_SUCCESS);
80 case '?':
81 exit(EXIT_FAILURE);
82 default:
83 assert_not_reached("Unknown option");
84 }
85
86 syspath = argv[optind];
87 if (syspath == NULL) {
88 fprintf(stderr, "syspath parameter missing\n");
89 rc = 2;
90 goto out;
91 }
92
93 printf("This program is for debugging only, it does not run any program\n"
94 "specified by a RUN key. It may show incorrect results, because\n"
95 "some values may be different, or not available at a simulation run.\n"
96 "\n");
97
98 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
99
100 udev_builtin_init(udev);
101
102 rules = udev_rules_new(udev, resolve_names);
103 if (rules == NULL) {
104 fprintf(stderr, "error reading rules\n");
105 rc = 3;
106 goto out;
107 }
108
109 /* add /sys if needed */
110 if (!startswith(syspath, "/sys"))
111 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
112 else
113 strscpy(filename, sizeof(filename), syspath);
114 util_remove_trailing_chars(filename, '/');
115
116 dev = udev_device_new_from_syspath(udev, filename);
117 if (dev == NULL) {
118 fprintf(stderr, "unable to open device '%s'\n", filename);
119 rc = 4;
120 goto out;
121 }
122
123 /* skip reading of db, but read kernel parameters */
124 udev_device_set_info_loaded(dev);
125 udev_device_read_uevent_file(dev);
126
127 udev_device_set_action(dev, action);
128 event = udev_event_new(dev);
129
130 sigfillset(&mask);
131 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
132 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
133 if (event->fd_signal < 0) {
134 fprintf(stderr, "error creating signalfd\n");
135 rc = 5;
136 goto out;
137 }
138
139 udev_event_execute_rules(event, 60 * USEC_PER_SEC, 20 * USEC_PER_SEC, rules, &sigmask_orig);
140
141 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
142 printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
143
144 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
145 char program[UTIL_PATH_SIZE];
146
147 udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program));
148 printf("run: '%s'\n", program);
149 }
150 out:
151 if (event != NULL && event->fd_signal >= 0)
152 close(event->fd_signal);
153 udev_builtin_exit(udev);
154 return rc;
155 }
156
157 const struct udevadm_cmd udevadm_test = {
158 .name = "test",
159 .cmd = adm_test,
160 .help = "test an event run",
161 .debug = true,
162 };