]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-rule-runner.c
resolved: add transaction result for upstream failures
[thirdparty/systemd.git] / src / udev / test-udev-rule-runner.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
4 ***/
5
6 #include <errno.h>
7 #include <sched.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mount.h>
11 #include <sys/signalfd.h>
12 #include <unistd.h>
13
14 #include "device-private.h"
15 #include "fs-util.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "mkdir-label.h"
19 #include "mount-util.h"
20 #include "namespace-util.h"
21 #include "parse-util.h"
22 #include "selinux-util.h"
23 #include "signal-util.h"
24 #include "string-util.h"
25 #include "tests.h"
26 #include "udev-event.h"
27 #include "udev-spawn.h"
28 #include "version.h"
29
30 static int device_new_from_synthetic_event(sd_device **ret, const char *syspath, const char *action) {
31 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
32 sd_device_action_t a;
33 int r;
34
35 assert(ret);
36 assert(syspath);
37 assert(action);
38
39 a = device_action_from_string(action);
40 if (a < 0)
41 return a;
42
43 r = sd_device_new_from_syspath(&dev, syspath);
44 if (r < 0)
45 return r;
46
47 r = device_read_uevent_file(dev);
48 if (r < 0)
49 return r;
50
51 r = device_set_action(dev, a);
52 if (r < 0)
53 return r;
54
55 *ret = TAKE_PTR(dev);
56 return 0;
57 }
58
59 static int fake_filesystems(void) {
60 static const struct fakefs {
61 const char *src;
62 const char *target;
63 const char *error;
64 bool ignore_mount_error;
65 } fakefss[] = {
66 { "tmpfs/sys", "/sys", "Failed to mount test /sys", false },
67 { "tmpfs/dev", "/dev", "Failed to mount test /dev", false },
68 { "run", "/run", "Failed to mount test /run", false },
69 { "run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
70 { "run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
71 };
72 int r;
73
74 r = detach_mount_namespace();
75 if (r < 0)
76 return log_error_errno(r, "Failed to detach mount namespace: %m");
77
78 for (size_t i = 0; i < ELEMENTSOF(fakefss); i++) {
79 r = mount_nofollow_verbose(fakefss[i].ignore_mount_error ? LOG_NOTICE : LOG_ERR,
80 fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
81 if (r < 0 && !fakefss[i].ignore_mount_error)
82 return r;
83 }
84
85 return 0;
86 }
87
88 static int run(int argc, char *argv[]) {
89 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
90 _cleanup_(udev_event_freep) UdevEvent *event = NULL;
91 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
92 const char *devpath, *devname, *action;
93 int r;
94
95 test_setup_logging(LOG_INFO);
96
97 if (!IN_SET(argc, 2, 3, 4))
98 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
99 "This program needs between one and three arguments, %d given", argc - 1);
100
101 r = fake_filesystems();
102 if (r < 0)
103 return r;
104
105 /* Let's make sure the test runs with selinux assumed disabled. */
106 #if HAVE_SELINUX
107 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) == 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, 0, NULL, log_get_max_level()));
145
146 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
147
148 /* do what devtmpfs usually provides us */
149 if (sd_device_get_devname(dev, &devname) >= 0) {
150 const char *subsystem;
151 mode_t mode = 0600;
152
153 if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
154 mode |= S_IFBLK;
155 else
156 mode |= S_IFCHR;
157
158 if (!streq(action, "remove")) {
159 dev_t devnum = makedev(0, 0);
160
161 (void) mkdir_parents_label(devname, 0755);
162 (void) sd_device_get_devnum(dev, &devnum);
163 if (mknod(devname, mode, devnum) < 0)
164 return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
165 } else {
166 if (unlink(devname) < 0)
167 return log_error_errno(errno, "unlink('%s') failed: %m", devname);
168 (void) rmdir_parents(devname, "/dev");
169 }
170 }
171
172 udev_event_execute_rules(event, -1, 3 * USEC_PER_SEC, SIGKILL, NULL, rules);
173 udev_event_execute_run(event, 3 * USEC_PER_SEC, SIGKILL);
174
175 return 0;
176 }
177
178 DEFINE_MAIN_FUNCTION(run);