2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/mount.h>
31 #include <sys/signalfd.h>
35 void udev_main_log(struct udev *udev, int priority,
36 const char *file, int line, const char *fn,
37 const char *format, va_list args) {}
39 static int fake_filesystems(void) {
40 static const struct fakefs {
45 { "test/sys", "/sys", "failed to mount test /sys" },
46 { "test/dev", "/dev", "failed to mount test /dev" },
47 { "test/run", "/run", "failed to mount test /run" },
48 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" },
49 { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" },
54 err = unshare(CLONE_NEWNS);
57 fprintf(stderr, "failed to call unshare(): %m\n");
61 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
63 fprintf(stderr, "failed to mount / as private: %m\n");
67 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
68 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
71 fprintf(stderr, "%s %m", fakefss[i].error);
80 int main(int argc, char *argv[])
83 struct udev_event *event = NULL;
84 struct udev_device *dev = NULL;
85 struct udev_rules *rules = NULL;
86 char syspath[UTIL_PATH_SIZE];
89 sigset_t mask, sigmask_orig;
92 err = fake_filesystems();
99 log_debug("version %s\n", VERSION);
102 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
105 if (action == NULL) {
106 log_error("action missing\n");
111 if (devpath == NULL) {
112 log_error("devpath missing\n");
116 rules = udev_rules_new(udev, 1);
118 util_strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
119 dev = udev_device_new_from_syspath(udev, syspath);
121 log_debug("unknown device '%s'\n", devpath);
125 udev_device_set_action(dev, action);
126 event = udev_event_new(dev);
129 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
130 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
131 if (event->fd_signal < 0) {
132 fprintf(stderr, "error creating signalfd\n");
136 /* do what devtmpfs usually provides us */
137 if (udev_device_get_devnode(dev) != NULL) {
140 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
145 if (strcmp(action, "remove") != 0) {
146 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
147 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
149 unlink(udev_device_get_devnode(dev));
150 util_delete_path(udev, udev_device_get_devnode(dev));
154 err = udev_event_execute_rules(event, rules, &sigmask_orig);
156 udev_event_execute_run(event, NULL);
158 if (event != NULL && event->fd_signal >= 0)
159 close(event->fd_signal);
160 udev_event_unref(event);
161 udev_device_unref(dev);
162 udev_rules_unref(rules);