#include <linux/module.h>
#include<linux/sched.h>
#include<linux/binfmts.h>
#include <linux/fs.h>
#include <linux/errno.h>


/*The Hooker function*/
static int load_hook(struct linux_binprm *bprm, struct pt_regs *regs)
{
  printk("\nAbhijit::The file execution hooked");
  
  printk("\nAbhijit::The file being launched is : %s", bprm->filename);
   /*return search_binary_handler(bprm,regs);*/
  return -ENOEXEC;
  
}

int hook_shlib(int fd)
{
  printk("\nAbhijit::hooking shared lib ");
  return -1;
};
  
int hook_core_dump(long signr, struct pt_regs * regs)
{
  printk("\nAbhijit::hooking core dump");
  return -1;
}

/*The structure to override the hook*/

struct linux_binfmt hook_format = {
  .module = THIS_MODULE,
  .load_binary = load_hook,
  .load_shlib = hook_shlib,
  .core_dump = hook_core_dump,
};

static int __init init_hooking_module(void)
{
  printk("\nAbhijit::Registering the hooking module");
  
  int err = 0;
   err = insert_binfmt(&hook_format);
   
   printk("\nAbhijit::The format register returned %d", err);
   
   return err;
}


static void __exit exit_hooking_module(void)
{
  printk("\nAbhijit::unregistering  the hooking module");
  
    unregister_binfmt(&hook_format);
 }

module_init(init_hooking_module);
module_exit(exit_hooking_module);

MODULE_AUTHOR("Abhijit Pawar <apawar.linux@gmail.com>");
MODULE_DESCRIPTION("A module to hook the application execution");
MODULE_LICENSE("GPL");
