chiark / gitweb /
480a1cdca224e595ff171898affaf9e2858bb54f
[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  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include "libsysfs/sysfs/libsysfs.h"
33 #include "udev.h"
34 #include "udev_lib.h"
35 #include "udev_sysfs.h"
36 #include "udev_version.h"
37 #include "logging.h"
38 #include "namedev.h"
39 #include "udevdb.h"
40
41
42 /* global variables */
43 char **main_argv;
44 char **main_envp;
45
46 #ifdef LOG
47 unsigned char logname[LOGNAME_SIZE];
48 void log_message(int level, const char *format, ...)
49 {
50         va_list args;
51
52         if (!udev_log)
53                 return;
54
55         va_start(args, format);
56         vsyslog(level, format, args);
57         va_end(args);
58 }
59 #endif
60
61 static void asmlinkage sig_handler(int signum)
62 {
63         switch (signum) {
64                 case SIGALRM:
65                         exit(1);
66                 case SIGINT:
67                 case SIGTERM:
68                         exit(20 + signum);
69         }
70 }
71
72 int main(int argc, char *argv[], char *envp[])
73 {
74         struct sigaction act;
75         struct sysfs_class_device *class_dev;
76         struct udevice udev;
77         char path[SYSFS_PATH_MAX];
78         int retval = -EINVAL;
79         enum {
80                 ADD,
81                 REMOVE,
82                 UDEVSTART,
83         } act_type;
84
85         dbg("version %s", UDEV_VERSION);
86
87         main_argv = argv;
88         main_envp = envp;
89
90         logging_init("udev");
91
92         udev_init_config();
93
94         if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) {
95                 act_type = UDEVSTART;
96         } else {
97                 const char *action = getenv("ACTION");
98                 const char *devpath = getenv("DEVPATH");
99                 const char *subsystem = argv[1];
100
101                 if (!action) {
102                         dbg("no action?");
103                         goto exit;
104                 }
105                 if (strcmp(action, "add") == 0) {
106                         act_type = ADD;
107                 } else if (strcmp(action, "remove") == 0) {
108                         act_type = REMOVE;
109                 } else {
110                         dbg("no action '%s' for us", action);
111                         goto exit;
112                 }
113
114                 if (!devpath) {
115                         dbg("no devpath?");
116                         goto exit;
117                 }
118                 dbg("looking at '%s'", devpath);
119
120                 /* we only care about class devices and block stuff */
121                 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
122                         dbg("not a block or class device");
123                         goto exit;
124                 }
125
126                 if (!subsystem) {
127                         dbg("no subsystem");
128                         goto exit;
129                 }
130
131                 udev_set_values(&udev, devpath, subsystem, action);
132
133                 /* skip blacklisted subsystems */
134                 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
135                         dbg("don't care about '%s' devices", subsystem);
136                         goto exit;
137                 };
138
139         }
140
141         /* set signal handlers */
142         act.sa_handler = (void (*) (int))sig_handler;
143         sigemptyset (&act.sa_mask);
144         act.sa_flags = 0;
145         /* alarm must not restart syscalls*/
146         sigaction(SIGALRM, &act, NULL);
147         sigaction(SIGINT, &act, NULL);
148         sigaction(SIGTERM, &act, NULL);
149
150         /* trigger timout to interrupt blocking syscalls */
151         alarm(ALARM_TIMEOUT);
152
153         switch(act_type) {
154         case UDEVSTART:
155                 dbg("udevstart");
156                 namedev_init();
157                 retval = udev_start();
158                 break;
159         case ADD:
160                 dbg("udev add");
161
162                 /* open the device */
163                 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
164                 class_dev = sysfs_open_class_device_path(path);
165                 if (class_dev == NULL) {
166                         dbg ("sysfs_open_class_device_path failed");
167                         break;
168                 }
169                 dbg("opened class_dev->name='%s'", class_dev->name);
170
171                 /* init rules */
172                 namedev_init();
173
174                 /* name, create node, store in db */
175                 retval = udev_add_device(&udev, class_dev);
176
177                 /* run scripts */
178                 dev_d_execute(&udev);
179
180                 sysfs_close_class_device(class_dev);
181                 break;
182         case REMOVE:
183                 dbg("udev remove");
184
185                 /* get node from db, delete it*/
186                 retval = udev_remove_device(&udev);
187
188                 /* run scripts */
189                 dev_d_execute(&udev);
190         }
191
192 exit:
193         logging_close();
194         return retval;
195 }