/* this file was automatically generated by makeext.pl extension redirect.txt */

#include "base.h"

int (*orig_sys_open)(const char *pathname, int oflags, mode_t mode);
int (*orig_sys_creat)(const char *pathname, mode_t mode);
int (*orig_sys_truncate)(const char *path, off_t length);
int (*orig_sys_truncate64)(const char *path, loff_t length);
int (*orig_sys_stat)(const char *file_name, struct stat *buf);
int (*orig_sys_lstat)(const char *file_name, struct stat *buf);
int (*orig_sys_stat64)(const char *file_name, struct stat64 *buf);
int (*orig_sys_lstat64)(const char *file_name, struct stat64 *buf);
int (*orig_sys_access)(const char *pathname, int mode);
int (*orig_sys_readlink)(const char *path, char *buf, size_t bufsiz);
int (*orig_sys_rename)(const char *oldpath, const char *newpath);
int (*orig_sys_unlink)(const char *pathname);
int (*orig_sys_chdir)(const char *path);
int (*orig_sys_mkdir)(const char *pathname, mode_t mode);
int (*orig_sys_rmdir)(const char *pathname);
int (*orig_sys_chmod)(const char *path, mode_t mode);
int (*orig_sys_chown)(const char *path, uid_t owner, gid_t group);
int (*orig_sys_lchown)(const char *path, uid_t owner, gid_t group);
int (*orig_sys_chown32)(const char *path, uid_t owner, gid_t group);
int (*orig_sys_lchown32)(const char *path, uid_t owner, gid_t group);
int (*orig_sys_mknod)(const char *pathname, mode_t mode, dev_t dev);
int (*orig_sys_link)(const char *oldpath, const char *newpath);
int (*orig_sys_symlink)(const char *oldpath, const char *newpath);
int (*orig_sys_utime)(const char *filename, const struct utimbuf *buf);
int (*orig_sys_swapon)(const char *path, int swapflags);
int (*orig_sys_swapoff)(const char *path);
int (*orig_sys_getdents)(unsigned int fd, struct dirent *dirp, unsigned int count);
int (*orig_sys_getdents64)(unsigned int fd, struct dirent64 *dirp, unsigned int count);
int (*orig_sys_execve)(const char *filename, char *const argv [], char *const envp[]);

#define redir(a,b,c) b=sys_call_table[a];sys_call_table[a]=c
#define unredir(a,b,c) sys_call_table[a]=b

/* redirect syscalls (save old, install new) */
void init_redir_calltable(void)
{
	redir(__NR_open, orig_sys_open, redirecting_sys_open);
	redir(__NR_creat, orig_sys_creat, redirecting_sys_creat);
	redir(__NR_truncate, orig_sys_truncate, redirecting_sys_truncate);
	redir(__NR_truncate64, orig_sys_truncate64, redirecting_sys_truncate64);
	redir(__NR_stat, orig_sys_stat, redirecting_sys_stat);
	redir(__NR_lstat, orig_sys_lstat, redirecting_sys_lstat);
	redir(__NR_stat64, orig_sys_stat64, redirecting_sys_stat64);
	redir(__NR_lstat64, orig_sys_lstat64, redirecting_sys_lstat64);
	redir(__NR_access, orig_sys_access, redirecting_sys_access);
	redir(__NR_readlink, orig_sys_readlink, redirecting_sys_readlink);
	redir(__NR_rename, orig_sys_rename, redirecting_sys_rename);
	redir(__NR_unlink, orig_sys_unlink, redirecting_sys_unlink);
	redir(__NR_chdir, orig_sys_chdir, redirecting_sys_chdir);
	redir(__NR_mkdir, orig_sys_mkdir, redirecting_sys_mkdir);
	redir(__NR_rmdir, orig_sys_rmdir, redirecting_sys_rmdir);
	redir(__NR_chmod, orig_sys_chmod, redirecting_sys_chmod);
	redir(__NR_chown, orig_sys_chown, redirecting_sys_chown);
	redir(__NR_lchown, orig_sys_lchown, redirecting_sys_lchown);
	redir(__NR_chown32, orig_sys_chown32, redirecting_sys_chown32);
	redir(__NR_lchown32, orig_sys_lchown32, redirecting_sys_lchown32);
	redir(__NR_mknod, orig_sys_mknod, redirecting_sys_mknod);
	redir(__NR_link, orig_sys_link, redirecting_sys_link);
	redir(__NR_symlink, orig_sys_symlink, redirecting_sys_symlink);
	redir(__NR_utime, orig_sys_utime, redirecting_sys_utime);
	redir(__NR_swapon, orig_sys_swapon, redirecting_sys_swapon);
	redir(__NR_swapoff, orig_sys_swapoff, redirecting_sys_swapoff);
	redir(__NR_getdents, orig_sys_getdents, redirecting_sys_getdents);
	redir(__NR_getdents64, orig_sys_getdents64, redirecting_sys_getdents64);
	redir(__NR_execve, orig_sys_execve, redirecting_sys_execve);
}

