2 This file is part of systemd.
4 Copyright 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5 Copyright 2004-2012 Kay Sievers <kay@vrfy.org>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/mount.h>
27 #include <sys/signalfd.h>
30 #include "selinux-util.h"
32 #include "udev-util.h"
34 static int fake_filesystems(void) {
35 static const struct fakefs {
40 { "test/sys", "/sys", "failed to mount test /sys" },
41 { "test/dev", "/dev", "failed to mount test /dev" },
42 { "test/run", "/run", "failed to mount test /run" },
43 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" },
44 { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" },
49 err = unshare(CLONE_NEWNS);
52 fprintf(stderr, "failed to call unshare(): %m\n");
56 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
58 fprintf(stderr, "failed to mount / as private: %m\n");
62 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
63 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
66 fprintf(stderr, "%s %m", fakefss[i].error);
74 int main(int argc, char *argv[]) {
75 _cleanup_udev_unref_ struct udev *udev = NULL;
76 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
77 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
78 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
79 char syspath[UTIL_PATH_SIZE];
82 sigset_t mask, sigmask_orig;
85 err = fake_filesystems();
93 log_debug("version %s", VERSION);
94 mac_selinux_init("/dev");
96 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
100 log_error("action missing");
105 if (devpath == NULL) {
106 log_error("devpath missing");
110 rules = udev_rules_new(udev, 1);
112 strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
113 dev = udev_device_new_from_synthetic_event(udev, syspath, action);
115 log_debug("unknown device '%s'", devpath);
119 event = udev_event_new(dev);
122 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
123 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
124 if (event->fd_signal < 0) {
125 fprintf(stderr, "error creating signalfd\n");
129 /* do what devtmpfs usually provides us */
130 if (udev_device_get_devnode(dev) != NULL) {
133 if (streq(udev_device_get_subsystem(dev), "block"))
138 if (!streq(action, "remove")) {
139 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
140 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
142 unlink(udev_device_get_devnode(dev));
143 rmdir_parents(udev_device_get_devnode(dev), "/");
147 udev_event_execute_rules(event,
148 3 * USEC_PER_SEC, USEC_PER_SEC,
152 udev_event_execute_run(event,
153 3 * USEC_PER_SEC, USEC_PER_SEC,
156 if (event != NULL && event->fd_signal >= 0)
157 close(event->fd_signal);
158 mac_selinux_finish();
160 return err ? EXIT_FAILURE : EXIT_SUCCESS;