chiark / gitweb /
update default rules
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
6  *
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.
10  * 
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.
15  * 
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <syslog.h>
32
33 #include "udev.h"
34 #include "udev_rules.h"
35 #include "udev_selinux.h"
36
37 #ifdef USE_LOG
38 void log_message(int priority, const char *format, ...)
39 {
40         va_list args;
41
42         if (priority > udev_log_priority)
43                 return;
44
45         va_start(args, format);
46         vsyslog(priority, format, args);
47         va_end(args);
48 }
49 #endif
50
51 static void asmlinkage sig_handler(int signum)
52 {
53         switch (signum) {
54                 case SIGALRM:
55                         exit(1);
56                 case SIGINT:
57                 case SIGTERM:
58                         exit(20 + signum);
59         }
60 }
61
62 int main(int argc, char *argv[], char *envp[])
63 {
64         struct sysfs_device *dev;
65         struct udevice *udev;
66         const char *maj, *min;
67         struct udev_rules rules;
68         const char *action;
69         const char *devpath;
70         const char *subsystem;
71         struct sigaction act;
72         int devnull;
73         int retval = -EINVAL;
74
75         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
76                 printf("%s\n", UDEV_VERSION);
77                 exit(0);
78         }
79
80         /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
81         devnull = open("/dev/null", O_RDWR);
82         if (devnull >= 0)  {
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)
90                         close(devnull);
91         }
92
93         logging_init("udev");
94         if (devnull < 0)
95                 err("fatal, could not open /dev/null: %s", strerror(errno));
96         udev_config_init();
97         selinux_init();
98         dbg("version %s", UDEV_VERSION);
99
100         /* set signal handlers */
101         memset(&act, 0x00, sizeof(act));
102         act.sa_handler = (void (*)(int)) sig_handler;
103         sigemptyset (&act.sa_mask);
104         act.sa_flags = 0;
105         sigaction(SIGALRM, &act, NULL);
106         sigaction(SIGINT, &act, NULL);
107         sigaction(SIGTERM, &act, NULL);
108
109         /* trigger timeout to prevent hanging processes */
110         alarm(UDEV_ALARM_TIMEOUT);
111
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)
117                 subsystem = argv[1];
118
119         if (action == NULL || subsystem == NULL || devpath == NULL) {
120                 err("action, subsystem or devpath missing");
121                 goto exit;
122         }
123
124         /* export log_priority , as called programs may want to do the same as udev */
125         if (udev_log_priority) {
126                 char priority[32];
127
128                 sprintf(priority, "%i", udev_log_priority);
129                 setenv("UDEV_LOG", priority, 1);
130         }
131
132         sysfs_init();
133         udev_rules_init(&rules, 0);
134
135         dev = sysfs_device_get(devpath);
136         if (dev == NULL) {
137                 info("unable to open '%s'", devpath);
138                 goto fail;
139         }
140
141         udev = udev_device_init();
142         if (udev == NULL)
143                 goto fail;
144
145         /* override built-in sysfs device */
146         udev->dev = dev;
147         strlcpy(udev->action, action, sizeof(udev->action));
148
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));
154         else
155                 udev->devt = udev_device_get_devt(udev);
156
157         retval = udev_device_event(&rules, udev);
158
159         if (retval == 0 && !udev->ignore_device && udev_run) {
160                 struct name_entry *name_loop;
161
162                 dbg("executing run list");
163                 list_for_each_entry(name_loop, &udev->run_list, node) {
164                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
165                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
166                         else {
167                                 char program[PATH_SIZE];
168
169                                 strlcpy(program, name_loop->name, sizeof(program));
170                                 udev_rules_apply_format(udev, program, sizeof(program));
171                                 run_program(program, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
172                         }
173                 }
174         }
175
176         udev_device_cleanup(udev);
177 fail:
178         udev_rules_cleanup(&rules);
179         sysfs_cleanup();
180
181 exit:
182         logging_close();
183         if (retval != 0)
184                 return 1;
185         return 0;
186 }