summaryrefslogtreecommitdiffstats
path: root/loader/modules.c
blob: 41bed8defab70f332446d513e1478932488edeab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <newt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>

#include "isys/imount.h"
#include "isys/isys.h"
#include "isys/cpio.h"

#include "lang.h"
#include "loader.h"
#include "log.h"
#include "misc.h"
#include "modules.h"
#include "devices.h"
#include "windows.h"

struct moduleDependency_s {
    char * name;
    char ** deps;
};


static char ** extractModules(struct driverDiskInfo * location, 
			char * const * modNames, char ** oldPaths);
static int ethCount(void);
static int scsiCount(void);
int mlReadLoadedList(moduleList * mlp);
void mlFreeList(moduleList ml);
moduleDeps mlNewDeps(void);
int mlLoadDeps(moduleDeps * moduleDepListPtr, const char * path);
char ** tsortModules(moduleList modLoaded, moduleDeps ml, char ** args, 
			    int depth, char *** listPtr, int * listSizePtr);
static int loadModule(const char * modName, char * path, moduleList modLoaded,
	         char ** args, moduleInfoSet modInfo, int flags);
static char * filterDriverModules(struct driverDiskInfo * ddi,
				  char * const * modNames);
static char ** extractModules(struct driverDiskInfo * ddi, 
			      char * const * modNames, char ** oldPaths);
static int doLoadModules(const char * origModNames, moduleList modLoaded, 
		    moduleDeps modDeps, moduleInfoSet modInfo, int flags,
		    const char * argModule, char ** args);
int mlLoadModule(const char * modName, 
		    moduleList modLoaded, moduleDeps modDeps, char ** args, 
		    moduleInfoSet modInfo, int flags);
int mlLoadModuleSet(const char * modNames, 
		    moduleList modLoaded, moduleDeps modDeps, 
		    moduleInfoSet modInfo, int flags);
char ** mlGetDeps(moduleDeps modDeps, const char * modName);
int mlModuleInList(const char * modName, moduleList list);
int mlWriteConfModules(moduleList list, int fd);
int simpleRemoveLoadedModule(const char * modName, moduleList modLoaded,
			     int flags);
int reloadUnloadedModule(char * modName, void * location,
			 moduleList modLoaded, char ** args, int flags);

static int ethCount(void) {
    int fd;
    char buf[16384];
    int i;
    char * chptr;
    int count = 0;

    fd = open("/proc/net/dev", O_RDONLY);
    i = read(fd, buf, sizeof(buf) - 1);
    buf[i] = '\0';

    /* skip first two header lines */
    chptr = strchr(buf, '\n') + 1;
    chptr = strchr(chptr, '\n') + 1;

    while (chptr) {
	while (*chptr && isspace(*chptr)) chptr++;
	if (!strncmp(chptr, "eth", 3))
	    count++;
	chptr = strchr(chptr, '\n');
	if (chptr) chptr++;
    }

    return count;
}

static int scsiCount(void) {
    FILE *f;
    char buf[16384];
    int count = 0;

    f = fopen("/tmp/modules.conf", "r");
    if (!f)
	return 0;
    while (fgets(buf, sizeof(buf) - 1, f)) {
	if (!strncmp(buf, "alias scsi_hostadapter", 22))
	    count++;
    }
    fclose(f);
    return count;
}

int mlReadLoadedList(moduleList * mlp) {
    int fd;
    char * start;
    char * end;
    char buf[4096];
    struct stat sb;
    int i;
    moduleList ml;

    if ((fd = open("/proc/modules", O_RDONLY)) < 0)
    	return -1;

    fstat(fd, &sb);
    i = read(fd, buf, sizeof(buf));
    buf[i] = '\0';
    close(fd);

    ml = malloc(sizeof(*ml));
    ml->numModules = 0;

    start = buf;
    while (start && *start) {
    	end = start;
	while (!isspace(*end) && *end != '\n') end++;
	*end = '\0';
	ml->mods[ml->numModules].name = strdup(start);
	ml->mods[ml->numModules].args = NULL;
	ml->mods[ml->numModules].path = NULL;
	ml->mods[ml->numModules].weLoaded = 0;
	*end = ' ';
	/*ml->numModules++;*/
	start = strchr(end, '\n');
	if (start) start++;
    }

    *mlp = ml;

    return 0;
}

