]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
c79384847da9bef1baa5104aabdae58b2a189e8b
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
6 Copyright 2004-2012 Kay Sievers <kay@vrfy.org>
13 #include <sys/mount.h>
14 #include <sys/signalfd.h>
20 #include "selinux-util.h"
21 #include "signal-util.h"
22 #include "string-util.h"
23 #include "udev-util.h"
26 static int fake_filesystems(void) {
27 static const struct fakefs
{
31 bool ignore_mount_error
;
33 { "test/tmpfs/sys", "/sys", "failed to mount test /sys", false },
34 { "test/tmpfs/dev", "/dev", "failed to mount test /dev", false },
35 { "test/run", "/run", "failed to mount test /run", false },
36 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true },
37 { "test/run", UDEVLIBEXECDIR
"/rules.d", "failed to mount empty " UDEVLIBEXECDIR
"/rules.d", true },
41 if (unshare(CLONE_NEWNS
) < 0)
42 return log_error_errno(errno
, "failed to call unshare(): %m");
44 if (mount(NULL
, "/", NULL
, MS_PRIVATE
|MS_REC
, NULL
) < 0)
45 return log_error_errno(errno
, "failed to mount / as private: %m");
47 for (i
= 0; i
< ELEMENTSOF(fakefss
); i
++) {
48 if (mount(fakefss
[i
].src
, fakefss
[i
].target
, NULL
, MS_BIND
, NULL
) < 0) {
49 log_full_errno(fakefss
[i
].ignore_mount_error
? LOG_DEBUG
: LOG_ERR
, errno
, "%s: %m", fakefss
[i
].error
);
50 if (!fakefss
[i
].ignore_mount_error
)
58 int main(int argc
, char *argv
[]) {
59 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
60 _cleanup_(udev_event_unrefp
) struct udev_event
*event
= NULL
;
61 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
62 _cleanup_(udev_rules_unrefp
) struct udev_rules
*rules
= NULL
;
63 char syspath
[UTIL_PATH_SIZE
];
68 log_parse_environment();
71 err
= fake_filesystems();
79 log_debug("version %s", PACKAGE_VERSION
);
84 log_error("action missing");
89 if (devpath
== NULL
) {
90 log_error("devpath missing");
94 rules
= udev_rules_new(udev
, 1);
96 strscpyl(syspath
, sizeof(syspath
), "/sys", devpath
, NULL
);
97 dev
= udev_device_new_from_synthetic_event(udev
, syspath
, action
);
99 log_debug("unknown device '%s'", devpath
);
103 event
= udev_event_new(dev
);
105 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
107 /* do what devtmpfs usually provides us */
108 if (udev_device_get_devnode(dev
) != NULL
) {
111 if (streq(udev_device_get_subsystem(dev
), "block"))
116 if (!streq(action
, "remove")) {
117 mkdir_parents_label(udev_device_get_devnode(dev
), 0755);
118 mknod(udev_device_get_devnode(dev
), mode
, udev_device_get_devnum(dev
));
120 unlink(udev_device_get_devnode(dev
));
121 rmdir_parents(udev_device_get_devnode(dev
), "/");
125 udev_event_execute_rules(event
,
126 3 * USEC_PER_SEC
, USEC_PER_SEC
,
129 udev_event_execute_run(event
,
130 3 * USEC_PER_SEC
, USEC_PER_SEC
);
132 mac_selinux_finish();
134 return err
? EXIT_FAILURE
: EXIT_SUCCESS
;