chiark / gitweb /
6c327a6e080b7a98888a844cefa83851138db032
[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 #include "udev_rules.h"
33
34 static void asmlinkage sig_handler(int signum)
35 {
36         switch (signum) {
37                 case SIGALRM:
38                         exit(1);
39                 case SIGINT:
40                 case SIGTERM:
41                         exit(20 + signum);
42         }
43 }
44
45 int main(int argc, char *argv[])
46 {
47         struct udev *udev;
48         struct udev_event *event;
49         struct udev_device *dev;
50         struct udev_rules rules;
51         char syspath[UTIL_PATH_SIZE];
52         const char *devpath;
53         const char *action;
54         const char *subsystem;
55         struct sigaction act;
56         int err = -EINVAL;
57
58         udev = udev_new();
59         if (udev == NULL)
60                 exit(1);
61         info(udev, "version %s\n", VERSION);
62         selinux_init(udev);
63
64         /* set signal handlers */
65         memset(&act, 0x00, sizeof(act));
66         act.sa_handler = (void (*)(int)) 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
73         /* trigger timeout to prevent hanging processes */
74         alarm(UDEV_EVENT_TIMEOUT);
75
76         action = getenv("ACTION");
77         devpath = getenv("DEVPATH");
78         subsystem = getenv("SUBSYSTEM");
79
80         if (action == NULL || subsystem == NULL || devpath == NULL) {
81                 err(udev, "action, subsystem or devpath missing\n");
82                 goto exit;
83         }
84
85         udev_rules_init(udev, &rules, 0);
86
87         util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
88         util_strlcat(syspath, devpath, sizeof(syspath));
89         dev = udev_device_new_from_syspath(udev, syspath);
90         if (dev == NULL) {
91                 info(udev, "unknown device '%s'\n", devpath);
92                 goto fail;
93         }
94
95         /* skip reading of db, but read kernel parameters */
96         udev_device_set_info_loaded(dev);
97         udev_device_read_uevent_file(dev);
98
99         udev_device_set_action(dev, action);
100         event = udev_event_new(dev);
101         err = udev_event_run(event, &rules);
102
103         /* rules may change/disable the timeout */
104         if (udev_device_get_event_timeout(dev) >= 0)
105                 alarm(udev_device_get_event_timeout(dev));
106
107         if (err == 0 && !event->ignore_device && udev_get_run(udev))
108                 udev_rules_run(event);
109
110         udev_event_unref(event);
111         udev_device_unref(dev);
112 fail:
113         udev_rules_cleanup(&rules);
114 exit:
115         selinux_exit(udev);
116         udev_unref(udev);
117         if (err != 0)
118                 return 1;
119         return 0;
120 }