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.
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
37 #include "udev_version.h"
38 #include "udev_rules.h"
42 void log_message(int priority, const char *format, ...)
46 if (priority > udev_log_priority)
49 va_start(args, format);
50 vsyslog(priority, format, args);
55 static void asmlinkage sig_handler(int signum)
66 int main(int argc, char *argv[], char *envp[])
69 struct udev_rules rules;
72 const char *subsystem;
77 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
78 printf("%s\n", UDEV_VERSION);
82 /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
83 devnull = open("/dev/null", O_RDWR);
85 if (devnull != STDIN_FILENO)
86 dup2(devnull, STDIN_FILENO);
87 if (devnull != STDOUT_FILENO)
88 dup2(devnull, STDOUT_FILENO);
89 if (devnull != STDERR_FILENO)
90 dup2(devnull, STDERR_FILENO);
91 if (devnull > STDERR_FILENO)
97 err("fatal, could not open /dev/null: %s", strerror(errno));
99 dbg("version %s", UDEV_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_ALARM_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 && argc == 2)
120 if (!action || !subsystem || !devpath) {
121 err("action, subsystem or devpath missing");
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);
133 udev_init_device(&udev, devpath, subsystem, action);
134 udev_rules_init(&rules, 0);
136 retval = udev_process_event(&rules, &udev);
138 if (!retval && udev_run && !list_empty(&udev.run_list)) {
139 struct name_entry *name_loop;
141 dbg("executing run list");
142 list_for_each_entry(name_loop, &udev.run_list, node) {
143 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
144 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
146 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
150 udev_rules_close(&rules);
151 udev_cleanup_device(&udev);