# mknodat ____________________________________________________
# long sys_mknodat(int dfd, const char __user *filename,
#	int mode, unsigned dev)
# new function with 2.6.16

@define _SYSCALL_MKNODAT_NAME
%(
	name = "mknodat"
%)

@define _SYSCALL_MKNODAT_ARGSTR
%(
	argstr = sprintf("%s, %s, %s, %u",
	                 dirfd_str, pathname, mode_str, dev)
%)

@define _SYSCALL_MKNODAT_REGARGS
%(
	dirfd = int_arg(1)
	dirfd_str = _dfd_str(dirfd)
	pathname = user_string_quoted(pointer_arg(2))
	mode = uint_arg(3)
	mode_str = _mknod_mode_str(mode)
	dev = uint_arg(4)
%)

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

# dw_mknodat _____________________________________________________

probe dw_syscall.mknodat = kernel.function("sys_mknodat").call ?
{
	@__syscall_compat_gate(@const("__NR_mknodat"), @const("__NR_compat_mknodat"))
	@_SYSCALL_MKNODAT_NAME
	dirfd = __int32($dfd)
	dirfd_str = _dfd_str(dirfd)
	pathname = user_string_quoted($filename)
	mode = __uint32($mode)
	mode_str = _mknod_mode_str(mode)
	dev = __uint32($dev)
	@_SYSCALL_MKNODAT_ARGSTR
}
probe dw_syscall.mknodat.return = kernel.function("sys_mknodat").return ?
{
	@__syscall_compat_gate(@const("__NR_mknodat"), @const("__NR_compat_mknodat"))
	@_SYSCALL_MKNODAT_NAME
	@SYSC_RETVALSTR($return)
}

# nd_mknodat _____________________________________________________

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

probe nd1_syscall.mknodat = kprobe.function("sys_mknodat") ?
{
	@__syscall_compat_gate(@const("__NR_mknodat"), @const("__NR_compat_mknodat"))
	@_SYSCALL_MKNODAT_NAME
	asmlinkage()
	@_SYSCALL_MKNODAT_REGARGS
	@_SYSCALL_MKNODAT_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.mknodat = kprobe.function(@arch_syscall_prefix "sys_mknodat") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_MKNODAT_NAME
	@_SYSCALL_MKNODAT_REGARGS
	@_SYSCALL_MKNODAT_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mknodat = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mknodat"), @const("__NR_compat_mknodat"))
	@_SYSCALL_MKNODAT_NAME
	@_SYSCALL_MKNODAT_REGARGS
	@_SYSCALL_MKNODAT_ARGSTR
}

probe nd_syscall.mknodat.return = nd1_syscall.mknodat.return!, nd2_syscall.mknodat.return!, tp_syscall.mknodat.return
  { }

probe nd1_syscall.mknodat.return = kprobe.function("sys_mknodat").return ?
{
	@__syscall_compat_gate(@const("__NR_mknodat"), @const("__NR_compat_mknodat"))
	@_SYSCALL_MKNODAT_NAME
	@SYSC_RETVALSTR(returnval())
}

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