# sysctl _____________________________________________________
#
# long sys_sysctl(struct __sysctl_args __user *args)
#

@define _SYSCALL_SYSCTL_NAME
%(
	name = "sysctl"
%)

@define _SYSCALL_SYSCTL_ARGSTR
%(
	argstr = sprintf("%p", args)
%)

@define _SYSCALL_SYSCTL_REGARGS
%(
	args = pointer_arg(1)
%)

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

# dw_sysctl _____________________________________________________

probe dw_syscall.sysctl = kernel.function("compat_sys_sysctl").call ?,
                          kernel.function("sys_sysctl").call ?
{
	@_SYSCALL_SYSCTL_NAME
	args = $args
	@_SYSCALL_SYSCTL_ARGSTR
}
probe dw_syscall.sysctl.return = kernel.function("compat_sys_sysctl").return ?,
                                 kernel.function("sys_sysctl").return ?
{
	@_SYSCALL_SYSCTL_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sysctl _____________________________________________________

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

probe nd1_syscall.sysctl = kprobe.function("compat_sys_sysctl") ?,
                           kprobe.function("sys_sysctl") ?
{
	@_SYSCALL_SYSCTL_NAME
	asmlinkage()
	@_SYSCALL_SYSCTL_REGARGS
	@_SYSCALL_SYSCTL_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.sysctl = kprobe.function(@arch_syscall_prefix "compat_sys_sysctl") ?,
                           kprobe.function(@arch_syscall_prefix "sys_sysctl") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SYSCTL_NAME
	@_SYSCALL_SYSCTL_REGARGS
	@_SYSCALL_SYSCTL_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sysctl = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR__sysctl"), @const("__NR_compat__sysctl"))
	@_SYSCALL_SYSCTL_NAME
	@_SYSCALL_SYSCTL_REGARGS
	@_SYSCALL_SYSCTL_ARGSTR
}

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

probe nd1_syscall.sysctl.return = kprobe.function("compat_sys_sysctl").return ?,
                                  kprobe.function("sys_sysctl").return ?
{
	@_SYSCALL_SYSCTL_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.sysctl.return = kprobe.function(@arch_syscall_prefix "compat_sys_sysctl").return ?,
                                  kprobe.function(@arch_syscall_prefix "sys_sysctl").return ?
{
	@_SYSCALL_SYSCTL_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sysctl.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR__sysctl"), @const("__NR_compat__sysctl"))
	@_SYSCALL_SYSCTL_NAME
	@SYSC_RETVALSTR($ret)
}
