1997-05-07 16:05:47 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Doug Rabson
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1997-05-07 16:05:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
1999-08-21 06:24:40 +00:00
|
|
|
#include <sys/eventhandler.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/linker.h>
|
1997-11-06 19:29:57 +00:00
|
|
|
#include <sys/proc.h>
|
2001-09-01 19:04:37 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-12-08 20:09:00 +00:00
|
|
|
static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2000-05-26 02:09:24 +00:00
|
|
|
typedef TAILQ_HEAD(, module) modulelist_t;
|
1997-05-07 16:05:47 +00:00
|
|
|
struct module {
|
2000-05-26 02:09:24 +00:00
|
|
|
TAILQ_ENTRY(module) link; /* chain together all modules */
|
|
|
|
TAILQ_ENTRY(module) flink; /* all modules in a file */
|
1997-05-07 16:05:47 +00:00
|
|
|
struct linker_file* file; /* file which contains this module */
|
|
|
|
int refs; /* reference count */
|
|
|
|
int id; /* unique id number */
|
|
|
|
char *name; /* module name */
|
|
|
|
modeventhand_t handler; /* event handler */
|
|
|
|
void *arg; /* argument for handler */
|
1999-01-09 14:59:50 +00:00
|
|
|
modspecific_t data; /* module specific data */
|
1997-05-07 16:05:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
|
|
|
|
|
|
|
|
static modulelist_t modules;
|
|
|
|
static int nextid = 1;
|
|
|
|
|
1999-08-21 06:24:40 +00:00
|
|
|
static void module_shutdown(void*, int);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1999-11-08 06:53:30 +00:00
|
|
|
static int
|
|
|
|
modevent_nop(module_t mod, int what, void* arg)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
static void
|
|
|
|
module_init(void* arg)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&modules);
|
1999-08-21 06:24:40 +00:00
|
|
|
EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
|
|
|
|
SHUTDOWN_PRI_DEFAULT);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
1998-10-16 03:55:01 +00:00
|
|
|
SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
static void
|
1999-08-21 06:24:40 +00:00
|
|
|
module_shutdown(void* arg1, int arg2)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
module_t mod;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(mod, &modules, link)
|
1997-05-07 16:05:47 +00:00
|
|
|
MOD_EVENT(mod, MOD_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-29 08:36:45 +00:00
|
|
|
module_register_init(const void *arg)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
1999-01-29 08:36:45 +00:00
|
|
|
const moduledata_t* data = (const moduledata_t*) arg;
|
1997-05-07 16:05:47 +00:00
|
|
|
int error;
|
1999-05-08 13:01:59 +00:00
|
|
|
module_t mod;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1999-05-08 13:01:59 +00:00
|
|
|
mod = module_lookupbyname(data->name);
|
2000-04-29 13:19:31 +00:00
|
|
|
if (mod == NULL)
|
1999-05-08 13:01:59 +00:00
|
|
|
panic("module_register_init: module named %s not found\n", data->name);
|
|
|
|
error = MOD_EVENT(mod, MOD_LOAD);
|
|
|
|
if (error) {
|
|
|
|
MOD_EVENT(mod, MOD_UNLOAD);
|
|
|
|
module_release(mod);
|
|
|
|
printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
|
1998-07-14 05:09:48 +00:00
|
|
|
data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
|
1999-05-08 13:01:59 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-08 13:01:59 +00:00
|
|
|
module_register(const moduledata_t *data, linker_file_t container)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
size_t namelen;
|
|
|
|
module_t newmod;
|
|
|
|
|
1999-05-08 13:01:59 +00:00
|
|
|
newmod = module_lookupbyname(data->name);
|
|
|
|
if (newmod != NULL) {
|
|
|
|
printf("module_register: module %s already exists!\n", data->name);
|
|
|
|
return EEXIST;
|
|
|
|
}
|
|
|
|
namelen = strlen(data->name) + 1;
|
1997-05-07 16:05:47 +00:00
|
|
|
newmod = (module_t) malloc(sizeof(struct module) + namelen,
|
|
|
|
M_MODULE, M_WAITOK);
|
|
|
|
if (newmod == 0)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
newmod->refs = 1;
|
|
|
|
newmod->id = nextid++;
|
|
|
|
newmod->name = (char *) (newmod + 1);
|
1999-05-08 13:01:59 +00:00
|
|
|
strcpy(newmod->name, data->name);
|
1999-11-08 06:53:30 +00:00
|
|
|
newmod->handler = data->evhand ? data->evhand : modevent_nop;
|
1999-05-08 13:01:59 +00:00
|
|
|
newmod->arg = data->priv;
|
1999-01-09 14:59:50 +00:00
|
|
|
bzero(&newmod->data, sizeof(newmod->data));
|
1997-05-07 16:05:47 +00:00
|
|
|
TAILQ_INSERT_TAIL(&modules, newmod, link);
|
|
|
|
|
1999-05-08 13:01:59 +00:00
|
|
|
if (container)
|
1998-10-10 00:03:07 +00:00
|
|
|
TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
|
1999-05-20 00:00:58 +00:00
|
|
|
newmod->file = container;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
module_reference(module_t mod)
|
|
|
|
{
|
|
|
|
MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
|
|
|
|
|
|
|
|
mod->refs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
module_release(module_t mod)
|
|
|
|
{
|
|
|
|
if (mod->refs <= 0)
|
|
|
|
panic("module_release: bad reference count");
|
|
|
|
|
|
|
|
MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
|
|
|
|
|
|
|
|
mod->refs--;
|
|
|
|
if (mod->refs == 0) {
|
|
|
|
TAILQ_REMOVE(&modules, mod, link);
|
|
|
|
if (mod->file) {
|
|
|
|
TAILQ_REMOVE(&mod->file->modules, mod, flink);
|
|
|
|
}
|
|
|
|
free(mod, M_MODULE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module_t
|
|
|
|
module_lookupbyname(const char* name)
|
|
|
|
{
|
|
|
|
module_t mod;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(mod, &modules, link) {
|
1997-05-07 16:05:47 +00:00
|
|
|
if (!strcmp(mod->name, name))
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_t
|
|
|
|
module_lookupbyid(int modid)
|
|
|
|
{
|
|
|
|
module_t mod;
|
|
|
|
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(mod, &modules, link) {
|
1997-05-07 16:05:47 +00:00
|
|
|
if (mod->id == modid)
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
module_unload(module_t mod)
|
|
|
|
{
|
|
|
|
return MOD_EVENT(mod, MOD_UNLOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
module_getid(module_t mod)
|
|
|
|
{
|
|
|
|
return mod->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_t
|
|
|
|
module_getfnext(module_t mod)
|
|
|
|
{
|
|
|
|
return TAILQ_NEXT(mod, flink);
|
|
|
|
}
|
|
|
|
|
1999-01-09 14:59:50 +00:00
|
|
|
void
|
|
|
|
module_setspecific(module_t mod, modspecific_t *datap)
|
|
|
|
{
|
|
|
|
mod->data = *datap;
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
/*
|
|
|
|
* Syscalls.
|
|
|
|
*/
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
1997-11-06 19:29:57 +00:00
|
|
|
modnext(struct proc* p, struct modnext_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
module_t mod;
|
2001-09-01 19:04:37 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
mtx_lock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = -1;
|
1997-05-07 16:05:47 +00:00
|
|
|
if (SCARG(uap, modid) == 0) {
|
|
|
|
mod = TAILQ_FIRST(&modules);
|
2001-09-01 19:04:37 +00:00
|
|
|
if (mod)
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = mod->id;
|
2001-09-01 19:04:37 +00:00
|
|
|
else
|
|
|
|
error = ENOENT;
|
|
|
|
goto done2;
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod = module_lookupbyid(SCARG(uap, modid));
|
2001-09-01 19:04:37 +00:00
|
|
|
if (mod == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto done2;
|
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
if (TAILQ_NEXT(mod, link))
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = TAILQ_NEXT(mod, link)->id;
|
1997-05-07 16:05:47 +00:00
|
|
|
else
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = 0;
|
2001-09-01 19:04:37 +00:00
|
|
|
done2:
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
1997-11-06 19:29:57 +00:00
|
|
|
modfnext(struct proc* p, struct modfnext_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
module_t mod;
|
2001-09-01 19:04:37 +00:00
|
|
|
int error;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = -1;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mod = module_lookupbyid(SCARG(uap, modid));
|
|
|
|
if (mod == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
} else {
|
|
|
|
error = 0;
|
|
|
|
if (TAILQ_NEXT(mod, flink))
|
|
|
|
p->p_retval[0] = TAILQ_NEXT(mod, flink)->id;
|
|
|
|
else
|
|
|
|
p->p_retval[0] = 0;
|
|
|
|
}
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
1999-01-09 14:59:50 +00:00
|
|
|
struct module_stat_v1 {
|
|
|
|
int version; /* set to sizeof(struct module_stat) */
|
|
|
|
char name[MAXMODNAME];
|
|
|
|
int refs;
|
|
|
|
int id;
|
|
|
|
};
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
1997-11-06 19:29:57 +00:00
|
|
|
modstat(struct proc* p, struct modstat_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
module_t mod;
|
|
|
|
int error = 0;
|
|
|
|
int namelen;
|
|
|
|
int version;
|
|
|
|
struct module_stat* stat;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
mod = module_lookupbyid(SCARG(uap, modid));
|
2001-09-01 19:04:37 +00:00
|
|
|
if (mod == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
stat = SCARG(uap, stat);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the version of the user's structure.
|
|
|
|
*/
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-09 14:59:50 +00:00
|
|
|
if (version != sizeof(struct module_stat_v1)
|
|
|
|
&& version != sizeof(struct module_stat)) {
|
1997-05-07 16:05:47 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(mod->name) + 1;
|
|
|
|
if (namelen > MAXMODNAME)
|
|
|
|
namelen = MAXMODNAME;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
1999-01-09 14:59:50 +00:00
|
|
|
/*
|
|
|
|
* >v1 stat includes module data.
|
|
|
|
*/
|
|
|
|
if (version == sizeof(struct module_stat)) {
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
|
1999-01-09 14:59:50 +00:00
|
|
|
goto out;
|
1999-01-09 16:50:04 +00:00
|
|
|
}
|
1999-01-09 14:59:50 +00:00
|
|
|
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
|
|
|
out:
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
1997-11-06 19:29:57 +00:00
|
|
|
modfind(struct proc* p, struct modfind_args* uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
char name[MAXMODNAME];
|
|
|
|
module_t mod;
|
|
|
|
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
|
1997-05-07 16:05:47 +00:00
|
|
|
goto out;
|
|
|
|
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_lock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
mod = module_lookupbyname(name);
|
2001-09-01 19:04:37 +00:00
|
|
|
if (mod == NULL)
|
1997-05-07 16:05:47 +00:00
|
|
|
error = ENOENT;
|
|
|
|
else
|
1997-11-06 19:29:57 +00:00
|
|
|
p->p_retval[0] = mod->id;
|
2001-09-01 19:04:37 +00:00
|
|
|
mtx_unlock(&Giant);
|
1997-05-07 16:05:47 +00:00
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|