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