# setpgid ____________________________________________________
#
# asmlinkage long
# sys_setpgid(pid_t pid,
#		 pid_t pgid)
#

@define _SYSCALL_SETPGID_NAME
%(
	name = "setpgid"
%)

@define _SYSCALL_SETPGID_ARGSTR
%(
	argstr = sprintf("%d, %d", pid, pgid)
%)

@define _SYSCALL_SETPGID_REGARGS
%(
	pid = int_arg(1)
	pgid = int_arg(2)
%)

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

	if (@probewrite(pgid))
	  set_int_arg(2, pgid)
%)

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

# dw_setpgid _____________________________________________________

probe dw_syscall.setpgid = kernel.function("sys_setpgid").call
{
	@_SYSCALL_SETPGID_NAME
	pid = __int32($pid)
	pgid = __int32($pgid)
	@_SYSCALL_SETPGID_ARGSTR
}
probe dw_syscall.setpgid.return = kernel.function("sys_setpgid").return
{
	@_SYSCALL_SETPGID_NAME
	@SYSC_RETVALSTR($return)
}

# nd_setpgid _____________________________________________________

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

probe nd1_syscall.setpgid = kprobe.function("sys_setpgid") ?
{
	@_SYSCALL_SETPGID_NAME
	asmlinkage()
	@_SYSCALL_SETPGID_REGARGS
	@_SYSCALL_SETPGID_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.setpgid = kprobe.function(@arch_syscall_prefix "sys_setpgid") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SETPGID_NAME
	@_SYSCALL_SETPGID_REGARGS
	@_SYSCALL_SETPGID_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SETPGID_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.setpgid = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_setpgid"), @const("__NR_compat_setpgid"))
	@_SYSCALL_SETPGID_NAME
	@_SYSCALL_SETPGID_REGARGS
	@_SYSCALL_SETPGID_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SETPGID_REGARGS_STORE %)
}

probe nd_syscall.setpgid.return = nd1_syscall.setpgid.return!, nd2_syscall.setpgid.return!, tp_syscall.setpgid.return
  { }
  
probe nd1_syscall.setpgid.return = kprobe.function("sys_setpgid").return ?
{
	@_SYSCALL_SETPGID_NAME
	@SYSC_RETVALSTR(returnval())
}

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