]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
Merge pull request #16866 from yuwata/networkctl-tiny-cleanups
[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 "path-util.h"
21 #include "string-util.h"
22 #include "strxcpyx.h"
23 #include "udev-builtin.h"
24 #include "udev-event.h"
25 #include "udevadm.h"
26
27 static const char *arg_action = "add";
28 static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
29 static char arg_syspath[UTIL_PATH_SIZE] = {};
30
31 static int help(void) {
32
33 printf("%s test [OPTIONS] DEVPATH\n\n"
34 "Test an event run.\n\n"
35 " -h --help Show this help\n"
36 " -V --version Show package version\n"
37 " -a --action=ACTION|help Set action string\n"
38 " -N --resolve-names=early|late|never When to resolve names\n"
39 , program_invocation_short_name);
40
41 return 0;
42 }
43
44 static int parse_argv(int argc, char *argv[]) {
45 static const struct option options[] = {
46 { "action", required_argument, NULL, 'a' },
47 { "resolve-names", required_argument, NULL, 'N' },
48 { "version", no_argument, NULL, 'V' },
49 { "help", no_argument, NULL, 'h' },
50 {}
51 };
52
53 int c;
54
55 while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
56 switch (c) {
57 case 'a': {
58 DeviceAction a;
59
60 if (streq(optarg, "help")) {
61 dump_device_action_table();
62 return 0;
63 }
64
65 a = device_action_from_string(optarg);
66 if (a < 0)
67 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
68 "Invalid action '%s'", optarg);
69
70 arg_action = optarg;
71 break;
72 }
73 case 'N':
74 arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
75 if (arg_resolve_name_timing < 0)
76 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
77 "--resolve-names= must be early, late or never");
78 break;
79 case 'V':
80 return print_version();
81 case 'h':
82 return help();
83 case '?':
84 return -EINVAL;
85 default:
86 assert_not_reached("Unknown option");
87 }
88
89 if (!argv[optind])
90 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
91 "syspath parameter missing.");
92
93 /* add /sys if needed */
94 if (!path_startswith(argv[optind], "/sys"))
95 strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL);
96 else
97 strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]);
98
99 return 1;
100 }
101
102 int test_main(int argc, char *argv[], void *userdata) {
103 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
104 _cleanup_(udev_event_freep) UdevEvent *event = NULL;
105 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
106 const char *cmd, *key, *value;
107 sigset_t mask, sigmask_orig;
108 Iterator i;
109 void *val;
110 int r;
111
112 log_set_max_level(LOG_DEBUG);
113
114 r = parse_argv(argc, argv);
115 if (r <= 0)
116 return r;
117
118 printf("This program is for debugging only, it does not run any program\n"
119 "specified by a RUN key. It may show incorrect results, because\n"
120 "some values may be different, or not available at a simulation run.\n"
121 "\n");
122
123 assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0);
124
125 udev_builtin_init();
126
127 r = udev_rules_load(&rules, arg_resolve_name_timing);
128 if (r < 0) {
129 log_error_errno(r, "Failed to read udev rules: %m");
130 goto out;
131 }
132
133 r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action);
134 if (r < 0) {
135 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
136 goto out;
137 }
138
139 /* don't read info from the db */
140 device_seal(dev);
141
142 event = udev_event_new(dev, 0, NULL);
143
144 assert_se(sigfillset(&mask) >= 0);
145 assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0);
146
147 udev_event_execute_rules(event, 60 * USEC_PER_SEC, SIGKILL, NULL, rules);
148
149 FOREACH_DEVICE_PROPERTY(dev, key, value)
150 printf("%s=%s\n", key, value);
151
152 ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
153 char program[UTIL_PATH_SIZE];
154
155 (void) udev_event_apply_format(event, cmd, program, sizeof(program), false);
156 printf("run: '%s'\n", program);
157 }
158
159 r = 0;
160 out:
161 udev_builtin_exit();
162 return r;
163 }