chiark / gitweb /
do not needlessly declare some local variables in udev_rules_parse.c as static
[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
33 #include "udev.h"
34 #include "udev_rules.h"
35 #include "udev_selinux.h"
36
37 #ifdef USE_LOG
38 void log_message(int priority, const char *format, ...)
39 {
40         va_list args;
41
42         if (priority > udev_log_priority)
43                 return;
44
45         va_start(args, format);
46         vsyslog(priority, format, args);
47         va_end(args);
48 }
49 #endif
50
51 static void asmlinkage sig_handler(int signum)
52 {
53         switch (signum) {
54                 case SIGALRM:
55                         exit(1);
56                 case SIGINT:
57                 case SIGTERM:
58                         exit(20 + signum);
59         }
60 }
61
62 int main(int argc, char *argv[], char *envp[])
63 {
64         struct sysfs_device *dev;
65         struct udevice *udev;
66         const char *maj, *min;
67         struct udev_rules rules;
68         const char *action;
69         const char *devpath;
70         const char *subsystem;
71         struct sigaction act;
72         int devnull;
73         int retval = -EINVAL;
74
75         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
76                 printf("%s\n", VERSION);
77                 exit(0);
78         }
79
80         /* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
81         devnull = open("/dev/null", O_RDWR);
82         if (devnull >= 0)  {
83                 if (devnull != STDIN_FILENO)
84                         dup2(devnull, STDIN_FILENO);
85                 if (devnull != STDOUT_FILENO)
86                         dup2(devnull, STDOUT_FILENO);
87                 if (devnull != STDERR_FILENO)
88                         dup2(devnull, STDERR_FILENO);
89                 if (devnull > STDERR_FILENO)
90                         close(devnull);
91         }
92
93         logging_init("udev");
94         if (devnull < 0)
95                 err("open /dev/null failed: %s\n", strerror(errno));
96         udev_config_init();
97         selinux_init();
98         dbg("version %s\n", VERSION);
99
100         /* set signal handlers */
101         memset(&act, 0x00, sizeof(act));
102         act.sa_handler = (void (*)(int)) sig_handler;
103         sigemptyset (&act.sa_mask);
104         act.sa_flags = 0;
105         sigaction(SIGALRM, &act, NULL);
106         sigaction(SIGINT, &act, NULL);
107         sigaction(SIGTERM, &act, NULL);
108
109         /* trigger timeout to prevent hanging processes */
110         alarm(UDEV_EVENT_TIMEOUT);
111
112         action = getenv("ACTION");
113         devpath = getenv("DEVPATH");
114         subsystem = getenv("SUBSYSTEM");
115         /* older kernels passed the SUBSYSTEM only as argument */
116         if (subsystem == NULL && argc == 2)
117                 subsystem = argv[1];
118
119         if (action == NULL || subsystem == NULL || devpath == NULL) {
120                 err("action, subsystem or devpath missing\n");
121                 goto exit;
122         }
123
124         /* export log_priority , as called programs may want to do the same as udev */
125         if (udev_log_priority) {
126                 char priority[32];
127
128                 sprintf(priority, "%i", udev_log_priority);
129                 setenv("UDEV_LOG", priority, 1);
130         }
131
132         sysfs_init();
133         udev_rules_init(&rules, 0);
134
135         dev = sysfs_device_get(devpath);
136         if (dev == NULL) {
137                 info("unable to open '%s'\n", devpath);
138                 goto fail;
139         }
140
141         udev = udev_device_init(NULL);
142         if (udev == NULL)
143                 goto fail;
144
145         /* override built-in sysfs device */
146         udev->dev = dev;
147         strlcpy(udev->action, action, sizeof(udev->action));
148
149         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
150         maj = getenv("MAJOR");
151         min = getenv("MINOR");
152         if (maj != NULL && min != NULL)
153                 udev->devt = makedev(atoi(maj), atoi(min));
154         else
155                 udev->devt = udev_device_get_devt(udev);
156
157         retval = udev_device_event(&rules, udev);
158
159         /* rules may change/disable the timeout */
160         if (udev->event_timeout >= 0)
161                 alarm(udev->event_timeout);
162
163         if (retval == 0 && !udev->ignore_device && udev_run)
164                 udev_rules_run(udev);
165
166         udev_device_cleanup(udev);
167 fail:
168         udev_rules_cleanup(&rules);
169         sysfs_cleanup();
170         selinux_exit();
171
172 exit:
173         logging_close();
174         if (retval != 0)
175                 return 1;
176         return 0;
177 }