]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev.c
[PATCH] prevent deadlocks on an corrupt udev database
[thirdparty/systemd.git] / udev.c
CommitLineData
f0083e3d
GKH
1/*
2 * udev.c
3 *
4 * Userspace devfs
5 *
8202eb32 6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
f0083e3d
GKH
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
c81b35c0
KS
23#include <stdio.h>
24#include <stddef.h>
f0083e3d
GKH
25#include <stdlib.h>
26#include <string.h>
e8baccca 27#include <ctype.h>
c81b35c0 28#include <errno.h>
d12ecb53 29#include <signal.h>
85511f02 30
c80da508 31#include "libsysfs/sysfs/libsysfs.h"
f0083e3d 32#include "udev.h"
c81b35c0 33#include "udev_lib.h"
f0083e3d 34#include "udev_version.h"
54988802 35#include "logging.h"
2232cac8 36#include "namedev.h"
dbfc520c 37#include "udevdb.h"
f0083e3d 38
7e89a569
KS
39/* timeout flag for udevdb */
40extern sig_atomic_t gotalarm;
41
c056c514
GKH
42/* global variables */
43char **main_argv;
44char **main_envp;
45
d026a35d 46#ifdef LOG
d00bd172 47unsigned char logname[LOGNAME_SIZE];
3fe07342 48void log_message(int level, const char *format, ...)
51a8bb2f 49{
e964c2c0 50 va_list args;
d026a35d
GKH
51
52 if (!udev_log)
53 return;
54
55 va_start(args, format);
56 vsyslog(level, format, args);
57 va_end(args);
51a8bb2f 58}
d026a35d 59#endif
51a8bb2f 60
80775577 61asmlinkage static void sig_handler(int signum)
d12ecb53 62{
d12ecb53 63 switch (signum) {
7e89a569
KS
64 case SIGALRM:
65 gotalarm = 1;
66 info("error: timeout reached, event probably not handled correctly");
67 break;
d12ecb53
MB
68 case SIGINT:
69 case SIGTERM:
d12ecb53
MB
70 udevdb_exit();
71 exit(20 + signum);
d12ecb53 72 default:
47bf9196 73 dbg("unhandled signal %d", signum);
d12ecb53
MB
74 }
75}
76
8eb38ef8 77static char *subsystem_blacklist[] = {
8eb38ef8
GKH
78 "scsi_host",
79 "scsi_device",
f130b156
GKH
80 "usb_host",
81 "pci_bus",
e9f504e8 82 "pcmcia_socket",
d00bd172 83 ""
8eb38ef8
GKH
84};
85
aee380b6 86int main(int argc, char *argv[], char *envp[])
f4dc8d11 87{
aee380b6
KS
88 main_argv = argv;
89 main_envp = envp;
f8911dbb 90 struct sigaction act;
aee380b6
KS
91 char *action;
92 char *devpath = "";
93 char *subsystem = "";
94 int i;
95 int retval = -EINVAL;
96 enum {
97 ADD,
98 REMOVE,
99 UDEVSTART,
100 } act_type;
f4dc8d11 101
aee380b6 102 dbg("version %s", UDEV_VERSION);
2232cac8 103
7e89a569
KS
104 init_logging("udev");
105
aee380b6 106 udev_init_config();
af815f88 107
aee380b6
KS
108 if (strstr(argv[0], "udevstart")) {
109 act_type = UDEVSTART;
110 } else {
111 action = get_action();
112 if (!action) {
113 dbg("no action?");
114 goto exit;
115 }
116 if (strcmp(action, "add") == 0) {
117 act_type = ADD;
118 } else if (strcmp(action, "remove") == 0) {
119 act_type = REMOVE;
120 } else {
121 dbg("unknown action '%s'", action);
8eb38ef8
GKH
122 goto exit;
123 }
f0083e3d 124
aee380b6
KS
125 devpath = get_devpath();
126 if (!devpath) {
127 dbg("no devpath?");
128 goto exit;
129 }
130 dbg("looking at '%s'", devpath);
131
132 /* we only care about class devices and block stuff */
6d74f996 133 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
aee380b6
KS
134 dbg("not a block or class device");
135 goto exit;
136 }
137
138 subsystem = get_subsystem(main_argv[1]);
139 if (!subsystem) {
140 dbg("no subsystem?");
141 goto exit;
142 }
143
144 /* skip blacklisted subsystems */
145 i = 0;
146 while (subsystem_blacklist[i][0] != '\0') {
147 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
148 dbg("don't care about '%s' devices", subsystem);
149 goto exit;
150 }
151 i++;
152 }
dbfc520c
DS
153 }
154
47bf9196 155 /* set signal handlers */
f8911dbb 156 act.sa_handler = sig_handler;
7e89a569 157
f8911dbb 158 sigemptyset (&act.sa_mask);
7e89a569
KS
159 /* alarm must interrupt syscalls*/
160 sigaction(SIGALRM, &act, NULL);
f8911dbb
KS
161 sigaction(SIGINT, &act, NULL);
162 sigaction(SIGTERM, &act, NULL);
3fd52a76 163
7e89a569
KS
164 /* trigger timout to interrupt blocking syscalls */
165 alarm(ALARM_TIMEOUT);
166
aee380b6 167 /* initialize udev database */
7e89a569
KS
168 if (udevdb_init(UDEVDB_DEFAULT) != 0)
169 info("error: unable to initialize database, continuing without database");
f61d732a 170
aee380b6
KS
171 switch(act_type) {
172 case UDEVSTART:
173 dbg("udevstart");
174 namedev_init();
175 udev_sleep = 0;
176 retval = udev_start();
177 break;
178 case ADD:
179 dbg("udev add");
180 namedev_init();
181 retval = udev_add_device(devpath, subsystem, NOFAKE);
182 break;
183 case REMOVE:
184 dbg("udev remove");
f61d732a 185 retval = udev_remove_device(devpath, subsystem);
dbfc520c 186 }
2a94c877 187
dbfc520c 188 udevdb_exit();
85511f02 189
7ac0feeb 190exit:
f61d732a 191 return retval;
f0083e3d 192}