Linux 如何知道“errno”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/503878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:57:24  来源:igfitidea点击:

How to know what the 'errno' means?

clinuxerrno

提问by Barth

When calling execl(...), I get an errno=2. What does it mean? How can I know the meaning of this errno?

打电话时execl(...),我得到一个errno=2. 这是什么意思?我怎么知道这是errno什么意思?

采纳答案by Commodore Jaeger

You can use strerror()to get a human-readable string for the error number. This is the same string printed by perror()but it's useful if you're formatting the error message for something other than standard error output.

您可以使用strerror()获取错误编号的人类可读字符串。这是由 打印的相同字符串,perror()但如果您正在为标准错误输出以外的其他内容格式化错误消息,则它很有用。

For example:

例如:

#include <errno.h>
#include <string.h>

/* ... */

if(read(fd, buf, 1)==-1) {
    printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
}

Linux also supports the explicitly-threadsafe variant strerror_r().

Linux 还支持显式线程安全变体strerror_r()

回答by Otávio Décio

It means:

它的意思是:

File or Directory not found.

找不到文件或目录。

回答by dsm

Here is the documentation. That should tell you what it means and what to do with them. You should avoid using the numeric value and use the constants listed there as well, as the number may change between different systems.

这是文档。这应该会告诉您这意味着什么以及如何处理它们。您应该避免使用数值并使用此处列出的常量,因为不同系统之间的数字可能会有所不同。

回答by Johannes Weiss

Call

称呼

perror("execl");

in case of error.

在错误的情况下。

Sample:

样本:

if(read(fd, buf, 1)==-1) {
    perror("read");
}

The manpages of errno(3)and perror(3)are interesting, too...

和的联机帮助页errno(3)perror(3)很有趣...

回答by schnaader

Error code 2 means "File/Directory not found". In general, you could use the perrorfunction to print a human readable string.

错误代码 2 表示“找不到文件/目录”。通常,您可以使用perror函数打印人类可读的字符串。

回答by Josh Kelley

Instead of running perroron any error code you get, you can retrieve a complete listing of errnovalues on your system with the following one-liner:

perror您可以errno使用以下单行代码检索系统上的完整值列表,而不是运行您获得的任何错误代码:

cpp -dM /usr/include/errno.h | grep 'define E' | sort -n -k 3

cpp -dM /usr/include/errno.h | grep 'define E' | sort -n -k 3

回答by Sarel Botha

When you use strace(on Linux) to run your binary, it will output the returns from system calls and what the error number means. This may sometimes be useful to you.

当您使用strace(在 Linux 上)运行您的二进制文件时,它将输出系统调用的返回值以及错误号的含义。这有时可能对您有用。

回答by ephemient

There's a few useful functions for dealing with errnos. (Just to make it clear, these are built-in to libc-- I'm just providing sample implementations because some people find reading code clearer than reading English.)

有一些处理errnos 的有用函数。(为了清楚起见,这些是内置的libc——我只是提供示例实现,因为有些人发现阅读代码比阅读英语更清晰。)

#include <string.h>
char *strerror(int errnum);

/* you can think of it as being implemented like this: */
static char strerror_buf[1024];
const char *sys_errlist[] = {
    [EPERM]  = "Operation not permitted",
    [ENOENT] = "No such file or directory",
    [ESRCH]  = "No such process",
    [EINTR]  = "Interrupted system call",
    [EIO]    = "I/O error",
    [ENXIO]  = "No such device or address",
    [E2BIG]  = "Argument list too long",
    /* etc. */
};
int sys_nerr = sizeof(sys_errlist) / sizeof(char *);
char *strerror(int errnum) {
    if (0 <= errnum && errnum < sys_nerr && sys_errlist[errnum])
        strcpy(strerror_buf, sys_errlist[errnum]);
    else
        sprintf(strerror_buf, "Unknown error %d", errnum);
    return strerror_buf;
}

strerrorreturns a string describing the error number you've passed to it. Caution, this is not thread- or interrupt-safe; it is free to rewrite the string and return the same pointer on the next invocation. Use strerror_rif you need to worry about that.

strerror返回一个字符串,描述您传递给它的错误号。注意,这不是线程或中断安全的;可以自由地重写字符串并在下一次调用时返回相同的指针。使用strerror_r如果需要有关的忧虑。

#include <stdio.h>
void perror(const char *s);

/* you can think of it as being implemented like this: */
void perror(const char *s) {
    fprintf(stderr, "%s: %s\n", s, strerror(errno));
}

perrorprints out the message you give it, plus a string describing the current errno, to standard error.

perror打印出你给它的消息,加上一个描述当前的字符串errno,到标准错误。

回答by ephemient

I use the following script:

我使用以下脚本:

#!/usr/bin/python

import errno
import os
import sys

toname = dict((str(getattr(errno, x)), x) 
              for x in dir(errno) 
              if x.startswith("E"))
tocode = dict((x, getattr(errno, x)) 
              for x in dir(errno) 
              if x.startswith("E"))

for arg in sys.argv[1:]:
    if arg in tocode:
        print arg, tocode[arg], os.strerror(tocode[arg])
    elif arg in toname:
        print toname[arg], arg, os.strerror(int(arg))
    else:
        print "Unknown:", arg

回答by Pithikos

On Linux there is also a very neat tool that can tell right away what each error code means. On Ubuntu: apt-get install errno.

在 Linux 上还有一个非常简洁的工具,可以立即告诉每个错误代码的含义。在Ubuntu上:apt-get install errno

Then if for example you want to get the description of error type 2, just type errno 2in the terminal.

然后,例如,如果您想获取错误类型 2 的描述,只需errno 2在终端中输入即可。

With errno -lyou get a list with all errors and their descriptions. Much easier that other methods mentioned by previous posters.

随着errno -l你得到所有的错误及其说明的列表。比以前的海报提到的其他方法容易得多。