]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev.c
[PATCH] add cdsymlinks.sh support to gentoo rules file.
[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 <stdio.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include "libsysfs/sysfs/libsysfs.h"
33 #include "udev.h"
34 #include "udev_lib.h"
35 #include "udev_sysfs.h"
36 #include "udev_version.h"
37 #include "logging.h"
38 #include "namedev.h"
39 #include "udevdb.h"
40
41 /* timeout flag for udevdb */
42 extern sig_atomic_t gotalarm;
43
44 /* global variables */
45 char **main_argv;
46 char **main_envp;
47
48 #ifdef LOG
49 unsigned char logname[LOGNAME_SIZE];
50 void log_message(int level, const char *format, ...)
51 {
52 va_list args;
53
54 if (!udev_log)
55 return;
56
57 va_start(args, format);
58 vsyslog(level, format, args);
59 va_end(args);
60 }
61 #endif
62
63 static void asmlinkage sig_handler(int signum)
64 {
65 switch (signum) {
66 case SIGALRM:
67 gotalarm = 1;
68 break;
69 case SIGINT:
70 case SIGTERM:
71 exit(20 + signum);
72 }
73 }
74
75 int main(int argc, char *argv[], char *envp[])
76 {
77 struct sigaction act;
78 struct sysfs_class_device *class_dev;
79 struct udevice udev;
80 char path[SYSFS_PATH_MAX];
81 int retval = -EINVAL;
82 enum {
83 ADD,
84 REMOVE,
85 UDEVSTART,
86 } act_type;
87
88 dbg("version %s", UDEV_VERSION);
89
90 main_argv = argv;
91 main_envp = envp;
92
93 logging_init("udev");
94
95 udev_init_config();
96
97 if (strstr(argv[0], "udevstart")) {
98 act_type = UDEVSTART;
99 } else {
100 const char *action = get_action();
101 const char *devpath = get_devpath();
102 const char *subsystem = get_subsystem(main_argv[1]);
103
104 if (!action) {
105 dbg("no action?");
106 goto exit;
107 }
108 if (strcmp(action, "add") == 0) {
109 act_type = ADD;
110 } else if (strcmp(action, "remove") == 0) {
111 act_type = REMOVE;
112 } else {
113 dbg("no action '%s' for us", action);
114 goto exit;
115 }
116
117 if (!devpath) {
118 dbg("no devpath?");
119 goto exit;
120 }
121 dbg("looking at '%s'", devpath);
122
123 /* we only care about class devices and block stuff */
124 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
125 dbg("not a block or class device");
126 goto exit;
127 }
128
129 if (!subsystem) {
130 dbg("no subsystem");
131 goto exit;
132 }
133
134 udev_set_values(&udev, devpath, subsystem);
135
136 /* skip blacklisted subsystems */
137 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
138 dbg("don't care about '%s' devices", subsystem);
139 goto exit;
140 };
141
142 }
143
144 /* set signal handlers */
145 act.sa_handler = (void (*) (int))sig_handler;
146 sigemptyset (&act.sa_mask);
147 act.sa_flags = 0;
148 /* alarm must not restart syscalls*/
149 sigaction(SIGALRM, &act, NULL);
150 sigaction(SIGINT, &act, NULL);
151 sigaction(SIGTERM, &act, NULL);
152
153 /* trigger timout to interrupt blocking syscalls */
154 alarm(ALARM_TIMEOUT);
155
156 /* initialize udev database */
157 if (udevdb_init(UDEVDB_DEFAULT) != 0)
158 info("error: unable to initialize database, continuing without database");
159
160 switch(act_type) {
161 case UDEVSTART:
162 dbg("udevstart");
163 namedev_init();
164 retval = udev_start();
165 break;
166 case ADD:
167 dbg("udev add");
168
169 /* open the device */
170 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
171 class_dev = sysfs_open_class_device_path(path);
172 if (class_dev == NULL) {
173 dbg ("sysfs_open_class_device_path failed");
174 break;
175 }
176 dbg("opened class_dev->name='%s'", class_dev->name);
177
178 /* init rules */
179 namedev_init();
180
181 /* name, create node, store in db */
182 retval = udev_add_device(&udev, class_dev);
183
184 /* run scripts */
185 dev_d_execute(&udev);
186
187 sysfs_close_class_device(class_dev);
188 break;
189 case REMOVE:
190 dbg("udev remove");
191
192 /* get node from db, delete it*/
193 retval = udev_remove_device(&udev);
194
195 /* run scripts */
196 dev_d_execute(&udev);
197 }
198
199 udevdb_exit();
200
201 exit:
202 logging_close();
203 return retval;
204 }