]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
udev: move global property handling from libudev to udevd
[thirdparty/systemd.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 static int fake_filesystems(void) {
39 static const struct fakefs {
40 const char *src;
41 const char *target;
42 const char *error;
43 } fakefss[] = {
44 { "test/sys", "/sys", "failed to mount test /sys" },
45 { "test/dev", "/dev", "failed to mount test /dev" },
46 { "test/run", "/run", "failed to mount test /run" },
47 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" },
48 { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" },
49 };
50 unsigned int i;
51 int err;
52
53 err = unshare(CLONE_NEWNS);
54 if (err < 0) {
55 err = -errno;
56 fprintf(stderr, "failed to call unshare(): %m\n");
57 goto out;
58 }
59
60 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
61 err = -errno;
62 fprintf(stderr, "failed to mount / as private: %m\n");
63 goto out;
64 }
65
66 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
67 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
68 if (err < 0) {
69 err = -errno;
70 fprintf(stderr, "%s %m", fakefss[i].error);
71 return err;
72 }
73 }
74 out:
75 return err;
76 }
77
78 int main(int argc, char *argv[]) {
79 _cleanup_udev_unref_ struct udev *udev = NULL;
80 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
81 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
82 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
83 char syspath[UTIL_PATH_SIZE];
84 const char *devpath;
85 const char *action;
86 sigset_t mask, sigmask_orig;
87 int err;
88
89 err = fake_filesystems();
90 if (err < 0)
91 return EXIT_FAILURE;
92
93 udev = udev_new();
94 if (udev == NULL)
95 return EXIT_FAILURE;
96
97 log_debug("version %s", VERSION);
98 mac_selinux_init("/dev");
99
100 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
101
102 action = argv[1];
103 if (action == NULL) {
104 log_error("action missing");
105 goto out;
106 }
107
108 devpath = argv[2];
109 if (devpath == NULL) {
110 log_error("devpath missing");
111 goto out;
112 }
113
114 rules = udev_rules_new(udev, 1);
115
116 strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
117 dev = udev_device_new_from_syspath(udev, syspath);
118 if (dev == NULL) {
119 log_debug("unknown device '%s'", devpath);
120 goto out;
121 }
122
123 udev_device_set_action(dev, action);
124 event = udev_event_new(dev);
125
126 sigfillset(&mask);
127 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
128 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
129 if (event->fd_signal < 0) {
130 fprintf(stderr, "error creating signalfd\n");
131 goto out;
132 }
133
134 /* do what devtmpfs usually provides us */
135 if (udev_device_get_devnode(dev) != NULL) {
136 mode_t mode = 0600;
137
138 if (streq(udev_device_get_subsystem(dev), "block"))
139 mode |= S_IFBLK;
140 else
141 mode |= S_IFCHR;
142
143 if (!streq(action, "remove")) {
144 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
145 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
146 } else {
147 unlink(udev_device_get_devnode(dev));
148 rmdir_parents(udev_device_get_devnode(dev), "/");
149 }
150 }
151
152 udev_event_execute_rules(event, 3 * USEC_PER_SEC, USEC_PER_SEC, rules, &sigmask_orig);
153 udev_event_execute_run(event, 3 * USEC_PER_SEC, USEC_PER_SEC, NULL);
154 out:
155 if (event != NULL && event->fd_signal >= 0)
156 close(event->fd_signal);
157 mac_selinux_finish();
158
159 return err ? EXIT_FAILURE : EXIT_SUCCESS;
160 }