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