chiark / gitweb /
udev: get rid of TEST_PREFIX and use unshare() to fake /sys, /dev, /run
[elogind.git] / src / test / test-udev.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <syslog.h>
28 #include <grp.h>
29 #include <sched.h>
30 #include <sys/mount.h>
31 #include <sys/signalfd.h>
32
33 #include "udev.h"
34
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) {}
38
39 static int fake_filesystems(void) {
40         static const struct fakefs {
41                 const char *src;
42                 const char *target;
43                 const char *error;
44         } fakefss[] = {
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" },
50         };
51         unsigned int i;
52         int err;
53
54         err = unshare(CLONE_NEWNS);
55         if (err < 0) {
56                 err = -errno;
57                 fprintf(stderr, "failed to call unshare() %m\n");
58                 return err;
59         }
60
61         for (i = 0; i < ELEMENTSOF(fakefss); i++) {
62                 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
63                 if (err < 0) {
64                         err = -errno;
65                         fprintf(stderr, "%s %m", fakefss[i].error);
66                         return err;
67                 }
68         }
69         return err;
70 }
71
72
73 int main(int argc, char *argv[])
74 {
75         struct udev *udev;
76         struct udev_event *event = NULL;
77         struct udev_device *dev = NULL;
78         struct udev_rules *rules = NULL;
79         char syspath[UTIL_PATH_SIZE];
80         const char *devpath;
81         const char *action;
82         sigset_t mask, sigmask_orig;
83         int err;
84
85         err = fake_filesystems();
86         if (err < 0)
87                 return EXIT_FAILURE;
88
89         udev = udev_new();
90         if (udev == NULL)
91                 exit(EXIT_FAILURE);
92         log_debug("version %s\n", VERSION);
93         label_init("/dev");
94
95         sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
96
97         action = argv[1];
98         if (action == NULL) {
99                 log_error("action missing\n");
100                 goto out;
101         }
102
103         devpath = argv[2];
104         if (devpath == NULL) {
105                 log_error("devpath missing\n");
106                 goto out;
107         }
108
109         rules = udev_rules_new(udev, 1);
110
111         util_strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
112         dev = udev_device_new_from_syspath(udev, syspath);
113         if (dev == NULL) {
114                 log_debug("unknown device '%s'\n", devpath);
115                 goto out;
116         }
117
118         udev_device_set_action(dev, action);
119         event = udev_event_new(dev);
120
121         sigfillset(&mask);
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");
126                 goto out;
127         }
128
129         /* do what devtmpfs usually provides us */
130         if (udev_device_get_devnode(dev) != NULL) {
131                 mode_t mode = 0600;
132
133                 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
134                         mode |= S_IFBLK;
135                 else
136                         mode |= S_IFCHR;
137
138                 if (strcmp(action, "remove") != 0) {
139                         mkdir_parents_label(udev_device_get_devnode(dev), 0755);
140                         mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
141                 } else {
142                         unlink(udev_device_get_devnode(dev));
143                         util_delete_path(udev, udev_device_get_devnode(dev));
144                 }
145         }
146
147         err = udev_event_execute_rules(event, rules, &sigmask_orig);
148         if (err == 0)
149                 udev_event_execute_run(event, NULL);
150 out:
151         if (event != NULL && event->fd_signal >= 0)
152                 close(event->fd_signal);
153         udev_event_unref(event);
154         udev_device_unref(dev);
155         udev_rules_unref(rules);
156         label_finish();
157         udev_unref(udev);
158         if (err != 0)
159                 return EXIT_FAILURE;
160         return EXIT_SUCCESS;
161 }