# symlink ____________________________________________________
# long sys_symlink(const char __user * oldname,
#		 const char __user * newname)

@define _SYSCALL_SYMLINK_NAME
%(
	name = "symlink"
%)

@define _SYSCALL_SYMLINK_ARGSTR
%(
	argstr = sprintf("%s, %s", oldpath, newpath)
%)

@define _SYSCALL_SYMLINK_REGARGS
%(
	oldpath = user_string_quoted(pointer_arg(1))
	oldpath_unquoted = user_string_nofault(pointer_arg(1))
	newpath = user_string_quoted(pointer_arg(2))
	oldpath_unquoted = user_string_nofault(pointer_arg(2))
%)

@define _SYSCALL_SYMLINK_REGARGS_STORE
%(
        if (@probewrite(oldpath_unquoted))
          set_user_string_arg(pointer_arg(1), oldpath_unquoted)

	if (@probewrite(newpath_unquoted))
	  set_user_string_arg(pointer_arg(2), newpath_unquoted)
%)

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

# dw_symlink _____________________________________________________

probe dw_syscall.symlink = kernel.function("sys_symlink").call
{
	@_SYSCALL_SYMLINK_NAME
	oldpath = user_string_quoted($oldname)
	newpath = user_string_quoted($newname)
	@_SYSCALL_SYMLINK_ARGSTR
}
probe dw_syscall.symlink.return = kernel.function("sys_symlink").return
{
	@_SYSCALL_SYMLINK_NAME
	@SYSC_RETVALSTR($return)
}

# nd_symlink _____________________________________________________

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

probe nd1_syscall.symlink = kprobe.function("sys_symlink") ?
{
	@_SYSCALL_SYMLINK_NAME
	asmlinkage()
	@_SYSCALL_SYMLINK_REGARGS
	@_SYSCALL_SYMLINK_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.symlink = kprobe.function(@arch_syscall_prefix "sys_symlink") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SYMLINK_NAME
	@_SYSCALL_SYMLINK_REGARGS
	@_SYSCALL_SYMLINK_ARGSTR
},
{
        @_SYSCALL_SYMLINK_REGARGS_STORE
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.symlink = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_symlink"), @const("__NR_compat_symlink"))
	@_SYSCALL_SYMLINK_NAME
	@_SYSCALL_SYMLINK_REGARGS
	@_SYSCALL_SYMLINK_ARGSTR
},
{
        @_SYSCALL_SYMLINK_REGARGS_STORE
}

probe nd_syscall.symlink.return = nd1_syscall.symlink.return!, nd2_syscall.symlink.return!, tp_syscall.symlink.return
  { }
  
probe nd1_syscall.symlink.return = kprobe.function("sys_symlink").return ?
{
	@_SYSCALL_SYMLINK_NAME
	@SYSC_RETVALSTR(returnval())
}

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