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