# mq_getsetattr ______________________________________________
# long sys_mq_getsetattr(mqd_t mqdes,
#                  const struct mq_attr __user *u_mqstat,
#                  struct mq_attr __user *u_omqstat)
# long compat_sys_mq_getsetattr(mqd_t mqdes,
#			const struct compat_mq_attr __user *u_mqstat,
#			struct compat_mq_attr __user *u_omqstat)
#

@define _SYSCALL_MQ_GETSETATTR_NAME
%(
	name = "mq_getsetattr"
%)

@define _SYSCALL_MQ_GETSETATTR_ARGSTR
%(
	argstr = sprintf("%d, %p, %p", mqdes, u_mqstat_uaddr, u_omqstat_uaddr)
%)

@define _SYSCALL_MQ_GETSETATTR_REGARGS
%(
	mqdes = int_arg(1)
	u_mqstat_uaddr = pointer_arg(2)
	u_omqstat_uaddr = pointer_arg(3)
%)

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

# dw_mq_getsetattr _____________________________________________________

probe dw_syscall.mq_getsetattr =
        __syscall.mq_getsetattr,
        kernel.function("compat_sys_mq_getsetattr").call ?
{
	@_SYSCALL_MQ_GETSETATTR_NAME
	mqdes = __int32($mqdes)
	u_mqstat_uaddr = @__pointer($u_mqstat)
	u_omqstat_uaddr = @__pointer($u_omqstat)
	@_SYSCALL_MQ_GETSETATTR_ARGSTR
}
probe __syscall.mq_getsetattr = kernel.function("sys_mq_getsetattr").call
{
        @__syscall_gate(@const("__NR_mq_getsetattr"))
}
probe dw_syscall.mq_getsetattr.return =
        __syscall.mq_getsetattr.return,
        kernel.function("compat_sys_mq_getsetattr").return ?
{
	@_SYSCALL_MQ_GETSETATTR_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.mq_getsetattr.return = kernel.function("sys_mq_getsetattr").return
{
        @__syscall_gate(@const("__NR_mq_getsetattr"))
}

# nd_mq_getsetattr _____________________________________________________

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

probe nd1_syscall.mq_getsetattr =
        __nd1_syscall.mq_getsetattr ?,
        kprobe.function("compat_sys_mq_getsetattr") ?
{
	@_SYSCALL_MQ_GETSETATTR_NAME
	asmlinkage()
	@_SYSCALL_MQ_GETSETATTR_REGARGS
	@_SYSCALL_MQ_GETSETATTR_ARGSTR
}
probe __nd1_syscall.mq_getsetattr = kprobe.function("sys_mq_getsetattr")
{
        @__syscall_gate(@const("__NR_mq_getsetattr"))
}

/* kernel 4.17+ */
probe nd2_syscall.mq_getsetattr =
	kprobe.function(@arch_syscall_prefix "sys_mq_getsetattr") ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_mq_getsetattr") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_MQ_GETSETATTR_NAME
	@_SYSCALL_MQ_GETSETATTR_REGARGS
	@_SYSCALL_MQ_GETSETATTR_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mq_getsetattr = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mq_getsetattr"), @const("__NR_compat_mq_getsetattr"))
	@_SYSCALL_MQ_GETSETATTR_NAME
	@_SYSCALL_MQ_GETSETATTR_REGARGS
	@_SYSCALL_MQ_GETSETATTR_ARGSTR
}

probe nd_syscall.mq_getsetattr.return = nd1_syscall.mq_getsetattr.return!, nd2_syscall.mq_getsetattr.return!, tp_syscall.mq_getsetattr.return
  { }
  
probe nd1_syscall.mq_getsetattr.return =
        __nd1_syscall.mq_getsetattr.return ?,
        kprobe.function("compat_sys_mq_getsetattr").return ?
{
	@_SYSCALL_MQ_GETSETATTR_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.mq_getsetattr.return = kprobe.function("sys_mq_getsetattr").return
{
        @__syscall_gate(@const("__NR_mq_getsetattr"))
}

/* kernel 4.17+ */
probe nd2_syscall.mq_getsetattr.return =
	kprobe.function(@arch_syscall_prefix "sys_mq_getsetattr").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_mq_getsetattr").return ?
{
	@_SYSCALL_MQ_GETSETATTR_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mq_getsetattr.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mq_getsetattr"), @const("__NR_compat_mq_getsetattr"))
	@_SYSCALL_MQ_GETSETATTR_NAME
	@SYSC_RETVALSTR($ret)
}
