chiark / gitweb /
[PATCH] introduce signal handler
[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         signal(SIGINT, sig_handler);
92         signal(SIGTERM, sig_handler);
93         signal(SIGKILL, sig_handler);
94
95         main_argv = argv;
96         main_envp = envp;
97
98         dbg("version %s", UDEV_VERSION);
99
100         if (argc != 2) {
101                 dbg ("unknown number of arguments");
102                 goto exit;
103         }
104
105         subsystem = argv[1];
106
107         devpath = get_devpath();
108         if (!devpath) {
109                 dbg ("no devpath?");
110                 goto exit;
111         }
112         dbg("looking at '%s'", devpath);
113
114         /* we only care about class devices and block stuff */
115         if (!strstr(devpath, "class") &&
116             !strstr(devpath, "block")) {
117                 dbg("not a block or class device");
118                 goto exit;
119         }
120
121         /* but we don't care about net class devices */
122         if (strcmp(subsystem, "net") == 0) {
123                 dbg("don't care about net devices");
124                 goto exit;
125         }
126
127         action = get_action();
128         if (!action) {
129                 dbg ("no action?");
130                 goto exit;
131         }
132
133         /* initialize our configuration */
134         udev_init_config();
135
136         /* connect to the system message bus */
137         sysbus_connect();
138
139         /* initialize udev database */
140         retval = udevdb_init(UDEVDB_DEFAULT);
141         if (retval != 0) {
142                 dbg("unable to initialize database");
143                 goto exit_sysbus;
144         }
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         return retval;
167 }