chiark / gitweb /
bus: fix bus_print_property() to use "int" for booleans
[elogind.git] / src / test / test-udev.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5   Copyright 2004-2012 Kay Sievers <kay@vrfy.org>
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <grp.h>
30 #include <sched.h>
31 #include <sys/mount.h>
32 #include <sys/signalfd.h>
33
34 #include "missing.h"
35 #include "udev.h"
36 #include "udev-util.h"
37
38 void udev_main_log(struct udev *udev, int priority,
39                    const char *file, int line, const char *fn,
40                    const char *format, va_list args) {}
41
42 static int fake_filesystems(void) {
43         static const struct fakefs {
44                 const char *src;
45                 const char *target;
46                 const char *error;
47         } fakefss[] = {
48                 { "test/sys", "/sys",                   "failed to mount test /sys" },
49                 { "test/dev", "/dev",                   "failed to mount test /dev" },
50                 { "test/run", "/run",                   "failed to mount test /run" },
51                 { "test/run", "/etc/udev/rules.d",      "failed to mount empty /etc/udev/rules.d" },
52                 { "test/run", "/usr/lib/udev/rules.d",  "failed to mount empty /usr/lib/udev/rules.d" },
53         };
54         unsigned int i;
55         int err;
56
57         err = unshare(CLONE_NEWNS);
58         if (err < 0) {
59                 err = -errno;
60                 fprintf(stderr, "failed to call unshare(): %m\n");
61                 goto out;
62         }
63
64         if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
65                 err = -errno;
66                 fprintf(stderr, "failed to mount / as private: %m\n");
67                 goto out;
68         }
69
70         for (i = 0; i < ELEMENTSOF(fakefss); i++) {
71                 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
72                 if (err < 0) {
73                         err = -errno;
74                         fprintf(stderr, "%s %m", fakefss[i].error);
75                         return err;
76                 }
77         }
78 out:
79         return err;
80 }
81
82 int main(int argc, char *argv[]) {
83         _cleanup_udev_unref_ struct udev *udev = NULL;
84         _cleanup_udev_event_unref_ struct udev_event *event = NULL;
85         _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
86         _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
87         char syspath[UTIL_PATH_SIZE];
88         const char *devpath;
89         const char *action;
90         sigset_t mask, sigmask_orig;
91         int err;
92
93         err = fake_filesystems();
94         if (err < 0)
95                 return EXIT_FAILURE;
96
97         udev = udev_new();
98         if (udev == NULL)
99                 return EXIT_FAILURE;
100
101         log_debug("version %s", VERSION);
102         label_init("/dev");
103
104         sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
105
106         action = argv[1];
107         if (action == NULL) {
108                 log_error("action missing");
109                 goto out;
110         }
111
112         devpath = argv[2];
113         if (devpath == NULL) {
114                 log_error("devpath missing");
115                 goto out;
116         }
117
118         rules = udev_rules_new(udev, 1);
119
120         strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
121         dev = udev_device_new_from_syspath(udev, syspath);
122         if (dev == NULL) {
123                 log_debug("unknown device '%s'", devpath);
124                 goto out;
125         }
126
127         udev_device_set_action(dev, action);
128         event = udev_event_new(dev);
129
130         sigfillset(&mask);
131         sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
132         event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
133         if (event->fd_signal < 0) {
134                 fprintf(stderr, "error creating signalfd\n");
135                 goto out;
136         }
137
138         /* do what devtmpfs usually provides us */
139         if (udev_device_get_devnode(dev) != NULL) {
140                 mode_t mode = 0600;
141
142                 if (streq(udev_device_get_subsystem(dev), "block"))
143                         mode |= S_IFBLK;
144                 else
145                         mode |= S_IFCHR;
146
147                 if (!streq(action, "remove")) {
148                         mkdir_parents_label(udev_device_get_devnode(dev), 0755);
149                         mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
150                 } else {
151                         unlink(udev_device_get_devnode(dev));
152                         rmdir_parents(udev_device_get_devnode(dev), "/");
153                 }
154         }
155
156         udev_event_execute_rules(event, 3 * USEC_PER_SEC, USEC_PER_SEC, rules, &sigmask_orig);
157         udev_event_execute_run(event, 3 * USEC_PER_SEC, USEC_PER_SEC, NULL);
158 out:
159         if (event != NULL && event->fd_signal >= 0)
160                 close(event->fd_signal);
161         label_finish();
162
163         return err ? EXIT_FAILURE : EXIT_SUCCESS;
164 }