# dup3 _______________________________________________________
# SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)

@define _SYSCALL_DUP3_NAME
%(
	name = "dup3";
%)

@define _SYSCALL_DUP3_ARGSTR
%(
	argstr = sprintf("%d, %d, %s", oldfd, newfd,  flags_str)
%)

@define _SYSCALL_DUP3_REGARGS
%(
	oldfd = int_arg(1)
	newfd = int_arg(2)
	flags = int_arg(3)
	flags_str = _dup3_flag_str(flags)
%)

@define _SYSCALL_DUP3_REGARGS_STORE
%(
        if (@probewrite(oldfd))
          set_int_arg(1, oldfd)

	if (@probewrite(newfd))
	  set_int_arg(2, newfd)

	if (@probewrite(flags))
	  set_int_arg(3, flags)
%)

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

# dw_dup3 _____________________________________________________

probe dw_syscall.dup3 = kernel.function("sys_dup3").call ?
{
	@__syscall_compat_gate(@const("__NR_dup3"), @const("__NR_compat_dup3"))
	@_SYSCALL_DUP3_NAME
	oldfd = __int32($oldfd)
	newfd = __int32($newfd)
	flags = __int32($flags)
	flags_str = _dup3_flag_str(flags)
	@_SYSCALL_DUP3_ARGSTR
}
probe dw_syscall.dup3.return = kernel.function("sys_dup3").return ?
{
	@__syscall_compat_gate(@const("__NR_dup3"), @const("__NR_compat_dup3"))
	@_SYSCALL_DUP3_NAME
	@SYSC_RETVALSTR($return)
}

# nd_dup3 _____________________________________________________

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

probe nd1_syscall.dup3 = kprobe.function("sys_dup3") ?
{
	@__syscall_compat_gate(@const("__NR_dup3"), @const("__NR_compat_dup3"))
	@_SYSCALL_DUP3_NAME
	asmlinkage()
	@_SYSCALL_DUP3_REGARGS
	@_SYSCALL_DUP3_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.dup3 = kprobe.function(@arch_syscall_prefix "sys_dup3") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_DUP3_NAME
	@_SYSCALL_DUP3_REGARGS
	@_SYSCALL_DUP3_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_DUP3_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.dup3 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_dup3"), @const("__NR_compat_dup3"))
	@_SYSCALL_DUP3_NAME
	@_SYSCALL_DUP3_REGARGS
	@_SYSCALL_DUP3_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_DUP3_REGARGS_STORE %)
}

probe nd_syscall.dup3.return = nd1_syscall.dup3.return!, nd2_syscall.dup3.return!, tp_syscall.dup3.return
  { }
  
probe nd1_syscall.dup3.return = kprobe.function("sys_dup3").return ?
{
	@__syscall_compat_gate(@const("__NR_dup3"), @const("__NR_compat_dup3"))
	@_SYSCALL_DUP3_NAME
	@SYSC_RETVALSTR(returnval())
}

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