4 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "udev_rules.h"
37 void log_message(int priority, const char *format, ...)
41 if (priority > udev_log_priority)
44 va_start(args, format);
45 vsyslog(priority, format, args);
50 static void asmlinkage sig_handler(int signum)
61 int main(int argc, char *argv[], char *envp[])
63 struct sysfs_device *dev;
65 const char *maj, *min;
66 struct udev_rules rules;
69 const char *subsystem;
74 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
75 printf("%s\n", UDEV_VERSION);
79 /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
80 devnull = open("/dev/null", O_RDWR);
82 if (devnull != STDIN_FILENO)
83 dup2(devnull, STDIN_FILENO);
84 if (devnull != STDOUT_FILENO)
85 dup2(devnull, STDOUT_FILENO);
86 if (devnull != STDERR_FILENO)
87 dup2(devnull, STDERR_FILENO);
88 if (devnull > STDERR_FILENO)
94 err("fatal, could not open /dev/null: %s", strerror(errno));
96 dbg("version %s", UDEV_VERSION);
98 /* set signal handlers */
99 memset(&act, 0x00, sizeof(act));
100 act.sa_handler = (void (*)(int)) sig_handler;
101 sigemptyset (&act.sa_mask);
103 sigaction(SIGALRM, &act, NULL);
104 sigaction(SIGINT, &act, NULL);
105 sigaction(SIGTERM, &act, NULL);
107 /* trigger timeout to prevent hanging processes */
108 alarm(UDEV_ALARM_TIMEOUT);
110 action = getenv("ACTION");
111 devpath = getenv("DEVPATH");
112 subsystem = getenv("SUBSYSTEM");
113 /* older kernels passed the SUBSYSTEM only as argument */
114 if (subsystem == NULL && argc == 2)
117 if (action == NULL || subsystem == NULL || devpath == NULL) {
118 err("action, subsystem or devpath missing");
122 /* export log_priority , as called programs may want to do the same as udev */
123 if (udev_log_priority) {
126 sprintf(priority, "%i", udev_log_priority);
127 setenv("UDEV_LOG", priority, 1);
131 udev_rules_init(&rules, 0);
133 dev = sysfs_device_get(devpath);
135 info("unable to open '%s'", devpath);
139 udev = udev_device_init();
143 /* override built-in sysfs device */
145 strlcpy(udev->action, action, sizeof(udev->action));
147 /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
148 maj = getenv("MAJOR");
149 min = getenv("MINOR");
150 if (maj != NULL && min != NULL)
151 udev->devt = makedev(atoi(maj), atoi(min));
153 udev->devt = udev_device_get_devt(udev);
155 retval = udev_device_event(&rules, udev);
157 if (!retval && udev_run && !list_empty(&udev->run_list)) {
158 struct name_entry *name_loop;
160 dbg("executing run list");
161 list_for_each_entry(name_loop, &udev->run_list, node) {
162 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
163 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
165 run_program(name_loop->name, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
169 udev_device_cleanup(udev);
171 udev_rules_cleanup(&rules);