summaryrefslogtreecommitdiffstats
path: root/isys/otherinsmod.c
blob: 8c9cd725a6439013677059784b841b0070d81c17 (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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/wait.h>

#include "cpio.h"
#include "isys.h"

/* hack */
int insmod_main(int argc, char ** argv);
int rmmod_main(int argc, char ** argv);

int ourInsmodCommand(int argc, char ** argv) {
    char * file;
    char finalName[100];
    char * chptr;
    FD_t fd;
    int rc, rmObj = 0;
    int sparc64 = 0;
#ifdef __sparc__

    struct utsname u;

    if (!uname(&u) && !strcmp(u.machine, "sparc64"))
       sparc64 = 1;
#endif

    if (argc < 2) {
	fprintf(stderr, "usage: insmod <module>.o [params]\n");
	return 1;
    }

    file = argv[1];
    if (access(file, R_OK)) {
	/* it might be having a ball */
	fd = fdOpen(sparc64 ?
		    "/modules/modules64.cgz" : "/modules/modules.cgz",
		    O_RDONLY, 0);
	if (fdFileno(fd) < 0) {
	    return 1;
	}

	chptr = strrchr(file, '/');
	if (chptr) file = chptr + 1;
	sprintf(finalName, "/tmp/%s", file);

	if (installCpioFile(fd, file, finalName, 0))
	    return 1;

	rmObj = 1;
	file = finalName;
    }

    argv[1] = file;

#ifdef __sparc__
    if (sparc64) {
       int pid, status;
       
       if (!(pid = fork())) {
           execv("/bin/insmod64", argv);
           exit(-1);
       }
       waitpid(pid, &status, 0);
       if (WIFEXITED(status))
           rc = WEXITSTATUS(status);
       else
           rc = -1;
    } else
#endif
       rc = insmod_main(argc, argv);
    
    if (rmObj) unlink(file);

    return rc;
}

int rmmod(char * modName) {
    pid_t child;
    int status;
    char * argv[] = { "/bin/rmmod", modName, NULL };
    int argc = 2;
    int rc = 0;

    if ((child = fork()) == 0) {
	exit(rmmod_main(argc, argv));
    }

    waitpid(child, &status, 0);

    if (WIFEXITED(status))
       rc = WEXITSTATUS(status);
    else
       rc = -1;

    return rc;
}

int insmod(char * modName, char ** args) {
    int argc;
    char ** argv;
    int rc = 0;
    pid_t child;
    int status;

    argc = 2;
    for (argv = args; argv && *argv; argv++, argc++);

    argv = malloc(sizeof(*argv) * (argc + 1));
    argv[0] = "/bin/insmod";
    argv[1] = modName;
    memcpy(argv + 2, args, argc - 1);

    if ((child = fork()) == 0) {
	exit(ourInsmodCommand(argc, argv));
    }

    waitpid(child, &status, 0);

    if (WIFEXITED(status))
       rc = WEXITSTATUS(status);
    else
       rc = -1;

    return rc;
}