]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev.c
[PATCH] fix udevstart badly broken in udev 031
[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
c056c514
GKH
39/* global variables */
40char **main_argv;
41char **main_envp;
42
d026a35d 43#ifdef LOG
d00bd172 44unsigned char logname[LOGNAME_SIZE];
3fe07342 45void log_message(int level, const char *format, ...)
51a8bb2f 46{
e964c2c0 47 va_list args;
d026a35d
GKH
48
49 if (!udev_log)
50 return;
51
52 va_start(args, format);
53 vsyslog(level, format, args);
54 va_end(args);
51a8bb2f 55}
d026a35d 56#endif
51a8bb2f 57
80775577 58asmlinkage static void sig_handler(int signum)
d12ecb53 59{
d12ecb53
MB
60 switch (signum) {
61 case SIGINT:
62 case SIGTERM:
d12ecb53
MB
63 udevdb_exit();
64 exit(20 + signum);
d12ecb53 65 default:
47bf9196 66 dbg("unhandled signal %d", signum);
d12ecb53
MB
67 }
68}
69
8eb38ef8 70static char *subsystem_blacklist[] = {
8eb38ef8
GKH
71 "scsi_host",
72 "scsi_device",
f130b156
GKH
73 "usb_host",
74 "pci_bus",
e9f504e8 75 "pcmcia_socket",
d00bd172 76 ""
8eb38ef8
GKH
77};
78
aee380b6 79int main(int argc, char *argv[], char *envp[])
f4dc8d11 80{
aee380b6
KS
81 main_argv = argv;
82 main_envp = envp;
f8911dbb 83 struct sigaction act;
aee380b6
KS
84 char *action;
85 char *devpath = "";
86 char *subsystem = "";
87 int i;
88 int retval = -EINVAL;
89 enum {
90 ADD,
91 REMOVE,
92 UDEVSTART,
93 } act_type;
f4dc8d11 94
aee380b6 95 dbg("version %s", UDEV_VERSION);
2232cac8 96
aee380b6
KS
97 /* initialize our configuration */
98 udev_init_config();
af815f88 99
aee380b6
KS
100 if (strstr(argv[0], "udevstart")) {
101 act_type = UDEVSTART;
102 } else {
103 action = get_action();
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("unknown action '%s'", action);
8eb38ef8
GKH
114 goto exit;
115 }
f0083e3d 116
aee380b6
KS
117 devpath = get_devpath();
118 if (!devpath) {
119 dbg("no devpath?");
120 goto exit;
121 }
122 dbg("looking at '%s'", devpath);
123
124 /* we only care about class devices and block stuff */
125 if (strstr(devpath, "class") && strstr(devpath, "block")) {
126 dbg("not a block or class device");
127 goto exit;
128 }
129
130 subsystem = get_subsystem(main_argv[1]);
131 if (!subsystem) {
132 dbg("no subsystem?");
133 goto exit;
134 }
135
136 /* skip blacklisted subsystems */
137 i = 0;
138 while (subsystem_blacklist[i][0] != '\0') {
139 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
140 dbg("don't care about '%s' devices", subsystem);
141 goto exit;
142 }
143 i++;
144 }
dbfc520c
DS
145 }
146
47bf9196 147 /* set signal handlers */
f8911dbb
KS
148 act.sa_handler = sig_handler;
149 sigemptyset (&act.sa_mask);
150 act.sa_flags = SA_RESTART;
151 sigaction(SIGINT, &act, NULL);
152 sigaction(SIGTERM, &act, NULL);
3fd52a76 153
aee380b6
KS
154 /* initialize udev database */
155 if (udevdb_init(UDEVDB_DEFAULT) != 0) {
156 dbg("unable to initialize database");
157 goto exit;
f61d732a
KS
158 }
159
aee380b6
KS
160 switch(act_type) {
161 case UDEVSTART:
162 dbg("udevstart");
163 namedev_init();
164 udev_sleep = 0;
165 retval = udev_start();
166 break;
167 case ADD:
168 dbg("udev add");
169 namedev_init();
170 retval = udev_add_device(devpath, subsystem, NOFAKE);
171 break;
172 case REMOVE:
173 dbg("udev remove");
f61d732a 174 retval = udev_remove_device(devpath, subsystem);
dbfc520c 175 }
2a94c877 176
dbfc520c 177 udevdb_exit();
85511f02 178
7ac0feeb 179exit:
f61d732a 180 return retval;
f0083e3d 181}