# fanotify_init ______________________________________________
# fanotify_init() was enabled in kernel version 2.6.37.
# SYSCALL_DEFINE2(fanotify_init, unsigned int, flags,
#		  unsigned int, event_f_flags)

@define _SYSCALL_FANOTIFY_INIT_NAME
%(
	name = "fanotify_init"
%)

@define _SYSCALL_FANOTIFY_INIT_ARGSTR
%(
	argstr = sprintf("%s, %s", flags_str, event_f_flags_str)
%)

@define _SYSCALL_FANOTIFY_INIT_REGARGS
%(
	flags = uint_arg(1)
	flags_str = _fanotify_init_flags_str(flags)
	event_f_flags = uint_arg(2)
	event_f_flags_str = _sys_open_flag_str(event_f_flags)
%)

@define _SYSCALL_FANOTIFY_INIT_REGARGS_STORE
%(
        if (@probewrite(flags))
          set_uint_arg(1, flags)

	if (@probewrite(event_f_flags))
	  set_uint_arg(2, event_f_flags)
%)

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

# dw_fanotify_init _____________________________________________________

probe dw_syscall.fanotify_init = kernel.function("sys_fanotify_init").call ?
{
	@_SYSCALL_FANOTIFY_INIT_NAME
	flags = __uint32($flags)
	flags_str = _fanotify_init_flags_str(flags)
	event_f_flags = __uint32($event_f_flags)
	event_f_flags_str = _sys_open_flag_str(event_f_flags)
	@_SYSCALL_FANOTIFY_INIT_ARGSTR
}
probe dw_syscall.fanotify_init.return =
	kernel.function("sys_fanotify_init").return ?
{
	@_SYSCALL_FANOTIFY_INIT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_fanotify_init _____________________________________________________

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

probe nd1_syscall.fanotify_init = kprobe.function("sys_fanotify_init") ?
{
	@_SYSCALL_FANOTIFY_INIT_NAME
	asmlinkage()
	@_SYSCALL_FANOTIFY_INIT_REGARGS
	@_SYSCALL_FANOTIFY_INIT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.fanotify_init = kprobe.function(@arch_syscall_prefix "sys_fanotify_init") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_FANOTIFY_INIT_NAME
	@_SYSCALL_FANOTIFY_INIT_REGARGS
	@_SYSCALL_FANOTIFY_INIT_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_FANOTIFY_INIT_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.fanotify_init = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_fanotify_init"), @const("__NR_compat_fanotify_init"))
	@_SYSCALL_FANOTIFY_INIT_NAME
	@_SYSCALL_FANOTIFY_INIT_REGARGS
	@_SYSCALL_FANOTIFY_INIT_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_FANOTIFY_INIT_REGARGS_STORE %)
}

probe nd_syscall.fanotify_init.return = nd1_syscall.fanotify_init.return!, nd2_syscall.fanotify_init.return!, tp_syscall.fanotify_init.return
  { }
  
probe nd1_syscall.fanotify_init.return =
	kprobe.function("sys_fanotify_init").return ?
{
	@_SYSCALL_FANOTIFY_INIT_NAME
	@SYSC_RETVALSTR(returnval())
}

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