]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/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"
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
;
38 a
= device_action_from_string(action
);
42 r
= sd_device_new_from_syspath(&dev
, syspath
);
46 r
= device_read_uevent_file(dev
);
50 r
= device_set_action(dev
, a
);
58 static int fake_filesystems(void) {
59 static const struct fakefs
{
63 bool ignore_mount_error
;
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 },
73 r
= detach_mount_namespace();
75 return log_error_errno(r
, "Failed to detach mount namespace: %m");
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
)
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
;
94 test_setup_logging(LOG_INFO
);
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);
100 r
= fake_filesystems();
104 /* Let's make sure the test runs with selinux assumed disabled. */
108 mac_selinux_retest();
111 if (!streq(argv
[1], "check"))
112 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
113 "Unknown argument: %s", argv
[1]);
118 log_debug("version %s", GIT_VERSION
);
130 r
= safe_atou(argv
[3], &us
);
132 return log_error_errno(r
, "Invalid delay '%s': %m", argv
[3]);
136 assert_se(udev_rules_load(&rules
, RESOLVE_NAME_EARLY
) == 0);
138 const char *syspath
= strjoina("/sys", devpath
);
139 r
= device_new_from_synthetic_event(&dev
, syspath
, action
);
141 return log_debug_errno(r
, "Failed to open device '%s'", devpath
);
143 assert_se(event
= udev_event_new(dev
, 0, NULL
, log_get_max_level()));
145 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
147 /* do what devtmpfs usually provides us */
148 if (sd_device_get_devname(dev
, &devname
) >= 0) {
149 const char *subsystem
;
152 if (sd_device_get_subsystem(dev
, &subsystem
) >= 0 && streq(subsystem
, "block"))
157 if (!streq(action
, "remove")) {
158 dev_t devnum
= makedev(0, 0);
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
);
165 if (unlink(devname
) < 0)
166 return log_error_errno(errno
, "unlink('%s') failed: %m", devname
);
167 (void) rmdir_parents(devname
, "/dev");
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
);
177 DEFINE_MAIN_FUNCTION(run
);