]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test.c
Merge pull request #11933 from keszybz/coverity
[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 DeviceAction a;
58
59 a = device_action_from_string(optarg);
60 if (a < 0)
61 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
62 "Invalid action '%s'", optarg);
63
64 arg_action = optarg;
65 break;
66 }
67 case 'N':
68 arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
69 if (arg_resolve_name_timing < 0)
70 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
71 "--resolve-names= must be early, late or never");
72 break;
73 case 'V':
74 return print_version();
75 case 'h':
76 return help();
77 case '?':
78 return -EINVAL;
79 default:
80 assert_not_reached("Unknown option");
81 }
82
83 if (!argv[optind])
84 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
85 "syspath parameter missing.");
86
87 /* add /sys if needed */
88 if (!startswith(argv[optind], "/sys"))
89 strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL);
90 else
91 strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]);
92
93 return 1;
94 }
95
96 int test_main(int argc, char *argv[], void *userdata) {
97 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
98 _cleanup_(udev_event_freep) UdevEvent *event = NULL;
99 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
100 const char *cmd, *key, *value;
101 sigset_t mask, sigmask_orig;
102 Iterator i;
103 void *val;
104 int r;
105
106 log_set_max_level(LOG_DEBUG);
107
108 r = parse_argv(argc, argv);
109 if (r <= 0)
110 return r;
111
112 printf("This program is for debugging only, it does not run any program\n"
113 "specified by a RUN key. It may show incorrect results, because\n"
114 "some values may be different, or not available at a simulation run.\n"
115 "\n");
116
117 assert_se(sigprocmask(SIG_SETMASK, NULL, &sigmask_orig) >= 0);
118
119 udev_builtin_init();
120
121 r = udev_rules_new(&rules, arg_resolve_name_timing);
122 if (r < 0) {
123 log_error_errno(r, "Failed to read udev rules: %m");
124 goto out;
125 }
126
127 r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action);
128 if (r < 0) {
129 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
130 goto out;
131 }
132
133 /* don't read info from the db */
134 device_seal(dev);
135
136 event = udev_event_new(dev, 0, NULL);
137
138 assert_se(sigfillset(&mask) >= 0);
139 assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0);
140
141 udev_event_execute_rules(event, 60 * USEC_PER_SEC, NULL, rules);
142
143 FOREACH_DEVICE_PROPERTY(dev, key, value)
144 printf("%s=%s\n", key, value);
145
146 ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
147 char program[UTIL_PATH_SIZE];
148
149 udev_event_apply_format(event, cmd, program, sizeof(program), false);
150 printf("run: '%s'\n", program);
151 }
152
153 r = 0;
154 out:
155 udev_builtin_exit();
156 return r;
157 }