# sethostname ________________________________________________
#
# asmlinkage long
# sys_sethostname(char __user *name, int len)
#

@define _SYSCALL_SETHOSTNAME_NAME
%(
	name = "sethostname"
%)

@define _SYSCALL_SETHOSTNAME_ARGSTR
%(
	argstr = sprintf("%s, %d", name_str, len)
%)

@define _SYSCALL_SETHOSTNAME_REGARGS
%(
	hostname_uaddr = pointer_arg(1)
	name_str = user_string_quoted(hostname_uaddr)
	name_str_unquoted = user_string_nofault(pointer_arg(1))
	len = int_arg(2)
%)

@define _SYSCALL_SETHOSTNAME_REGARGS_STORE
%(
        if (@probewrite(hostname_uaddr))
          set_pointer_arg(1, hostname_uaddr)

	if (@probewrite(name_str_unquoted))
	  set_user_string_arg(pointer_arg(1), name_str_unquoted)

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

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

# dw_sethostname _____________________________________________________

probe dw_syscall.sethostname = kernel.function("sys_sethostname").call
{
	@_SYSCALL_SETHOSTNAME_NAME
	hostname_uaddr = $name
	name_str = user_string_quoted($name)
	len = __int32($len)
	@_SYSCALL_SETHOSTNAME_ARGSTR
}
probe dw_syscall.sethostname.return = kernel.function("sys_sethostname").return
{
	@_SYSCALL_SETHOSTNAME_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sethostname _____________________________________________________

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

probe nd1_syscall.sethostname = kprobe.function("sys_sethostname") ?
{
	@_SYSCALL_SETHOSTNAME_NAME
	asmlinkage()
	@_SYSCALL_SETHOSTNAME_REGARGS
	@_SYSCALL_SETHOSTNAME_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.sethostname = kprobe.function(@arch_syscall_prefix "sys_sethostname") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SETHOSTNAME_NAME
	@_SYSCALL_SETHOSTNAME_REGARGS
	@_SYSCALL_SETHOSTNAME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SETHOSTNAME_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sethostname = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_sethostname"), @const("__NR_compat_sethostname"))
	@_SYSCALL_SETHOSTNAME_NAME
	@_SYSCALL_SETHOSTNAME_REGARGS
	@_SYSCALL_SETHOSTNAME_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SETHOSTNAME_REGARGS_STORE %)
}

probe nd_syscall.sethostname.return = nd1_syscall.sethostname.return!, nd2_syscall.sethostname.return!, tp_syscall.sethostname.return
  { }
  
probe nd1_syscall.sethostname.return = kprobe.function("sys_sethostname").return ?
{
	@_SYSCALL_SETHOSTNAME_NAME
	@SYSC_RETVALSTR(returnval())
}

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