]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
libudev-list: move libudev-list related definitions to libudev-list-internal.h
[thirdparty/systemd.git] / src / test / test-udev.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "device-private.h"
15 #include "fs-util.h"
16 #include "log.h"
17 #include "missing.h"
18 #include "mkdir.h"
19 #include "selinux-util.h"
20 #include "signal-util.h"
21 #include "string-util.h"
22 #include "tests.h"
23 #include "udev.h"
24
25 static int fake_filesystems(void) {
26 static const struct fakefs {
27 const char *src;
28 const char *target;
29 const char *error;
30 bool ignore_mount_error;
31 } fakefss[] = {
32 { "test/tmpfs/sys", "/sys", "Failed to mount test /sys", false },
33 { "test/tmpfs/dev", "/dev", "Failed to mount test /dev", false },
34 { "test/run", "/run", "Failed to mount test /run", false },
35 { "test/run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
36 { "test/run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
37 };
38 unsigned i;
39
40 if (unshare(CLONE_NEWNS) < 0)
41 return log_error_errno(errno, "Failed to call unshare(): %m");
42
43 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
44 return log_error_errno(errno, "Failed to mount / as private: %m");
45
46 for (i = 0; i < ELEMENTSOF(fakefss); i++)
47 if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) {
48 log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error);
49 if (!fakefss[i].ignore_mount_error)
50 return -errno;
51 }
52
53 return 0;
54 }
55
56 int main(int argc, char *argv[]) {
57 _cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
58 _cleanup_(udev_event_freep) struct udev_event *event = NULL;
59 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
60 const char *devpath, *devname, *action;
61 int r;
62
63 test_setup_logging(LOG_INFO);
64
65 if (!IN_SET(argc, 2, 3)) {
66 log_error("This program needs one or two arguments, %d given", argc - 1);
67 return EXIT_FAILURE;
68 }
69
70 if (fake_filesystems() < 0)
71 return EXIT_FAILURE;
72
73 if (argc == 2) {
74 if (!streq(argv[1], "check")) {
75 log_error("Unknown argument: %s", argv[1]);
76 return EXIT_FAILURE;
77 }
78
79 return EXIT_SUCCESS;
80 }
81
82 log_debug("version %s", PACKAGE_VERSION);
83 mac_selinux_init();
84
85 action = argv[1];
86 devpath = argv[2];
87
88 rules = udev_rules_new(RESOLVE_NAME_EARLY);
89
90 const char *syspath = strjoina("/sys", devpath);
91 r = device_new_from_synthetic_event(&dev, syspath, action);
92 if (r < 0) {
93 log_debug_errno(r, "Failed to open device '%s'", devpath);
94 goto out;
95 }
96
97 assert_se(event = udev_event_new(dev, 0, NULL));
98
99 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
100
101 /* do what devtmpfs usually provides us */
102 if (sd_device_get_devname(dev, &devname) >= 0) {
103 const char *subsystem;
104 mode_t mode = 0600;
105
106 if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
107 mode |= S_IFBLK;
108 else
109 mode |= S_IFCHR;
110
111 if (!streq(action, "remove")) {
112 dev_t devnum = makedev(0, 0);
113
114 (void) mkdir_parents_label(devname, 0755);
115 (void) sd_device_get_devnum(dev, &devnum);
116 assert_se(mknod(devname, mode, devnum) == 0);
117 } else {
118 assert_se(unlink(devname) == 0);
119 (void) rmdir_parents(devname, "/");
120 }
121 }
122
123 udev_event_execute_rules(event, 3 * USEC_PER_SEC, NULL, rules);
124 udev_event_execute_run(event, 3 * USEC_PER_SEC);
125 out:
126 mac_selinux_finish();
127
128 return EXIT_SUCCESS;
129 }