]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev.c
mkdir: append _label to all mkdir() calls that explicitly set the selinux context
[thirdparty/systemd.git] / src / test / test-udev.c
1 /*
2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4 *
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.
9 *
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/>.
17 */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <syslog.h>
28 #include <grp.h>
29 #include <sys/signalfd.h>
30
31 #include "udev.h"
32
33 void udev_main_log(struct udev *udev, int priority,
34 const char *file, int line, const char *fn,
35 const char *format, va_list args) {}
36
37 int main(int argc, char *argv[])
38 {
39 struct udev *udev;
40 struct udev_event *event = NULL;
41 struct udev_device *dev = NULL;
42 struct udev_rules *rules = NULL;
43 char syspath[UTIL_PATH_SIZE];
44 const char *devpath;
45 const char *action;
46 sigset_t mask, sigmask_orig;
47 int err = -EINVAL;
48 const char *prefixes[] = { "/dev", "/run", NULL };
49
50 udev = udev_new();
51 if (udev == NULL)
52 exit(EXIT_FAILURE);
53 log_debug("version %s\n", VERSION);
54 label_init(prefixes);
55
56 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
57
58 action = argv[1];
59 if (action == NULL) {
60 log_error("action missing\n");
61 goto out;
62 }
63
64 devpath = argv[2];
65 if (devpath == NULL) {
66 log_error("devpath missing\n");
67 goto out;
68 }
69
70 rules = udev_rules_new(udev, 1);
71
72 util_strscpyl(syspath, sizeof(syspath), TEST_PREFIX "/sys", devpath, NULL);
73 dev = udev_device_new_from_syspath(udev, syspath);
74 if (dev == NULL) {
75 log_debug("unknown device '%s'\n", devpath);
76 goto out;
77 }
78
79 udev_device_set_action(dev, action);
80 event = udev_event_new(dev);
81
82 sigfillset(&mask);
83 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
84 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
85 if (event->fd_signal < 0) {
86 fprintf(stderr, "error creating signalfd\n");
87 goto out;
88 }
89
90 /* do what devtmpfs usually provides us */
91 if (udev_device_get_devnode(dev) != NULL) {
92 mode_t mode = 0600;
93
94 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
95 mode |= S_IFBLK;
96 else
97 mode |= S_IFCHR;
98
99 if (strcmp(action, "remove") != 0) {
100 mkdir_parents_label(udev_device_get_devnode(dev), 0755);
101 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
102 } else {
103 unlink(udev_device_get_devnode(dev));
104 util_delete_path(udev, udev_device_get_devnode(dev));
105 }
106 }
107
108 err = udev_event_execute_rules(event, rules, &sigmask_orig);
109 if (err == 0)
110 udev_event_execute_run(event, NULL);
111 out:
112 if (event != NULL && event->fd_signal >= 0)
113 close(event->fd_signal);
114 udev_event_unref(event);
115 udev_device_unref(dev);
116 udev_rules_unref(rules);
117 label_finish();
118 udev_unref(udev);
119 if (err != 0)
120 return EXIT_FAILURE;
121 return EXIT_SUCCESS;
122 }