# rmdir ______________________________________________________
#
# asmlinkage long
# sys_rmdir(const char __user * pathname)
#

@define _SYSCALL_RMDIR_NAME
%(
	name = "rmdir"
%)

@define _SYSCALL_RMDIR_ARGSTR
%(
	argstr = pathname
%)

@define _SYSCALL_RMDIR_REGARGS
%(
	pathname = user_string_quoted(pointer_arg(1))
	pathname_unquoted = user_string_nofault(pointer_arg(1))
%)

@define _SYSCALL_RMDIR_REGARGS_STORE
%(
        if (@probewrite(pathname_unquoted))
          set_user_string_arg(pointer_arg(1), pathname_unquoted)
%)

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

# dw_rmdir _____________________________________________________

probe dw_syscall.rmdir = kernel.function("sys_rmdir").call
{
	@_SYSCALL_RMDIR_NAME
	pathname = user_string_quoted($pathname)
	@_SYSCALL_RMDIR_ARGSTR
}
probe dw_syscall.rmdir.return = kernel.function("sys_rmdir").return
{
	@_SYSCALL_RMDIR_NAME
	@SYSC_RETVALSTR($return)
}

# nd_rmdir _____________________________________________________

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

probe nd1_syscall.rmdir = kprobe.function("sys_rmdir") ?
{
	@_SYSCALL_RMDIR_NAME
	asmlinkage()
	@_SYSCALL_RMDIR_REGARGS
	@_SYSCALL_RMDIR_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.rmdir = kprobe.function(@arch_syscall_prefix "sys_rmdir")
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_RMDIR_NAME
	@_SYSCALL_RMDIR_REGARGS
	@_SYSCALL_RMDIR_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RMDIR_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.rmdir = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_rmdir"), @const("__NR_compat_rmdir"))
	@_SYSCALL_RMDIR_NAME
	@_SYSCALL_RMDIR_REGARGS
	@_SYSCALL_RMDIR_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_RMDIR_REGARGS_STORE %)
}

probe nd_syscall.rmdir.return = nd1_syscall.rmdir.return!, nd2_syscall.rmdir.return!, tp_syscall.rmdir.return
  { }

probe nd1_syscall.rmdir.return = kprobe.function("sys_rmdir").return ?
{
	@_SYSCALL_RMDIR_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.rmdir.return = kprobe.function(@arch_syscall_prefix "sys_rmdir").return
{
	@_SYSCALL_RMDIR_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.rmdir.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_rmdir"), @const("__NR_compat_rmdir"))
	@_SYSCALL_RMDIR_NAME
	@SYSC_RETVALSTR($ret)
}
