]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/udev-rule-runner.c
label: Introduce LabelOps to do pre/post labelling operations
[thirdparty/systemd.git] / src / 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 "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 for (size_t i = 0; i < ELEMENTSOF(fakefss); i++) {
78 r = mount_nofollow_verbose(fakefss[i].ignore_mount_error ? LOG_NOTICE : LOG_ERR,
79 fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
80 if (r < 0 && !fakefss[i].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_freep) 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 fini_selinuxmnt();
107 #endif
108 mac_selinux_retest();
109
110 if (argc == 2) {
111 if (!streq(argv[1], "check"))
112 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
113 "Unknown argument: %s", argv[1]);
114
115 return 0;
116 }
117
118 log_debug("version %s", GIT_VERSION);
119
120 r = mac_init();
121 if (r < 0)
122 return r;
123
124 action = argv[1];
125 devpath = argv[2];
126
127 if (argv[3]) {
128 unsigned us;
129
130 r = safe_atou(argv[3], &us);
131 if (r < 0)
132 return log_error_errno(r, "Invalid delay '%s': %m", argv[3]);
133 usleep(us);
134 }
135
136 assert_se(udev_rules_load(&rules, RESOLVE_NAME_EARLY) == 0);
137
138 const char *syspath = strjoina("/sys", devpath);
139 r = device_new_from_synthetic_event(&dev, syspath, action);
140 if (r < 0)
141 return log_debug_errno(r, "Failed to open device '%s'", devpath);
142
143 assert_se(event = udev_event_new(dev, 0, NULL, log_get_max_level()));
144
145 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
146
147 /* do what devtmpfs usually provides us */
148 if (sd_device_get_devname(dev, &devname) >= 0) {
149 const char *subsystem;
150 mode_t mode = 0600;
151
152 if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
153 mode |= S_IFBLK;
154 else
155 mode |= S_IFCHR;
156
157 if (!streq(action, "remove")) {
158 dev_t devnum = makedev(0, 0);
159
160 (void) mkdir_parents_label(devname, 0755);
161 (void) sd_device_get_devnum(dev, &devnum);
162 if (mknod(devname, mode, devnum) < 0)
163 return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
164 } else {
165 if (unlink(devname) < 0)
166 return log_error_errno(errno, "unlink('%s') failed: %m", devname);
167 (void) rmdir_parents(devname, "/dev");
168 }
169 }
170
171 udev_event_execute_rules(event, -1, 3 * USEC_PER_SEC, SIGKILL, NULL, rules);
172 udev_event_execute_run(event, 3 * USEC_PER_SEC, SIGKILL);
173
174 return 0;
175 }
176
177 DEFINE_MAIN_FUNCTION(run);