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>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/mount.h>
27 #include <sys/signalfd.h>
33 #include "selinux-util.h"
34 #include "signal-util.h"
35 #include "string-util.h"
36 #include "udev-util.h"
39 static int fake_filesystems(void) {
40 static const struct fakefs
{
44 bool ignore_mount_error
;
46 { "test/tmpfs/sys", "/sys", "failed to mount test /sys", false },
47 { "test/tmpfs/dev", "/dev", "failed to mount test /dev", false },
48 { "test/run", "/run", "failed to mount test /run", false },
49 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true },
50 { "test/run", UDEVLIBEXECDIR
"/rules.d", "failed to mount empty " UDEVLIBEXECDIR
"/rules.d", true },
54 if (unshare(CLONE_NEWNS
) < 0)
55 return log_error_errno(errno
, "failed to call unshare(): %m");
57 if (mount(NULL
, "/", NULL
, MS_PRIVATE
|MS_REC
, NULL
) < 0)
58 return log_error_errno(errno
, "failed to mount / as private: %m");
60 for (i
= 0; i
< ELEMENTSOF(fakefss
); i
++) {
61 if (mount(fakefss
[i
].src
, fakefss
[i
].target
, NULL
, MS_BIND
, NULL
) < 0) {
62 log_full_errno(fakefss
[i
].ignore_mount_error
? LOG_DEBUG
: LOG_ERR
, errno
, "%s: %m", fakefss
[i
].error
);
63 if (!fakefss
[i
].ignore_mount_error
)
71 int main(int argc
, char *argv
[]) {
72 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
73 _cleanup_udev_event_unref_
struct udev_event
*event
= NULL
;
74 _cleanup_udev_device_unref_
struct udev_device
*dev
= NULL
;
75 _cleanup_udev_rules_unref_
struct udev_rules
*rules
= NULL
;
76 char syspath
[UTIL_PATH_SIZE
];
81 log_parse_environment();
84 err
= fake_filesystems();
92 log_debug("version %s", PACKAGE_VERSION
);
97 log_error("action missing");
102 if (devpath
== NULL
) {
103 log_error("devpath missing");
107 rules
= udev_rules_new(udev
, 1);
109 strscpyl(syspath
, sizeof(syspath
), "/sys", devpath
, NULL
);
110 dev
= udev_device_new_from_synthetic_event(udev
, syspath
, action
);
112 log_debug("unknown device '%s'", devpath
);
116 event
= udev_event_new(dev
);
118 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
120 /* do what devtmpfs usually provides us */
121 if (udev_device_get_devnode(dev
) != NULL
) {
124 if (streq(udev_device_get_subsystem(dev
), "block"))
129 if (!streq(action
, "remove")) {
130 mkdir_parents_label(udev_device_get_devnode(dev
), 0755);
131 mknod(udev_device_get_devnode(dev
), mode
, udev_device_get_devnum(dev
));
133 unlink(udev_device_get_devnode(dev
));
134 rmdir_parents(udev_device_get_devnode(dev
), "/");
138 udev_event_execute_rules(event
,
139 3 * USEC_PER_SEC
, USEC_PER_SEC
,
142 udev_event_execute_run(event
,
143 3 * USEC_PER_SEC
, USEC_PER_SEC
);
145 mac_selinux_finish();
147 return err
? EXIT_FAILURE
: EXIT_SUCCESS
;