void mlFreeList(moduleList ml) {
    int i;
    int j;

    for (i = 0; i < ml->numModules; i++) {
        free(ml->mods[i].name);
	if (ml->mods[i].args) {
	    for (j = 0; ml->mods[j].args[j]; j++)
		free(ml->mods[i].args[j]);
	    free(ml->mods[i].args);
	}
    }
    free(ml);
}

moduleDeps mlNewDeps(void) {
    moduleDeps md;

    md = malloc(sizeof(*md));
    md->name = NULL;
    md->deps = NULL;

    return md;
}

int mlLoadDeps(moduleDeps * moduleDepListPtr, const char * path) {
    int fd;
    char * buf;
    struct stat sb;
    char * start, * end, * chptr;
    int i, numItems;
    moduleDeps nextDep;
    moduleDeps moduleDepList = *moduleDepListPtr;

    fd = open(path, O_RDONLY);
    if (fd < 0) {
	return -1;
    }

    fstat(fd, &sb);
    buf = alloca(sb.st_size + 1);
    read(fd, buf, sb.st_size);
    buf[sb.st_size] = '\0';
    close(fd);

    start = buf;
    numItems = 0;
    while (start) {
	numItems++;
	start = strchr(start + 1, '\n');
    }

    for (nextDep = moduleDepList; nextDep->name; nextDep++) numItems++;

    moduleDepList = realloc(moduleDepList, sizeof(*moduleDepList) * numItems);
    for (nextDep = moduleDepList; nextDep->name; nextDep++) ;

    start = buf;
    while (start < (buf + sb.st_size) && *start) {
	end = strchr(start, '\n');
	*end = '\0';

	chptr = strchr(start, ':');
	if (!chptr) {
	    start = end + 1;
	    continue;
	}

	*chptr++ = '\0';
	while (*chptr && isspace(*chptr)) chptr++;
	if (!*chptr) {
	    start = end + 1;
	    continue;
	}

	/* found something */
	nextDep->name = strdup(start);
	nextDep->deps = malloc(sizeof(char *) * (strlen(chptr) + 1));
	start = chptr, i = 0;
	while (start && *start) {
	    chptr = strchr(start, ' ');
	    if (chptr) *chptr = '\0';
	    nextDep->deps[i++] = strdup(start);
	    if (chptr)
		start = chptr + 1;
	    else
		start = NULL;
	    while (start && *start && isspace(*start)) start++;
	}
	nextDep->deps[i] = NULL;
	nextDep->deps = realloc(nextDep->deps, sizeof(char *) * (i + 1));
	nextDep++;

	start = end + 1;
    }

    nextDep->name = NULL;
    nextDep->deps = NULL;
    moduleDepList = realloc(moduleDepList, sizeof(*moduleDepList) *
				(nextDep - moduleDepList + 1));

    *moduleDepListPtr = moduleDepList;

    return 0;
}

/* this leaks memory if their is a loop in the modules. oh well. */
char ** tsortModules(moduleList modLoaded, moduleDeps ml, char ** args, 
			    int depth, char *** listPtr, int * listSizePtr) {
    int listSize;
    char ** list;
    char ** next;
    char ** deps;

    if (!depth) {
	int count;

	listSize = 5;
	list = malloc((listSize + 1) * sizeof(*list));
	*list = NULL;

	listPtr = &list;
	listSizePtr = &listSize;

	for (deps = args, count = 0; *deps; deps++, count++);
    } else {
	list = *listPtr;
	listSize = *listSizePtr;
    }

    if (depth++ > 100) {
	return NULL;
    }

    while (*args) {
	/* don't load it twice */
	next = list;
	while (*next && strcmp(*next, *args)) next++;

	if (*next || mlModuleInList(*args, modLoaded)) {
	    args++;
	    continue;
	}

	/* load everything this depends on */
	deps = mlGetDeps(ml, *args);
	if (deps) {
	    if (!tsortModules(modLoaded, ml, deps, depth, listPtr, listSizePtr))
		return NULL;

	    list = *listPtr;
	    listSize = *listSizePtr;
	}	    

	/* add this to the list */
	next = list;
	while (*next) next++;

	if ((next - list) >= listSize) {
	    int count = next - list;

	    listSize += 10;
	    /* leave room for a NULL */
	    list = realloc(list, sizeof(*list) * (listSize + 1));

	    *listSizePtr = listSize;
	    *listPtr = list;

	    next = list + count;
	}

	next[0] = *args;
	next[1] = NULL;

	args++;
    }

    return list;
}

