]>
Commit | Line | Data |
---|---|---|
1298001e KS |
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 | ***/ | |
f0083e3d | 20 | |
c81b35c0 KS |
21 | #include <stdio.h> |
22 | #include <stddef.h> | |
f0083e3d GKH |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
c449b25e | 25 | #include <fcntl.h> |
e8baccca | 26 | #include <ctype.h> |
c81b35c0 | 27 | #include <errno.h> |
1ceba936 | 28 | #include <unistd.h> |
27f877e6 | 29 | #include <syslog.h> |
44aff4cd | 30 | #include <grp.h> |
4cb72937 KS |
31 | #include <sched.h> |
32 | #include <sys/mount.h> | |
2181d30a | 33 | #include <sys/signalfd.h> |
85511f02 | 34 | |
f0083e3d | 35 | #include "udev.h" |
f0083e3d | 36 | |
dd8a93e0 | 37 | void udev_main_log(struct udev *udev, int priority, |
912541b0 KS |
38 | const char *file, int line, const char *fn, |
39 | const char *format, va_list args) {} | |
dd8a93e0 | 40 | |
4cb72937 KS |
41 | static int fake_filesystems(void) { |
42 | static const struct fakefs { | |
43 | const char *src; | |
44 | const char *target; | |
45 | const char *error; | |
46 | } fakefss[] = { | |
47 | { "test/sys", "/sys", "failed to mount test /sys" }, | |
48 | { "test/dev", "/dev", "failed to mount test /dev" }, | |
49 | { "test/run", "/run", "failed to mount test /run" }, | |
50 | { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" }, | |
51 | { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" }, | |
52 | }; | |
53 | unsigned int i; | |
54 | int err; | |
55 | ||
56 | err = unshare(CLONE_NEWNS); | |
57 | if (err < 0) { | |
58 | err = -errno; | |
04eaa668 KS |
59 | fprintf(stderr, "failed to call unshare(): %m\n"); |
60 | goto out; | |
61 | } | |
62 | ||
63 | if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) { | |
64 | err = -errno; | |
65 | fprintf(stderr, "failed to mount / as private: %m\n"); | |
66 | goto out; | |
4cb72937 KS |
67 | } |
68 | ||
69 | for (i = 0; i < ELEMENTSOF(fakefss); i++) { | |
70 | err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL); | |
71 | if (err < 0) { | |
72 | err = -errno; | |
73 | fprintf(stderr, "%s %m", fakefss[i].error); | |
74 | return err; | |
75 | } | |
76 | } | |
04eaa668 | 77 | out: |
4cb72937 KS |
78 | return err; |
79 | } | |
80 | ||
81 | ||
59345311 | 82 | int main(int argc, char *argv[]) |
f4dc8d11 | 83 | { |
912541b0 KS |
84 | struct udev *udev; |
85 | struct udev_event *event = NULL; | |
86 | struct udev_device *dev = NULL; | |
87 | struct udev_rules *rules = NULL; | |
88 | char syspath[UTIL_PATH_SIZE]; | |
89 | const char *devpath; | |
90 | const char *action; | |
91 | sigset_t mask, sigmask_orig; | |
4cb72937 KS |
92 | int err; |
93 | ||
94 | err = fake_filesystems(); | |
95 | if (err < 0) | |
96 | return EXIT_FAILURE; | |
912541b0 KS |
97 | |
98 | udev = udev_new(); | |
99 | if (udev == NULL) | |
1c0f62e3 | 100 | exit(EXIT_FAILURE); |
baa30fbc | 101 | log_debug("version %s\n", VERSION); |
0f9963a8 | 102 | label_init("/dev"); |
912541b0 KS |
103 | |
104 | sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); | |
105 | ||
106 | action = argv[1]; | |
107 | if (action == NULL) { | |
baa30fbc | 108 | log_error("action missing\n"); |
912541b0 KS |
109 | goto out; |
110 | } | |
111 | ||
112 | devpath = argv[2]; | |
113 | if (devpath == NULL) { | |
baa30fbc | 114 | log_error("devpath missing\n"); |
912541b0 KS |
115 | goto out; |
116 | } | |
117 | ||
118 | rules = udev_rules_new(udev, 1); | |
119 | ||
d5a89d7d | 120 | strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL); |
912541b0 KS |
121 | dev = udev_device_new_from_syspath(udev, syspath); |
122 | if (dev == NULL) { | |
baa30fbc | 123 | log_debug("unknown device '%s'\n", devpath); |
912541b0 KS |
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) { | |
83cd6b75 | 140 | mode_t mode = 0600; |
912541b0 KS |
141 | |
142 | if (strcmp(udev_device_get_subsystem(dev), "block") == 0) | |
143 | mode |= S_IFBLK; | |
144 | else | |
145 | mode |= S_IFCHR; | |
146 | ||
147 | if (strcmp(action, "remove") != 0) { | |
d2e54fae | 148 | mkdir_parents_label(udev_device_get_devnode(dev), 0755); |
912541b0 KS |
149 | mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev)); |
150 | } else { | |
151 | unlink(udev_device_get_devnode(dev)); | |
152 | util_delete_path(udev, udev_device_get_devnode(dev)); | |
153 | } | |
154 | } | |
155 | ||
156 | err = udev_event_execute_rules(event, rules, &sigmask_orig); | |
157 | if (err == 0) | |
158 | udev_event_execute_run(event, NULL); | |
2181d30a | 159 | out: |
912541b0 KS |
160 | if (event != NULL && event->fd_signal >= 0) |
161 | close(event->fd_signal); | |
162 | udev_event_unref(event); | |
163 | udev_device_unref(dev); | |
164 | udev_rules_unref(rules); | |
e9a5ef7c | 165 | label_finish(); |
912541b0 KS |
166 | udev_unref(udev); |
167 | if (err != 0) | |
1c0f62e3 KS |
168 | return EXIT_FAILURE; |
169 | return EXIT_SUCCESS; | |
f0083e3d | 170 | } |