chiark / gitweb /
[PATCH] Fix old-style pty breakage in rules file for tty device.
[elogind.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
31 #include "libsysfs/sysfs/libsysfs.h"
32 #include "udev.h"
33 #include "udev_lib.h"
34 #include "udev_version.h"
35 #include "logging.h"
36 #include "namedev.h"
37 #include "udevdb.h"
38
39 /* global variables */
40 char **main_argv;
41 char **main_envp;
42
43 /* local variables */
44 static int is_udevstart;
45
46 #ifdef LOG
47 unsigned char logname[LOGNAME_SIZE];
48 void log_message(int level, const char *format, ...)
49 {
50         va_list args;
51
52         if (!udev_log)
53                 return;
54
55         va_start(args, format);
56         vsyslog(level, format, args);
57         va_end(args);
58 }
59 #endif
60
61 asmlinkage static void sig_handler(int signum)
62 {
63         switch (signum) {
64                 case SIGINT:
65                 case SIGTERM:
66                         udevdb_exit();
67                         exit(20 + signum);
68                 default:
69                         dbg("unhandled signal %d", signum);
70         }
71 }
72
73 static char *subsystem_blacklist[] = {
74         "scsi_host",
75         "scsi_device",
76         "usb_host",
77         "pci_bus",
78         "pcmcia_socket",
79         ""
80 };
81
82 int __udev_hotplug(char *action, const char *devpath, const char *subsystem)
83 {
84         int retval = -EINVAL;
85         int i;
86         struct sigaction act;
87         const int nofake = 0;
88
89         dbg("looking at '%s'", devpath);
90
91         /* we only care about class devices and block stuff */
92         if (!strstr(devpath, "class") &&
93             !strstr(devpath, "block")) {
94                 dbg("not a block or class device");
95                 goto exit;
96         }
97
98         i = 0;
99         while (subsystem_blacklist[i][0] != '\0') {
100                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
101                         dbg("don't care about '%s' devices", subsystem);
102                         goto exit;
103                 }
104                 i++;
105         }
106
107         /* initialize udev database */
108         retval = udevdb_init(UDEVDB_DEFAULT);
109         if (retval != 0) {
110                 dbg("unable to initialize database");
111                 goto exit;
112         }
113
114         /* set signal handlers */
115         act.sa_handler = sig_handler;
116         sigemptyset (&act.sa_mask);
117         act.sa_flags = SA_RESTART;
118         sigaction(SIGINT, &act, NULL);
119         sigaction(SIGTERM, &act, NULL);
120
121         if (strcmp(action, "add") == 0) {
122                 /* Already done. */
123                 if (!is_udevstart)
124                         namedev_init();
125                 retval = udev_add_device(devpath, subsystem, nofake);
126                 goto action_done;
127         }
128
129         if (strcmp(action, "remove") == 0) {
130                 retval = udev_remove_device(devpath, subsystem);
131                 goto action_done;
132         }
133
134         dbg("unknown action '%s'", action);
135         retval = -EINVAL;
136
137 action_done:
138         udevdb_exit();
139
140 exit:
141         return retval;
142 }
143
144 static int udev_hotplug(void)
145 {
146         char *action;
147         char *devpath;
148         char *subsystem;
149
150         action = get_action();
151         if (!action) {
152                 dbg("no action?");
153                 return -EINVAL;
154         }
155
156         devpath = get_devpath();
157         if (!devpath) {
158                 dbg("no devpath?");
159                 return -EINVAL;
160         }
161
162         /* skip blacklisted subsystems */
163         subsystem = get_subsystem(main_argv[1]);
164         if (!subsystem) {
165                 dbg("no subsystem?");
166                 return -EINVAL;
167         }
168
169         return __udev_hotplug(action, devpath, subsystem);
170 }
171
172 int main(int argc, char *argv[], char *envp[])
173 {
174         main_argv = argv;
175         main_envp = envp;
176
177         if (strstr(argv[0], "udevstart")) {
178                 /* Setup env variables. */
179                 setenv("UDEV_NO_SLEEP", "1", 1);
180                 is_udevstart = 1;
181         }
182
183         /* initialize our configuration */
184         udev_init_config();
185
186         dbg("version %s", UDEV_VERSION);
187
188         if (is_udevstart) {
189                 namedev_init();
190                 return udev_start();
191         } else
192                 return udev_hotplug();
193 }