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"
39 void log_message(int priority, const char *format, ...)
43 if (priority > udev_log_priority)
46 va_start(args, format);
47 vsyslog(priority, format, args);
52 static void asmlinkage sig_handler(int signum)
63 int main(int argc, char *argv[], char *envp[])
65 struct sysfs_device *dev;
67 const char *maj, *min;
68 struct udev_rules rules;
71 const char *subsystem;
76 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
77 printf("%s\n", VERSION);
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);
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)
96 err("open /dev/null failed: %s\n", strerror(errno));
99 dbg("version %s\n", VERSION);
101 /* set signal handlers */
102 memset(&act, 0x00, sizeof(act));
103 act.sa_handler = (void (*)(int)) sig_handler;
104 sigemptyset (&act.sa_mask);
106 sigaction(SIGALRM, &act, NULL);
107 sigaction(SIGINT, &act, NULL);
108 sigaction(SIGTERM, &act, NULL);
110 /* trigger timeout to prevent hanging processes */
111 alarm(UDEV_EVENT_TIMEOUT);
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)
120 if (action == NULL || subsystem == NULL || devpath == NULL) {
121 err("action, subsystem or devpath missing\n");
125 /* export log_priority , as called programs may want to do the same as udev */
126 if (udev_log_priority) {
129 sprintf(priority, "%i", udev_log_priority);
130 setenv("UDEV_LOG", priority, 1);
134 udev_rules_init(&rules, 0);
136 dev = sysfs_device_get(devpath);
138 info("unable to open '%s'\n", devpath);
142 udev = udev_device_init();
146 /* override built-in sysfs device */
148 strlcpy(udev->action, action, sizeof(udev->action));
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));
156 udev->devt = udev_device_get_devt(udev);
158 retval = udev_device_event(&rules, udev);
160 /* rules may change/disable the timeout */
161 if (udev->event_timeout >= 0)
162 alarm(udev->event_timeout);
164 if (retval == 0 && !udev->ignore_device && udev_run)
165 udev_rules_run(udev);
167 udev_device_cleanup(udev);
169 udev_rules_cleanup(&rules);