chiark / gitweb /
update TODO
[elogind.git] / udev / 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 int main(int argc, char *argv[])
34 {
35         struct udev *udev;
36         struct udev_event *event = NULL;
37         struct udev_device *dev = NULL;
38         struct udev_rules *rules = NULL;
39         char syspath[UTIL_PATH_SIZE];
40         const char *devpath;
41         const char *action;
42         const char *subsystem;
43         sigset_t mask, sigmask_orig;
44         int err = -EINVAL;
45
46         udev = udev_new();
47         if (udev == NULL)
48                 exit(1);
49         info(udev, "version %s\n", VERSION);
50         udev_selinux_init(udev);
51
52         sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
53
54         action = getenv("ACTION");
55         devpath = getenv("DEVPATH");
56         subsystem = getenv("SUBSYSTEM");
57
58         if (action == NULL || subsystem == NULL || devpath == NULL) {
59                 err(udev, "action, subsystem or devpath missing\n");
60                 goto out;
61         }
62
63         rules = udev_rules_new(udev, 1);
64
65         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
66         dev = udev_device_new_from_syspath(udev, syspath);
67         if (dev == NULL) {
68                 info(udev, "unknown device '%s'\n", devpath);
69                 goto out;
70         }
71
72         /* skip reading of db, but read kernel parameters */
73         udev_device_set_info_loaded(dev);
74         udev_device_read_uevent_file(dev);
75
76         udev_device_set_action(dev, action);
77         event = udev_event_new(dev);
78
79         sigfillset(&mask);
80         sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
81         event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
82         if (event->fd_signal < 0) {
83                 fprintf(stderr, "error creating signalfd\n");
84                 goto out;
85         }
86
87         err = udev_event_execute_rules(event, rules, &sigmask_orig);
88         if (err == 0)
89                 udev_event_execute_run(event, NULL);
90 out:
91         if (event != NULL && event->fd_signal >= 0)
92                 close(event->fd_signal);
93         udev_event_unref(event);
94         udev_device_unref(dev);
95         udev_rules_unref(rules);
96         udev_selinux_exit(udev);
97         udev_unref(udev);
98         if (err != 0)
99                 return 1;
100         return 0;
101 }