]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev.c
[PATCH] rework the logging code so that each program logs with the proper name in...
[thirdparty/systemd.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 static void sig_handler(int signum)
44 {
45 dbg("caught signal %d", signum);
46 switch (signum) {
47 case SIGINT:
48 case SIGTERM:
49 sysbus_disconnect();
50 udevdb_exit();
51 exit(20 + signum);
52 break;
53 default:
54 dbg("unhandled signal");
55 }
56 }
57
58 static inline char *get_action(void)
59 {
60 char *action;
61
62 action = getenv("ACTION");
63 return action;
64 }
65
66 static inline char *get_devpath(void)
67 {
68 char *devpath;
69
70 devpath = getenv("DEVPATH");
71 return devpath;
72 }
73
74 static inline char *get_seqnum(void)
75 {
76 char *seqnum;
77
78 seqnum = getenv("SEQNUM");
79 return seqnum;
80 }
81
82 static char *subsystem_blacklist[] = {
83 "net",
84 "scsi_host",
85 "scsi_device",
86 "usb_host",
87 "pci_bus",
88 "",
89 };
90
91 static int udev_hotplug(int argc, char **argv)
92 {
93 char *action;
94 char *devpath;
95 char *subsystem;
96 int retval = -EINVAL;
97 int i;
98
99 action = get_action();
100 if (!action) {
101 dbg ("no action?");
102 goto exit;
103 }
104
105 devpath = get_devpath();
106 if (!devpath) {
107 dbg ("no devpath?");
108 goto exit;
109 }
110 dbg("looking at '%s'", devpath);
111
112 /* we only care about class devices and block stuff */
113 if (!strstr(devpath, "class") &&
114 !strstr(devpath, "block")) {
115 dbg("not a block or class device");
116 goto exit;
117 }
118
119 /* skip blacklisted subsystems */
120 subsystem = argv[1];
121 i = 0;
122 while (subsystem_blacklist[i][0] != '\0') {
123 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
124 dbg("don't care about '%s' devices", subsystem);
125 goto exit;
126 }
127 i++;
128 }
129
130 /* connect to the system message bus */
131 sysbus_connect();
132
133 /* initialize our configuration */
134 udev_init_config();
135
136 /* initialize udev database */
137 retval = udevdb_init(UDEVDB_DEFAULT);
138 if (retval != 0) {
139 dbg("unable to initialize database");
140 goto exit_sysbus;
141 }
142
143 /* set up a default signal handler for now */
144 signal(SIGINT, sig_handler);
145 signal(SIGTERM, 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 if (retval > 0)
168 retval = 0;
169
170 return -retval;
171 }
172
173 int main(int argc, char **argv, char **envp)
174 {
175 main_argv = argv;
176 main_envp = envp;
177
178 init_logging("udev");
179 dbg("version %s", UDEV_VERSION);
180
181 return udev_hotplug(argc, argv);
182 }
183
184