chiark / gitweb /
[PATCH] Add a bunch of devices without "device" symlinks.
[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 /* timeout flag for udevdb */
42 extern sig_atomic_t gotalarm;
43
44 /* global variables */
45 char **main_argv;
46 char **main_envp;
47
48 #ifdef LOG
49 unsigned char logname[LOGNAME_SIZE];
50 void log_message(int level, const char *format, ...)
51 {
52         va_list args;
53
54         if (!udev_log)
55                 return;
56
57         va_start(args, format);
58         vsyslog(level, format, args);
59         va_end(args);
60 }
61 #endif
62
63 static void asmlinkage sig_handler(int signum)
64 {
65         switch (signum) {
66                 case SIGALRM:
67                         gotalarm = 1;
68                         info("error: timeout reached, event probably not handled correctly");
69                         break;
70                 case SIGINT:
71                 case SIGTERM:
72                         udevdb_exit();
73                         exit(20 + signum);
74                 default:
75                         dbg("unhandled signal %d", signum);
76         }
77 }
78
79 int main(int argc, char *argv[], char *envp[])
80 {
81         struct sigaction act;
82         struct sysfs_class_device *class_dev;
83         struct udevice udev;
84         char path[SYSFS_PATH_MAX];
85         int retval = -EINVAL;
86         enum {
87                 ADD,
88                 REMOVE,
89                 UDEVSTART,
90         } act_type;
91
92         dbg("version %s", UDEV_VERSION);
93
94         main_argv = argv;
95         main_envp = envp;
96
97         logging_init("udev");
98
99         udev_init_config();
100
101         if (strstr(argv[0], "udevstart")) {
102                 act_type = UDEVSTART;
103         } else {
104                 const char *action = get_action();
105                 const char *devpath = get_devpath();
106                 const char *subsystem = get_subsystem(main_argv[1]);
107
108                 if (!action) {
109                         dbg("no action?");
110                         goto exit;
111                 }
112                 if (strcmp(action, "add") == 0) {
113                         act_type = ADD;
114                 } else if (strcmp(action, "remove") == 0) {
115                         act_type = REMOVE;
116                 } else {
117                         dbg("no action '%s' for us", action);
118                         goto exit;
119                 }
120
121                 if (!devpath) {
122                         dbg("no devpath?");
123                         goto exit;
124                 }
125                 dbg("looking at '%s'", devpath);
126
127                 /* we only care about class devices and block stuff */
128                 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
129                         dbg("not a block or class device");
130                         goto exit;
131                 }
132
133                 if (!subsystem) {
134                         dbg("no subsystem");
135                         goto exit;
136                 }
137
138                 udev_set_values(&udev, devpath, subsystem);
139
140                 /* skip blacklisted subsystems */
141                 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
142                         dbg("don't care about '%s' devices", subsystem);
143                         goto exit;
144                 };
145
146         }
147
148         /* set signal handlers */
149         act.sa_handler = (void (*) (int))sig_handler;
150         sigemptyset (&act.sa_mask);
151         /* alarm must not restart syscalls*/
152         sigaction(SIGALRM, &act, NULL);
153         sigaction(SIGINT, &act, NULL);
154         sigaction(SIGTERM, &act, NULL);
155
156         /* trigger timout to interrupt blocking syscalls */
157         alarm(ALARM_TIMEOUT);
158
159         /* initialize udev database */
160         if (udevdb_init(UDEVDB_DEFAULT) != 0)
161                 info("error: unable to initialize database, continuing without database");
162
163         switch(act_type) {
164         case UDEVSTART:
165                 dbg("udevstart");
166                 namedev_init();
167                 retval = udev_start();
168                 break;
169         case ADD:
170                 dbg("udev add");
171
172                 /* open the device */
173                 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
174                 class_dev = sysfs_open_class_device_path(path);
175                 if (class_dev == NULL) {
176                         dbg ("sysfs_open_class_device_path failed");
177                         break;
178                 }
179                 dbg("opened class_dev->name='%s'", class_dev->name);
180
181                 /* init rules */
182                 namedev_init();
183
184                 /* name, create node, store in db */
185                 retval = udev_add_device(&udev, class_dev);
186
187                 /* run scripts */
188                 dev_d_execute(&udev);
189
190                 sysfs_close_class_device(class_dev);
191                 break;
192         case REMOVE:
193                 dbg("udev remove");
194
195                 /* get node from db, delete it*/
196                 retval = udev_remove_device(&udev);
197
198                 /* run scripts */
199                 dev_d_execute(&udev);
200         }
201
202         udevdb_exit();
203
204 exit:
205         logging_close();
206         return retval;
207 }