# signalfd _____________________________________________________
#
# long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
# long sys_signalfd4(int ufd, sigset_t __user *user_mask, size_t sizemask,
#		 int flags)
# long compat_sys_signalfd(int ufd, const compat_sigset_t __user *sigmask,
# 		 compat_size_t sigsetsize)
# long compat_sys_signalfd4(int ufd, const compat_sigset_t __user *sigmask,
#		 compat_size_t sigsetsize, int flags)
#

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

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

# dw_signalfd _____________________________________________________

probe dw_syscall.signalfd = __syscall.signalfd4 !, __syscall.signalfd ?
{
	flags = __int32(@choose_defined($flags, 0))
	if (flags == 0) {
		name = "signalfd"
		argstr = sprintf("%d, %p, %d", __int32($ufd), @__pointer($user_mask),
				 $sizemask)
	} else {
		name = "signalfd4"
		argstr = sprintf("%d, %p, %d, %s", __int32($ufd), @__pointer($user_mask),
		       	 	 $sizemask, _signalfd4_flags_str(flags))
	}
}
probe __syscall.signalfd4 = kernel.function("sys_signalfd4").call
{
	@__syscall_gate(@const("__NR_signalfd4"))
}
probe __syscall.signalfd = kernel.function("sys_signalfd").call
{
	@__syscall_gate(@const("__NR_signalfd"))
}
probe dw_syscall.signalfd.return = __syscall.signalfd4.return !,
				__syscall.signalfd.return ?
{
	retstr = return_str(1, $return)
}
probe __syscall.signalfd4.return = kernel.function("sys_signalfd4").return
{
	@__syscall_gate(@const("__NR_signalfd4"))
	flags = __int32(@entry($flags))
	name = (flags == 0) ? "signalfd" : "signalfd4"
}
probe __syscall.signalfd.return = kernel.function("sys_signalfd").return
{
	@__syscall_gate(@const("__NR_signalfd"))
	flags = 0
	name = "signalfd"
}
probe dw_syscall.compat_signalfd = kernel.function("compat_sys_signalfd4").call !,
                                kernel.function("compat_sys_signalfd").call ?
{
	flags = __int32(@choose_defined($flags, 0))
	if (flags == 0) {
		name = "signalfd"
		argstr = sprintf("%d, %p, %d", __int32($ufd), @__pointer($sigmask),
				 $sigsetsize)
	} else {
		name = "signalfd4"
		argstr = sprintf("%d, %p, %d, %s", __int32($ufd), @__pointer($sigmask),
				 $sigsetsize, _signalfd4_flags_str(flags))
	}
}
probe dw_syscall.compat_signalfd.return =
		kernel.function("compat_sys_signalfd4").return !,
		kernel.function("compat_sys_signalfd").return ?
{
	flags = __int32(@entry(@choose_defined($flags, 0)))
	name = (flags == 0) ? "signalfd" : "signalfd4"
	retstr = return_str(1, $return)
}

# nd_signalfd _____________________________________________________

probe nd_syscall.signalfd = __nd_syscall.signalfd4 !,
                            __nd_syscall.signalfd ?
{
}
probe __nd_syscall.signalfd4 = kprobe.function("sys_signalfd4")
{
	@__syscall_gate(@const("__NR_signalfd4"))
	asmlinkage()
	flags = int_arg(4)
	if (flags == 0) {
		name = "signalfd"
		argstr = sprintf("%d, %p, %d", int_arg(1), pointer_arg(2),
				 ulong_arg(3))
	} else {
		name = "signalfd4"
		argstr = sprintf("%d, %p, %d, %s", int_arg(1), pointer_arg(2),
		       	 	 ulong_arg(3), _signalfd4_flags_str(flags))
	}
}
probe __nd_syscall.signalfd = kprobe.function("sys_signalfd")
{
	@__syscall_gate(@const("__NR_signalfd"))
	name = "signalfd"
	asmlinkage()
	flags = 0
	argstr = sprintf("%d, %p, %d", int_arg(1), pointer_arg(2),
			 ulong_arg(3))
}
probe nd_syscall.signalfd.return = __nd_syscall.signalfd4.return !,
                                   __nd_syscall.signalfd.return ?
{
	retstr = returnstr(1)
}
probe __nd_syscall.signalfd4.return = kprobe.function("sys_signalfd4").return
{
	@__syscall_gate(@const("__NR_signalfd4"))
	flags = @entry(__asmlinkage_int_arg(4))
	name = (flags == 0) ? "signalfd" : "signalfd4"
}
probe __nd_syscall.signalfd.return = kprobe.function("sys_signalfd").return
{
	@__syscall_gate(@const("__NR_signalfd"))
	flags = 0
	name = "signalfd"
}
probe nd_syscall.compat_signalfd = __nd_syscall.compat_signalfd4 !,
                                   __nd_syscall.compat_signalfd ?
{
}
probe __nd_syscall.compat_signalfd4 = kprobe.function("compat_sys_signalfd4")
{
	asmlinkage()
	flags = int_arg(4)
	if (flags == 0) {
		name = "signalfd"
		argstr = sprintf("%d, %p, %d", int_arg(1), pointer_arg(2),
				 u32_arg(3))
	} else {
		name = "signalfd4"
		argstr = sprintf("%d, %p, %d, %s", int_arg(1),
				 pointer_arg(2), u32_arg(3),
				 _signalfd4_flags_str(flags))
	}
}
probe __nd_syscall.compat_signalfd = kprobe.function("compat_sys_signalfd")
{
	asmlinkage()
	flags = 0
	name = "signalfd"
	argstr = sprintf("%d, %p, %d", int_arg(1), pointer_arg(2),
			 u32_arg(3))
}
probe nd_syscall.compat_signalfd.return =
		__nd_syscall.compat_signalfd4.return !,
		__nd_syscall.compat_signalfd.return ?
{
}
probe __nd_syscall.compat_signalfd4.return =
		kprobe.function("compat_sys_signalfd4").return
{
	flags = @entry(__asmlinkage_int_arg(4))
	name = (flags == 0) ? "signalfd" : "signalfd4"
	retstr = returnstr(1)
}
probe __nd_syscall.compat_signalfd.return =
		kprobe.function("compat_sys_signalfd").return
{
	flags = 0
	name = "signalfd"
	retstr = returnstr(1)
}
