/*
   Should be compiled with
     gcc -Wall -c -O2 countermod.c
   This results in countermod.o, which you can install into the kernel by
     insmod countermod.o
   Note the -O2 flag: it is necessary, due to a mistake in 2.2 kernel.
*/

#ifndef MODULE
#define MODULE
#endif
#ifndef __KERNEL__
#define __KERNEL__
#endif

#include <linux/module.h>
#include <linux/kernel.h>

/* These can be made into a .h file. */
#define __NR_inccount 250

static int counter = 0;

asmlinkage int sys_inccount(void) {
	++counter;
	if (counter < 0)
		counter = 0;
	return counter;
}

/* The module init and cleanup code */
extern void *sys_call_table[];
#define sys_ni_syscall sys_call_table[0]

int init_module(void) {
	/* Don't install it if the syscall slots are used,
	   i.e., not storing sys_ni_syscall */
	if (sys_call_table[__NR_inccount] != sys_ni_syscall) {
		printk("Syscall slots already used.");
		return 1;
	}
	sys_call_table[__NR_inccount] = sys_inccount;
        return 0;
}

void cleanup_module(void) {
	/* Warn if our slots are not the value we set */
	if (sys_call_table[__NR_inccount] != sys_inccount) {
		printk("Syscall slots changed!  The kernel is unstable now.\n");
	}
        sys_call_table[__NR_inccount] = sys_ni_syscall;
}

