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