Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning error code in linux kernel

I was trying to understand how Linux system calls return error codes. I bumped into times() system call. This simple system call copies some data to user space and if that operation was not successful returns -EFAULT:

SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
{
    if (tbuf) {
        struct tms tmp;

        do_sys_times(&tmp);
        if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
            return -EFAULT;
    }
    force_successful_syscall_return();
    return (long) jiffies_64_to_clock_t(get_jiffies_64());
}

My questions are:

  1. Why -EFAULT? Shouldn't it be EFAULT without minus?
  2. Is it a common to return negative error codes?
like image 499
Aidin.T Avatar asked Apr 06 '17 13:04

Aidin.T


People also ask

How do I create a patch for a kernel?

To create kernel patches, firstly create two copies of the Linux kernel source code. To retrieve this source code, navigate to netkit-jh-build/kernel/Makefile and use the URL available KERNEL_URL . Once you have a . patch file, move it to netkit-jh-build/kernel/patches/ and name it appropiately.

What does it mean to patch a kernel?

Linux kernel live patching is a way to apply critical and important security patches to a running Linux kernel, without the need to reboot or interrupt runtime.

What are Linux errors?

error() is a general error-reporting function. It flushes stdout, and then outputs to stderr the program name, a colon and a space, the message specified by the printf(3)-style format string format, and, if errnum is nonzero, a second colon and a space followed by the string given by strerror(errnum).


1 Answers

From man 2 syscalls:

Note: system calls indicate a failure by returning a negative error number to the caller; when this happens, the wrapper function negates the returned error number (to make it positive), copies it to errno, and returns -1 to the caller of the wrapper.

See also next answers:

  • What are the return values of system calls in Assembly?
  • Why doesn't a custom system call work properly with negative numbers?
like image 63
Sam Protsenko Avatar answered Oct 18 '22 14:10

Sam Protsenko