2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "udev_rules.h"
34 static void asmlinkage sig_handler(int signum)
45 int main(int argc, char *argv[])
48 struct sysfs_device *dev;
49 struct udevice *udevice;
50 const char *maj, *min;
51 struct udev_rules rules;
54 const char *subsystem;
61 dbg(udev, "version %s\n", VERSION);
64 /* set signal handlers */
65 memset(&act, 0x00, sizeof(act));
66 act.sa_handler = (void (*)(int)) sig_handler;
67 sigemptyset (&act.sa_mask);
69 sigaction(SIGALRM, &act, NULL);
70 sigaction(SIGINT, &act, NULL);
71 sigaction(SIGTERM, &act, NULL);
73 /* trigger timeout to prevent hanging processes */
74 alarm(UDEV_EVENT_TIMEOUT);
76 action = getenv("ACTION");
77 devpath = getenv("DEVPATH");
78 subsystem = getenv("SUBSYSTEM");
79 /* older kernels passed the SUBSYSTEM only as argument */
80 if (subsystem == NULL && argc == 2)
83 if (action == NULL || subsystem == NULL || devpath == NULL) {
84 err(udev, "action, subsystem or devpath missing\n");
88 /* export log_priority , as called programs may want to do the same as udev */
89 if (udev_get_log_priority(udev) > 0) {
92 sprintf(priority, "%i", udev_get_log_priority(udev));
93 setenv("UDEV_LOG", priority, 1);
97 udev_rules_init(udev, &rules, 0);
99 dev = sysfs_device_get(udev, devpath);
101 info(udev, "unable to open '%s'\n", devpath);
105 udevice = udev_device_init(udev);
109 /* override built-in sysfs device */
111 util_strlcpy(udevice->action, action, sizeof(udevice->action));
113 /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
114 maj = getenv("MAJOR");
115 min = getenv("MINOR");
116 if (maj != NULL && min != NULL)
117 udevice->devt = makedev(atoi(maj), atoi(min));
119 udevice->devt = udev_device_get_devt(udevice);
121 retval = udev_device_event(&rules, udevice);
123 /* rules may change/disable the timeout */
124 if (udevice->event_timeout >= 0)
125 alarm(udevice->event_timeout);
127 if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
128 udev_rules_run(udevice);
130 udev_device_cleanup(udevice);
132 udev_rules_cleanup(&rules);