/*
*
* -=FreeBSD chdir_watcher=-
*
* just an chdir syscall replacement,
* shows how easily you can watch what
* happens on a system, you could do that with
* all syscalls to have a full overview of bg
* operations and such stuff.
*
* Copyright (c) 2003, Gino Thomas
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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. Neither the name of the nux-acid.org nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*
*
*/

#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/linker.h>
#include <sys/sysproto.h>
#include <sys/syscall.h>

/* args for new syscall */
struct new_chdir_args {
	char	*path;
};


/* the syscall itself */
static int new_chdir(struct proc *p, struct new_chdir_args *uap)
{

  int size = 0;
  int argv_size;


  printf("---------------------------------\n");
 
	printf("[chdir] uap->path: 'cd %s'\n",uap->path);

  printf("---------------------------------\n");
   
 


  return(chdir(p,uap));
}

/* Args from execve and pointer to new syscall */
static struct sysent new_chdir_sysent = {
        (sizeof(struct chdir_args) / sizeof(register_t)),
        (sy_call_t *)new_chdir
};


/* set the internal ID for the call, NO_SYSCALL means next free one */
static int syscall_num = NO_SYSCALL;


/* the load_handler, called by kldload/kldunload and shutdown */
static int
  load_handler(struct module *m, int what, void *arg)
  {
          int err = 0;

          switch (what) {
          case MOD_LOAD:
                  sysent[SYS_chdir]=new_chdir_sysent;
                  break;
          case MOD_UNLOAD:
                  sysent[SYS_chdir].sy_call = (sy_call_t *)chdir;
                  break;
          default:
                  err = EINVAL;
                  break;
      }
          return(err);
  }


/* syscall macro filled out*/
  SYSCALL_MODULE(
  new_chdir,
  &syscall_num,
  &new_chdir_sysent,
  load_handler,
  NULL);