Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / sysinstall / modules.c
1 /*-
2  * Copyright (c) 2000  "HOSOKAWA, Tatsumi" <hosokawa@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/release/sysinstall/modules.c,v 1.2.2.4 2002/06/05 20:13:50 jhb Exp $
27  */
28
29 #include "sysinstall.h"
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <fcntl.h>
36 #include <dirent.h>
37 #include <fcntl.h>
38 #include <fnmatch.h>
39
40 /* Prototypes */
41 static int              kldModuleFire(dialogMenuItem *self);
42
43 #define MODULESDIR "/stand/modules"
44 #define DISTMOUNT "/dist"
45
46 void
47 moduleInitialize(void)
48 {
49     int fd, len;
50     DIR *dirp;
51     struct dirent *dp;
52     char module[MAXPATHLEN], desc[MAXPATHLEN];
53     char desc_str[BUFSIZ];
54
55     if (!RunningAsInit && !Fake) {
56         /* It's not my job... */
57         return;
58     }
59
60     dirp = opendir(MODULESDIR);
61     if (dirp) {
62         while ((dp = readdir(dirp))) {
63             if (dp->d_namlen < (sizeof(".ko") - 1)) continue;
64             if (strcmp(dp->d_name + dp->d_namlen - (sizeof(".ko") - 1), ".ko") == 0) {
65                 strcpy(module, MODULESDIR);
66                 strcat(module, "/");
67                 strcat(module, dp->d_name);
68                 strcpy(desc, module);
69                 len = strlen(desc);
70                 strcpy(desc + (len - (sizeof(".ko") - 1)), ".dsc");
71                 fd = open(module, O_RDONLY);
72                 if (fd < 0) continue;
73                 close(fd);
74                 fd = open(desc, O_RDONLY);
75                 if (fd < 0) {
76                     desc_str[0] = 0;
77                 }
78                 else {
79                     len = read(fd, desc_str, BUFSIZ);
80                     close(fd);
81                     if (len < BUFSIZ) desc_str[len] = 0;
82                 }
83                 if (desc_str[0])
84                     msgDebug("Loading module %s (%s)\n", dp->d_name, desc_str);
85                 else
86                     msgDebug("Loading module %s\n", dp->d_name);
87                 if (kldload(module) < 0 && errno != EEXIST) {
88                     if (desc_str[0])
89                         msgConfirm("Loading module %s failed\n%s", dp->d_name, desc_str);
90                     else
91                         msgConfirm("Loading module %s failed", dp->d_name);
92                 }
93             }
94         }
95         closedir(dirp);
96     }
97 }
98
99 int
100 kldBrowser(dialogMenuItem *self)
101 {
102     DMenu       *menu;
103     int         i, what = DITEM_SUCCESS, msize, count;
104     DIR         *dir;
105     struct dirent *de;
106     char        *err;
107     
108     err = NULL;
109     
110     if (DITEM_STATUS(mediaSetFloppy(NULL)) == DITEM_FAILURE) {
111         msgConfirm("Unable to set media device to floppy.");
112         what |= DITEM_FAILURE;
113         mediaClose();
114         return what;
115     }
116
117     if (!DEVICE_INIT(mediaDevice)) {
118         msgConfirm("Unable to mount floppy filesystem.");
119         what |= DITEM_FAILURE;
120         mediaClose();
121         return what;
122     }
123
124     msize = sizeof(DMenu) + (sizeof(dialogMenuItem) * 2);
125     count = 0;
126     if ((menu = malloc(msize)) == NULL) {
127         err = "Failed to allocate memory for menu";
128         goto errout;
129     }
130
131     bcopy(&MenuKLD, menu, sizeof(DMenu));
132         
133     bzero(&menu->items[count], sizeof(menu->items[0]));
134     menu->items[count].prompt = strdup("X Exit");
135     menu->items[count].title = strdup("Exit this menu (returning to previous)");
136     menu->items[count].fire = dmenuExit;
137     count++;
138         
139     if ((dir = opendir(DISTMOUNT)) == NULL) {
140         err = "Couldn't open directory";
141         goto errout;
142     }
143     
144     while ((de = readdir(dir)) != NULL) {
145         if (fnmatch("*.ko", de->d_name, FNM_CASEFOLD))
146             continue;
147         
148         msize += sizeof(dialogMenuItem);
149         if ((menu = realloc(menu, msize)) == NULL) {
150             err = "Failed to allocate memory for menu item";
151             goto errout;
152         }
153             
154         bzero(&menu->items[count], sizeof(menu->items[0]));
155         menu->items[count].fire = kldModuleFire;
156
157         menu->items[count].prompt = strdup(de->d_name);
158         menu->items[count].title = menu->items[count].prompt;
159             
160         count++;
161     }
162
163     closedir(dir);
164
165     menu->items[count].prompt = NULL;
166     menu->items[count].title = NULL;
167     
168     dmenuOpenSimple(menu, FALSE);
169     
170     mediaClose();
171
172     deviceRescan();
173     
174   errout:    
175     for (i = 0; i < count; i++)
176         free(menu->items[i].prompt);
177     
178     free(menu);
179
180     if (err != NULL) {
181         what |= DITEM_FAILURE;
182         if (!variable_get(VAR_NO_ERROR))
183             msgConfirm(err);
184     }
185     
186     return (what);
187 }
188
189 static int
190 kldModuleFire(dialogMenuItem *self) {
191     char        fname[256];
192
193     bzero(fname, sizeof(fname));
194     snprintf(fname, sizeof(fname), "%s/%s", DISTMOUNT, self->prompt);
195
196     if (kldload(fname) < 0 && errno != EEXIST) {
197         if (!variable_get(VAR_NO_ERROR))
198             msgConfirm("Loading module %s failed\n", fname);
199     } else {
200         if (!variable_get(VAR_NO_ERROR))
201             msgConfirm("Loaded module %s OK", fname);
202     }
203
204     return(0);
205  }