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