]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1298001e KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2003-2004 Greg Kroah-Hartman <greg@kroah.com> | |
6 | Copyright 2004-2012 Kay Sievers <kay@vrfy.org> | |
1298001e | 7 | ***/ |
f0083e3d | 8 | |
c81b35c0 | 9 | #include <errno.h> |
4cb72937 | 10 | #include <sched.h> |
07630cea LP |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
4cb72937 | 13 | #include <sys/mount.h> |
2181d30a | 14 | #include <sys/signalfd.h> |
07630cea | 15 | #include <unistd.h> |
85511f02 | 16 | |
f4f15635 | 17 | #include "fs-util.h" |
dbab702a | 18 | #include "log.h" |
10efe2cd | 19 | #include "missing.h" |
d7b8eec7 | 20 | #include "selinux-util.h" |
8314de1d | 21 | #include "signal-util.h" |
07630cea | 22 | #include "string-util.h" |
1ca208fb | 23 | #include "udev-util.h" |
07630cea | 24 | #include "udev.h" |
f0083e3d | 25 | |
4cb72937 KS |
26 | static int fake_filesystems(void) { |
27 | static const struct fakefs { | |
28 | const char *src; | |
29 | const char *target; | |
30 | const char *error; | |
ad43ccb0 | 31 | bool ignore_mount_error; |
4cb72937 | 32 | } fakefss[] = { |
ad43ccb0 EV |
33 | { "test/tmpfs/sys", "/sys", "failed to mount test /sys", false }, |
34 | { "test/tmpfs/dev", "/dev", "failed to mount test /dev", false }, | |
35 | { "test/run", "/run", "failed to mount test /run", false }, | |
36 | { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true }, | |
37 | { "test/run", UDEVLIBEXECDIR "/rules.d", "failed to mount empty " UDEVLIBEXECDIR "/rules.d", true }, | |
4cb72937 KS |
38 | }; |
39 | unsigned int i; | |
4cb72937 | 40 | |
dbab702a EV |
41 | if (unshare(CLONE_NEWNS) < 0) |
42 | return log_error_errno(errno, "failed to call unshare(): %m"); | |
04eaa668 | 43 | |
dbab702a EV |
44 | if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) |
45 | return log_error_errno(errno, "failed to mount / as private: %m"); | |
4cb72937 KS |
46 | |
47 | for (i = 0; i < ELEMENTSOF(fakefss); i++) { | |
dbab702a EV |
48 | if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) { |
49 | log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error); | |
ad43ccb0 | 50 | if (!fakefss[i].ignore_mount_error) |
dbab702a | 51 | return -errno; |
4cb72937 KS |
52 | } |
53 | } | |
dbab702a EV |
54 | |
55 | return 0; | |
4cb72937 KS |
56 | } |
57 | ||
f168c273 | 58 | int main(int argc, char *argv[]) { |
8e766630 LP |
59 | _cleanup_(udev_unrefp) struct udev *udev = NULL; |
60 | _cleanup_(udev_event_unrefp) struct udev_event *event = NULL; | |
61 | _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; | |
62 | _cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL; | |
912541b0 KS |
63 | char syspath[UTIL_PATH_SIZE]; |
64 | const char *devpath; | |
65 | const char *action; | |
4cb72937 KS |
66 | int err; |
67 | ||
dbab702a EV |
68 | log_parse_environment(); |
69 | log_open(); | |
70 | ||
4cb72937 KS |
71 | err = fake_filesystems(); |
72 | if (err < 0) | |
73 | return EXIT_FAILURE; | |
912541b0 KS |
74 | |
75 | udev = udev_new(); | |
76 | if (udev == NULL) | |
1ca208fb ZJS |
77 | return EXIT_FAILURE; |
78 | ||
948aaa7c | 79 | log_debug("version %s", PACKAGE_VERSION); |
c3dacc8b | 80 | mac_selinux_init(); |
912541b0 | 81 | |
912541b0 KS |
82 | action = argv[1]; |
83 | if (action == NULL) { | |
9f6445e3 | 84 | log_error("action missing"); |
912541b0 KS |
85 | goto out; |
86 | } | |
87 | ||
88 | devpath = argv[2]; | |
89 | if (devpath == NULL) { | |
9f6445e3 | 90 | log_error("devpath missing"); |
912541b0 KS |
91 | goto out; |
92 | } | |
93 | ||
94 | rules = udev_rules_new(udev, 1); | |
95 | ||
d5a89d7d | 96 | strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL); |
bf0e00ec | 97 | dev = udev_device_new_from_synthetic_event(udev, syspath, action); |
912541b0 | 98 | if (dev == NULL) { |
9f6445e3 | 99 | log_debug("unknown device '%s'", devpath); |
912541b0 KS |
100 | goto out; |
101 | } | |
102 | ||
912541b0 KS |
103 | event = udev_event_new(dev); |
104 | ||
72c0a2c2 | 105 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0); |
912541b0 KS |
106 | |
107 | /* do what devtmpfs usually provides us */ | |
108 | if (udev_device_get_devnode(dev) != NULL) { | |
83cd6b75 | 109 | mode_t mode = 0600; |
912541b0 | 110 | |
090be865 | 111 | if (streq(udev_device_get_subsystem(dev), "block")) |
912541b0 KS |
112 | mode |= S_IFBLK; |
113 | else | |
114 | mode |= S_IFCHR; | |
115 | ||
090be865 | 116 | if (!streq(action, "remove")) { |
d2e54fae | 117 | mkdir_parents_label(udev_device_get_devnode(dev), 0755); |
912541b0 KS |
118 | mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev)); |
119 | } else { | |
120 | unlink(udev_device_get_devnode(dev)); | |
37d52274 | 121 | rmdir_parents(udev_device_get_devnode(dev), "/"); |
912541b0 KS |
122 | } |
123 | } | |
124 | ||
adeba500 KS |
125 | udev_event_execute_rules(event, |
126 | 3 * USEC_PER_SEC, USEC_PER_SEC, | |
127 | NULL, | |
8314de1d | 128 | rules); |
adeba500 | 129 | udev_event_execute_run(event, |
8314de1d | 130 | 3 * USEC_PER_SEC, USEC_PER_SEC); |
2181d30a | 131 | out: |
cc56fafe | 132 | mac_selinux_finish(); |
1ca208fb ZJS |
133 | |
134 | return err ? EXIT_FAILURE : EXIT_SUCCESS; | |
f0083e3d | 135 | } |