# renameat ___________________________________________________
# long sys_renameat(int olddfd, const char __user *oldname,
#	int newdfd, const char __user *newname)
# new function with 2.6.16

@define _SYSCALL_RENAMEAT_NAME
%(
	name = "renameat"
%)

@define _SYSCALL_RENAMEAT_ARGSTR
%(
	argstr = sprintf("%s, %s, %s, %s", olddfd_str, oldname_str, newdfd_str, newname_str)
%)

@define _SYSCALL_RENAMEAT_REGARGS
%(
	olddfd = int_arg(1)
	olddfd_str = _dfd_str(olddfd)
	oldname = pointer_arg(2)
	oldname_str = user_string_quoted(oldname)
	oldname_str_unquoted = user_string_nofault(pointer_arg(2))
	newdfd = int_arg(3)
	newdfd_str = _dfd_str(newdfd)
	newname = pointer_arg(4)
	newname_str = user_string_quoted(newname)
	newname_str_unquoted = user_string_nofault(pointer_arg(4))
%)

@define _SYSCALL_RENAMEAT_REGARGS_STORE
%(
        if (@probewrite(olddfd))
          set_int_arg(1, olddfd)

        if (@probewrite(oldname))
          set_pointer_arg(2, oldname)

        if (@probewrite(oldname_str_unquoted))
          set_user_string_arg(pointer_arg(2), oldname_str_unquoted)

        if (@probewrite(newdfd))
          set_int_arg(3, newdfd)

        if (@probewrite(newname))
          set_pointer_arg(4, newname)

        if (@probewrite(newname_str_unquoted))
          set_user_string_arg(pointer_arg(4), newname_str_unquoted)
%)

probe syscall.renameat = dw_syscall.renameat !, nd_syscall.renameat ? {}
probe syscall.renameat.return = dw_syscall.renameat.return !,
                                nd_syscall.renameat.return ? {}

# dw_renameat _____________________________________________________

probe dw_syscall.renameat = kernel.function("sys_renameat").call ?
{
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	olddfd = __int32($olddfd)
	olddfd_str = _dfd_str(__int32($olddfd))
	oldname = $oldname
	oldname_str = user_string_quoted($oldname)
	newdfd = __int32($newdfd)
	newdfd_str = _dfd_str(__int32($newdfd))
	newname = $newname
	newname_str = user_string_quoted($newname)
	@_SYSCALL_RENAMEAT_ARGSTR
}
probe dw_syscall.renameat.return = kernel.function("sys_renameat").return ?
{
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_renameat _____________________________________________________

probe nd_syscall.renameat = nd1_syscall.renameat!, nd2_syscall.renameat!, tp_syscall.renameat
  { }

probe nd1_syscall.renameat = kprobe.function("sys_renameat") ?
{
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	asmlinkage()
	@_SYSCALL_RENAMEAT_REGARGS
	@_SYSCALL_RENAMEAT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.renameat = kprobe.function(@arch_syscall_prefix "sys_renameat") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_RENAMEAT_NAME
	@_SYSCALL_RENAMEAT_REGARGS
	@_SYSCALL_RENAMEAT_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_RENAMEAT_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.renameat = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	@_SYSCALL_RENAMEAT_REGARGS
	@_SYSCALL_RENAMEAT_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_RENAMEAT_REGARGS_STORE %)
}

probe nd_syscall.renameat.return = nd1_syscall.renameat.return!, nd2_syscall.renameat.return!, tp_syscall.renameat.return
  { }
  
probe nd1_syscall.renameat.return = kprobe.function("sys_renameat").return ?
{
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.renameat.return = kprobe.function(@arch_syscall_prefix "sys_renameat").return ?
{
	@_SYSCALL_RENAMEAT_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.renameat.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_renameat"), @const("__NR_compat_renameat"))
	@_SYSCALL_RENAMEAT_NAME
	@SYSC_RETVALSTR($ret)
}
