chiark / gitweb /
6e0f7f1d0c535389c0ff56b1c2cbdc35213f1667
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <unistd.h>
33
34 #include "libsysfs/sysfs/libsysfs.h"
35 #include "udev_libc_wrapper.h"
36 #include "udev.h"
37 #include "udev_utils.h"
38 #include "udev_sysfs.h"
39 #include "udev_version.h"
40 #include "udev_rules.h"
41 #include "logging.h"
42
43 #ifdef USE_LOG
44 void log_message(int priority, const char *format, ...)
45 {
46         va_list args;
47
48         if (priority > udev_log_priority)
49                 return;
50
51         va_start(args, format);
52         vsyslog(priority, format, args);
53         va_end(args);
54 }
55 #endif
56
57 static void asmlinkage sig_handler(int signum)
58 {
59         switch (signum) {
60                 case SIGALRM:
61                         exit(1);
62                 case SIGINT:
63                 case SIGTERM:
64                         exit(20 + signum);
65         }
66 }
67
68 int main(int argc, char *argv[], char *envp[])
69 {
70         struct udevice udev;
71         struct udev_rules rules;
72         char path[PATH_SIZE];
73         const char *error;
74         const char *action;
75         const char *devpath;
76         const char *subsystem;
77         struct sigaction act;
78         int retval = -EINVAL;
79
80         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
81                 printf("%s\n", UDEV_VERSION);
82                 exit(0);
83         }
84
85         logging_init("udev");
86         udev_init_config();
87         dbg("version %s", UDEV_VERSION);
88
89         /* set signal handlers */
90         memset(&act, 0x00, sizeof(act));
91         act.sa_handler = (void (*)(int)) sig_handler;
92         sigemptyset (&act.sa_mask);
93         act.sa_flags = 0;
94         sigaction(SIGALRM, &act, NULL);
95         sigaction(SIGINT, &act, NULL);
96         sigaction(SIGTERM, &act, NULL);
97
98         /* trigger timeout to prevent hanging processes */
99         alarm(UDEV_ALARM_TIMEOUT);
100
101         action = getenv("ACTION");
102         devpath = getenv("DEVPATH");
103         subsystem = getenv("SUBSYSTEM");
104         /* older kernels passed the SUBSYSTEM only as argument */
105         if (!subsystem && argc == 2)
106                 subsystem = argv[1];
107
108         if (!action || !subsystem || !devpath) {
109                 err("action, subsystem or devpath missing");
110                 goto exit;
111         }
112
113         /* export log_priority , as called programs may want to do the same as udev */
114         if (udev_log_priority) {
115                 char priority[32];
116
117                 sprintf(priority, "%i", udev_log_priority);
118                 setenv("UDEV_LOG", priority, 1);
119         }
120
121         udev_init_device(&udev, devpath, subsystem, action);
122         udev_rules_init(&rules, 0);
123
124         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
125                 /* handle device node */
126                 if (strcmp(action, "add") == 0) {
127                         struct sysfs_class_device *class_dev;
128
129                         /* wait for sysfs of /sys/class /sys/block */
130                         dbg("node add");
131                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
132                         path[sizeof(path)-1] = '\0';
133                         class_dev = wait_class_device_open(path);
134                         if (class_dev == NULL) {
135                                 dbg("open class device failed");
136                                 goto run;
137                         }
138                         dbg("opened class_dev->name='%s'", class_dev->name);
139                         wait_for_class_device(class_dev, &error);
140
141                         /* get major/minor */
142                         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
143                                 udev.devt = get_devt(class_dev);
144
145                         if (udev.type == DEV_NET || udev.devt) {
146                                 /* name device */
147                                 udev_rules_get_name(&rules, &udev, class_dev);
148                                 if (udev.ignore_device) {
149                                         info("device event will be ignored");
150                                         sysfs_close_class_device(class_dev);
151                                         goto cleanup;
152                                 }
153                                 if (udev.name[0] == '\0') {
154                                         info("device node creation supressed");
155                                         sysfs_close_class_device(class_dev);
156                                         goto cleanup;
157                                 }
158                                 /* create node, store in db */
159                                 retval = udev_add_device(&udev, class_dev);
160                         } else {
161                                 dbg("no dev-file found");
162                                 udev_rules_get_run(&rules, &udev, class_dev, NULL);
163                                 if (udev.ignore_device) {
164                                         info("device event will be ignored");
165                                         sysfs_close_class_device(class_dev);
166                                         goto cleanup;
167                                 }
168                         }
169                         sysfs_close_class_device(class_dev);
170                 } else if (strcmp(action, "remove") == 0) {
171                         dbg("node remove");
172                         udev_rules_get_run(&rules, &udev, NULL, NULL);
173                         if (udev.ignore_device) {
174                                 dbg("device event will be ignored");
175                                 goto cleanup;
176                         }
177
178                         /* get name from db, remove db-entry, delete node */
179                         retval = udev_remove_device(&udev);
180                 }
181
182                 /* export name of device node or netif */
183                 if (udev.devname[0] != '\0')
184                         setenv("DEVNAME", udev.devname, 1);
185         } else if (udev.type == DEV_DEVICE && strcmp(action, "add") == 0) {
186                 struct sysfs_device *devices_dev;
187
188                 /* wait for sysfs of /sys/devices/ */
189                 dbg("devices add");
190                 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
191                 path[sizeof(path)-1] = '\0';
192                 devices_dev = wait_devices_device_open(path);
193                 if (!devices_dev) {
194                         dbg("devices device unavailable (probably remove has beaten us)");
195                         goto run;
196                 }
197                 dbg("devices device opened '%s'", path);
198                 wait_for_devices_device(devices_dev, &error);
199                 udev_rules_get_run(&rules, &udev, NULL, devices_dev);
200                 sysfs_close_device(devices_dev);
201                 if (udev.ignore_device) {
202                         info("device event will be ignored");
203                         goto cleanup;
204                 }
205         } else {
206                 dbg("default handling");
207                 udev_rules_get_run(&rules, &udev, NULL, NULL);
208                 if (udev.ignore_device) {
209                         info("device event will be ignored");
210                         goto cleanup;
211                 }
212         }
213
214 run:
215         if (udev_run && !list_empty(&udev.run_list)) {
216                 struct name_entry *name_loop;
217
218                 dbg("executing run list");
219                 list_for_each_entry(name_loop, &udev.run_list, node)
220                         execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
221         }
222
223 cleanup:
224         udev_cleanup_device(&udev);
225
226 exit:
227         logging_close();
228         return retval;
229 }