chiark / gitweb /
Fix ChangeLog titles
[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         char path[PATH_SIZE];
72         const char *error;
73         const char *action;
74         const char *devpath;
75         const char *subsystem;
76         struct sigaction act;
77         int retval = -EINVAL;
78
79         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
80                 printf("%s\n", UDEV_VERSION);
81                 exit(0);
82         }
83
84         logging_init("udev");
85         udev_init_config();
86         dbg("version %s", UDEV_VERSION);
87
88         /* set signal handlers */
89         memset(&act, 0x00, sizeof(act));
90         act.sa_handler = (void (*)(int)) sig_handler;
91         sigemptyset (&act.sa_mask);
92         act.sa_flags = 0;
93         sigaction(SIGALRM, &act, NULL);
94         sigaction(SIGINT, &act, NULL);
95         sigaction(SIGTERM, &act, NULL);
96
97         /* trigger timeout to prevent hanging processes */
98         alarm(ALARM_TIMEOUT);
99
100         action = getenv("ACTION");
101         devpath = getenv("DEVPATH");
102         subsystem = getenv("SUBSYSTEM");
103         /* older kernels passed the SUBSYSTEM only as argument */
104         if (!subsystem && argc == 2)
105                 subsystem = argv[1];
106
107         if (!action || !subsystem || !devpath) {
108                 err("action, subsystem or devpath missing");
109                 goto exit;
110         }
111
112         /* export log_priority , as called programs may want to do the same as udev */
113         if (udev_log_priority) {
114                 char priority[32];
115
116                 sprintf(priority, "%i", udev_log_priority);
117                 setenv("UDEV_LOG", priority, 1);
118         }
119
120         udev_init_device(&udev, devpath, subsystem, action);
121         udev_rules_init();
122
123         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
124                 /* handle device node */
125                 if (strcmp(action, "add") == 0) {
126                         struct sysfs_class_device *class_dev;
127
128                         /* wait for sysfs of /sys/class /sys/block */
129                         dbg("node add");
130                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
131                         path[sizeof(path)-1] = '\0';
132                         class_dev = wait_class_device_open(path);
133                         if (class_dev == NULL) {
134                                 dbg("open class device failed");
135                                 goto run;
136                         }
137                         dbg("opened class_dev->name='%s'", class_dev->name);
138                         wait_for_class_device(class_dev, &error);
139
140                         /* get major/minor */
141                         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
142                                 udev.devt = get_devt(class_dev);
143
144                         if (udev.type == DEV_NET || udev.devt) {
145                                 /* name device */
146                                 udev_rules_get_name(&udev, class_dev);
147                                 if (udev.ignore_device) {
148                                         info("device event will be ignored");
149                                         goto cleanup;
150                                 }
151                                 if (udev.name[0] == '\0') {
152                                         info("device node creation supressed");
153                                         goto cleanup;
154                                 }
155                                 
156                                 /* create node, store in db */
157                                 retval = udev_add_device(&udev, class_dev);
158                         } else {
159                                 dbg("no dev-file found");
160                                 udev_rules_get_run(&udev, NULL);
161                                 if (udev.ignore_device) {
162                                         info("device event will be ignored");
163                                         goto cleanup;
164                                 }
165                         }
166                         sysfs_close_class_device(class_dev);
167                 } else if (strcmp(action, "remove") == 0) {
168                         dbg("node remove");
169                         udev_rules_get_run(&udev, NULL);
170                         if (udev.ignore_device) {
171                                 dbg("device event will be ignored");
172                                 goto cleanup;
173                         }
174
175                         /* get name from db, remove db-entry, delete node */
176                         retval = udev_remove_device(&udev);
177                 }
178
179                 /* export name of device node or netif */
180                 if (udev.devname[0] != '\0')
181                         setenv("DEVNAME", udev.devname, 1);
182         } else if (udev.type == DEV_DEVICE && strcmp(action, "add") == 0) {
183                 struct sysfs_device *devices_dev;
184
185                 /* wait for sysfs of /sys/devices/ */
186                 dbg("devices add");
187                 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
188                 path[sizeof(path)-1] = '\0';
189                 devices_dev = wait_devices_device_open(path);
190                 if (!devices_dev) {
191                         dbg("devices device unavailable (probably remove has beaten us)");
192                         goto run;
193                 }
194                 dbg("devices device opened '%s'", path);
195                 wait_for_devices_device(devices_dev, &error);
196                 udev_rules_get_run(&udev, devices_dev);
197                 sysfs_close_device(devices_dev);
198                 if (udev.ignore_device) {
199                         info("device event will be ignored");
200                         goto cleanup;
201                 }
202         } else {
203                 dbg("default handling");
204                 udev_rules_get_run(&udev, NULL);
205                 if (udev.ignore_device) {
206                         info("device event will be ignored");
207                         goto cleanup;
208                 }
209         }
210
211 run:
212         if (udev_run && !list_empty(&udev.run_list)) {
213                 struct name_entry *name_loop;
214
215                 dbg("executing run list");
216                 list_for_each_entry(name_loop, &udev.run_list, node)
217                         execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
218         }
219
220 cleanup:
221         udev_cleanup_device(&udev);
222
223 exit:
224         logging_close();
225         return retval;
226 }