# mq_open ____________________________________________________
# long  sys_mq_open(const char __user *u_name,
#             int oflag,
#             mode_t mode,
#             struct mq_attr __user *u_attr)
# long compat_sys_mq_open(const char __user *u_name,
#			int oflag, compat_mode_t mode,
#			struct compat_mq_attr __user *u_attr)
#

@define _SYSCALL_MQ_OPEN_NAME
%(
	name = "mq_open"
%)

@define _SYSCALL_MQ_OPEN_ARGSTR
%(
	if (oflag & @const("O_CREAT"))
		argstr = sprintf("%s, %s, %#o, %p", filename, oflag_str, mode, u_attr_uaddr)
	else
		argstr = sprintf("%s, %s", filename, oflag_str)
%)

@define _SYSCALL_MQ_OPEN_REGARGS
%(
	name_uaddr = pointer_arg(1)
	filename = user_string_quoted(name_uaddr)
	u_attr_uaddr = pointer_arg(4)
	oflag = int_arg(2)
	oflag_str = _sys_open_flag_str(oflag)
%)

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

# dw_mq_open _____________________________________________________

probe dw_syscall.mq_open = __syscall.mq_open,
                        kernel.function("compat_sys_mq_open").call ?
{
	@_SYSCALL_MQ_OPEN_NAME
	name_uaddr = @__pointer($u_name)
	filename = user_string_quoted(name_uaddr)
	u_attr_uaddr = @__pointer($u_attr)
	oflag = __int32($oflag)
	oflag_str = _sys_open_flag_str(oflag)
	mode = (@__compat_task ? __ushort($mode) : __uint32($mode))
	@_SYSCALL_MQ_OPEN_ARGSTR
}

probe __syscall.mq_open = kernel.function("sys_mq_open").call
{
	@__syscall_gate_compat_simple
}
probe dw_syscall.mq_open.return =
        __syscall.mq_open.return,
        kernel.function("compat_sys_mq_open").return ?
{
	@_SYSCALL_MQ_OPEN_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.mq_open.return = kernel.function("sys_mq_open").return
{
        @__syscall_gate(@const("__NR_mq_open"))
}

# nd_mq_open _____________________________________________________

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

probe nd1_syscall.mq_open = __nd1_syscall.mq_open,
                            __nd1_syscall.compat_mq_open ?
{
	asmlinkage()
	@_SYSCALL_MQ_OPEN_NAME
	@_SYSCALL_MQ_OPEN_REGARGS
	@_SYSCALL_MQ_OPEN_ARGSTR
}
probe __nd1_syscall.mq_open = kprobe.function("sys_mq_open")
{
	asmlinkage()
        @__syscall_gate(@const("__NR_mq_open"))
%( CONFIG_64BIT == "y" %?
        mode = uint_arg(3)
%:
        mode = (uint_arg(3) & 0xffff)
%)
}
probe __nd1_syscall.compat_mq_open = kprobe.function("compat_sys_mq_open")
{
	asmlinkage()
	mode = (uint_arg(3) & 0xffff)
}

/* kernel 4.17+ */
probe nd2_syscall.mq_open = __nd2_syscall.mq_open,
                            __nd2_syscall.compat_mq_open ? 
{
	@_SYSCALL_MQ_OPEN_NAME
	@_SYSCALL_MQ_OPEN_REGARGS
	@_SYSCALL_MQ_OPEN_ARGSTR
}
probe __nd2_syscall.mq_open = kprobe.function(@arch_syscall_prefix "sys_mq_open")
{
	__set_syscall_pt_regs(pointer_arg(1))
        mode = uint_arg(3)
}
probe __nd2_syscall.compat_mq_open = kprobe.function(@arch_syscall_prefix "compat_sys_mq_open")
{
	__set_syscall_pt_regs(pointer_arg(1))
	mode = (uint_arg(3) & 0xffff)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mq_open = __tp_syscall.mq_open,
                           __tp_syscall.compat_mq_open ?
{
	@_SYSCALL_MQ_OPEN_NAME
	@_SYSCALL_MQ_OPEN_REGARGS
	@_SYSCALL_MQ_OPEN_ARGSTR
}
probe __tp_syscall.mq_open = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_mq_open"))
	mode = uint_arg(3)
}
probe __tp_syscall.compat_mq_open = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_mq_open"))
	mode = (uint_arg(3) & 0xffff)
}

probe nd_syscall.mq_open.return = nd1_syscall.mq_open.return!, nd2_syscall.mq_open.return!, tp_syscall.mq_open.return
  { }
  
probe nd1_syscall.mq_open.return =
        __nd1_syscall.mq_open.return ?,
        kprobe.function("compat_sys_mq_open").return ?
{
	@_SYSCALL_MQ_OPEN_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.mq_open.return = kprobe.function("sys_mq_open").return
{
        @__syscall_gate(@const("__NR_mq_open"))
}

/* kernel 4.17+ */
probe nd2_syscall.mq_open.return =
	kprobe.function(@arch_syscall_prefix "sys_mq_open").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_mq_open").return ?
{
	@_SYSCALL_MQ_OPEN_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mq_open.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mq_open"), @const("__NR_compat_mq_open"))
	@_SYSCALL_MQ_OPEN_NAME
	@SYSC_RETVALSTR($ret)
}