static int loadModule(const char * modName, char * path, moduleList modLoaded,
	         char ** args, moduleInfoSet modInfo, int flags) {
    char fileName[200];
    int rc, i;
    char ** arg, ** newArgs, ** argPtr;
    struct moduleInfo * mi = NULL;
    int ethDevices = -1;
    pid_t child;
    int status;
    int popWindow = 0;

    if (mlModuleInList(modName, modLoaded))
	return 0;

    if (modInfo && (mi = isysFindModuleInfo(modInfo, modName))) {
	if (mi->major == DRIVER_NET && mi->minor == DRIVER_MINOR_ETHERNET) {
	    ethDevices = ethCount();
	}

	if (mi->major == DRIVER_SCSI) {
	    startNewt(flags);
	    scsiWindow(modName);
	    popWindow = 1;
	}
    }

    sprintf(fileName, "%s.o", modName);
    for (argPtr = args; argPtr && *argPtr; argPtr++)  {
	strcat(fileName, " ");
	strcat(fileName, *argPtr);
    }

    if (modInfo && (mi = isysFindModuleInfo(modInfo, modName))) {
	if (mi->major == DRIVER_SCSI) {
	    /* XXX this shouldn't happen before every load but instead
	     * just before loading a module group */
	    simpleRemoveLoadedModule("usb-storage", modLoaded, flags);
	}
    }

    if (FL_TESTING(flags)) {
	logMessage("would have insmod %s", path);
	rc = 0;
    } else {
	if (!(child = fork())) {
	    int fd = open("/dev/tty3", O_RDWR);

	    dup2(fd, 0);
	    dup2(fd, 1);
	    dup2(fd, 2);
	    close(fd);

	    rc = insmod(path, NULL, args);
	    _exit(rc);
	}

	waitpid(child, &status, 0);

	if (!WIFEXITED(status) || WEXITSTATUS(status)) {
	    rc = 1;
	} else {
	    rc = 0;
	}
    }

    if (modInfo && (strncmp(modName, "usb-storage", 11) != 0) && (mi = isysFindModuleInfo(modInfo, modName))) {
 	if (mi->major == DRIVER_SCSI) {
 	    reloadUnloadedModule("usb-storage", NULL, modLoaded, NULL, flags);
	    setFloppyDevice(flags);
 	}
    }

    if (!rc) {
	modLoaded->mods[modLoaded->numModules].name = strdup(modName);
	modLoaded->mods[modLoaded->numModules].weLoaded = 1;
	modLoaded->mods[modLoaded->numModules].path = strdup(path);
	modLoaded->mods[modLoaded->numModules].firstDevNum = -1;
	modLoaded->mods[modLoaded->numModules].lastDevNum = -1;
	modLoaded->mods[modLoaded->numModules].written = 0;
	
	if (ethDevices >= 0) {
	    modLoaded->mods[modLoaded->numModules].firstDevNum = ethDevices;
	    modLoaded->mods[modLoaded->numModules].lastDevNum = ethCount() - 1;
	}

	if (mi) {
	    modLoaded->mods[modLoaded->numModules].major = mi->major;
	    modLoaded->mods[modLoaded->numModules].minor = mi->minor;
	} else {
	    modLoaded->mods[modLoaded->numModules].major = DRIVER_NONE;
	    modLoaded->mods[modLoaded->numModules].minor = DRIVER_MINOR_NONE;
	}

	if (args) {
	    for (i = 0, arg = args; *arg; arg++, i++);
	    newArgs = malloc(sizeof(*newArgs) * (i + 1));
	    for (i = 0, arg = args; *arg; arg++, i++)
		newArgs[i] = strdup(*arg);
	    newArgs[i] = NULL;
	} else {
	    newArgs = NULL;
	}

	modLoaded->mods[modLoaded->numModules++].args = newArgs;
    }

    if (popWindow) {
	sleep(1);
	newtPopWindow();
    }

    return rc;
}

