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