]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/test-udev.c
remove mknod() logic and rely on 'devtmpfs'
[thirdparty/systemd.git] / udev / 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>
2181d30a 29#include <sys/signalfd.h>
85511f02 30
f0083e3d 31#include "udev.h"
f0083e3d 32
dd8a93e0
KS
33void 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
59345311 37int main(int argc, char *argv[])
f4dc8d11 38{
7d563a17 39 struct udev *udev;
2181d30a
KS
40 struct udev_event *event = NULL;
41 struct udev_device *dev = NULL;
42 struct udev_rules *rules = NULL;
aa8734ff 43 char syspath[UTIL_PATH_SIZE];
8544c9ad 44 const char *devpath;
aa8734ff 45 const char *action;
2181d30a 46 sigset_t mask, sigmask_orig;
aa8734ff 47 int err = -EINVAL;
f4dc8d11 48
7d563a17
KS
49 udev = udev_new();
50 if (udev == NULL)
51 exit(1);
aa8734ff 52 info(udev, "version %s\n", VERSION);
c3b1fa66 53 udev_selinux_init(udev);
51737eb8 54
2181d30a 55 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
7e89a569 56
220893b3
KS
57 action = argv[1];
58 if (action == NULL) {
59 err(udev, "action missing\n");
60 goto out;
61 }
7e0bd584 62
220893b3
KS
63 devpath = argv[2];
64 if (devpath == NULL) {
65 err(udev, "devpath missing\n");
2181d30a 66 goto out;
c449b25e 67 }
f0713480 68
154a7b84 69 rules = udev_rules_new(udev, 1);
821d0ec8 70
065db052 71 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
aa8734ff 72 dev = udev_device_new_from_syspath(udev, syspath);
1aa1e248 73 if (dev == NULL) {
aa8734ff 74 info(udev, "unknown device '%s'\n", devpath);
2181d30a 75 goto out;
1aa1e248
KS
76 }
77
aa8734ff
KS
78 udev_device_set_action(dev, action);
79 event = udev_event_new(dev);
2a94c877 80
2181d30a
KS
81 sigfillset(&mask);
82 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
83 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
84 if (event->fd_signal < 0) {
85 fprintf(stderr, "error creating signalfd\n");
86 goto out;
87 }
bf50425b 88
220893b3
KS
89 /* do what devtmpfs usually provides us */
90 if (udev_device_get_devnode(dev) != NULL) {
91 mode_t mode;
92
93 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
94 mode |= S_IFBLK;
95 else
96 mode |= S_IFCHR;
97
98 if (strcmp(action, "remove") != 0) {
99 util_create_path(udev, udev_device_get_devnode(dev));
100 mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
101 } else {
102 unlink(udev_device_get_devnode(dev));
103 util_delete_path(udev, udev_device_get_devnode(dev));
104 }
105 }
106
2181d30a 107 err = udev_event_execute_rules(event, rules, &sigmask_orig);
f46d2e54 108 if (err == 0)
d412a685 109 udev_event_execute_run(event, NULL);
2181d30a
KS
110out:
111 if (event != NULL && event->fd_signal >= 0)
112 close(event->fd_signal);
aa8734ff
KS
113 udev_event_unref(event);
114 udev_device_unref(dev);
d7ddce18 115 udev_rules_unref(rules);
c3b1fa66 116 udev_selinux_exit(udev);
a035bf27 117 udev_unref(udev);
aa8734ff 118 if (err != 0)
37854ffc
KS
119 return 1;
120 return 0;
f0083e3d 121}