]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / test / test-udev.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
4 ***/
5
6 #include <errno.h>
7 #include <sched.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mount.h>
11 #include <sys/signalfd.h>
12 #include <unistd.h>
13
14 #include "build.h"
15 #include "device-private.h"
16 #include "fs-util.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "mkdir.h"
20 #include "mount-util.h"
21 #include "namespace-util.h"
22 #include "selinux-util.h"
23 #include "signal-util.h"
24 #include "string-util.h"
25 #include "tests.h"
26 #include "udev-event.h"
27
28 static int fake_filesystems(void) {
29 static const struct fakefs {
30 const char *src;
31 const char *target;
32 const char *error;
33 bool ignore_mount_error;
34 } fakefss[] = {
35 { "test/tmpfs/sys", "/sys", "Failed to mount test /sys", false },
36 { "test/tmpfs/dev", "/dev", "Failed to mount test /dev", false },
37 { "test/run", "/run", "Failed to mount test /run", false },
38 { "test/run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
39 { "test/run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
40 };
41 int r;
42
43 r = detach_mount_namespace();
44 if (r < 0)
45 return log_error_errno(r, "Failed to detach mount namespace: %m");
46
47 for (size_t i = 0; i < ELEMENTSOF(fakefss); i++) {
48 r = mount_nofollow_verbose(fakefss[i].ignore_mount_error ? LOG_NOTICE : LOG_ERR,
49 fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
50 if (r < 0 && !fakefss[i].ignore_mount_error)
51 return r;
52 }
53
54 return 0;
55 }
56
57 static int run(int argc, char *argv[]) {
58 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
59 _cleanup_(udev_event_freep) UdevEvent *event = NULL;
60 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
61 const char *devpath, *devname, *action;
62 int r;
63
64 test_setup_logging(LOG_INFO);
65
66 if (!IN_SET(argc, 2, 3))
67 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
68 "This program needs one or two arguments, %d given", argc - 1);
69
70 r = fake_filesystems();
71 if (r < 0)
72 return r;
73
74 /* Let's make sure the test runs with selinux assumed disabled. */
75 #if HAVE_SELINUX
76 fini_selinuxmnt();
77 #endif
78 mac_selinux_retest();
79
80 if (argc == 2) {
81 if (!streq(argv[1], "check"))
82 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
83 "Unknown argument: %s", argv[1]);
84
85 return 0;
86 }
87
88 log_debug("version %s", GIT_VERSION);
89
90 r = mac_selinux_init();
91 if (r < 0)
92 return r;
93
94 action = argv[1];
95 devpath = argv[2];
96
97 assert_se(udev_rules_load(&rules, RESOLVE_NAME_EARLY) == 0);
98
99 const char *syspath;
100 syspath = strjoina("/sys", devpath);
101 r = device_new_from_synthetic_event(&dev, syspath, action);
102 if (r < 0)
103 return log_debug_errno(r, "Failed to open device '%s'", devpath);
104
105 assert_se(event = udev_event_new(dev, 0, NULL));
106
107 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
108
109 /* do what devtmpfs usually provides us */
110 if (sd_device_get_devname(dev, &devname) >= 0) {
111 const char *subsystem;
112 mode_t mode = 0600;
113
114 if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
115 mode |= S_IFBLK;
116 else
117 mode |= S_IFCHR;
118
119 if (!streq(action, "remove")) {
120 dev_t devnum = makedev(0, 0);
121
122 (void) mkdir_parents_label(devname, 0755);
123 (void) sd_device_get_devnum(dev, &devnum);
124 if (mknod(devname, mode, devnum) < 0)
125 return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
126 } else {
127 if (unlink(devname) < 0)
128 return log_error_errno(errno, "unlink('%s') failed: %m", devname);
129 (void) rmdir_parents(devname, "/");
130 }
131 }
132
133 udev_event_execute_rules(event, 3 * USEC_PER_SEC, SIGKILL, NULL, rules);
134 udev_event_execute_run(event, 3 * USEC_PER_SEC, SIGKILL);
135
136 return 0;
137 }
138
139 DEFINE_MAIN_FUNCTION(run);