]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
util-lib: split our string related calls from util.[ch] into its own file string...
[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 <errno.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/signalfd.h>
26 #include <unistd.h>
27
28 #include "string-util.h"
29 #include "udev-util.h"
30 #include "udev.h"
31
32 static void help(void) {
33
34 printf("%s test OPTIONS <syspath>\n\n"
35 "Test an event run.\n"
36 " -h --help Show this help\n"
37 " --version Show package version\n"
38 " -a --action=ACTION Set action string\n"
39 " -N --resolve-names=early|late|never When to resolve names\n"
40 , program_invocation_short_name);
41 }
42
43 static int adm_test(struct udev *udev, int argc, char *argv[]) {
44 int resolve_names = 1;
45 char filename[UTIL_PATH_SIZE];
46 const char *action = "add";
47 const char *syspath = NULL;
48 struct udev_list_entry *entry;
49 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
50 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
51 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
52 sigset_t mask, sigmask_orig;
53 int rc = 0, c;
54
55 static const struct option options[] = {
56 { "action", required_argument, NULL, 'a' },
57 { "resolve-names", required_argument, NULL, 'N' },
58 { "help", no_argument, NULL, 'h' },
59 {}
60 };
61
62 log_debug("version %s", VERSION);
63
64 while((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0)
65 switch (c) {
66 case 'a':
67 action = optarg;
68 break;
69 case 'N':
70 if (streq (optarg, "early")) {
71 resolve_names = 1;
72 } else if (streq (optarg, "late")) {
73 resolve_names = 0;
74 } else if (streq (optarg, "never")) {
75 resolve_names = -1;
76 } else {
77 fprintf(stderr, "resolve-names must be early, late or never\n");
78 log_error("resolve-names must be early, late or never");
79 exit(EXIT_FAILURE);
80 }
81 break;
82 case 'h':
83 help();
84 exit(EXIT_SUCCESS);
85 case '?':
86 exit(EXIT_FAILURE);
87 default:
88 assert_not_reached("Unknown option");
89 }
90
91 syspath = argv[optind];
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 (!startswith(syspath, "/sys"))
116 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
117 else
118 strscpy(filename, sizeof(filename), syspath);
119 util_remove_trailing_chars(filename, '/');
120
121 dev = udev_device_new_from_synthetic_event(udev, filename, action);
122 if (dev == NULL) {
123 fprintf(stderr, "unable to open device '%s'\n", filename);
124 rc = 4;
125 goto out;
126 }
127
128 /* don't read info from the db */
129 udev_device_set_info_loaded(dev);
130
131 event = udev_event_new(dev);
132
133 sigfillset(&mask);
134 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
135
136 udev_event_execute_rules(event,
137 60 * USEC_PER_SEC, 20 * USEC_PER_SEC,
138 NULL,
139 rules);
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 udev_builtin_exit(udev);
152 return rc;
153 }
154
155 const struct udevadm_cmd udevadm_test = {
156 .name = "test",
157 .cmd = adm_test,
158 .help = "Test an event run",
159 .debug = true,
160 };