chiark / gitweb /
[PATCH] add NAME{ignore_remove} attribute
[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         if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) {
85                 act_type = UDEVSTART;
86         } else {
87                 const char *action = getenv("ACTION");
88                 const char *devpath = getenv("DEVPATH");
89                 const char *subsystem = argv[1];
90
91                 if (!action) {
92                         dbg("no action?");
93                         goto exit;
94                 }
95                 if (strcmp(action, "add") == 0) {
96                         act_type = ADD;
97                 } else if (strcmp(action, "remove") == 0) {
98                         act_type = REMOVE;
99                 } else {
100                         dbg("no action '%s' for us", action);
101                         goto exit;
102                 }
103
104                 if (!devpath) {
105                         dbg("no devpath?");
106                         goto exit;
107                 }
108                 dbg("looking at '%s'", devpath);
109
110                 /* we only care about class devices and block stuff */
111                 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
112                         dbg("not a block or class device");
113                         goto exit;
114                 }
115
116                 if (!subsystem) {
117                         dbg("no subsystem");
118                         goto exit;
119                 }
120
121                 udev_set_values(&udev, devpath, subsystem, action);
122
123                 /* skip blacklisted subsystems */
124                 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
125                         dbg("don't care about '%s' devices", subsystem);
126                         goto exit;
127                 };
128
129         }
130
131         /* set signal handlers */
132         act.sa_handler = (void (*) (int))sig_handler;
133         sigemptyset (&act.sa_mask);
134         act.sa_flags = 0;
135         /* alarm must not restart syscalls*/
136         sigaction(SIGALRM, &act, NULL);
137         sigaction(SIGINT, &act, NULL);
138         sigaction(SIGTERM, &act, NULL);
139
140         /* trigger timout to interrupt blocking syscalls */
141         alarm(ALARM_TIMEOUT);
142
143         switch(act_type) {
144         case UDEVSTART:
145                 dbg("udevstart");
146                 namedev_init();
147                 retval = udev_start();
148                 break;
149         case ADD:
150                 dbg("udev add");
151
152                 /* open the device */
153                 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
154                 class_dev = sysfs_open_class_device_path(path);
155                 if (class_dev == NULL) {
156                         dbg ("sysfs_open_class_device_path failed");
157                         break;
158                 }
159                 dbg("opened class_dev->name='%s'", class_dev->name);
160
161                 /* init rules */
162                 namedev_init();
163
164                 /* name, create node, store in db */
165                 retval = udev_add_device(&udev, class_dev);
166
167                 /* run dev.d/ scripts if we created a node or changed a netif name */
168                 if (udev.devname[0] != '\0') {
169                         setenv("DEVNAME", udev.devname, 1);
170                         dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
171                 }
172
173                 sysfs_close_class_device(class_dev);
174                 break;
175         case REMOVE:
176                 dbg("udev remove");
177
178                 /* get node from db, delete it*/
179                 retval = udev_remove_device(&udev);
180
181                 /* run scripts */
182                 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
183         }
184
185 exit:
186         logging_close();
187         return retval;
188 }