static char * filterDriverModules(struct driverDiskInfo * ddi,
				  char * const * modNames) {
    struct utsname un;
    gzFile from;
    gzFile to;
    int first = 1;
    int fd;
    char * buf;
    struct stat sb;
    int rc;
    int failed;
    char * toPath;
    char * chptr;
    char ** pattern, ** p;
    int i;

    uname(&un);
    /* strip off BOOT, -SMP, whatever */
    chptr = un.release + strlen(un.release) - 1;
    while (!isdigit(*chptr)) chptr--;
    *(chptr + 1) = '\0';

    for (i = 0; modNames[i]; i++) ;
    pattern = alloca((i + 1) * sizeof(*pattern));

    for (i = 0, p = pattern; modNames[i]; i++, p++) {
	*p = alloca(strlen(modNames[i]) + strlen(un.release) + 5);
	sprintf(*p, "%s*/%s.o", un.release, modNames[i]);
	logMessage("extracting pattern %s%s%s", *p,
		    ddi->title ? " from " : "",
		    ddi->title ? ddi->title : "");
    }
    *p = NULL;

    if (ddi->device)
	devMakeInode(ddi->device, ddi->mntDevice);

    while (1) {
	failed = 0;

	if (doPwMount(ddi->mntDevice, "/tmp/drivers", ddi->fs, 1, 0, 
		      NULL, NULL))
	    failed = 1;

	if (failed && !first) {
	    newtWinMessage(_("Error"), _("OK"), 
		    _("Failed to mount driver disk: %s."), strerror(errno));
	} else if (!failed) {
	    if ((fd = open("/tmp/drivers/rhdd-6.1", O_RDONLY)) < 0)
		failed = 1;
	    if (!failed) {
		fstat(fd, &sb);
		buf = malloc(sb.st_size + 1);
		read(fd, buf, sb.st_size);
		if (buf[sb.st_size - 1] == '\n')
		    sb.st_size--;
		buf[sb.st_size] = '\0';
		close(fd);

		failed = strcmp(buf, ddi->title);
		free(buf);
	    }

	    if (failed && !first) {
		umount("/tmp/drivers");
		newtWinMessage(_("Error"), _("OK"),
			_("The wrong diskette was inserted."));
	    }
	}

	if (!failed) {
	    from = gunzip_open("/tmp/drivers/modules.cgz");
	    toPath = malloc(strlen(modNames[0]) + 30);
	    sprintf(toPath, "/tmp/modules/%s", modNames[0]);
	    mkdirChain(toPath);
	    strcat(toPath, "/modules.cgz");
	    to = gzip_open(toPath, O_TRUNC | O_RDWR | O_CREAT, 0600);

	    /* This message isn't good, but it'll do. */
	    winStatus(50, 3, _("Loading"), _("Loading %s driver..."), 
		      modNames[0]);

	    myCpioFilterArchive(from, to, pattern);

	    newtPopWindow();

	    gunzip_close(from);
	    gunzip_close(to);
	    umount("/tmp/drivers");

	    return toPath;
	}

	first = 0;

	if (ddi->device)
	    eject(ddi->device);

	rc = newtWinChoice(_("Driver Disk"), _("OK"), _("Cancel"),
		_("Please insert the %s driver disk now."), ddi->title);
	if (rc == 2) return NULL;
    }
}

