Debian Codegraph @

src/udev/test-udev-rule-runner.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2003-2004 Greg Kroah-Hartman <[email protected]>
4 ***/
5
6 #include <sys/mount.h>
7 #include <unistd.h>
8
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "fs-util.h"
12 #include "label-util.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "mkdir.h"
16 #include "mount-util.h"
17 #include "namespace-util.h"
18 #include "parse-util.h"
19 #include "selinux-util.h"
20 #include "signal-util.h"
21 #include "string-util.h"
22 #include "tests.h"
23 #include "time-util.h"
24 #include "udev-event.h"
25 #include "udev-rules.h"
26 #include "udev-spawn.h"
27 #include "version.h"
28
29 static int device_new_from_synthetic_event(sd_device **ret, const char *syspath, const char *action) {
30 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
31 sd_device_action_t a;
32 int r;
33
34 assert(ret);
35 assert(syspath);
36 assert(action);
37
38 a = device_action_from_string(action);
39 if (a < 0)
40 return a;
41
42 r = sd_device_new_from_syspath(&dev, syspath);
43 if (r < 0)
44 return r;
45
46 r = device_read_uevent_file(dev);
47 if (r < 0)
48 return r;
49
50 r = device_set_action(dev, a);
51 if (r < 0)
52 return r;
53
54 *ret = TAKE_PTR(dev);
55 return 0;
56 }
57
58 static int fake_filesystems(void) {
59 static const struct fakefs {
60 const char *src;
61 const char *target;
62 const char *error;
63 bool ignore_mount_error;
64 } fakefss[] = {
65 { "tmpfs/sys", "/sys", "Failed to mount test /sys", false },
66 { "tmpfs/dev", "/dev", "Failed to mount test /dev", false },
67 { "run", "/run", "Failed to mount test /run", false },
68 { "run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
69 { "run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
70 };
71 int r;
72
73 r = detach_mount_namespace();
74 if (r < 0)
75 return log_error_errno(r, "Failed to detach mount namespace: %m");
76
77 FOREACH_ELEMENT(fakefs, fakefss) {
78 r = mount_nofollow_verbose(fakefs->ignore_mount_error ? LOG_NOTICE : LOG_ERR,
79 fakefs->src, fakefs->target, NULL, MS_BIND, NULL);
80 if (r < 0 && !fakefs->ignore_mount_error)
81 return r;
82 }
83
84 return 0;
85 }
86
87 static int run(int argc, char *argv[]) {
88 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
89 _cleanup_(udev_event_unrefp) UdevEvent *event = NULL;
90 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
91 const char *devpath, *devname, *action;
92 int r;
93
94 test_setup_logging(LOG_INFO);
95
96 if (!IN_SET(argc, 2, 3, 4))
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
98 "This program needs between one and three arguments, %d given", argc - 1);
99
100 r = fake_filesystems();
101 if (r < 0)
102 return r;
103
104 /* Let's make sure the test runs with selinux assumed disabled. */
105 #if HAVE_SELINUX
106 if (dlopen_libselinux(LOG_DEBUG) >= 0)
107 sym_fini_selinuxmnt();
108 #endif
109 mac_selinux_retest();
110
111 if (argc == 2) {
112 if (!streq(argv[1], "check"))
113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
114 "Unknown argument: %s", argv[1]);
115
116 return 0;
117 }
118
119 log_debug("version %s", GIT_VERSION);
120
121 r = mac_init();
122 if (r < 0)
123 return r;
124
125 action = argv[1];
126 devpath = argv[2];
127
128 if (argv[3]) {
129 unsigned us;
130
131 r = safe_atou(argv[3], &us);
132 if (r < 0)
133 return log_error_errno(r, "Invalid delay '%s': %m", argv[3]);
134 usleep_safe(us);
135 }
136
137 assert_se(udev_rules_load(&rules, RESOLVE_NAME_EARLY, /* extra= */ NULL) == 0);
138
139 const char *syspath = strjoina("/sys", devpath);
140 r = device_new_from_synthetic_event(&dev, syspath, action);
141 if (r < 0)
142 return log_debug_errno(r, "Failed to open device '%s'", devpath);
143
144 assert_se(event = udev_event_new(dev, NULL, EVENT_TEST_RULE_RUNNER));
145
146 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP) >= 0);
147
148 /* do what devtmpfs usually provides us */
149 if (sd_device_get_devname(dev, &devname) >= 0) {
150 mode_t mode = 0600;
151
152 r = device_in_subsystem(dev, "block");
153 if (r < 0)
154 return r;
155 if (r > 0)
156 mode |= S_IFBLK;
157 else
158 mode |= S_IFCHR;
159
160 if (!streq(action, "remove")) {
161 dev_t devnum = makedev(0, 0);
162
163 (void) mkdir_parents_label(devname, 0755);
164 (void) sd_device_get_devnum(dev, &devnum);
165 if (mknod(devname, mode, devnum) < 0)
166 return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
167 } else {
168 if (unlink(devname) < 0)
169 return log_error_errno(errno, "unlink('%s') failed: %m", devname);
170 (void) rmdir_parents(devname, "/dev");
171 }
172 }
173
174 udev_event_execute_rules(event, rules);
175 udev_event_execute_run(event);
176
177 return 0;
178 }
179
180 DEFINE_MAIN_FUNCTION(run);