]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
udev: switch to systemd logging functions
[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.sievers@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 <syslog.h>
29 #include <getopt.h>
30 #include <sys/signalfd.h>
31
32 #include "udev.h"
33
34 static int adm_test(struct udev *udev, int argc, char *argv[])
35 {
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;
46 int rc = 0;
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
55 log_debug("version %s\n", VERSION);
56
57 for (;;) {
58 int option;
59
60 option = getopt_long(argc, argv, "a:s:N:fh", options, NULL);
61 if (option == -1)
62 break;
63
64 switch (option) {
65 case 'a':
66 action = optarg;
67 break;
68 case 'N':
69 if (strcmp (optarg, "early") == 0) {
70 resolve_names = 1;
71 } else if (strcmp (optarg, "late") == 0) {
72 resolve_names = 0;
73 } else if (strcmp (optarg, "never") == 0) {
74 resolve_names = -1;
75 } else {
76 fprintf(stderr, "resolve-names must be early, late or never\n");
77 log_error("resolve-names must be early, late or never\n");
78 exit(EXIT_FAILURE);
79 }
80 break;
81 case 'h':
82 printf("Usage: udevadm test OPTIONS <syspath>\n"
83 " --action=<string> set action string\n"
84 " --help\n\n");
85 exit(EXIT_SUCCESS);
86 default:
87 exit(EXIT_FAILURE);
88 }
89 }
90 syspath = argv[optind];
91
92 if (syspath == NULL) {
93 fprintf(stderr, "syspath parameter missing\n");
94 rc = 2;
95 goto out;
96 }
97
98 printf("This program is for debugging only, it does not run any program\n"
99 "specified by a RUN key. It may show incorrect results, because\n"
100 "some values may be different, or not available at a simulation run.\n"
101 "\n");
102
103 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
104
105 udev_builtin_init(udev);
106
107 rules = udev_rules_new(udev, resolve_names);
108 if (rules == NULL) {
109 fprintf(stderr, "error reading rules\n");
110 rc = 3;
111 goto out;
112 }
113
114 /* add /sys if needed */
115 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
116 util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), syspath, NULL);
117 else
118 util_strscpy(filename, sizeof(filename), syspath);
119 util_remove_trailing_chars(filename, '/');
120
121 dev = udev_device_new_from_syspath(udev, filename);
122 if (dev == NULL) {
123 fprintf(stderr, "unable to open device '%s'\n", filename);
124 rc = 4;
125 goto out;
126 }
127
128 /* skip reading of db, but read kernel parameters */
129 udev_device_set_info_loaded(dev);
130 udev_device_read_uevent_file(dev);
131
132 udev_device_set_action(dev, action);
133 event = udev_event_new(dev);
134
135 sigfillset(&mask);
136 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
137 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
138 if (event->fd_signal < 0) {
139 fprintf(stderr, "error creating signalfd\n");
140 rc = 5;
141 goto out;
142 }
143
144 err = udev_event_execute_rules(event, rules, &sigmask_orig);
145
146 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
147 printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
148
149 if (err == 0) {
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));
154 printf("run: '%s'\n", program);
155 }
156 }
157 out:
158 if (event != NULL && event->fd_signal >= 0)
159 close(event->fd_signal);
160 udev_event_unref(event);
161 udev_device_unref(dev);
162 udev_rules_unref(rules);
163 udev_builtin_exit(udev);
164 return rc;
165 }
166
167 const struct udevadm_cmd udevadm_test = {
168 .name = "test",
169 .cmd = adm_test,
170 .help = "test an event run",
171 .debug = true,
172 };