chiark / gitweb /
[PATCH] clarify udevinfo device walk
[elogind.git] / udevtest.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 "logging.h"
34 #include "namedev.h"
35
36 /* global variables */
37 char **main_argv;
38 char **main_envp;
39
40 #ifdef LOG
41 unsigned char logname[42];
42 void log_message (int level, const char *format, ...)
43 {
44         va_list args;
45
46 //      if (!udev_log)
47 //              return;
48
49         /* FIXME use level... */
50         va_start(args, format);
51         vprintf(format, args);
52         va_end(args);
53         if (format[strlen(format)-1] != '\n')
54                 printf("\n");
55 }
56 #endif
57
58 static void sig_handler(int signum)
59 {
60         switch (signum) {
61                 case SIGINT:
62                 case SIGTERM:
63                         exit(20 + signum);
64                 default:
65                         dbg("unhandled signal");
66         }
67 }
68
69 static char *subsystem_blacklist[] = {
70         "net",
71         "scsi_host",
72         "scsi_device",
73         "usb_host",
74         "pci_bus",
75         "",
76 };
77
78 static int udev_hotplug(int argc, char **argv)
79 {
80         char *devpath;
81         char *subsystem;
82         int retval = -EINVAL;
83         int i;
84         struct sigaction act;
85
86         devpath = argv[1];
87         if (!devpath) {
88                 dbg("no devpath?");
89                 goto exit;
90         }
91         dbg("looking at '%s'", devpath);
92
93         /* we only care about class devices and block stuff */
94         if (!strstr(devpath, "class") &&
95             !strstr(devpath, "block")) {
96                 dbg("not a block or class device");
97                 goto exit;
98         }
99
100         /* skip blacklisted subsystems */
101         subsystem = argv[1];
102         i = 0;
103         while (subsystem_blacklist[i][0] != '\0') {
104                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
105                         dbg("don't care about '%s' devices", subsystem);
106                         goto exit;
107                 }
108                 i++;
109         }
110
111         /* initialize our configuration */
112         udev_init_config();
113
114         /* set up a default signal handler for now */
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         /* initialize the naming deamon */
122         namedev_init();
123
124         retval = udev_add_device(devpath, subsystem, 1);
125
126 exit:
127         if (retval > 0)
128                 retval = 0;
129
130         return -retval;
131 }
132
133 int main(int argc, char **argv, char **envp)
134 {
135         main_argv = argv;
136         main_envp = envp;
137
138         dbg("version %s", UDEV_VERSION);
139
140         return udev_hotplug(argc, argv);
141 }
142
143