]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-udev.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <sched.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/mount.h>
27 #include <sys/signalfd.h>
28 #include <unistd.h>
29
30 #include "fs-util.h"
31 #include "log.h"
32 #include "missing.h"
33 #include "selinux-util.h"
34 #include "signal-util.h"
35 #include "string-util.h"
36 #include "udev-util.h"
37 #include "udev.h"
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 bool ignore_mount_error;
45 } fakefss[] = {
46 { "test/tmpfs/sys", "/sys", "failed to mount test /sys", false },
47 { "test/tmpfs/dev", "/dev", "failed to mount test /dev", false },
48 { "test/run", "/run", "failed to mount test /run", false },
49 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true },
50 { "test/run", UDEVLIBEXECDIR "/rules.d", "failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
51 };
52 unsigned int i;
53
54 if (unshare(CLONE_NEWNS) < 0)
55 return log_error_errno(errno, "failed to call unshare(): %m");
56
57 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0)
58 return log_error_errno(errno, "failed to mount / as private: %m");
59
60 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
61 if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) {
62 log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error);
63 if (!fakefss[i].ignore_mount_error)
64 return -errno;
65 }
66 }
67
68 return 0;
69 }
70
71 int main(int argc, char *argv[]) {
72 _cleanup_udev_unref_ struct udev *udev = NULL;
73 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
74 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
75 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
76 char syspath[UTIL_PATH_SIZE];
77 const char *devpath;
78 const char *action;
79 int err;
80
81 log_parse_environment();
82 log_open();
83
84 err = fake_filesystems();
85 if (err < 0)
86 return EXIT_FAILURE;
87
88 udev = udev_new();
89 if (udev == NULL)
90 return EXIT_FAILURE;
91
92 log_debug("version %s", PACKAGE_VERSION);
93 mac_selinux_init();
94
95 action = argv[1];
96 if (action == NULL) {
97 log_error("action missing");
98 goto out;
99 }
100
101 devpath = argv[2];
102 if (devpath == NULL) {
103 log_error("devpath missing");
104 goto out;
105 }
106
107 rules = udev_rules_new(udev, 1);
108
109 strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
110 dev = udev_device_new_from_synthetic_event(udev, syspath, action);
111 if (dev == NULL) {
112 log_debug("unknown device '%s'", devpath);
113 goto out;
114 }
115
116 event = udev_event_new(dev);
117
118 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
119
120 /* do what devtmpfs usually provides us */
121 if (udev_device_get_devnode(dev) != NULL) {
122 mode_t mode = 0600;
123
124 if (streq(udev_device_get_subsystem(dev), "block"))
125 mode |= S_IFBLK;
126 else
127 mode |= S_IFCHR;
128
129 if (!streq(action, "remove")) {
130 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
131 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
132 } else {
133 unlink(udev_device_get_devnode(dev));
134 rmdir_parents(udev_device_get_devnode(dev), "/");
135 }
136 }
137
138 udev_event_execute_rules(event,
139 3 * USEC_PER_SEC, USEC_PER_SEC,
140 NULL,
141 rules);
142 udev_event_execute_run(event,
143 3 * USEC_PER_SEC, USEC_PER_SEC);
144 out:
145 mac_selinux_finish();
146
147 return err ? EXIT_FAILURE : EXIT_SUCCESS;
148 }