# name_to_handle_at __________________________________________
#
# SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
#		struct file_handle __user *, handle, int __user *, mnt_id,
#		int, flag)

@define _SYSCALL_NAME_TO_HANDLE_AT_NAME
%(
	name = "name_to_handle_at"
%)

@define _SYSCALL_NAME_TO_HANDLE_AT_ARGSTR
%(
	argstr = sprintf("%s, %s, %p, %p, %s", dfd_str, pathname,
	                 handle_uaddr, mnt_id_uaddr, flags_str)
%)

@define _SYSCALL_NAME_TO_HANDLE_AT_REGARGS
%(
	dfd = int_arg(1)
	dfd_str = _dfd_str(dfd)
	pathname = user_string_quoted(pointer_arg(2))
	pathname_unquoted = user_string_nofault(pointer_arg(2))
	handle_uaddr = pointer_arg(3)
	mnt_id_uaddr = pointer_arg(4)
	flags = int_arg(5)
	flags_str = _at_flag_str(flags)
%)

@define _SYSCALL_NAME_TO_HANDLE_AT_REGARGS_STORE
%(
        if (@probewrite(dfd))
          set_int_arg(1, dfd)

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

	if (@probewrite(handle_uaddr))
	  set_pointer_arg(3, handle_uaddr)

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

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

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

# dw_name_to_handle_at _____________________________________________________

probe dw_syscall.name_to_handle_at =
	kernel.function("sys_name_to_handle_at").call ?
{
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	dfd = __int32($dfd)
	dfd_str = _dfd_str(dfd)
	pathname = user_string_quoted($name)
	handle_uaddr = $handle
	mnt_id_uaddr = $mnt_id
	flags = $flag
	flags_str = _at_flag_str(flags)
	@_SYSCALL_NAME_TO_HANDLE_AT_ARGSTR
}
probe dw_syscall.name_to_handle_at.return =
	kernel.function("sys_name_to_handle_at").return ?
{
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_name_to_handle_at _____________________________________________________

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

probe nd1_syscall.name_to_handle_at =
	kprobe.function("sys_name_to_handle_at").call ?
{
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	asmlinkage()
	@_SYSCALL_NAME_TO_HANDLE_AT_REGARGS
	@_SYSCALL_NAME_TO_HANDLE_AT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.name_to_handle_at =
	kprobe.function(@arch_syscall_prefix "sys_name_to_handle_at") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	@_SYSCALL_NAME_TO_HANDLE_AT_REGARGS
	@_SYSCALL_NAME_TO_HANDLE_AT_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_NAME_TO_HANDLE_AT_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.name_to_handle_at = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_name_to_handle_at"), 
			       @const("__NR_compat_name_to_handle_at"))
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	@_SYSCALL_NAME_TO_HANDLE_AT_REGARGS
	@_SYSCALL_NAME_TO_HANDLE_AT_ARGSTR
},
{
	%( @_IS_SREG_KERNEL %? @_SYSCALL_NAME_TO_HANDLE_AT_REGARGS_STORE %)
}

probe nd_syscall.name_to_handle_at.return = nd1_syscall.name_to_handle_at.return!, nd2_syscall.name_to_handle_at.return!, tp_syscall.name_to_handle_at.return
  { }
  
probe nd1_syscall.name_to_handle_at.return =
	kprobe.function("sys_name_to_handle_at").return ?
{
	@_SYSCALL_NAME_TO_HANDLE_AT_NAME
	@SYSC_RETVALSTR(returnval())
}

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