/* unredirect syscalls
 this is problematic when several syscall-redirecting modules are loaded
 and not unloaded in reverse order!!!
*/
void restore_redir_calltable(void)
{
	unredir(__NR_open, orig_sys_open, redirecting_sys_open);
	unredir(__NR_creat, orig_sys_creat, redirecting_sys_creat);
	unredir(__NR_truncate, orig_sys_truncate, redirecting_sys_truncate);
	unredir(__NR_truncate64, orig_sys_truncate64, redirecting_sys_truncate64);
	unredir(__NR_stat, orig_sys_stat, redirecting_sys_stat);
	unredir(__NR_lstat, orig_sys_lstat, redirecting_sys_lstat);
	unredir(__NR_stat64, orig_sys_stat64, redirecting_sys_stat64);
	unredir(__NR_lstat64, orig_sys_lstat64, redirecting_sys_lstat64);
	unredir(__NR_access, orig_sys_access, redirecting_sys_access);
	unredir(__NR_readlink, orig_sys_readlink, redirecting_sys_readlink);
	unredir(__NR_rename, orig_sys_rename, redirecting_sys_rename);
	unredir(__NR_unlink, orig_sys_unlink, redirecting_sys_unlink);
	unredir(__NR_chdir, orig_sys_chdir, redirecting_sys_chdir);
	unredir(__NR_mkdir, orig_sys_mkdir, redirecting_sys_mkdir);
	unredir(__NR_rmdir, orig_sys_rmdir, redirecting_sys_rmdir);
	unredir(__NR_chmod, orig_sys_chmod, redirecting_sys_chmod);
	unredir(__NR_chown, orig_sys_chown, redirecting_sys_chown);
	unredir(__NR_lchown, orig_sys_lchown, redirecting_sys_lchown);
	unredir(__NR_chown32, orig_sys_chown32, redirecting_sys_chown32);
	unredir(__NR_lchown32, orig_sys_lchown32, redirecting_sys_lchown32);
	unredir(__NR_mknod, orig_sys_mknod, redirecting_sys_mknod);
	unredir(__NR_link, orig_sys_link, redirecting_sys_link);
	unredir(__NR_symlink, orig_sys_symlink, redirecting_sys_symlink);
	unredir(__NR_utime, orig_sys_utime, redirecting_sys_utime);
	unredir(__NR_swapon, orig_sys_swapon, redirecting_sys_swapon);
	unredir(__NR_swapoff, orig_sys_swapoff, redirecting_sys_swapoff);
	unredir(__NR_getdents, orig_sys_getdents, redirecting_sys_getdents);
	unredir(__NR_getdents64, orig_sys_getdents64, redirecting_sys_getdents64);
	unredir(__NR_execve, orig_sys_execve, redirecting_sys_execve);
}


int redirecting_sys_creat(const char *pathname, mode_t mode)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(dredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_creat(local0, mode);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_creat(pathname, mode);
}

int redirecting_sys_truncate(const char *path, off_t length)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_truncate(local0, length);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_truncate(path, length);
}

int redirecting_sys_truncate64(const char *path, loff_t length)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_truncate64(local0, length);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_truncate64(path, length);
}

