/*
* -=FreeBSD vnode KLD=-
*
* example how to catch i/o stream via the
* vnode tables.
*
* written 2003 by Gino "dairaen" Thomas
* dairaen@nux-acid.org (http://nux-acid.org)
*
* 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/proc.h>
#include <sys/syscall.h>
#include <sys/file.h>
#include <sys/malloc.h>
#include <sys/namei.h>
#include <sys/kernel.h>
#include <dirent.h>
#include "vnode.h"


int new_ufs_lookup(struct vop_cachedlookup_args *);

vop_t *old_ufs_lookup;


int
new_ufs_lookup(struct vop_cachedlookup_args *ap)
{

    struct componentname *cnp = ap->a_cnp;

    printf("[UFS]cnp->cn_nameptr is: %s\n",  cnp->cn_nameptr);
    printf("[UFS]cnp->cn_pnbuf is: %s\n",  cnp->cn_pnbuf);
    printf("[UFS]cnp->cn_cred->cr_uid is: %d\n",  cnp->cn_cred->cr_uid);

    return(old_ufs_lookup(ap));
}


static struct sysent new_ufs_lookup_sysent = {
    2,
    (sy_call_t *)new_ufs_lookup
};


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


extern vop_t **ufs_vnodeop_p;



static int load(struct module *module, int cmd, void *arg)
    {
     int err = 0;
        switch(cmd) {
            case MOD_LOAD:
                old_ufs_lookup = ufs_vnodeop_p[VOFFSET(vop_lookup)];
                ufs_vnodeop_p[VOFFSET(vop_lookup)] = (vop_t *) new_ufs_lookup;
                break;
            case MOD_UNLOAD:
                ufs_vnodeop_p[VOFFSET(vop_lookup)] = old_ufs_lookup;
                break;
            default:
                err = EINVAL;
                break;
        }
        return(err);
    }


/* syscall macro filled out*/
  SYSCALL_MODULE(new_ufs_lookup, &syscall_num,  &new_ufs_lookup_sysent,  load,  NULL);