]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
ab31f5a2f1bbaa25c363aa97068bac51550eb70f
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
10 #include <sys/mount.h>
11 #include <sys/signalfd.h>
15 #include "device-private.h"
18 #include "main-func.h"
21 #include "selinux-util.h"
22 #include "signal-util.h"
23 #include "string-util.h"
27 static int fake_filesystems(void) {
28 static const struct fakefs
{
32 bool ignore_mount_error
;
34 { "test/tmpfs/sys", "/sys", "Failed to mount test /sys", false },
35 { "test/tmpfs/dev", "/dev", "Failed to mount test /dev", false },
36 { "test/run", "/run", "Failed to mount test /run", false },
37 { "test/run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
38 { "test/run", UDEVLIBEXECDIR
"/rules.d", "Failed to mount empty " UDEVLIBEXECDIR
"/rules.d", true },
42 if (unshare(CLONE_NEWNS
) < 0)
43 return log_error_errno(errno
, "Failed to call unshare(): %m");
45 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
46 return log_error_errno(errno
, "Failed to mount / as private: %m");
48 for (i
= 0; i
< ELEMENTSOF(fakefss
); i
++)
49 if (mount(fakefss
[i
].src
, fakefss
[i
].target
, NULL
, MS_BIND
, NULL
) < 0) {
50 log_full_errno(fakefss
[i
].ignore_mount_error
? LOG_DEBUG
: LOG_ERR
, errno
, "%s: %m", fakefss
[i
].error
);
51 if (!fakefss
[i
].ignore_mount_error
)
58 static int run(int argc
, char *argv
[]) {
59 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
60 _cleanup_(udev_event_freep
) UdevEvent
*event
= NULL
;
61 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
62 const char *devpath
, *devname
, *action
;
65 test_setup_logging(LOG_INFO
);
67 if (!IN_SET(argc
, 2, 3)) {
68 log_error("This program needs one or two arguments, %d given", argc
- 1);
72 r
= fake_filesystems();
77 if (!streq(argv
[1], "check")) {
78 log_error("Unknown argument: %s", argv
[1]);
85 log_debug("version %s", GIT_VERSION
);
91 assert_se(udev_rules_new(&rules
, RESOLVE_NAME_EARLY
) == 0);
93 const char *syspath
= strjoina("/sys", devpath
);
94 r
= device_new_from_synthetic_event(&dev
, syspath
, action
);
96 return log_debug_errno(r
, "Failed to open device '%s'", devpath
);
98 assert_se(event
= udev_event_new(dev
, 0, NULL
));
100 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
102 /* do what devtmpfs usually provides us */
103 if (sd_device_get_devname(dev
, &devname
) >= 0) {
104 const char *subsystem
;
107 if (sd_device_get_subsystem(dev
, &subsystem
) >= 0 && streq(subsystem
, "block"))
112 if (!streq(action
, "remove")) {
113 dev_t devnum
= makedev(0, 0);
115 (void) mkdir_parents_label(devname
, 0755);
116 (void) sd_device_get_devnum(dev
, &devnum
);
117 if (mknod(devname
, mode
, devnum
) < 0)
118 return log_error_errno(errno
, "mknod() failed for '%s': %m", devname
);
120 if (unlink(devname
) < 0)
121 return log_error_errno(errno
, "unlink('%s') failed: %m", devname
);
122 (void) rmdir_parents(devname
, "/");
126 udev_event_execute_rules(event
, 3 * USEC_PER_SEC
, NULL
, rules
);
127 udev_event_execute_run(event
, 3 * USEC_PER_SEC
);
132 DEFINE_MAIN_FUNCTION(run
);