]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
2ee78da51b0a6b641f8df3da4823dcb104d4f4b7
[thirdparty/systemd.git] / src / udev / udevadm-test.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
4 */
5
6 #include <errno.h>
7 #include <getopt.h>
8 #include <signal.h>
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/signalfd.h>
13 #include <unistd.h>
14
15 #include "sd-device.h"
16
17 #include "device-private.h"
18 #include "device-util.h"
19 #include "libudev-util.h"
20 #include "string-util.h"
21 #include "strxcpyx.h"
22 #include "udev-builtin.h"
23 #include "udev.h"
24 #include "udevadm.h"
25
26 static const char *arg_action = "add";
27 static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
28 static char arg_syspath[UTIL_PATH_SIZE] = {};
29
30 static int help(void) {
31
32 printf("%s test [OPTIONS] DEVPATH\n\n"
33 "Test an event run.\n\n"
34 " -h --help Show this help\n"
35 " -V --version Show package version\n"
36 " -a --action=ACTION Set action string\n"
37 " -N --resolve-names=early|late|never When to resolve names\n"
38 , program_invocation_short_name);
39
40 return 0;
41 }
42
43 static int parse_argv(int argc, char *argv[]) {
44 static const struct option options[] = {
45 { "action", required_argument, NULL, 'a' },
46 { "resolve-names", required_argument, NULL, 'N' },
47 { "version", no_argument, NULL, 'V' },
48 { "help", no_argument, NULL, 'h' },
49 {}
50 };
51
52 int c;
53
54 while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
55 switch (c) {
56 case 'a':
57 arg_action = optarg;
58 break;
59 case 'N':
60 arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
61 if (arg_resolve_name_timing < 0) {
62 log_error("--resolve-names= must be early, late or never");
63 return -EINVAL;
64 }
65 break;
66 case 'V':
67 return print_version();
68 case 'h':
69 return help();
70 case '?':
71 return -EINVAL;
72 default:
73 assert_not_reached("Unknown option");
74 }
75
76 if (!argv[optind]) {
77 log_error("syspath parameter missing.");
78 return -EINVAL;
79 }
80
81 /* add /sys if needed */
82 if (!startswith(argv[optind], "/sys"))
83 strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL);
84 else
85 strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]);
86
87 return 1;
88 }
89
90 int test_main(int argc, char *argv[], void *userdata) {
91 _cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
92 _cleanup_(udev_event_freep) struct udev_event *event = NULL;
93 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
94 const char *cmd, *key, *value;
95 sigset_t mask, sigmask_orig;
96 Iterator i;
97 void *val;
98 int r;
99
100 log_set_max_level(LOG_DEBUG);
101
102 r = parse_argv(argc, argv);
103 if (r <= 0)
104 return r;
105
106 printf("This program is for debugging only, it does not run any program\n"
107 "specified by a RUN key. It may show incorrect results, because\n"
108 "some values may be different, or not available at a simulation run.\n"
109 "\n");
110
111 assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0);
112
113 udev_builtin_init();
114
115 rules = udev_rules_new(arg_resolve_name_timing);
116 if (!rules) {
117 log_error("Failed to read udev rules.");
118 r = -ENOMEM;
119 goto out;
120 }
121
122 r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action);
123 if (r < 0) {
124 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
125 goto out;
126 }
127
128 /* don't read info from the db */
129 device_seal(dev);
130
131 event = udev_event_new(dev, 0, NULL);
132
133 sigfillset(&mask);
134 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
135
136 udev_event_execute_rules(event, 60 * USEC_PER_SEC, NULL, rules);
137
138 FOREACH_DEVICE_PROPERTY(dev, key, value)
139 printf("%s=%s\n", key, value);
140
141 HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
142 char program[UTIL_PATH_SIZE];
143
144 udev_event_apply_format(event, cmd, program, sizeof(program), false);
145 printf("run: '%s'\n", program);
146 }
147
148 r = 0;
149 out:
150 udev_builtin_exit();
151 return r;
152 }