static char ** extractModules(struct driverDiskInfo * ddi, 
			      char * const * modNames, char ** oldPaths) {
    gzFile fd;
    char * ballPath;
    struct cpioFileMapping * map;
    int i, numMaps;
    char * const * m;
    struct utsname u;
    int rc;
    const char * failedFile;
    char fn[255];
    struct stat sb;

    /* this needs to know about modules64.cgz for sparc */

    uname(&u);

    if (ddi) {
	logMessage("looking for drivers on driver disk");
	ballPath = filterDriverModules(ddi, modNames);
    } else {
	ballPath = strdup("/modules/modules.cgz");
    }

    fd = gunzip_open(ballPath);
    if (!fd) {
	logMessage("failed to open %s", ballPath);
	free(ballPath);
	return NULL;
    }

    for (m = modNames, i = 0; *m; i++, m++);
    
    map = alloca(sizeof(*map) * i);
    memset(map, 0, sizeof(*map) * i);
    if (!oldPaths)
	/* +1 NULL terminates this list */
	oldPaths = calloc(i + 1, sizeof(*oldPaths));

    for (m = modNames, i = 0, numMaps = 0; *m; m++, i++) {
	if (!oldPaths[i]) {
	    map[numMaps].archivePath = alloca(strlen(u.release) + 
						strlen(*m) + 25);
	    sprintf(map[numMaps].archivePath, "%s/%s.o", u.release, *m);
	    map[numMaps].fsPath = alloca(10 + strlen(*m));
	    sprintf(map[numMaps].fsPath, "/tmp/%s.o", *m);
	    unlink(map[numMaps].fsPath);
	    map[numMaps].mapFlags = CPIO_MAP_PATH;
	    numMaps++;
	}
    }

    /* nothing to do */
    if (!numMaps) {
	gunzip_close(fd);
	return oldPaths;
    }

    qsort(map, numMaps, sizeof(*map), myCpioFileMapCmp);
    rc = myCpioInstallArchive(fd, map, numMaps, NULL, NULL, &failedFile);

    gunzip_close(fd);

    for (m = modNames, i = 0, numMaps = 0; *m; m++, i++) {
	if (!oldPaths[i]) {
	    /* can't trust map; the order changed thanks to qsort */
	    sprintf(fn, "/tmp/%s.o", modNames[i]);
	    if (!stat(fn, &sb)) {
		if (ddi)
		    logMessage("module %s found on driver disk %s (%d bytes)", 
				modNames[i], ddi->title, sb.st_size);
		oldPaths[i] = strdup(fn);
	    }
	    numMaps++;
	}
    }

    return oldPaths;
}

