chiark / gitweb /
[PATCH] no error on enoent
[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 <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <signal.h>
29
30 #include "libsysfs/sysfs/libsysfs.h"
31 #include "udev.h"
32 #include "udev_version.h"
33 #include "udev_dbus.h"
34 #include "logging.h"
35 #include "namedev.h"
36 #include "udevdb.h"
37
38 /* global variables */
39 char **main_argv;
40 char **main_envp;
41
42 #ifdef LOG
43 unsigned char logname[42];
44 void log_message(int level, const char *format, ...)
45 {
46         va_list args;
47
48         if (!udev_log)
49                 return;
50
51         va_start(args, format);
52         vsyslog(level, format, args);
53         va_end(args);
54 }
55 #endif
56
57 static void sig_handler(int signum)
58 {
59         switch (signum) {
60                 case SIGINT:
61                 case SIGTERM:
62                         sysbus_disconnect();
63                         udevdb_exit();
64                         exit(20 + signum);
65                 default:
66                         dbg("unhandled signal");
67         }
68 }
69
70 static char *subsystem_blacklist[] = {
71         "net",
72         "scsi_host",
73         "scsi_device",
74         "usb_host",
75         "pci_bus",
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
88         action = get_action();
89         if (!action) {
90                 dbg("no action?");
91                 goto exit;
92         }
93
94         devpath = get_devpath();
95         if (!devpath) {
96                 dbg("no devpath?");
97                 goto exit;
98         }
99         dbg("looking at '%s'", devpath);
100
101         /* we only care about class devices and block stuff */
102         if (!strstr(devpath, "class") &&
103             !strstr(devpath, "block")) {
104                 dbg("not a block or class device");
105                 goto exit;
106         }
107
108         /* skip blacklisted subsystems */
109         subsystem = get_subsystem(main_argv[1]);
110         if (!subsystem) {
111                 dbg("no subsystem?");
112                 goto exit;
113         }
114         i = 0;
115         while (subsystem_blacklist[i][0] != '\0') {
116                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
117                         dbg("don't care about '%s' devices", subsystem);
118                         goto exit;
119                 }
120                 i++;
121         }
122
123         /* connect to the system message bus */
124         sysbus_connect();
125
126         /* initialize udev database */
127         retval = udevdb_init(UDEVDB_DEFAULT);
128         if (retval != 0) {
129                 dbg("unable to initialize database");
130                 goto exit_sysbus;
131         }
132
133         /* set up a default signal handler for now */
134         act.sa_handler = sig_handler;
135         sigemptyset (&act.sa_mask);
136         act.sa_flags = SA_RESTART;
137         sigaction(SIGINT, &act, NULL);
138         sigaction(SIGTERM, &act, NULL);
139
140         /* initialize the naming deamon */
141         namedev_init();
142
143         if (strcmp(action, "add") == 0)
144                 retval = udev_add_device(devpath, subsystem, 0);
145
146         else if (strcmp(action, "remove") == 0)
147                 retval = udev_remove_device(devpath, subsystem);
148
149         else {
150                 dbg("unknown action '%s'", action);
151                 retval = -EINVAL;
152         }
153         udevdb_exit();
154
155 exit_sysbus:
156         /* disconnect from the system message bus */
157         sysbus_disconnect();
158
159 exit:
160         if (retval > 0)
161                 retval = 0;
162
163         return -retval;
164 }
165
166 int main(int argc, char **argv, char **envp)
167 {
168         main_argv = argv;
169         main_envp = envp;
170
171         init_logging("udev");
172
173         /* initialize our configuration */
174         udev_init_config();
175
176         dbg("version %s", UDEV_VERSION);
177
178         return udev_hotplug();
179 }