summaryrefslogtreecommitdiffstats
path: root/share/examples/kld/misc/module/miscmod.c
blob: 07be90f592eb2efd633f9885f448e158102b6b1c (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
/* 08 Nov 1998*/
/*
 * miscmod.c
 *
 * 08 Nov 1998  Rajesh Vaidheeswarran  - adapted from the lkm miscmod.c
 *
 * Copyright (c) 1998 Rajesh Vaidheeswarran
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Rajesh Vaidheeswarran.
 * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 * Copyright (c) 1993 Terrence R. Lambert.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Terrence R. Lambert.
 * 4. The name Terrence R. Lambert may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/exec.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/conf.h>

#include "misccall.h"

/*
 * These two entries define our system call and module information.  We
 * have 0 arguments to our system call.
 */
static struct sysent newent = {
	0,	misccall		/* # of args, function pointer*/
};

/*
 * Miscellaneous modules must have their own save areas...
 */
static struct sysent	oldent;		/* save area for old callslot entry*/

/*
 * Number of syscall entries for a.out executables
 */
#define nsysent (aout_sysvec.sv_size)

/*
 * If you have a data structure to pass ...
 */

typedef struct misc_data {
    char name[20];
    struct sysent * nent;
    struct sysent * oent;
    int offset;
} misc_data_t;

static misc_data_t misc_arg = {
    "misccall",
    &newent,
    &oldent,
    -1
};

static int misc_load(module_t,   modeventtype_t, void *);

static moduledata_t misc_mod = {
    "misc_mod",
    misc_load,
    (void *)&misc_arg /* This is our real data */
};

/*
 * This function is called each time the module is loaded or unloaded.
 *
 * For the system call table, we duplicate the code in the kern_lkm.c
 * file for patching into the system call table.  We can tell what
 * has been allocated, etc. by virtue of the fact that we know the
 * criteria used by the system call loader interface.  We still
 * kick out the copyright to the console here (to give an example).
 */
static int
misc_load(mod, cmd, arg)
    module_t	mod;
    modeventtype_t cmd;
    void * arg;
{
    int i, err = 0;

    misc_data_t * args = (misc_data_t *)arg;

    switch (cmd) {
    case MOD_LOAD:
	
	/*
	 * Search the table looking for a slot...
	 */
	for(i = 1; i < nsysent; i++)
	    if(sysent[i].sy_call == (sy_call_t *)nosys)
		break;		/* found it!*/
	/* out of allocable slots?*/
	if(i == nsysent) {
	    err = ENFILE;
	    break;
	}

	/* save old -- we must provide our own data area*/
	bcopy(&sysent[i], args->oent, sizeof(struct sysent));

	/* replace with new*/
	bcopy(args->nent, &sysent[i], sizeof(struct sysent));

	/* done!*/
	args->offset = i;	/* slot in sysent[]*/


	/* if we make it to here, print copyright on console*/
	printf("\nSample Loaded kld module (system call)\n");
	printf("Copyright (c) 1998\n");
	printf("Rajesh Vaidheeswarran\n");
	printf("All rights reserved\n");
	printf("System call %s loaded into slot %d\n", args->name, 
	       args->offset);
	break;		/* Success*/

    case MOD_UNLOAD:
	/* current slot...*/
	i = args->offset;

	/* replace current slot contents with old contents*/
	bcopy(args->oent, &sysent[ i], sizeof( struct sysent));

	printf("Unloaded kld module (system call)\n");

	break;		/* Success*/

    default:	/* we only understand load/unload*/
	err = EINVAL;
	break;
    }

    return(err);
}

/* Now declare the module to the system */

DECLARE_MODULE(misc, misc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
OpenPOWER on IntegriCloud