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