]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev.c
[PATCH] cleanup netif handling and netif-dev.d/ events
[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
8dc0e138 23#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
c81b35c0
KS
24#include <stdio.h>
25#include <stddef.h>
f0083e3d
GKH
26#include <stdlib.h>
27#include <string.h>
e8baccca 28#include <ctype.h>
c81b35c0 29#include <errno.h>
d12ecb53 30#include <signal.h>
85511f02 31
c80da508 32#include "libsysfs/sysfs/libsysfs.h"
f0083e3d 33#include "udev.h"
c81b35c0 34#include "udev_lib.h"
f0083e3d 35#include "udev_version.h"
54988802 36#include "logging.h"
2232cac8 37#include "namedev.h"
dbfc520c 38#include "udevdb.h"
f0083e3d 39
7e89a569
KS
40/* timeout flag for udevdb */
41extern sig_atomic_t gotalarm;
42
c056c514
GKH
43/* global variables */
44char **main_argv;
45char **main_envp;
46
d026a35d 47#ifdef LOG
d00bd172 48unsigned char logname[LOGNAME_SIZE];
3fe07342 49void log_message(int level, const char *format, ...)
51a8bb2f 50{
e964c2c0 51 va_list args;
d026a35d
GKH
52
53 if (!udev_log)
54 return;
55
56 va_start(args, format);
57 vsyslog(level, format, args);
58 va_end(args);
51a8bb2f 59}
d026a35d 60#endif
51a8bb2f 61
e5a5b54a 62static void asmlinkage sig_handler(int signum)
d12ecb53 63{
d12ecb53 64 switch (signum) {
7e89a569
KS
65 case SIGALRM:
66 gotalarm = 1;
67 info("error: timeout reached, event probably not handled correctly");
68 break;
d12ecb53
MB
69 case SIGINT:
70 case SIGTERM:
d12ecb53
MB
71 udevdb_exit();
72 exit(20 + signum);
d12ecb53 73 default:
47bf9196 74 dbg("unhandled signal %d", signum);
d12ecb53
MB
75 }
76}
77
707680b1
KS
78/* list of subsystems we don't care about. not listing such systems here
79 * is not critical, but it makes it faster as we don't look for the "dev" file
80 */
81static int subsystem_without_dev(const char *subsystem)
82{
83 char *subsystem_blacklist[] = {
84 "scsi_host",
85 "scsi_device",
86 "usb_host",
87 "pci_bus",
88 "pcmcia_socket",
89 "bluetooth",
90 "i2c-adapter",
91 "pci_bus",
92 "ieee1394",
93 "ieee1394_host",
94 "ieee1394_node",
95 NULL
96 };
97 char **subsys;
98
99 for (subsys = subsystem_blacklist; *subsys != NULL; subsys++) {
100 if (strcmp(subsystem, *subsys) == 0)
101 return 1;
102 }
103
104 return 0;
105}
8eb38ef8 106
aee380b6 107int main(int argc, char *argv[], char *envp[])
f4dc8d11 108{
f8911dbb 109 struct sigaction act;
7a947ce5
KS
110 struct sysfs_class_device *class_dev;
111 struct udevice udev;
112 char path[SYSFS_PATH_MAX];
aee380b6
KS
113 int retval = -EINVAL;
114 enum {
115 ADD,
116 REMOVE,
117 UDEVSTART,
118 } act_type;
f4dc8d11 119
aee380b6 120 dbg("version %s", UDEV_VERSION);
2232cac8 121
92307b9e
PM
122 main_argv = argv;
123 main_envp = envp;
124
7257cb18 125 logging_init("udev");
7e89a569 126
aee380b6 127 udev_init_config();
af815f88 128
aee380b6
KS
129 if (strstr(argv[0], "udevstart")) {
130 act_type = UDEVSTART;
131 } else {
7a947ce5
KS
132 const char *action = get_action();
133 const char *devpath = get_devpath();
134 const char *subsystem = get_subsystem(main_argv[1]);
135
aee380b6
KS
136 if (!action) {
137 dbg("no action?");
138 goto exit;
139 }
140 if (strcmp(action, "add") == 0) {
141 act_type = ADD;
142 } else if (strcmp(action, "remove") == 0) {
143 act_type = REMOVE;
144 } else {
7a947ce5 145 dbg("no action '%s' for us", action);
8eb38ef8
GKH
146 goto exit;
147 }
f0083e3d 148
aee380b6
KS
149 if (!devpath) {
150 dbg("no devpath?");
151 goto exit;
152 }
7a947ce5 153 dbg("looking at '%s'", udev.devpath);
aee380b6
KS
154
155 /* we only care about class devices and block stuff */
6d74f996 156 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
aee380b6
KS
157 dbg("not a block or class device");
158 goto exit;
159 }
160
aee380b6 161 if (!subsystem) {
7a947ce5 162 dbg("no subsystem");
aee380b6
KS
163 goto exit;
164 }
165
166 /* skip blacklisted subsystems */
707680b1
KS
167 if (subsystem_without_dev(subsystem)) {
168 dbg("don't care about '%s' devices", subsystem);
7a947ce5 169 goto exit;
707680b1 170 };
7a947ce5
KS
171
172 udev_set_values(&udev, devpath, subsystem);
dbfc520c
DS
173 }
174
47bf9196 175 /* set signal handlers */
dc117daa 176 act.sa_handler = (void (*) (int))sig_handler;
f8911dbb 177 sigemptyset (&act.sa_mask);
707680b1 178 /* alarm must not restart syscalls*/
7e89a569 179 sigaction(SIGALRM, &act, NULL);
f8911dbb
KS
180 sigaction(SIGINT, &act, NULL);
181 sigaction(SIGTERM, &act, NULL);
3fd52a76 182
7e89a569
KS
183 /* trigger timout to interrupt blocking syscalls */
184 alarm(ALARM_TIMEOUT);
185
aee380b6 186 /* initialize udev database */
7e89a569
KS
187 if (udevdb_init(UDEVDB_DEFAULT) != 0)
188 info("error: unable to initialize database, continuing without database");
f61d732a 189
aee380b6
KS
190 switch(act_type) {
191 case UDEVSTART:
192 dbg("udevstart");
193 namedev_init();
aee380b6
KS
194 retval = udev_start();
195 break;
196 case ADD:
197 dbg("udev add");
7a947ce5
KS
198
199 /* init rules */
aee380b6 200 namedev_init();
7a947ce5
KS
201
202 /* open the device */
203 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
204 class_dev = sysfs_open_class_device_path(path);
205 if (class_dev == NULL) {
206 dbg ("sysfs_open_class_device_path failed");
207 break;
208 }
209 dbg("opened class_dev->name='%s'", class_dev->name);
210
211 /* name, create node, store in db */
212 retval = udev_add_device(&udev, class_dev);
5d24c6ca
KS
213
214 /* run scripts */
215 dev_d_execute(&udev);
216
217 sysfs_close_class_device(class_dev);
aee380b6
KS
218 break;
219 case REMOVE:
220 dbg("udev remove");
5d24c6ca
KS
221
222 /* get node from db, delete it*/
7a947ce5 223 retval = udev_remove_device(&udev);
5d24c6ca
KS
224
225 /* run scripts */
226 dev_d_execute(&udev);
dbfc520c 227 }
2a94c877 228
dbfc520c 229 udevdb_exit();
85511f02 230
7ac0feeb 231exit:
7257cb18 232 logging_close();
f61d732a 233 return retval;
f0083e3d 234}