]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
c79384847da9bef1baa5104aabdae58b2a189e8b
[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
9 #include <errno.h>
10 #include <sched.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/mount.h>
14 #include <sys/signalfd.h>
15 #include <unistd.h>
16
17 #include "fs-util.h"
18 #include "log.h"
19 #include "missing.h"
20 #include "selinux-util.h"
21 #include "signal-util.h"
22 #include "string-util.h"
23 #include "udev-util.h"
24 #include "udev.h"
25
26 static int fake_filesystems(void) {
27 static const struct fakefs {
28 const char *src;
29 const char *target;
30 const char *error;
31 bool ignore_mount_error;
32 } fakefss[] = {
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 },
38 };
39 unsigned int i;
40
41 if (unshare(CLONE_NEWNS) < 0)
42 return log_error_errno(errno, "failed to call unshare(): %m");
43
44 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0)
45 return log_error_errno(errno, "failed to mount / as private: %m");
46
47 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
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);
50 if (!fakefss[i].ignore_mount_error)
51 return -errno;
52 }
53 }
54
55 return 0;
56 }
57
58 int main(int argc, char *argv[]) {
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;
63 char syspath[UTIL_PATH_SIZE];
64 const char *devpath;
65 const char *action;
66 int err;
67
68 log_parse_environment();
69 log_open();
70
71 err = fake_filesystems();
72 if (err < 0)
73 return EXIT_FAILURE;
74
75 udev = udev_new();
76 if (udev == NULL)
77 return EXIT_FAILURE;
78
79 log_debug("version %s", PACKAGE_VERSION);
80 mac_selinux_init();
81
82 action = argv[1];
83 if (action == NULL) {
84 log_error("action missing");
85 goto out;
86 }
87
88 devpath = argv[2];
89 if (devpath == NULL) {
90 log_error("devpath missing");
91 goto out;
92 }
93
94 rules = udev_rules_new(udev, 1);
95
96 strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
97 dev = udev_device_new_from_synthetic_event(udev, syspath, action);
98 if (dev == NULL) {
99 log_debug("unknown device '%s'", devpath);
100 goto out;
101 }
102
103 event = udev_event_new(dev);
104
105 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
106
107 /* do what devtmpfs usually provides us */
108 if (udev_device_get_devnode(dev) != NULL) {
109 mode_t mode = 0600;
110
111 if (streq(udev_device_get_subsystem(dev), "block"))
112 mode |= S_IFBLK;
113 else
114 mode |= S_IFCHR;
115
116 if (!streq(action, "remove")) {
117 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
118 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
119 } else {
120 unlink(udev_device_get_devnode(dev));
121 rmdir_parents(udev_device_get_devnode(dev), "/");
122 }
123 }
124
125 udev_event_execute_rules(event,
126 3 * USEC_PER_SEC, USEC_PER_SEC,
127 NULL,
128 rules);
129 udev_event_execute_run(event,
130 3 * USEC_PER_SEC, USEC_PER_SEC);
131 out:
132 mac_selinux_finish();
133
134 return err ? EXIT_FAILURE : EXIT_SUCCESS;
135 }