]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-udev.c
libudev: update copyright headers
[thirdparty/systemd.git] / src / test / test-udev.c
CommitLineData
f0083e3d 1/*
e8d569b4 2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
55e9959b 3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
f0083e3d 4 *
55e9959b
KS
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
f0083e3d 9 *
55e9959b
KS
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f0083e3d
GKH
17 */
18
c81b35c0
KS
19#include <stdio.h>
20#include <stddef.h>
f0083e3d
GKH
21#include <stdlib.h>
22#include <string.h>
c449b25e 23#include <fcntl.h>
e8baccca 24#include <ctype.h>
c81b35c0 25#include <errno.h>
1ceba936 26#include <unistd.h>
27f877e6 27#include <syslog.h>
44aff4cd 28#include <grp.h>
4cb72937
KS
29#include <sched.h>
30#include <sys/mount.h>
2181d30a 31#include <sys/signalfd.h>
85511f02 32
f0083e3d 33#include "udev.h"
f0083e3d 34
dd8a93e0 35void udev_main_log(struct udev *udev, int priority,
912541b0
KS
36 const char *file, int line, const char *fn,
37 const char *format, va_list args) {}
dd8a93e0 38
4cb72937
KS
39static int fake_filesystems(void) {
40 static const struct fakefs {
41 const char *src;
42 const char *target;
43 const char *error;
44 } fakefss[] = {
45 { "test/sys", "/sys", "failed to mount test /sys" },
46 { "test/dev", "/dev", "failed to mount test /dev" },
47 { "test/run", "/run", "failed to mount test /run" },
48 { "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d" },
49 { "test/run", "/usr/lib/udev/rules.d", "failed to mount empty /usr/lib/udev/rules.d" },
50 };
51 unsigned int i;
52 int err;
53
54 err = unshare(CLONE_NEWNS);
55 if (err < 0) {
56 err = -errno;
04eaa668
KS
57 fprintf(stderr, "failed to call unshare(): %m\n");
58 goto out;
59 }
60
61 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
62 err = -errno;
63 fprintf(stderr, "failed to mount / as private: %m\n");
64 goto out;
4cb72937
KS
65 }
66
67 for (i = 0; i < ELEMENTSOF(fakefss); i++) {
68 err = mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
69 if (err < 0) {
70 err = -errno;
71 fprintf(stderr, "%s %m", fakefss[i].error);
72 return err;
73 }
74 }
04eaa668 75out:
4cb72937
KS
76 return err;
77}
78
79
59345311 80int main(int argc, char *argv[])
f4dc8d11 81{
912541b0
KS
82 struct udev *udev;
83 struct udev_event *event = NULL;
84 struct udev_device *dev = NULL;
85 struct udev_rules *rules = NULL;
86 char syspath[UTIL_PATH_SIZE];
87 const char *devpath;
88 const char *action;
89 sigset_t mask, sigmask_orig;
4cb72937
KS
90 int err;
91
92 err = fake_filesystems();
93 if (err < 0)
94 return EXIT_FAILURE;
912541b0
KS
95
96 udev = udev_new();
97 if (udev == NULL)
1c0f62e3 98 exit(EXIT_FAILURE);
baa30fbc 99 log_debug("version %s\n", VERSION);
0f9963a8 100 label_init("/dev");
912541b0
KS
101
102 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
103
104 action = argv[1];
105 if (action == NULL) {
baa30fbc 106 log_error("action missing\n");
912541b0
KS
107 goto out;
108 }
109
110 devpath = argv[2];
111 if (devpath == NULL) {
baa30fbc 112 log_error("devpath missing\n");
912541b0
KS
113 goto out;
114 }
115
116 rules = udev_rules_new(udev, 1);
117
4cb72937 118 util_strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
912541b0
KS
119 dev = udev_device_new_from_syspath(udev, syspath);
120 if (dev == NULL) {
baa30fbc 121 log_debug("unknown device '%s'\n", devpath);
912541b0
KS
122 goto out;
123 }
124
125 udev_device_set_action(dev, action);
126 event = udev_event_new(dev);
127
128 sigfillset(&mask);
129 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
130 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
131 if (event->fd_signal < 0) {
132 fprintf(stderr, "error creating signalfd\n");
133 goto out;
134 }
135
136 /* do what devtmpfs usually provides us */
137 if (udev_device_get_devnode(dev) != NULL) {
83cd6b75 138 mode_t mode = 0600;
912541b0
KS
139
140 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
141 mode |= S_IFBLK;
142 else
143 mode |= S_IFCHR;
144
145 if (strcmp(action, "remove") != 0) {
d2e54fae 146 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
912541b0
KS
147 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
148 } else {
149 unlink(udev_device_get_devnode(dev));
150 util_delete_path(udev, udev_device_get_devnode(dev));
151 }
152 }
153
154 err = udev_event_execute_rules(event, rules, &sigmask_orig);
155 if (err == 0)
156 udev_event_execute_run(event, NULL);
2181d30a 157out:
912541b0
KS
158 if (event != NULL && event->fd_signal >= 0)
159 close(event->fd_signal);
160 udev_event_unref(event);
161 udev_device_unref(dev);
162 udev_rules_unref(rules);
e9a5ef7c 163 label_finish();
912541b0
KS
164 udev_unref(udev);
165 if (err != 0)
1c0f62e3
KS
166 return EXIT_FAILURE;
167 return EXIT_SUCCESS;
f0083e3d 168}