# prlimit64 ____________________________________________________
#
# SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
#		const struct rlimit64 __user *, new_rlim,
#		struct rlimit64 __user *, old_rlim)

@define _SYSCALL_PRLIMIT64_NAME
%(
	name = "prlimit64"
%)

@define _SYSCALL_PRLIMIT64_ARGSTR
%(
	argstr = sprintf("%d, %s, %s, %p", pid, resource_str, new_rlim_str, old_rlim_uaddr) 
%)

@define _SYSCALL_PRLIMIT64_REGARGS
%(
	pid = int_arg(1)
	resource = int_arg(2)
	resource_str = _rlimit_resource_str(resource)
	new_rlim_uaddr = pointer_arg(3)
	new_rlim_str = _struct_rlimit_u(new_rlim_uaddr)
	old_rlim_uaddr = pointer_arg(4)
%)

@define _SYSCALL_PRLIMIT64_REGARGS_STORE
%(
        if (@probewrite(pid))
          set_int_arg(1, pid)

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

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

	if (@probewrite(old_rlim_uaddr))
	  set_pointer_arg(4, old_rlim_uaddr)
%)

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

# dw_prlimit64 _____________________________________________________

probe dw_syscall.prlimit64 = kernel.function("sys_prlimit64").call ?
{
	@_SYSCALL_PRLIMIT64_NAME
	pid = __int32($pid)
	resource = __int32($resource)
	resource_str = _rlimit_resource_str(resource)
	new_rlim_uaddr = $new_rlim
	new_rlim_str = _struct_rlimit_u(new_rlim_uaddr)
	old_rlim_uaddr = $old_rlim
	@_SYSCALL_PRLIMIT64_ARGSTR
}
probe dw_syscall.prlimit64.return = kernel.function("sys_prlimit64").return ?
{
	@_SYSCALL_PRLIMIT64_NAME
	@SYSC_RETVALSTR($return)
}

# nd_prlimit64 _____________________________________________________

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

probe nd1_syscall.prlimit64 = kprobe.function("sys_prlimit64") ?
{
	@_SYSCALL_PRLIMIT64_NAME
	asmlinkage()
	@_SYSCALL_PRLIMIT64_REGARGS
	@_SYSCALL_PRLIMIT64_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.prlimit64 = kprobe.function(@arch_syscall_prefix "sys_prlimit64") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PRLIMIT64_NAME
	@_SYSCALL_PRLIMIT64_REGARGS
	@_SYSCALL_PRLIMIT64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PRLIMIT64_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.prlimit64 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_prlimit64"), @const("__NR_compat_prlimit64"))
	@_SYSCALL_PRLIMIT64_NAME
	@_SYSCALL_PRLIMIT64_REGARGS
	@_SYSCALL_PRLIMIT64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PRLIMIT64_REGARGS_STORE %)
}

probe nd_syscall.prlimit64.return = nd1_syscall.prlimit64.return!, nd2_syscall.prlimit64.return!, tp_syscall.prlimit64.return
  { }
  
probe nd1_syscall.prlimit64.return = kprobe.function("sys_prlimit64").return ?
{
	@_SYSCALL_PRLIMIT64_NAME
	@SYSC_RETVALSTR(returnval())
}

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