]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
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>
17 #include "selinux-util.h"
18 #include "signal-util.h"
19 #include "string-util.h"
22 static int fake_filesystems(void) {
23 static const struct fakefs
{
27 bool ignore_mount_error
;
29 { "test/tmpfs/sys", "/sys", "failed to mount test /sys", false },
30 { "test/tmpfs/dev", "/dev", "failed to mount test /dev", false },
31 { "test/run", "/run", "failed to mount test /run", false },
32 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true },
33 { "test/run", UDEVLIBEXECDIR
"/rules.d", "failed to mount empty " UDEVLIBEXECDIR
"/rules.d", true },
37 if (unshare(CLONE_NEWNS
) < 0)
38 return log_error_errno(errno
, "failed to call unshare(): %m");
40 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
41 return log_error_errno(errno
, "failed to mount / as private: %m");
43 for (i
= 0; i
< ELEMENTSOF(fakefss
); i
++) {
44 if (mount(fakefss
[i
].src
, fakefss
[i
].target
, NULL
, MS_BIND
, NULL
) < 0) {
45 log_full_errno(fakefss
[i
].ignore_mount_error
? LOG_DEBUG
: LOG_ERR
, errno
, "%s: %m", fakefss
[i
].error
);
46 if (!fakefss
[i
].ignore_mount_error
)
54 int main(int argc
, char *argv
[]) {
55 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
56 _cleanup_(udev_event_unrefp
) struct udev_event
*event
= NULL
;
57 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
58 _cleanup_(udev_rules_unrefp
) struct udev_rules
*rules
= NULL
;
59 char syspath
[UTIL_PATH_SIZE
];
64 log_parse_environment();
67 err
= fake_filesystems();
75 log_debug("version %s", PACKAGE_VERSION
);
80 log_error("action missing");
85 if (devpath
== NULL
) {
86 log_error("devpath missing");
90 rules
= udev_rules_new(udev
, 1);
92 strscpyl(syspath
, sizeof(syspath
), "/sys", devpath
, NULL
);
93 dev
= udev_device_new_from_synthetic_event(udev
, syspath
, action
);
95 log_debug("unknown device '%s'", devpath
);
99 event
= udev_event_new(dev
);
101 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
103 /* do what devtmpfs usually provides us */
104 if (udev_device_get_devnode(dev
) != NULL
) {
107 if (streq(udev_device_get_subsystem(dev
), "block"))
112 if (!streq(action
, "remove")) {
113 mkdir_parents_label(udev_device_get_devnode(dev
), 0755);
114 mknod(udev_device_get_devnode(dev
), mode
, udev_device_get_devnum(dev
));
116 unlink(udev_device_get_devnode(dev
));
117 rmdir_parents(udev_device_get_devnode(dev
), "/");
121 udev_event_execute_rules(event
,
122 3 * USEC_PER_SEC
, USEC_PER_SEC
,
125 udev_event_execute_run(event
,
126 3 * USEC_PER_SEC
, USEC_PER_SEC
);
128 mac_selinux_finish();
130 return err
? EXIT_FAILURE
: EXIT_SUCCESS
;