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/>.
32 #include <sys/mount.h>
33 #include <sys/signalfd.h>
37 #include "udev-util.h"
39 void udev_main_log(struct udev *udev, int priority,
40 const char *file, int line, const char *fn,
41 const char *format, va_list args) {}
43 static int fake_filesystems(void) {
44 static const struct fakefs {
49 { "test/sys", "/sys", "failed to mount test /sys" },
50 { "test/dev", "/dev", "failed to mount test /dev" },
51 { "test/run", "/run", "failed to mount test /run" },
52 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" },
53 { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" },
58 err = unshare(CLONE_NEWNS);
61 fprintf(stderr, "failed to call unshare(): %m\n");
65 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
67 fprintf(stderr, "failed to mount / as private: %m\n");
71 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
72 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
75 fprintf(stderr, "%s %m", fakefss[i].error);
84 int main(int argc, char *argv[]) {
85 _cleanup_udev_unref_ struct udev *udev = NULL;
86 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
87 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
88 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
89 char syspath[UTIL_PATH_SIZE];
92 sigset_t mask, sigmask_orig;
95 err = fake_filesystems();
103 log_debug("version %s\n", VERSION);
106 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
109 if (action == NULL) {
110 log_error("action missing\n");
115 if (devpath == NULL) {
116 log_error("devpath missing\n");
120 rules = udev_rules_new(udev, 1);
122 strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
123 dev = udev_device_new_from_syspath(udev, syspath);
125 log_debug("unknown device '%s'\n", devpath);
129 udev_device_set_action(dev, action);
130 event = udev_event_new(dev);
133 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
134 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
135 if (event->fd_signal < 0) {
136 fprintf(stderr, "error creating signalfd\n");
140 /* do what devtmpfs usually provides us */
141 if (udev_device_get_devnode(dev) != NULL) {
144 if (streq(udev_device_get_subsystem(dev), "block"))
149 if (!streq(action, "remove")) {
150 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
151 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
153 unlink(udev_device_get_devnode(dev));
154 util_delete_path(udev, udev_device_get_devnode(dev));
158 err = udev_event_execute_rules(event, rules, &sigmask_orig);
160 udev_event_execute_run(event, NULL);
162 if (event != NULL && event->fd_signal >= 0)
163 close(event->fd_signal);
166 return err ? EXIT_FAILURE : EXIT_SUCCESS;