在 linux 内核中返回错误代码

returning error code in linux kernel

我试图了解 Linux 系统如何调用 return 错误代码。我遇到了 times() 系统调用。这个简单的系统调用将一些数据复制给用户 space,如果该操作不成功 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());
}

我的问题是:

  1. 为什么-EFAULT?不应该是EFAULT没有减号吗?
  2. return负错误码是常见的吗?

来自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.

另请参阅下一个答案: