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