chiark / gitweb /
Merge branch 'master' of git+ssh://master.kernel.org/pub/scm/linux/hotplug/udev
[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 <signal.h>
27 #include <unistd.h>
28 #include <syslog.h>
29 #include <grp.h>
30
31 #include "udev.h"
32
33 static void sig_handler(int signum)
34 {
35         switch (signum) {
36                 case SIGALRM:
37                         _exit(1);
38                 case SIGINT:
39                 case SIGTERM:
40                         _exit(20 + signum);
41         }
42 }
43
44 int main(int argc, char *argv[])
45 {
46         struct udev *udev;
47         struct udev_event *event;
48         struct udev_device *dev;
49         struct udev_rules *rules;
50         char syspath[UTIL_PATH_SIZE];
51         const char *devpath;
52         const char *action;
53         const char *subsystem;
54         struct sigaction act;
55         sigset_t mask;
56         int err = -EINVAL;
57
58         udev = udev_new();
59         if (udev == NULL)
60                 exit(1);
61         info(udev, "version %s\n", VERSION);
62         udev_selinux_init(udev);
63
64         /* set signal handlers */
65         memset(&act, 0x00, sizeof(act));
66         act.sa_handler = sig_handler;
67         sigemptyset (&act.sa_mask);
68         act.sa_flags = 0;
69         sigaction(SIGALRM, &act, NULL);
70         sigaction(SIGINT, &act, NULL);
71         sigaction(SIGTERM, &act, NULL);
72         sigemptyset(&mask);
73         sigaddset(&mask, SIGALRM);
74         sigaddset(&mask, SIGINT);
75         sigaddset(&mask, SIGTERM);
76         sigprocmask(SIG_UNBLOCK, &mask, NULL);
77
78         /* trigger timeout to prevent hanging processes */
79         alarm(UDEV_EVENT_TIMEOUT);
80
81         action = getenv("ACTION");
82         devpath = getenv("DEVPATH");
83         subsystem = getenv("SUBSYSTEM");
84
85         if (action == NULL || subsystem == NULL || devpath == NULL) {
86                 err(udev, "action, subsystem or devpath missing\n");
87                 goto exit;
88         }
89
90         rules = udev_rules_new(udev, 1);
91
92         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
93         dev = udev_device_new_from_syspath(udev, syspath);
94         if (dev == NULL) {
95                 info(udev, "unknown device '%s'\n", devpath);
96                 goto fail;
97         }
98
99         /* skip reading of db, but read kernel parameters */
100         udev_device_set_info_loaded(dev);
101         udev_device_read_uevent_file(dev);
102
103         udev_device_set_action(dev, action);
104         event = udev_event_new(dev);
105         err = udev_event_execute_rules(event, rules);
106
107         /* rules may change/disable the timeout */
108         if (udev_device_get_event_timeout(dev) >= 0)
109                 alarm(udev_device_get_event_timeout(dev));
110
111         if (err == 0)
112                 udev_event_execute_run(event, NULL);
113
114         udev_event_unref(event);
115         udev_device_unref(dev);
116 fail:
117         udev_rules_unref(rules);
118 exit:
119         udev_selinux_exit(udev);
120         udev_unref(udev);
121         if (err != 0)
122                 return 1;
123         return 0;
124 }