static int doLoadModules(const char * origModNames, moduleList modLoaded, 
		    moduleDeps modDeps, moduleInfoSet modInfo, int flags,
		    const char * argModule, char ** args) {
    char * modNames;
    char * end, * start, * next;
    char ** initialList;
    int i;
    char ** list, ** l;
    char ** paths, ** p;
    struct moduleInfo * mi;
    char items[1024] = "";

    start = modNames = alloca(strlen(origModNames) + 1);
    strcpy(modNames, origModNames);

    next = start, i = 1;
    while (*next) {
	if (*next == ':') i++;
	next++;
    }

    initialList = alloca(sizeof(*initialList) * (i + 1));

    i = 0;
    while (start) {
	next = end = strchr(start, ':');
	if (next) *end = '\0', next++;

	if (mlModuleInList(start, modLoaded)) {
	    /* already loaded */
	    start = next;
	    continue;
	}

	initialList[i++] = start;

	start = next;
    }
    initialList[i] = NULL;

    list = tsortModules(modLoaded, modDeps, initialList, 0, NULL, NULL);
    if (!list) {
	logMessage("found loop in module dependencies; not inserting anything");
	return 1;
    }

    for (i = 0; list[i]; i++) {
	strcat(items, " ");
	strcat(items, list[i]);
    }

    logMessage("modules to insert%s", items);

    paths = NULL;
    if (modInfo) {
	for (i = 0; list[i]; i++) {
	    if (paths && paths[i]) continue;
	    mi = isysFindModuleInfo(modInfo, list[i]);

	    if (mi && mi->locationID)
		paths = extractModules(mi->locationID, list, paths); 
	    }
    }

    paths = extractModules(NULL, list, paths); 
    i = 0;
    if (!paths) {
	logMessage("no modules found -- aborting insertion");
	i++;
    } else {
	*items = '\0';

	/* if any modules weren't found, holler */
	for (l = list, p = paths; *l && p; l++, p++) {
	    if (!*p) {
		if (*items) strcat(items, " ");
		strcat(items, *l);
		i++;
	    }
	}

	if (*items) logMessage("modules %s not found", items);
    }

    /* insert the modules now */
    for (l = list, p = paths; paths && *l; l++, p++) {
	if (*p && loadModule(*l, *p, modLoaded, 
		       (argModule && !strcmp(argModule, *l)) ? args : NULL, 
		       modInfo, flags)) {
	    logMessage("failed to insert %s", *p);
	} else if (*p) {
	    logMessage("inserted %s", *p);
	}
    }

    if (!FL_TESTING(flags)) {
	int fd;
	
	fd = open("/tmp/modules.conf", O_WRONLY | O_CREAT | O_APPEND,
		  0666);
	if (fd == -1) {
	    logMessage("error appending to /tmp/modules.conf: %s\n", 
		       strerror(errno));
	} else {
	    mlWriteConfModules(modLoaded, fd);
	    close(fd);
	}
    }

    for (p = paths; p && *p; p++) {
	unlink(*p);
	free(*p);
    }

    free(paths);
    free(list);

    logMessage("load module set done");

    return i;
}

/* loads a single module (preloading and dependencies), passing "args" to
   the module as its argument */
int mlLoadModule(const char * modName, 
		    moduleList modLoaded, moduleDeps modDeps, char ** args, 
		    moduleInfoSet modInfo, int flags) {
    return doLoadModules(modName, modLoaded, modDeps, modInfo, flags,
			 modName, args);
}

/* loads a : separated list of modules. the arg only applies to the
   first module in the list */
int mlLoadModuleSet(const char * modNames, 
		    moduleList modLoaded, moduleDeps modDeps, 
		    moduleInfoSet modInfo, int flags) {
    return doLoadModules(modNames, modLoaded, modDeps, modInfo, flags,
			 NULL, NULL);
}

char ** mlGetDeps(moduleDeps modDeps, const char * modName) {
    moduleDeps dep;
    
    for (dep = modDeps; dep->name && strcmp(dep->name, modName); dep++);

    if (dep) return dep->deps;

    return NULL;
}

int mlModuleInList(const char * modName, moduleList list) {
    int i;

    if (!list) return 0;

    for (i = 0; i < list->numModules; i++)
        if (!strcmp(list->mods[i].name, modName)) return 1;

    return 0;
}

