open 函数的 flag 参数和错误代码

时间:2022-12-13 08:40:44
一、flag 参数
  • 定义头文件:<bits/fcntl-linux.h>
----------------------------------------------------------------------------------------------------------------------------
  • 必选参数说明:
#define O_ACCMODE 0003:读写文件操作时取出 flag 的低两位
#define O_RDONLY 00:只读打开
#define O_WRONLY 01:只写打开
#define O_RDWR 02:读写打开
----------------------------------------------------------------------------------------------------------------------------
  • 可选参数说明:
# define O_APPEND 02000:每次写时都追加到文件的尾端
# define __O_CLOEXEC 02000000:把 FD_CLOEXEC 常量设置为文件描述符标志
#define O_CREAT 0100: 若文件不存在则创建它
# define __O_DIRECTORY 0200000:如果 path 引用的不是目录,则出错
#define O_EXCL 0200:如果同时指定了 O_CREAT,而文件已经存在,则出错。用此可以测试一个文件是否存在,如果不存在,则创建此文件,这使测试和创建两者成为一个原子操作
#define O_NOCTTY 0400:如果 path 引用的是终端设备,则不将该设备分配作为此进程的控制终端
# define __O_NOFOLLOW 0400000:如果 path 引用的是一个符号链接,则出错
# define O_NONBLOCK 04000:如果 path 引用的是 FIFO、块特殊文件或字符特殊文件,则此选项为文件的本次打开操作和后续的 I/O 操作设置非阻塞方式
# define O_SYNC 04010000:以同步的方式打开文件
#define O_TRUNC 01000:如果此文件存在,而且为只写或写-读成功打开,则将其长度截断为 0
# define __O_DSYNC 010000:使每次 write 要等待物理 I/O 操作完成,但是如果该写操作并不影响读取刚写入的数据,则不需等待文件属性被更新
# define __O_RSYNC O_SYNC

=============================================================================
二、错误代码(1-133)
  • 定义头文件:
/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h
----------------------------------------------------------------------------------------------------------------------------
  • 必选参数说明(标记为 open 函数可能产生的错误代码):
#define EPERM 1 /* Operation not permitted */严禁操作
#define ENOENT 2 /* No such file or directory */没有该文件或目录
#define ESRCH 3 /* No such process */没有该进程
#define EINTR 4 /* Interrupted system call */中断系统调用
#define EIO 5 /* I/O error */I/O错误
#define ENXIO 6 /* No such device or address */没有该驱动或地址
#define E2BIG 7 /* Argument list too long */参数列表太长
#define ENOEXEC 8 /* Exec format error */执行文件格式错误
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */没有子进程
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */内存不足
#define EACCES 13 /* Permission denied */权限不够
#define EFAULT 14 /* Bad address */错误地址
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */文件不存在
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */不是目录文件
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */错误参数
#define ENFILE 23 /* File table overflow */文件表溢出:已达到系统可同时打开的文件数上限
#define EMFILE 24 /* Too many open files */进程文件描述符用完
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */只读文件系统
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */文件名太长
#define ENOLCK 37 /* No record locks available */
#define ENOSYS 38 /* Invalid system call number */
#define ENOTEMPTY 39 /* Directory not empty */
#define ELOOP 40 /* Too many symbolic links encountered */太多符号链接错误
#define EWOULDBLOCK EAGAIN /* Operation would block */
#define ENOMSG 42 /* No message of desired type */
#define EIDRM 43 /* Identifier removed */
#define ECHRNG 44 /* Channel number out of range */
#define EL2NSYNC 45 /* Level 2 not synchronized */
#define EL3HLT 46 /* Level 3 halted */
#define EL3RST 47 /* Level 3 reset */
#define ELNRNG 48 /* Link number out of range */
#define EUNATCH 49 /* Protocol driver not attached */
#define ENOCSI 50 /* No CSI structure available */
#define EL2HLT 51 /* Level 2 halted */
#define EBADE 52 /* Invalid exchange */
#define EBADR 53 /* Invalid request descriptor */
#define EXFULL 54 /* Exchange full */
#define ENOANO 55 /* No anode */
#define EBADRQC 56 /* Invalid request code */
#define EBADSLT 57 /* Invalid slot */
#define EDEADLOCK EDEADLK
#define EBFONT 59 /* Bad font file format */
#define ENOSTR 60 /* Device not a stream */
#define ENODATA 61 /* No data available */
#define ETIME 62 /* Timer expired */
#define ENOSR 63 /* Out of streams resources */
#define ENONET 64 /* Machine is not on the network */
#define ENOPKG 65 /* Package not installed */
#define EREMOTE 66 /* Object is remote */
#define ENOLINK 67 /* Link has been severed */
#define EADV 68 /* Advertise error */
#define ESRMNT 69 /* Srmount error */
#define ECOMM 70 /* Communication error on send */
#define EPROTO 71 /* Protocol error */
#define EMULTIHOP 72 /* Multihop attempted */
#define EDOTDOT 73 /* RFS specific error */
#define EBADMSG 74 /* Not a data message */
#define EOVERFLOW 75 /* Value too large for defined data type */
#define ENOTUNIQ 76 /* Name not unique on network */
#define EBADFD 77 /* File descriptor in bad state */
#define EREMCHG 78 /* Remote address changed */
#define ELIBACC 79 /* Can not access a needed shared library */
#define ELIBBAD 80 /* Accessing a corrupted shared library */
#define ELIBSCN 81 /* .lib section in a.out corrupted */
#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
#define ELIBEXEC 83 /* Cannot exec a shared library directly */
#define EILSEQ 84 /* Illegal byte sequence */
#define ERESTART 85 /* Interrupted system call should be restarted */
#define ESTRPIPE 86 /* Streams pipe error */
#define EUSERS 87 /* Too many users */
#define ENOTSOCK 88 /* Socket operation on non-socket */
#define EDESTADDRREQ 89 /* Destination address required */
#define EMSGSIZE 90 /* Message too long */
#define EPROTOTYPE 91 /* Protocol wrong type for socket */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET 102 /* Network dropped connection because of reset */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS 109 /* Too many references: cannot splice */
#define ETIMEDOUT 110 /* Connection timed out */
#define ECONNREFUSED 111 /* Connection refused */
#define EHOSTDOWN 112 /* Host is down */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE 116 /* Stale file handle */
#define EUCLEAN 117 /* Structure needs cleaning */
#define ENOTNAM 118 /* Not a XENIX named type file */
#define ENAVAIL 119 /* No XENIX semaphores available */
#define EISNAM 120 /* Is a named type file */
#define EREMOTEIO 121 /* Remote I/O error */
#define EDQUOT 122 /* Quota exceeded */
#define ENOMEDIUM 123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#define ECANCELED 125 /* Operation Canceled */
#define ENOKEY 126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED 129 /* Key was rejected by service */
#define EOWNERDEAD 130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */
#define ERFKILL 132 /* Operation not possible due to RF-kill */
#define EHWPOISON 133 /* Memory page has hardware error */