chiark / gitweb /
17eade221ec867a8420c56bc77f6b28c9c5f8a44
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <signal.h>
32
33 #include "udev.h"
34 #include "udev_version.h"
35 #include "udev_dbus.h"
36 #include "namedev.h"
37 #include "udevdb.h"
38 #include "libsysfs/libsysfs.h"
39
40 /* global variables */
41 char **main_argv;
42 char **main_envp;
43
44 static void sig_handler(int signum)
45 {
46         dbg("caught signal %d", signum);
47         switch (signum) {
48                 case SIGINT:
49                 case SIGTERM:
50                 case SIGKILL:
51                         sysbus_disconnect();
52                         udevdb_exit();
53                         exit(20 + signum);
54                         break;
55                 default:
56                         dbg("unhandled signal");
57         }
58 }
59
60 static inline char *get_action(void)
61 {
62         char *action;
63
64         action = getenv("ACTION");
65         return action;
66 }
67
68 static inline char *get_devpath(void)
69 {
70         char *devpath;
71
72         devpath = getenv("DEVPATH");
73         return devpath;
74 }
75
76 static inline char *get_seqnum(void)
77 {
78         char *seqnum;
79
80         seqnum = getenv("SEQNUM");
81         return seqnum;
82 }
83
84 int main(int argc, char **argv, char **envp)
85 {
86         char *action;
87         char *devpath;
88         char *subsystem;
89         int retval = -EINVAL;
90
91         main_argv = argv;
92         main_envp = envp;
93
94         dbg("version %s", UDEV_VERSION);
95
96         if (argc != 2) {
97                 dbg ("unknown number of arguments");
98                 goto exit;
99         }
100
101         subsystem = argv[1];
102
103         devpath = get_devpath();
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") &&
112             !strstr(devpath, "block")) {
113                 dbg("not a block or class device");
114                 goto exit;
115         }
116
117         /* but we don't care about net class devices */
118         if (strcmp(subsystem, "net") == 0) {
119                 dbg("don't care about net devices");
120                 goto exit;
121         }
122
123         action = get_action();
124         if (!action) {
125                 dbg ("no action?");
126                 goto exit;
127         }
128
129         /* initialize our configuration */
130         udev_init_config();
131
132         /* connect to the system message bus */
133         sysbus_connect();
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         signal(SIGKILL, sig_handler);
146
147         /* initialize the naming deamon */
148         namedev_init();
149
150         if (strcmp(action, "add") == 0)
151                 retval = udev_add_device(devpath, subsystem);
152
153         else if (strcmp(action, "remove") == 0)
154                 retval = udev_remove_device(devpath, subsystem);
155
156         else {
157                 dbg("unknown action '%s'", action);
158                 retval = -EINVAL;
159         }
160         udevdb_exit();
161
162 exit_sysbus:
163         /* disconnect from the system message bus */
164         sysbus_disconnect();
165
166 exit:
167         return retval;
168 }