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.
32 #include "udev_rules.h"
33 #include "udev_selinux.h"
36 void log_message(int priority, const char *format, ...)
40 if (priority > udev_log_priority)
43 va_start(args, format);
44 vsyslog(priority, format, args);
49 static void asmlinkage sig_handler(int signum)
60 int main(int argc, char *argv[], char *envp[])
62 struct sysfs_device *dev;
64 const char *maj, *min;
65 struct udev_rules rules;
68 const char *subsystem;
73 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
74 printf("%s\n", UDEV_VERSION);
78 /* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
79 devnull = open("/dev/null", O_RDWR);
81 if (devnull != STDIN_FILENO)
82 dup2(devnull, STDIN_FILENO);
83 if (devnull != STDOUT_FILENO)
84 dup2(devnull, STDOUT_FILENO);
85 if (devnull != STDERR_FILENO)
86 dup2(devnull, STDERR_FILENO);
87 if (devnull > STDERR_FILENO)
93 err("open /dev/null failed: %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 == 0 && !udev->ignore_device && udev_run) {
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 char program[PATH_SIZE];
167 strlcpy(program, name_loop->name, sizeof(program));
168 udev_rules_apply_format(udev, program, sizeof(program));
169 run_program(program, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
174 udev_device_cleanup(udev);
176 udev_rules_cleanup(&rules);