]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-rule-runner.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
10 #include <sys/mount.h>
11 #include <sys/signalfd.h>
14 #include "device-private.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"
26 #include "udev-event.h"
27 #include "udev-spawn.h"
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
;
39 a
= device_action_from_string(action
);
43 r
= sd_device_new_from_syspath(&dev
, syspath
);
47 r
= device_read_uevent_file(dev
);
51 r
= device_set_action(dev
, a
);
59 static int fake_filesystems(void) {
60 static const struct fakefs
{
64 bool ignore_mount_error
;
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 },
74 r
= detach_mount_namespace();
76 return log_error_errno(r
, "Failed to detach mount namespace: %m");
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
)
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
;
95 test_setup_logging(LOG_INFO
);
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);
101 r
= fake_filesystems();
105 /* Let's make sure the test runs with selinux assumed disabled. */
109 mac_selinux_retest();
112 if (!streq(argv
[1], "check"))
113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
114 "Unknown argument: %s", argv
[1]);
119 log_debug("version %s", GIT_VERSION
);
131 r
= safe_atou(argv
[3], &us
);
133 return log_error_errno(r
, "Invalid delay '%s': %m", argv
[3]);
137 assert_se(udev_rules_load(&rules
, RESOLVE_NAME_EARLY
) == 0);
139 const char *syspath
= strjoina("/sys", devpath
);
140 r
= device_new_from_synthetic_event(&dev
, syspath
, action
);
142 return log_debug_errno(r
, "Failed to open device '%s'", devpath
);
144 assert_se(event
= udev_event_new(dev
, 0, NULL
, log_get_max_level()));
146 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
148 /* do what devtmpfs usually provides us */
149 if (sd_device_get_devname(dev
, &devname
) >= 0) {
150 const char *subsystem
;
153 if (sd_device_get_subsystem(dev
, &subsystem
) >= 0 && streq(subsystem
, "block"))
158 if (!streq(action
, "remove")) {
159 dev_t devnum
= makedev(0, 0);
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
);
166 if (unlink(devname
) < 0)
167 return log_error_errno(errno
, "unlink('%s') failed: %m", devname
);
168 (void) rmdir_parents(devname
, "/dev");
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
);
178 DEFINE_MAIN_FUNCTION(run
);