chiark / gitweb /
b05990ac073c94ef6fe2f9340c6413c7b9eff1a6
[elogind.git] / udev / test-udev.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <syslog.h>
32 #include <grp.h>
33
34 #include "udev.h"
35 #include "udev_rules.h"
36 #include "udev_selinux.h"
37
38 static void asmlinkage sig_handler(int signum)
39 {
40         switch (signum) {
41                 case SIGALRM:
42                         exit(1);
43                 case SIGINT:
44                 case SIGTERM:
45                         exit(20 + signum);
46         }
47 }
48
49 int main(int argc, char *argv[])
50 {
51         struct udev *udev;
52         struct sysfs_device *dev;
53         struct udevice *udevice;
54         const char *maj, *min;
55         struct udev_rules rules;
56         const char *action;
57         const char *devpath;
58         const char *subsystem;
59         struct sigaction act;
60         int devnull;
61         int retval = -EINVAL;
62
63         udev = udev_new();
64         if (udev == NULL)
65                 exit(1);
66         dbg(udev, "version %s\n", VERSION);
67         selinux_init(udev);
68
69         /* set signal handlers */
70         memset(&act, 0x00, sizeof(act));
71         act.sa_handler = (void (*)(int)) sig_handler;
72         sigemptyset (&act.sa_mask);
73         act.sa_flags = 0;
74         sigaction(SIGALRM, &act, NULL);
75         sigaction(SIGINT, &act, NULL);
76         sigaction(SIGTERM, &act, 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         /* older kernels passed the SUBSYSTEM only as argument */
85         if (subsystem == NULL && argc == 2)
86                 subsystem = argv[1];
87
88         if (action == NULL || subsystem == NULL || devpath == NULL) {
89                 err(udev, "action, subsystem or devpath missing\n");
90                 goto exit;
91         }
92
93         /* export log_priority , as called programs may want to do the same as udev */
94         if (udev_get_log_priority(udev) > 0) {
95                 char priority[32];
96
97                 sprintf(priority, "%i", udev_get_log_priority(udev));
98                 setenv("UDEV_LOG", priority, 1);
99         }
100
101         sysfs_init();
102         udev_rules_init(udev, &rules, 0);
103
104         dev = sysfs_device_get(udev, devpath);
105         if (dev == NULL) {
106                 info(udev, "unable to open '%s'\n", devpath);
107                 goto fail;
108         }
109
110         udevice = udev_device_init(udev);
111         if (udevice == NULL)
112                 goto fail;
113
114         /* override built-in sysfs device */
115         udevice->dev = dev;
116         strlcpy(udevice->action, action, sizeof(udevice->action));
117
118         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
119         maj = getenv("MAJOR");
120         min = getenv("MINOR");
121         if (maj != NULL && min != NULL)
122                 udevice->devt = makedev(atoi(maj), atoi(min));
123         else
124                 udevice->devt = udev_device_get_devt(udevice);
125
126         retval = udev_device_event(&rules, udevice);
127
128         /* rules may change/disable the timeout */
129         if (udevice->event_timeout >= 0)
130                 alarm(udevice->event_timeout);
131
132         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
133                 udev_rules_run(udevice);
134
135         udev_device_cleanup(udevice);
136 fail:
137         udev_rules_cleanup(&rules);
138         sysfs_cleanup();
139         selinux_exit(udev);
140 exit:
141         udev_unref(udev);
142         if (retval != 0)
143                 return 1;
144         return 0;
145 }