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.
34 #include "udev_rules.h"
35 #include "udev_selinux.h"
38 void log_message(int priority, const char *format, ...)
42 if (priority > udev_log_priority)
45 va_start(args, format);
46 vsyslog(priority, format, args);
51 static void asmlinkage sig_handler(int signum)
62 int main(int argc, char *argv[], char *envp[])
64 struct sysfs_device *dev;
66 const char *maj, *min;
67 struct udev_rules rules;
70 const char *subsystem;
75 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
76 printf("%s\n", VERSION);
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);
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)
95 err("open /dev/null failed: %s\n", strerror(errno));
98 dbg("version %s\n", VERSION);
100 /* set signal handlers */
101 memset(&act, 0x00, sizeof(act));
102 act.sa_handler = (void (*)(int)) sig_handler;
103 sigemptyset (&act.sa_mask);
105 sigaction(SIGALRM, &act, NULL);
106 sigaction(SIGINT, &act, NULL);
107 sigaction(SIGTERM, &act, NULL);
109 /* trigger timeout to prevent hanging processes */
110 alarm(UDEV_EVENT_TIMEOUT);
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)
119 if (action == NULL || subsystem == NULL || devpath == NULL) {
120 err("action, subsystem or devpath missing\n");
124 /* export log_priority , as called programs may want to do the same as udev */
125 if (udev_log_priority) {
128 sprintf(priority, "%i", udev_log_priority);
129 setenv("UDEV_LOG", priority, 1);
133 udev_rules_init(&rules, 0);
135 dev = sysfs_device_get(devpath);
137 info("unable to open '%s'\n", devpath);
141 udev = udev_device_init(NULL);
145 /* override built-in sysfs device */
147 strlcpy(udev->action, action, sizeof(udev->action));
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));
155 udev->devt = udev_device_get_devt(udev);
157 retval = udev_device_event(&rules, udev);
159 /* rules may change/disable the timeout */
160 if (udev->event_timeout >= 0)
161 alarm(udev->event_timeout);
163 if (retval == 0 && !udev->ignore_device && udev_run)
164 udev_rules_run(udev);
166 udev_device_cleanup(udev);
168 udev_rules_cleanup(&rules);