chiark / gitweb /
[PATCH] udev - fix "ignore method"
[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 <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <signal.h>
29
30 #include "udev.h"
31 #include "udev_version.h"
32 #include "udev_dbus.h"
33 #include "logging.h"
34 #include "namedev.h"
35 #include "udevdb.h"
36 #include "libsysfs/libsysfs.h"
37
38 /* global variables */
39 char **main_argv;
40 char **main_envp;
41 unsigned char logname[42];
42
43 int log_ok(void)
44 {
45         return udev_log;
46 }
47
48 static void sig_handler(int signum)
49 {
50         dbg("caught signal %d", signum);
51         switch (signum) {
52                 case SIGINT:
53                 case SIGTERM:
54                         sysbus_disconnect();
55                         udevdb_exit();
56                         exit(20 + signum);
57                         break;
58                 default:
59                         dbg("unhandled signal");
60         }
61 }
62
63 static inline char *get_action(void)
64 {
65         char *action;
66
67         action = getenv("ACTION");
68         return action;
69 }
70
71 static inline char *get_devpath(void)
72 {
73         char *devpath;
74
75         devpath = getenv("DEVPATH");
76         return devpath;
77 }
78
79 static inline char *get_seqnum(void)
80 {
81         char *seqnum;
82
83         seqnum = getenv("SEQNUM");
84         return seqnum;
85 }
86
87 static char *subsystem_blacklist[] = {
88         "net",
89         "scsi_host",
90         "scsi_device",
91         "usb_host",
92         "pci_bus",
93         "",
94 };
95
96 static int udev_hotplug(int argc, char **argv)
97 {
98         char *action;
99         char *devpath;
100         char *subsystem;
101         int retval = -EINVAL;
102         int i;
103
104         action = get_action();
105         if (!action) {
106                 dbg ("no action?");
107                 goto exit;
108         }
109
110         devpath = get_devpath();
111         if (!devpath) {
112                 dbg ("no devpath?");
113                 goto exit;
114         }
115         dbg("looking at '%s'", devpath);
116
117         /* we only care about class devices and block stuff */
118         if (!strstr(devpath, "class") &&
119             !strstr(devpath, "block")) {
120                 dbg("not a block or class device");
121                 goto exit;
122         }
123
124         /* skip blacklisted subsystems */
125         subsystem = argv[1];
126         i = 0;
127         while (subsystem_blacklist[i][0] != '\0') {
128                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
129                         dbg("don't care about '%s' devices", subsystem);
130                         goto exit;
131                 }
132                 i++;
133         }
134
135         /* connect to the system message bus */
136         sysbus_connect();
137
138         /* initialize our configuration */
139         udev_init_config();
140
141         /* initialize udev database */
142         retval = udevdb_init(UDEVDB_DEFAULT);
143         if (retval != 0) {
144                 dbg("unable to initialize database");
145                 goto exit_sysbus;
146         }
147
148         /* set up a default signal handler for now */
149         signal(SIGINT, sig_handler);
150         signal(SIGTERM, sig_handler);
151
152         /* initialize the naming deamon */
153         namedev_init();
154
155         if (strcmp(action, "add") == 0)
156                 retval = udev_add_device(devpath, subsystem);
157
158         else if (strcmp(action, "remove") == 0)
159                 retval = udev_remove_device(devpath, subsystem);
160
161         else {
162                 dbg("unknown action '%s'", action);
163                 retval = -EINVAL;
164         }
165         udevdb_exit();
166
167 exit_sysbus:
168         /* disconnect from the system message bus */
169         sysbus_disconnect();
170
171 exit:
172         if (retval > 0)
173                 retval = 0;
174
175         return -retval;
176 }
177
178 int main(int argc, char **argv, char **envp)
179 {
180         main_argv = argv;
181         main_envp = envp;
182
183         init_logging("udev");
184         dbg("version %s", UDEV_VERSION);
185
186         return udev_hotplug(argc, argv);
187 }
188
189