chiark / gitweb /
[PATCH] fix devpath for netdev
[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 #ifdef LOG
44 unsigned char logname[LOGNAME_SIZE];
45 void log_message(int level, const char *format, ...)
46 {
47         va_list args;
48
49         if (!udev_log)
50                 return;
51
52         va_start(args, format);
53         vsyslog(level, format, args);
54         va_end(args);
55 }
56 #endif
57
58 static void sig_handler(int signum)
59 {
60         switch (signum) {
61                 case SIGINT:
62                 case SIGTERM:
63                         udevdb_exit();
64                         exit(20 + signum);
65                 default:
66                         dbg("unhandled signal");
67         }
68 }
69
70 static char *subsystem_blacklist[] = {
71         "scsi_host",
72         "scsi_device",
73         "usb_host",
74         "pci_bus",
75         "pcmcia_socket",
76         ""
77 };
78
79 static int udev_hotplug(void)
80 {
81         char *action;
82         char *devpath;
83         char *subsystem;
84         int retval = -EINVAL;
85         int i;
86         struct sigaction act;
87         const int nofake = 0;
88
89         action = get_action();
90         if (!action) {
91                 dbg("no action?");
92                 goto exit;
93         }
94
95         devpath = get_devpath();
96         if (!devpath) {
97                 dbg("no devpath?");
98                 goto exit;
99         }
100         dbg("looking at '%s'", devpath);
101
102         /* we only care about class devices and block stuff */
103         if (!strstr(devpath, "class") &&
104             !strstr(devpath, "block")) {
105                 dbg("not a block or class device");
106                 goto exit;
107         }
108
109         /* skip blacklisted subsystems */
110         subsystem = get_subsystem(main_argv[1]);
111         if (!subsystem) {
112                 dbg("no subsystem?");
113                 goto exit;
114         }
115         i = 0;
116         while (subsystem_blacklist[i][0] != '\0') {
117                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
118                         dbg("don't care about '%s' devices", subsystem);
119                         goto exit;
120                 }
121                 i++;
122         }
123
124         /* initialize udev database */
125         retval = udevdb_init(UDEVDB_DEFAULT);
126         if (retval != 0) {
127                 dbg("unable to initialize database");
128                 goto exit;
129         }
130
131         /* set up a default signal handler for now */
132         act.sa_handler = sig_handler;
133         sigemptyset (&act.sa_mask);
134         act.sa_flags = SA_RESTART;
135         sigaction(SIGINT, &act, NULL);
136         sigaction(SIGTERM, &act, NULL);
137
138         if (strcmp(action, "add") == 0) {
139                 namedev_init();
140                 retval = udev_add_device(devpath, subsystem, nofake);
141                 goto action_done;
142         }
143
144         if (strcmp(action, "remove") == 0) {
145                 retval = udev_remove_device(devpath, subsystem);
146                 goto action_done;
147         }
148
149         dbg("unknown action '%s'", action);
150         retval = -EINVAL;
151
152 action_done:
153         udevdb_exit();
154
155 exit:
156         return retval;
157 }
158
159 int main(int argc, char *argv[], char *envp[])
160 {
161         main_argv = argv;
162         main_envp = envp;
163
164         init_logging("udev");
165
166         /* initialize our configuration */
167         udev_init_config();
168
169         dbg("version %s", UDEV_VERSION);
170
171         return udev_hotplug();
172 }