int mlWriteConfModules(moduleList list, int fd) {
    int i;
    struct loadedModuleInfo * lm;
    char buf[200], buf2[200];
    int scsiNum;
    int ethNum;
    int trNum = 0;
    int iucvNum = 0;
    char ** arg;
    char *iucvopt;

    if (!list) return 0;

    scsiNum = scsiCount();
    
    for (i = 0, lm = list->mods; i < list->numModules; i++, lm++) {
    	if (!lm->weLoaded) continue;
	if (lm->written) continue;
	lm->written = 1;
	if (lm->major != DRIVER_NONE) {
	    strcpy(buf, "alias ");
	    switch (lm->major) {
	      case DRIVER_CDROM:
		strcat(buf, "cdrom ");
		break;

	      case DRIVER_SCSI:
	      	if (scsiNum)
		    sprintf(buf2, "scsi_hostadapter%d ", scsiNum);
		else
		    strcpy(buf2, "scsi_hostadapter ");
		scsiNum++;
		strcat(buf, buf2);
		break;

	      case DRIVER_NET:
	        switch (lm->minor) {
		  case DRIVER_MINOR_ETHERNET:
		      for (ethNum = lm->firstDevNum; 
				ethNum <= lm->lastDevNum; ethNum++) {
			  sprintf(buf2, "eth%d ", ethNum);
			  if (ethNum != lm->lastDevNum) {
			      strcat(buf2, lm->name);
			      strcat(buf2, "\nalias ");
			  }
			  strcat(buf, buf2);
			  if(!strstr(lm->name, "iucv"))
			     iucvNum++;
		      }
			  
		      break;
		  case DRIVER_MINOR_TR:
		      sprintf(buf2, "tr%d ", trNum++);
		      strcat(buf, buf2);
		      break;
		  default:
		}

	      default:
	    }

	    strcat(buf, lm->name);
	    strcat(buf, "\n");
	    write(fd, buf, strlen(buf));
	}

	if (lm->args) {
	    strcpy(buf, "options ");
	    strcat(buf, lm->name);
	    for (arg = lm->args; *arg; arg++) {
		strcat(buf, " ");
		strcat(buf, *arg);
	    }
	    strcat(buf, "\n");
	    write(fd, buf, strlen(buf));
	}
    }
    if (iucvNum) {
        iucvopt = getenv("IUCV");
        if (iucvopt && *iucvopt) {
        sprintf(buf, "options netiucv %s\n", iucvopt);
        write(fd, buf, strlen(buf));
	}
    }
    return 0;
}

/* simple removal of a loaded module which is going to be reloaded.
 * Note that this does NOT modify the modLoaded struct at all 
 */
int simpleRemoveLoadedModule(const char * modName, moduleList modLoaded,
			     int flags) {
    int status, rc = 0;
    pid_t child;

    if (!mlModuleInList(modName, modLoaded)) {
	return 0;
    }    
    
    if (FL_TESTING(flags)) {
	logMessage("would have rmmod %s", modName);
	rc = 0;
    } else {
	logMessage("going to rmmod %s", modName);
	if (!(child = fork())) {
	    int fd = open("/dev/tty3", O_RDWR);

	    dup2(fd, 0);
	    dup2(fd, 1);
	    dup2(fd, 2);
	    close(fd);

	    execl("/sbin/rmmod", "/sbin/rmmod", modName, NULL);
	    _exit(rc);
	}

	waitpid(child, &status, 0);
	
	if (!WIFEXITED(status) || WEXITSTATUS(status)) {
	    rc = 1;
	} else {
	    rc = 0;
	}
    }
    return rc;
}

/* simple reinsertion of a module; just looks for the module and reloads it
 * if we think it was already loaded
 */
int reloadUnloadedModule(char * modName, void * location,
			 moduleList modLoaded, char ** args, int flags) {
    char fileName[200];
    int rc, status;
    pid_t child;
    char ** path;
    char ** argPtr;
    char * list[2];

    if (!mlModuleInList(modName, modLoaded)) {
	return 0;
    }

    list[0] = modName;
    list[1] = NULL;

    if (location)
	path = extractModules(location, tsortModules(modLoaded, NULL, list, 0, NULL, NULL), (char **) NULL);

    sprintf(fileName, "%s.o", modName);
    for (argPtr = args; argPtr && *argPtr; argPtr++)  {
	strcat(fileName, " ");
	strcat(fileName, *argPtr);
    }

    sprintf(fileName, "%s.o", modName);

    if (FL_TESTING(flags)) {
	logMessage("would have insmod %s", fileName);
	rc = 0;
    } else {
	logMessage("going to insmod %s", fileName);

	if (!(child = fork())) {
	    int fd = open("/dev/tty3", O_RDWR);

	    dup2(fd, 0);
	    dup2(fd, 1);
	    dup2(fd, 2);
	    close(fd);

	    rc = insmod(fileName, NULL, args);
	    _exit(rc);
	}

	waitpid(child, &status, 0);

	if (!WIFEXITED(status) || WEXITSTATUS(status)) {
	    rc = 1;
	} else {
	    rc = 0;
	}
    }

    logMessage("reloadModule returning %d", rc);
    return rc;
}