2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
35 #include "udev_rules.h"
36 #include "udev_selinux.h"
38 static void asmlinkage sig_handler(int signum)
49 int main(int argc, char *argv[])
52 struct sysfs_device *dev;
53 struct udevice *udevice;
54 const char *maj, *min;
55 struct udev_rules rules;
58 const char *subsystem;
66 dbg(udev, "version %s\n", VERSION);
69 /* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
70 devnull = open("/dev/null", O_RDWR);
72 if (devnull != STDIN_FILENO)
73 dup2(devnull, STDIN_FILENO);
74 if (devnull != STDOUT_FILENO)
75 dup2(devnull, STDOUT_FILENO);
76 if (devnull != STDERR_FILENO)
77 dup2(devnull, STDERR_FILENO);
78 if (devnull > STDERR_FILENO)
81 err(udev, "open /dev/null failed: %s\n", strerror(errno));
84 /* set signal handlers */
85 memset(&act, 0x00, sizeof(act));
86 act.sa_handler = (void (*)(int)) sig_handler;
87 sigemptyset (&act.sa_mask);
89 sigaction(SIGALRM, &act, NULL);
90 sigaction(SIGINT, &act, NULL);
91 sigaction(SIGTERM, &act, NULL);
93 /* trigger timeout to prevent hanging processes */
94 alarm(UDEV_EVENT_TIMEOUT);
96 action = getenv("ACTION");
97 devpath = getenv("DEVPATH");
98 subsystem = getenv("SUBSYSTEM");
99 /* older kernels passed the SUBSYSTEM only as argument */
100 if (subsystem == NULL && argc == 2)
103 if (action == NULL || subsystem == NULL || devpath == NULL) {
104 err(udev, "action, subsystem or devpath missing\n");
108 /* export log_priority , as called programs may want to do the same as udev */
109 if (udev_get_log_priority(udev) > 0) {
112 sprintf(priority, "%i", udev_get_log_priority(udev));
113 setenv("UDEV_LOG", priority, 1);
117 udev_rules_init(udev, &rules, 0);
119 dev = sysfs_device_get(udev, devpath);
121 info(udev, "unable to open '%s'\n", devpath);
125 udevice = udev_device_init(udev);
129 /* override built-in sysfs device */
131 strlcpy(udevice->action, action, sizeof(udevice->action));
133 /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
134 maj = getenv("MAJOR");
135 min = getenv("MINOR");
136 if (maj != NULL && min != NULL)
137 udevice->devt = makedev(atoi(maj), atoi(min));
139 udevice->devt = udev_device_get_devt(udevice);
141 retval = udev_device_event(&rules, udevice);
143 /* rules may change/disable the timeout */
144 if (udevice->event_timeout >= 0)
145 alarm(udevice->event_timeout);
147 if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
148 udev_rules_run(udevice);
150 udev_device_cleanup(udevice);
152 udev_rules_cleanup(&rules);