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