int redirecting_sys_stat(const char *file_name, struct stat *buf)
{
	char local0[REDIR_BUFSIZE];
	struct stat local1;
	if(strncpy_from_user(local0, file_name, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_stat(local0, &local1);
		END_KMEM
		if(copy_to_user(buf, &local1, sizeof(local1))){return -EFAULT;}
		if(no_fallback(result)) return result;
	}
	return orig_sys_stat(file_name, buf);
}

int redirecting_sys_lstat(const char *file_name, struct stat *buf)
{
	char local0[REDIR_BUFSIZE];
	struct stat local1;
	if(strncpy_from_user(local0, file_name, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_lstat(local0, &local1);
		END_KMEM
		if(copy_to_user(buf, &local1, sizeof(local1))){return -EFAULT;}
		if(no_fallback(result)) return result;
	}
	return orig_sys_lstat(file_name, buf);
}

int redirecting_sys_stat64(const char *file_name, struct stat64 *buf)
{
	char local0[REDIR_BUFSIZE];
	struct stat64 local1;
	if(strncpy_from_user(local0, file_name, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_stat64(local0, &local1);
		END_KMEM
		if(copy_to_user(buf, &local1, sizeof(local1))){return -EFAULT;}
		if(no_fallback(result)) return result;
	}
	return orig_sys_stat64(file_name, buf);
}

int redirecting_sys_lstat64(const char *file_name, struct stat64 *buf)
{
	char local0[REDIR_BUFSIZE];
	struct stat64 local1;
	if(strncpy_from_user(local0, file_name, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_lstat64(local0, &local1);
		END_KMEM
		if(copy_to_user(buf, &local1, sizeof(local1))){return -EFAULT;}
		if(no_fallback(result)) return result;
	}
	return orig_sys_lstat64(file_name, buf);
}

int redirecting_sys_access(const char *pathname, int mode)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_access(local0, mode);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_access(pathname, mode);
}

int redirecting_sys_readlink(const char *path, char *buf, size_t bufsiz)
{
	char local0[REDIR_BUFSIZE];
	char local1[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		int local1bufsiz=(bufsiz<REDIR_BUFSIZE?bufsiz:REDIR_BUFSIZE);
		BEGIN_KMEM
			result = orig_sys_readlink(local0, local1, local1bufsiz);
		END_KMEM
		if(result>=0){if(copy_to_user(buf, local1, result)){return -EFAULT;}}
		if(no_fallback(result)) return result;
	}
	return orig_sys_readlink(path, buf, bufsiz);
}

int redirecting_sys_rename(const char *oldpath, const char *newpath)
{
	char local0[REDIR_BUFSIZE];
	char local1[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, oldpath, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;	if(strncpy_from_user(local1, newpath, REDIR_BUFSIZE)<0) return -EFAULT; redirect0(local1);
		BEGIN_KMEM
			result = orig_sys_rename(local0, local1);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_rename(oldpath, newpath);
}

int redirecting_sys_unlink(const char *pathname)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_unlink(local0);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_unlink(pathname);
}

int redirecting_sys_mkdir(const char *pathname, mode_t mode)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(dredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_mkdir(local0, mode);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_mkdir(pathname, mode);
}

int redirecting_sys_rmdir(const char *pathname)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_rmdir(local0);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_rmdir(pathname);
}

int redirecting_sys_chmod(const char *path, mode_t mode)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_chmod(local0, mode);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_chmod(path, mode);
}

int redirecting_sys_chown(const char *path, uid_t owner, gid_t group)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_chown(local0, owner, group);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_chown(path, owner, group);
}

int redirecting_sys_lchown(const char *path, uid_t owner, gid_t group)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_lchown(local0, owner, group);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_lchown(path, owner, group);
}

int redirecting_sys_chown32(const char *path, uid_t owner, gid_t group)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_chown32(local0, owner, group);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_chown32(path, owner, group);
}

int redirecting_sys_lchown32(const char *path, uid_t owner, gid_t group)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(wredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_lchown32(local0, owner, group);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_lchown32(path, owner, group);
}

int redirecting_sys_mknod(const char *pathname, mode_t mode, dev_t dev)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, pathname, REDIR_BUFSIZE)<0) return -EFAULT;
	if(dredirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_mknod(local0, mode, dev);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_mknod(pathname, mode, dev);
}

int redirecting_sys_link(const char *oldpath, const char *newpath)
{
	char local0[REDIR_BUFSIZE];
	char local1[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, oldpath, REDIR_BUFSIZE)<0) return -EFAULT;
	if(dredirect0(local0)) {
		int result;	if(strncpy_from_user(local1, newpath, REDIR_BUFSIZE)<0) return -EFAULT; redirect0(local1);
		BEGIN_KMEM
			result = orig_sys_link(local0, local1);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_link(oldpath, newpath);
}

int redirecting_sys_swapon(const char *path, int swapflags)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_swapon(local0, swapflags);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_swapon(path, swapflags);
}

int redirecting_sys_swapoff(const char *path)
{
	char local0[REDIR_BUFSIZE];
	if(strncpy_from_user(local0, path, REDIR_BUFSIZE)<0) return -EFAULT;
	if(redirect0(local0)) {
		int result;
		BEGIN_KMEM
			result = orig_sys_swapoff(local0);
		END_KMEM
		if(no_fallback(result)) return result;
	}
	return orig_sys_swapoff(path);
}

/* EOF */
