chiark / gitweb /
58bc28e02a1875796941c1e609a7f9789693e919
[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
37 static void asmlinkage sig_handler(int signum)
38 {
39         switch (signum) {
40                 case SIGALRM:
41                         exit(1);
42                 case SIGINT:
43                 case SIGTERM:
44                         exit(20 + signum);
45         }
46 }
47
48 int main(int argc, char *argv[])
49 {
50         struct udev *udev;
51         struct sysfs_device *dev;
52         struct udevice *udevice;
53         const char *maj, *min;
54         struct udev_rules rules;
55         const char *action;
56         const char *devpath;
57         const char *subsystem;
58         struct sigaction act;
59         int retval = -EINVAL;
60
61         udev = udev_new();
62         if (udev == NULL)
63                 exit(1);
64         dbg(udev, "version %s\n", VERSION);
65
66         /* set signal handlers */
67         memset(&act, 0x00, sizeof(act));
68         act.sa_handler = (void (*)(int)) sig_handler;
69         sigemptyset (&act.sa_mask);
70         act.sa_flags = 0;
71         sigaction(SIGALRM, &act, NULL);
72         sigaction(SIGINT, &act, NULL);
73         sigaction(SIGTERM, &act, NULL);
74
75         /* trigger timeout to prevent hanging processes */
76         alarm(UDEV_EVENT_TIMEOUT);
77
78         action = getenv("ACTION");
79         devpath = getenv("DEVPATH");
80         subsystem = getenv("SUBSYSTEM");
81         /* older kernels passed the SUBSYSTEM only as argument */
82         if (subsystem == NULL && argc == 2)
83                 subsystem = argv[1];
84
85         if (action == NULL || subsystem == NULL || devpath == NULL) {
86                 err(udev, "action, subsystem or devpath missing\n");
87                 goto exit;
88         }
89
90         /* export log_priority , as called programs may want to do the same as udev */
91         if (udev_get_log_priority(udev) > 0) {
92                 char priority[32];
93
94                 sprintf(priority, "%i", udev_get_log_priority(udev));
95                 setenv("UDEV_LOG", priority, 1);
96         }
97
98         sysfs_init();
99         udev_rules_init(udev, &rules, 0);
100
101         dev = sysfs_device_get(udev, devpath);
102         if (dev == NULL) {
103                 info(udev, "unable to open '%s'\n", devpath);
104                 goto fail;
105         }
106
107         udevice = udev_device_init(udev);
108         if (udevice == NULL)
109                 goto fail;
110
111         /* override built-in sysfs device */
112         udevice->dev = dev;
113         strlcpy(udevice->action, action, sizeof(udevice->action));
114
115         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
116         maj = getenv("MAJOR");
117         min = getenv("MINOR");
118         if (maj != NULL && min != NULL)
119                 udevice->devt = makedev(atoi(maj), atoi(min));
120         else
121                 udevice->devt = udev_device_get_devt(udevice);
122
123         retval = udev_device_event(&rules, udevice);
124
125         /* rules may change/disable the timeout */
126         if (udevice->event_timeout >= 0)
127                 alarm(udevice->event_timeout);
128
129         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
130                 udev_rules_run(udevice);
131
132         udev_device_cleanup(udevice);
133 fail:
134         udev_rules_cleanup(&rules);
135         sysfs_cleanup();
136 exit:
137         udev_unref(udev);
138         if (retval != 0)
139                 return 1;
140         return 0;
141 }