1 | /* Copyright 2004 Red Hat, Inc. |
---|
2 | * |
---|
3 | * This program is free software; you can redistribute it and/or modify |
---|
4 | * it under the terms of the GNU General Public License, version 2, |
---|
5 | * as published by the Free Software Foundation. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
15 | */ |
---|
16 | |
---|
17 | #include <ctype.h> |
---|
18 | #include <fcntl.h> |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <string.h> |
---|
22 | #include <unistd.h> |
---|
23 | #include <errno.h> |
---|
24 | |
---|
25 | #include <popt.h> |
---|
26 | |
---|
27 | #include <sys/mman.h> |
---|
28 | #include <sys/socket.h> |
---|
29 | #include <sys/types.h> |
---|
30 | #include <sys/un.h> |
---|
31 | #include <sys/utsname.h> |
---|
32 | |
---|
33 | #include <kudzu/kudzu.h> |
---|
34 | |
---|
35 | char *setupFile() |
---|
36 | { |
---|
37 | struct stat sbuf; |
---|
38 | char path[512]; |
---|
39 | int fd; |
---|
40 | struct utsname utsbuf; |
---|
41 | char *buf = NULL; |
---|
42 | |
---|
43 | uname(&utsbuf); |
---|
44 | snprintf(path,512,"/lib/modules/%s/modules.dep",utsbuf.release); |
---|
45 | if (!stat(path,&sbuf)) { |
---|
46 | fd = open(path,O_RDONLY); |
---|
47 | buf = mmap(0,sbuf.st_size,PROT_READ,MAP_SHARED,fd,0); |
---|
48 | close(fd); |
---|
49 | } |
---|
50 | return buf; |
---|
51 | } |
---|
52 | |
---|
53 | int isAvailable(char *modulename) |
---|
54 | { |
---|
55 | char mod_name[100]; |
---|
56 | static char *buf = NULL; |
---|
57 | |
---|
58 | if (!buf) { |
---|
59 | buf = setupFile(); |
---|
60 | if (!buf) |
---|
61 | return 0; |
---|
62 | } |
---|
63 | snprintf(mod_name,100,"/%s.ko:",modulename); |
---|
64 | if (strstr(buf,mod_name)) |
---|
65 | return 1; |
---|
66 | snprintf(mod_name,100,"/%s.ko.gz:",modulename); |
---|
67 | if (strstr(buf,mod_name)) |
---|
68 | return 1; |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | void waitForConnection(struct device **devlist) |
---|
73 | { |
---|
74 | int sock, fd, socklen, x; |
---|
75 | struct sockaddr_un addr; |
---|
76 | FILE *tmp; |
---|
77 | |
---|
78 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
---|
79 | if (sock == -1) return; |
---|
80 | memset(addr.sun_path,'\0',sizeof(addr.sun_path)); |
---|
81 | sprintf(addr.sun_path, "akudzu_config_socket"); |
---|
82 | addr.sun_family= AF_UNIX; |
---|
83 | addr.sun_path[0] = '\0'; |
---|
84 | if (bind(sock, &addr, sizeof(struct sockaddr_un)) == -1) |
---|
85 | return; |
---|
86 | if (listen(sock, 1) == -1) |
---|
87 | return; |
---|
88 | fd = accept(sock, &addr, &socklen); |
---|
89 | if (fd == -1) |
---|
90 | return; |
---|
91 | tmp = fdopen(fd,"w"); |
---|
92 | for (x = 0; devlist[x]; x++) { |
---|
93 | devlist[x]->writeDevice(tmp,devlist[x]); |
---|
94 | fflush(tmp); |
---|
95 | } |
---|
96 | fclose(tmp); |
---|
97 | close(sock); |
---|
98 | } |
---|
99 | |
---|
100 | int main(int argc, char **argv) |
---|
101 | { |
---|
102 | char *bus = NULL, *class = NULL; |
---|
103 | int x, rc, isdaemon = 0; |
---|
104 | enum deviceBus probeBus = BUS_UNSPEC & ~BUS_SERIAL; |
---|
105 | enum deviceClass probeClass = CLASS_UNSPEC & ~CLASS_FLOPPY & ~CLASS_HD & ~CLASS_CDROM; |
---|
106 | poptContext context; |
---|
107 | struct device **devs; |
---|
108 | struct poptOption options[] = { |
---|
109 | POPT_AUTOHELP |
---|
110 | { "bus", 'b', POPT_ARG_STRING, &bus, 0, |
---|
111 | "probe only the specified 'bus'", |
---|
112 | NULL |
---|
113 | }, |
---|
114 | { "class", 'c', POPT_ARG_STRING, &class, 0, |
---|
115 | "probe only for the specified 'class'", |
---|
116 | NULL |
---|
117 | }, |
---|
118 | { "daemon", 'd', POPT_ARG_NONE, &isdaemon, 0, |
---|
119 | NULL, NULL |
---|
120 | }, |
---|
121 | { 0, 0, 0, 0, 0, 0 } |
---|
122 | }; |
---|
123 | |
---|
124 | context = poptGetContext("kmodule", argc, (const char **)argv, options, 0); |
---|
125 | while ((rc = poptGetNextOpt(context)) > 0) { |
---|
126 | } |
---|
127 | if (( rc < -1)) { |
---|
128 | fprintf(stderr, "%s: %s\n", |
---|
129 | poptBadOption(context, POPT_BADOPTION_NOALIAS), |
---|
130 | poptStrerror(rc)); |
---|
131 | exit(-1); |
---|
132 | } |
---|
133 | |
---|
134 | if (bus) { |
---|
135 | for (x=0; bus[x]; x++) |
---|
136 | bus[x] = toupper(bus[x]); |
---|
137 | for (x=0; buses[x].string && strcmp(buses[x].string,bus); x++); |
---|
138 | if (buses[x].string) |
---|
139 | probeBus = buses[x].busType; |
---|
140 | } |
---|
141 | if (class) { |
---|
142 | for (x=0; class[x]; x++) |
---|
143 | class[x] = toupper(class[x]); |
---|
144 | for (x=0; classes[x].string && strcmp(classes[x].string,class); x++); |
---|
145 | if (classes[x].string) |
---|
146 | probeClass = classes[x].classType; |
---|
147 | } |
---|
148 | initializeBusDeviceList(probeBus); |
---|
149 | |
---|
150 | devs = probeDevices(probeClass, probeBus, PROBE_ALL|PROBE_NOLOAD|PROBE_SAFE); |
---|
151 | if (!devs) |
---|
152 | return 0; |
---|
153 | for (x = 0; devs[x]; x++) { |
---|
154 | if (devs[x]->driver && isAvailable(devs[x]->driver)) { |
---|
155 | int i; |
---|
156 | |
---|
157 | for (i = 0; classes[i].classType; i++) |
---|
158 | if (devs[x]->type == classes[i].classType) { |
---|
159 | break; |
---|
160 | } |
---|
161 | printf("%s %s\n",classes[i].string,devs[x]->driver); |
---|
162 | } |
---|
163 | } |
---|
164 | fflush(stdout); |
---|
165 | if (isdaemon) { |
---|
166 | daemon(0,0); |
---|
167 | waitForConnection(devs); |
---|
168 | } |
---|
169 | return 0; |
---|
170 | } |
---|