/*
 * IBM Dynamic Probes
 * Copyright (c) International Business Machines Corp., 2000
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
 */
#include <linux/dprobes.h>
#include <linux/hook.h>

void dprobes_hooks_code_start(void) { }

#ifdef CONFIG_MODULES

USE_HOOK(SYS_INIT_MODULE);
USE_HOOK(SYS_INIT_MODULE_ERROR);
USE_HOOK(FREE_MODULE);

static int insmod_exit(struct hook *h, struct module * mod)
{
	dp_insmod(mod);
	return HOOK_CONTINUE;
}	

static int remmod_exit(struct hook *h, struct module * mod)
{
	dp_remmod(mod);
	return HOOK_CONTINUE;
}	

struct hook_rec insmod_hook_rec;
struct hook_rec insmod_err_hook_rec;
struct hook_rec remmod_hook_rec;

static int register_core_dprobes_hooks(void)
{
	int retval;
	
	insmod_hook_rec.hook_exit = insmod_exit;
	if ((retval = hook_exit_register(&SYS_INIT_MODULE, &insmod_hook_rec)))
		goto err;

	insmod_err_hook_rec.hook_exit = remmod_exit;
	if ((retval = hook_exit_register(&SYS_INIT_MODULE_ERROR, &insmod_err_hook_rec)))
		goto err1;

	remmod_hook_rec.hook_exit = remmod_exit;
	if ((retval = hook_exit_register(&FREE_MODULE, &remmod_hook_rec)))
		goto err2;
	return 0;
	
err2:	hook_exit_deregister(&insmod_err_hook_rec);
err1:	hook_exit_deregister(&insmod_hook_rec);
err:	return retval;
}

static int deregister_core_dprobes_hooks(void)
{
	hook_exit_deregister(&insmod_hook_rec);
	hook_exit_deregister(&insmod_err_hook_rec);
	hook_exit_deregister(&remmod_hook_rec);
	return 0;
}

static int arm_all_core_dprobes_hooks(void)
{
	hook_exit_arm(&insmod_hook_rec);
	hook_exit_arm(&insmod_err_hook_rec);
	hook_exit_arm(&remmod_hook_rec);
	return 0;
}

static int disarm_all_core_dprobes_hooks(void)
{
	hook_exit_disarm(&remmod_hook_rec);
	hook_exit_disarm(&insmod_err_hook_rec);
	hook_exit_disarm(&insmod_hook_rec);
	return 0;
}
#else
static inline int register_core_dprobes_hooks(void) { return 0; }
static inline int deregister_core_dprobes_hooks(void) { return 0; }
static inline int arm_all_core_dprobes_hooks(void) { return 0; }
static inline int disarm_all_core_dprobes_hooks(void) { return 0; }
#endif /* !CONFIG_MODULES */

int register_dprobes_hooks(void)
{
	int retval;
	static int done = 0;
	if (done)
		return 0;
	done = 1;
	
	if ((retval = register_core_dprobes_hooks()))
		goto err;
	if ((retval = register_asm_dprobes_hooks()))
		goto err1;
	return 0;
	
err1:	deregister_core_dprobes_hooks();
err:	return retval;
}

int deregister_dprobes_hooks(void)
{
	deregister_core_dprobes_hooks();
	deregister_asm_dprobes_hooks();
	return 0;
}

int arm_all_dprobes_hooks(void)
{
	arm_all_core_dprobes_hooks();
	arm_all_asm_dprobes_hooks();
	return 0;
}

int disarm_all_dprobes_hooks(void)
{
	disarm_all_core_dprobes_hooks();
	disarm_all_asm_dprobes_hooks();
	return 0;
}

void dprobes_hooks_code_end(void) { }
