📄 errno.texi
字号:
@node Error Reporting, Memory Allocation, Introduction, Top@chapter Error Reporting@cindex error reporting@cindex reporting errors@cindex error codes@cindex status codesMany functions in the GNU C library detect and report error conditions,and sometimes your programs need to check for these error conditions.For example, when you open an input file, you should verify that thefile was actually opened correctly, and print an error message or takeother appropriate action if the call to the library function failed.This chapter describes how the error reporting facility works. Yourprogram should include the header file @file{errno.h} to use thisfacility.@pindex errno.h@menu* Checking for Errors:: How errors are reported by library functions.* Error Codes:: Error code macros; all of these expand into integer constant values.* Error Messages:: Mapping error codes onto error messages.@end menu@node Checking for Errors, Error Codes, , Error Reporting@section Checking for ErrorsMost library functions return a special value to indicate that they havefailed. The special value is typically @code{-1}, a null pointer, or aconstant such as @code{EOF} that is defined for that purpose. But thisreturn value tells you only that an error has occurred. To find outwhat kind of error it was, you need to look at the error code stored in thevariable @code{errno}. This variable is declared in the header file@file{errno.h}.@pindex errno.h@comment errno.h@comment ANSI@deftypevr {Variable} {volatile int} errnoThe variable @code{errno} contains the system error number. You canchange the value of @code{errno}.Since @code{errno} is declared @code{volatile}, it might be changedasynchronously by a signal handler; see @ref{Defining Handlers}.However, a properly written signal handler saves and restores the valueof @code{errno}, so you generally do not need to worry about thispossibility except when writing signal handlers.The initial value of @code{errno} at program startup is zero. Manylibrary functions are guaranteed to set it to certain nonzero valueswhen they encounter certain kinds of errors. These error conditions arelisted for each function. These functions do not change @code{errno}when they succeed; thus, the value of @code{errno} after a successfulcall is not necessarily zero, and you should not use @code{errno} todetermine @emph{whether} a call failed. The proper way to do that isdocumented for each function. @emph{If} the call the failed, you canexamine @code{errno}.Many library functions can set @code{errno} to a nonzero value as aresult of calling other library functions which might fail. You shouldassume that any library function might alter @code{errno} when thefunction returns an error.@strong{Portability Note:} ANSI C specifies @code{errno} as a``modifiable lvalue'' rather than as a variable, permitting it to beimplemented as a macro. For example, its expansion might involve afunction call, like @code{*_errno ()}. In fact, that is what it ison the GNU system itself. The GNU library, on non-GNU systems, doeswhatever is right for the particular system.There are a few library functions, like @code{sqrt} and @code{atan},that return a perfectly legitimate value in case of an error, but alsoset @code{errno}. For these functions, if you want to check to seewhether an error occurred, the recommended method is to set @code{errno}to zero before calling the function, and then check its value afterward.@end deftypevr@pindex errno.hAll the error codes have symbolic names; they are macros defined in@file{errno.h}. The names start with @samp{E} and an upper-caseletter or digit; you should consider names of this form to bereserved names. @xref{Reserved Names}.The error code values are all positive integers and are all distinct,with one exception: @code{EWOULDBLOCK} and @code{EAGAIN} are the same.Since the values are distinct, you can use them as labels in a@code{switch} statement; just don't use both @code{EWOULDBLOCK} and@code{EAGAIN}. Your program should not make any other assumptions aboutthe specific values of these symbolic constants.The value of @code{errno} doesn't necessarily have to correspond to anyof these macros, since some library functions might return other errorcodes of their own for other situations. The only values that areguaranteed to be meaningful for a particular library function are theones that this manual lists for that function.On non-GNU systems, almost any system call can return @code{EFAULT} ifit is given an invalid pointer as an argument. Since this could onlyhappen as a result of a bug in your program, and since it will nothappen on the GNU system, we have saved space by not mentioning@code{EFAULT} in the descriptions of individual functions.In some Unix systems, many system calls can also return @code{EFAULT} ifgiven as an argument a pointer into the stack, and the kernel for someobscure reason fails in its attempt to extend the stack. If this everhappens, you should probably try using statically or dynamicallyallocated memory instead of stack memory on that system.@node Error Codes, Error Messages, Checking for Errors, Error Reporting@section Error Codes@pindex errno.hThe error code macros are defined in the header file @file{errno.h}.All of them expand into integer constant values. Some of these errorcodes can't occur on the GNU system, but they can occur using the GNUlibrary on other systems.@comment errno.h@comment POSIX.1: Operation not permitted@deftypevr Macro int EPERM@comment errno 1 @c DO NOT REMOVEOperation not permitted; only the owner of the file (or other resource)or processes with special privileges can perform the operation.@end deftypevr@comment errno.h@comment POSIX.1: No such file or directory@deftypevr Macro int ENOENT@comment errno 2 @c DO NOT REMOVENo such file or directory. This is a ``file doesn't exist'' errorfor ordinary files that are referenced in contexts where they areexpected to already exist.@end deftypevr@comment errno.h@comment POSIX.1: No such process@deftypevr Macro int ESRCH@comment errno 3 @c DO NOT REMOVENo process matches the specified process ID.@end deftypevr@comment errno.h@comment POSIX.1: Interrupted system call@deftypevr Macro int EINTR@comment errno 4 @c DO NOT REMOVEInterrupted function call; an asynchronous signal occured and preventedcompletion of the call. When this happens, you should try the callagain.You can choose to have functions resume after a signal that is handled,rather than failing with @code{EINTR}; see @ref{InterruptedPrimitives}.@end deftypevr@comment errno.h@comment POSIX.1: Input/output error@deftypevr Macro int EIO@comment errno 5 @c DO NOT REMOVEInput/output error; usually used for physical read or write errors.@end deftypevr@comment errno.h@comment POSIX.1: Device not configured@deftypevr Macro int ENXIO@comment errno 6 @c DO NOT REMOVENo such device or address. The system tried to use the devicerepresented by a file you specified, and it couldn't find the device.This can mean that the device file was installed incorrectly, or thatthe physical device is missing or not correctly attached to thecomputer.@end deftypevr@comment errno.h@comment POSIX.1: Argument list too long@deftypevr Macro int E2BIG@comment errno 7 @c DO NOT REMOVEArgument list too long; used when the arguments passed to a new programbeing executed with one of the @code{exec} functions (@pxref{Executing aFile}) occupy too much memory space. This condition never arises in theGNU system.@end deftypevr@comment errno.h@comment POSIX.1: Exec format error@deftypevr Macro int ENOEXEC@comment errno 8 @c DO NOT REMOVEInvalid executable file format. This condition is detected by the@code{exec} functions; see @ref{Executing a File}.@end deftypevr@comment errno.h@comment POSIX.1: Bad file descriptor@deftypevr Macro int EBADF@comment errno 9 @c DO NOT REMOVEBad file descriptor; for example, I/O on a descriptor that has beenclosed or reading from a descriptor open only for writing (or viceversa).@end deftypevr@comment errno.h@comment POSIX.1: No child processes@deftypevr Macro int ECHILD@comment errno 10 @c DO NOT REMOVEThere are no child processes. This error happens on operations that aresupposed to manipulate child processes, when there aren't any processesto manipulate.@end deftypevr@comment errno.h@comment POSIX.1: Resource deadlock avoided@deftypevr Macro int EDEADLK@comment errno 11 @c DO NOT REMOVEDeadlock avoided; allocating a system resource would have resulted in adeadlock situation. The system does not guarantee that it will noticeall such situations. This error means you got lucky and the systemnoticed; it might just hang. @xref{File Locks}, for an example.@end deftypevr@comment errno.h@comment POSIX.1: Cannot allocate memory@deftypevr Macro int ENOMEM@comment errno 12 @c DO NOT REMOVENo memory available. The system cannot allocate more virtual memorybecause its capacity is full.@end deftypevr@comment errno.h@comment POSIX.1: Permission denied@deftypevr Macro int EACCES@comment errno 13 @c DO NOT REMOVEPermission denied; the file permissions do not allow the attempted operation.@end deftypevr@comment errno.h@comment POSIX.1: Bad address@deftypevr Macro int EFAULT@comment errno 14 @c DO NOT REMOVEBad address; an invalid pointer was detected.@end deftypevr@comment errno.h@comment BSD: Block device required@deftypevr Macro int ENOTBLK@comment errno 15 @c DO NOT REMOVEA file that isn't a block special file was given in a situation thatrequires one. For example, trying to mount an ordinary file as a filesystem in Unix gives this error.@end deftypevr@comment errno.h@comment POSIX.1: Device busy@deftypevr Macro int EBUSY@comment errno 16 @c DO NOT REMOVEResource busy; a system resource that can't be shared is already in use.For example, if you try to delete a file that is the root of a currentlymounted filesystem, you get this error.@end deftypevr@comment errno.h@comment POSIX.1: File exists@deftypevr Macro int EEXIST@comment errno 17 @c DO NOT REMOVEFile exists; an existing file was specified in a context where it onlymakes sense to specify a new file.@end deftypevr@comment errno.h@comment POSIX.1: Invalid cross-device link@deftypevr Macro int EXDEV@comment errno 18 @c DO NOT REMOVEAn attempt to make an improper link across file systems was detected.This happens not only when you use @code{link} (@pxref{Hard Links}) butalso when you rename a file with @code{rename} (@pxref{Renaming Files}).@end deftypevr@comment errno.h@comment POSIX.1: Operation not supported by device@deftypevr Macro int ENODEV@comment errno 19 @c DO NOT REMOVEThe wrong type of device was given to a function that expects aparticular sort of device.@end deftypevr@comment errno.h@comment POSIX.1: Not a directory@deftypevr Macro int ENOTDIR@comment errno 20 @c DO NOT REMOVEA file that isn't a directory was specified when a directory is required.@end deftypevr@comment errno.h@comment POSIX.1: Is a directory@deftypevr Macro int EISDIR@comment errno 21 @c DO NOT REMOVEFile is a directory; attempting to open a directory for writing givesthis error.@end deftypevr@comment errno.h@comment POSIX.1: Invalid argument@deftypevr Macro int EINVAL@comment errno 22 @c DO NOT REMOVEInvalid argument. This is used to indicate various kinds of problemswith passing the wrong argument to a library function.@end deftypevr@comment errno.h@comment POSIX.1: Too many open files@deftypevr Macro int EMFILE@comment errno 24 @c DO NOT REMOVEThe current process has too many files open and can't open any more.Duplicate descriptors do count toward this limit.In BSD and GNU, the number of open files is controlled by a resourcelimit that can usually be increased. If you get this error, you mightwant to increase the @code{RLIMIT_NOFILE} limit or make it unlimited;@pxref{Limits on Resources}.@end deftypevr@comment errno.h@comment POSIX.1: Too many open files in system@deftypevr Macro int ENFILE@comment errno 23 @c DO NOT REMOVEThere are too many distinct file openings in the entire system. Notethat any number of linked channels count as just one file opening; see@ref{Linked Channels}. This error never occurs in the GNU system.@end deftypevr@comment errno.h@comment POSIX.1: Inappropriate ioctl for device@deftypevr Macro int ENOTTY@comment errno 25 @c DO NOT REMOVEInappropriate I/O control operation, such as trying to set terminalmodes on an ordinary file.@end deftypevr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -