xref: /DragonOS/kernel/crates/system_error/src/lib.rs (revision 52da9a59374752b4d01907b052135a0d317781dd)
191e9d4abSLoGin #![no_std]
291e9d4abSLoGin 
391e9d4abSLoGin use num_derive::{FromPrimitive, ToPrimitive};
491e9d4abSLoGin 
591e9d4abSLoGin #[repr(i32)]
691e9d4abSLoGin #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
791e9d4abSLoGin #[allow(dead_code, non_camel_case_types)]
891e9d4abSLoGin pub enum SystemError {
991e9d4abSLoGin     /// 操作不被允许 Operation not permitted.
1091e9d4abSLoGin     EPERM = 1,
1191e9d4abSLoGin     /// 没有指定的文件或目录 No such file or directory.
1291e9d4abSLoGin     ENOENT = 2,
1391e9d4abSLoGin     /// 没有这样的进程 No such process.
1491e9d4abSLoGin     ESRCH = 3,
1591e9d4abSLoGin     /// 被中断的函数 Interrupted function.
1691e9d4abSLoGin     EINTR = 4,
1791e9d4abSLoGin     /// I/O错误 I/O error.
1891e9d4abSLoGin     EIO = 5,
1991e9d4abSLoGin     /// 没有这样的设备或地址 No such device or address.
2091e9d4abSLoGin     ENXIO = 6,
2191e9d4abSLoGin     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
2291e9d4abSLoGin     E2BIG = 7,
2391e9d4abSLoGin     /// 可执行文件格式错误 Executable file format error
2491e9d4abSLoGin     ENOEXEC = 8,
2591e9d4abSLoGin     /// 错误的文件描述符 Bad file descriptor.
2691e9d4abSLoGin     EBADF = 9,
2791e9d4abSLoGin     /// 没有子进程 No child processes.
2891e9d4abSLoGin     ECHILD = 10,
2991e9d4abSLoGin     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
3091e9d4abSLoGin     ///
3191e9d4abSLoGin     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
3291e9d4abSLoGin     EAGAIN_OR_EWOULDBLOCK = 11,
3391e9d4abSLoGin     /// 没有足够的空间 Not enough space.
3491e9d4abSLoGin     ENOMEM = 12,
3591e9d4abSLoGin     /// 访问被拒绝 Permission denied
3691e9d4abSLoGin     EACCES = 13,
3791e9d4abSLoGin     /// 错误的地址 Bad address
3891e9d4abSLoGin     EFAULT = 14,
3991e9d4abSLoGin     /// 需要块设备 Block device required
4091e9d4abSLoGin     ENOTBLK = 15,
4191e9d4abSLoGin     /// 设备或资源忙 Device or resource busy.
4291e9d4abSLoGin     EBUSY = 16,
4391e9d4abSLoGin     /// 文件已存在 File exists.
4491e9d4abSLoGin     EEXIST = 17,
4591e9d4abSLoGin     /// 跨设备连接 Cross-device link.
4691e9d4abSLoGin     EXDEV = 18,
4791e9d4abSLoGin     /// 没有指定的设备 No such device.
4891e9d4abSLoGin     ENODEV = 19,
4991e9d4abSLoGin     /// 不是目录 Not a directory.
5091e9d4abSLoGin     ENOTDIR = 20,
5191e9d4abSLoGin     /// 是一个目录 Is a directory
5291e9d4abSLoGin     EISDIR = 21,
5391e9d4abSLoGin     /// 不可用的参数 Invalid argument.
5491e9d4abSLoGin     EINVAL = 22,
5591e9d4abSLoGin     /// 系统中打开的文件过多 Too many files open in system.
5691e9d4abSLoGin     ENFILE = 23,
5791e9d4abSLoGin     /// 文件描述符的值过大 File descriptor value too large.
5891e9d4abSLoGin     EMFILE = 24,
5991e9d4abSLoGin     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
6091e9d4abSLoGin     ENOTTY = 25,
6191e9d4abSLoGin     /// 文本文件忙 Text file busy.
6291e9d4abSLoGin     ETXTBSY = 26,
6391e9d4abSLoGin     /// 文件太大 File too large.
6491e9d4abSLoGin     EFBIG = 27,
6591e9d4abSLoGin     /// 设备上没有空间 No space left on device.
6691e9d4abSLoGin     ENOSPC = 28,
6791e9d4abSLoGin     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
6891e9d4abSLoGin     ESPIPE = 29,
6991e9d4abSLoGin     /// 只读的文件系统 Read-only file system.
7091e9d4abSLoGin     EROFS = 30,
7191e9d4abSLoGin     /// 链接数过多 Too many links.
7291e9d4abSLoGin     EMLINK = 31,
7391e9d4abSLoGin     /// 断开的管道 Broken pipe.
7491e9d4abSLoGin     EPIPE = 32,
7591e9d4abSLoGin     /// 数学参数超出作用域 Mathematics argument out of domain of function.
7691e9d4abSLoGin     EDOM = 33,
7791e9d4abSLoGin     /// 结果过大 Result too large.
7891e9d4abSLoGin     ERANGE = 34,
7991e9d4abSLoGin     /// 资源死锁将要发生 Resource deadlock would occur.
800e2c2e8bS裕依     EDEADLK_OR_EDEADLOCK = 35,
8191e9d4abSLoGin     /// 文件名过长 Filename too long.
8291e9d4abSLoGin     ENAMETOOLONG = 36,
8391e9d4abSLoGin     /// 没有可用的锁 No locks available.
8491e9d4abSLoGin     ENOLCK = 37,
8591e9d4abSLoGin     /// 功能不支持 Function not supported.
8691e9d4abSLoGin     ENOSYS = 38,
8791e9d4abSLoGin     /// 目录非空 Directory not empty.
8891e9d4abSLoGin     ENOTEMPTY = 39,
8991e9d4abSLoGin     /// 符号链接级别过多 Too many levels of symbolic links.
9091e9d4abSLoGin     ELOOP = 40,
9191e9d4abSLoGin     /// 没有期待类型的消息 No message of the desired type.
920e2c2e8bS裕依     ENOMSG = 42,
9391e9d4abSLoGin     /// 标志符被移除 Identifier removed.
940e2c2e8bS裕依     EIDRM = 43,
9591e9d4abSLoGin     /// 通道号超出范围 Channel number out of range
960e2c2e8bS裕依     ECHRNG = 44,
9791e9d4abSLoGin     /// 二级不同步 Level 2 not synchronized
980e2c2e8bS裕依     EL2NSYNC = 45,
9991e9d4abSLoGin     /// 三级暂停 Level 3 halted
1000e2c2e8bS裕依     EL3HLT = 46,
10191e9d4abSLoGin     /// 三级重置 Level 3 reset
1020e2c2e8bS裕依     EL3RST = 47,
10391e9d4abSLoGin     /// 链接号超出范围 Link number out of range
1040e2c2e8bS裕依     ELNRNG = 48,
10591e9d4abSLoGin     /// 未连接协议驱动程序 Protocol driver not attached
1060e2c2e8bS裕依     EUNATCH = 49,
10791e9d4abSLoGin     /// 没有可用的CSI结构 No CSI structure available
1080e2c2e8bS裕依     ENOCSI = 50,
10991e9d4abSLoGin     /// 二级暂停 Level 2 halted
1100e2c2e8bS裕依     EL2HLT = 51,
11191e9d4abSLoGin     /// 无效交换 Invalid exchange
1120e2c2e8bS裕依     EBADE = 52,
11391e9d4abSLoGin     /// 无效的请求描述符 Invalid request descriptor
1140e2c2e8bS裕依     EBADR = 53,
11591e9d4abSLoGin     /// 交换满 Exchange full
1160e2c2e8bS裕依     EXFULL = 54,
11791e9d4abSLoGin     /// 无阳极 No anode
1180e2c2e8bS裕依     ENOANO = 55,
11991e9d4abSLoGin     /// 请求码无效 Invalid request code
1200e2c2e8bS裕依     EBADRQC = 56,
12191e9d4abSLoGin     /// 无效插槽 Invalid slot
1220e2c2e8bS裕依     EBADSLT = 57,
12391e9d4abSLoGin     /// 错误的字体文件格式 Bad font file format
1240e2c2e8bS裕依     EBFONT = 59,
12591e9d4abSLoGin     /// 不是STREAM Not a STREAM
1260e2c2e8bS裕依     ENOSTR = 60,
12791e9d4abSLoGin     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
1280e2c2e8bS裕依     ENODATA = 61,
12991e9d4abSLoGin     /// 流式ioctl()超时 Stream ioctl() timeout
1300e2c2e8bS裕依     ETIME = 62,
13191e9d4abSLoGin     /// 没有STREAM资源  No STREAM resources.
1320e2c2e8bS裕依     ENOSR = 63,
13391e9d4abSLoGin     /// 机器不在网络上 Machine is not on the network
1340e2c2e8bS裕依     ENONET = 64,
13591e9d4abSLoGin     /// 未安装软件包 Package not installed
1360e2c2e8bS裕依     ENOPKG = 65,
13791e9d4abSLoGin     /// 远程对象 Object is remote
1380e2c2e8bS裕依     EREMOTE = 66,
13991e9d4abSLoGin     /// 保留 Reserved.
1400e2c2e8bS裕依     ENOLINK = 67,
14191e9d4abSLoGin     /// 外设错误 Advertise error.
1420e2c2e8bS裕依     EADV = 68,
14391e9d4abSLoGin     /// 安装错误 Srmount error
1440e2c2e8bS裕依     ESRMNT = 69,
14591e9d4abSLoGin     /// 发送时发生通信错误 Communication error on send
1460e2c2e8bS裕依     ECOMM = 70,
14791e9d4abSLoGin     /// 协议错误 Protocol error.
1480e2c2e8bS裕依     EPROTO = 71,
14991e9d4abSLoGin     /// 保留使用 Reserved.
1500e2c2e8bS裕依     EMULTIHOP = 72,
15191e9d4abSLoGin     /// RFS特定错误 RFS specific error
1520e2c2e8bS裕依     EDOTDOT = 73,
15391e9d4abSLoGin     /// 错误的消息 Bad message.
1540e2c2e8bS裕依     EBADMSG = 74,
15591e9d4abSLoGin     /// 数值过大,产生溢出 Value too large to be stored in data type.
1560e2c2e8bS裕依     EOVERFLOW = 75,
15791e9d4abSLoGin     /// 名称在网络上不是唯一的 Name not unique on network
1580e2c2e8bS裕依     ENOTUNIQ = 76,
15991e9d4abSLoGin     /// 处于不良状态的文件描述符 File descriptor in bad state
1600e2c2e8bS裕依     EBADFD = 77,
16191e9d4abSLoGin     /// 远程地址已更改 Remote address changed
1620e2c2e8bS裕依     EREMCHG = 78,
16391e9d4abSLoGin     /// 无法访问所需的共享库 Can not access a needed shared library
1640e2c2e8bS裕依     ELIBACC = 79,
16591e9d4abSLoGin     /// 访问损坏的共享库 Accessing a corrupted shared library
1660e2c2e8bS裕依     ELIBBAD = 80,
16791e9d4abSLoGin     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
1680e2c2e8bS裕依     ELIBSCN = 81,
16991e9d4abSLoGin     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
1700e2c2e8bS裕依     ELIBMAX = 82,
17191e9d4abSLoGin     /// 无法直接执行共享库 Cannot exec a shared library directly
1720e2c2e8bS裕依     ELIBEXEC = 83,
17391e9d4abSLoGin     /// 不合法的字符序列 Illegal byte sequence.
1740e2c2e8bS裕依     EILSEQ = 84,
17591e9d4abSLoGin     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
1760e2c2e8bS裕依     ERESTART = 85,
17791e9d4abSLoGin     /// 流管道错误 Streams pipe error
1780e2c2e8bS裕依     ESTRPIPE = 86,
17991e9d4abSLoGin     /// 用户太多 Too many users
1800e2c2e8bS裕依     EUSERS = 87,
18191e9d4abSLoGin     /// 不是一个套接字 Not a socket.
1820e2c2e8bS裕依     ENOTSOCK = 88,
18391e9d4abSLoGin     /// 需要目标地址 Destination address required.
1840e2c2e8bS裕依     EDESTADDRREQ = 89,
18591e9d4abSLoGin     /// 消息过大 Message too large.
1860e2c2e8bS裕依     EMSGSIZE = 90,
18791e9d4abSLoGin     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
1880e2c2e8bS裕依     EPROTOTYPE = 91,
18991e9d4abSLoGin     /// 协议不可用 Protocol not available.
1900e2c2e8bS裕依     ENOPROTOOPT = 92,
19191e9d4abSLoGin     /// 协议不被支持 Protocol not supported.
1920e2c2e8bS裕依     EPROTONOSUPPORT = 93,
19391e9d4abSLoGin     /// 不支持套接字类型 Socket type not supported
1940e2c2e8bS裕依     ESOCKTNOSUPPORT = 94,
19591e9d4abSLoGin     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
19691e9d4abSLoGin     ///
19791e9d4abSLoGin     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
1980e2c2e8bS裕依     EOPNOTSUPP_OR_ENOTSUP = 95,
19991e9d4abSLoGin     /// 不支持协议系列 Protocol family not supported
2000e2c2e8bS裕依     EPFNOSUPPORT = 96,
20191e9d4abSLoGin     /// 地址family不支持 Address family not supported.
2020e2c2e8bS裕依     EAFNOSUPPORT = 97,
20391e9d4abSLoGin     /// 地址正在被使用 Address in use.
2040e2c2e8bS裕依     EADDRINUSE = 98,
20591e9d4abSLoGin     /// 地址不可用 Address  not available.
2060e2c2e8bS裕依     EADDRNOTAVAIL = 99,
20791e9d4abSLoGin     /// 网络已关闭 Network is down.
2080e2c2e8bS裕依     ENETDOWN = 100,
20991e9d4abSLoGin     /// 网络不可达 Network unreachable.
2100e2c2e8bS裕依     ENETUNREACH = 101,
21191e9d4abSLoGin     /// 网络连接已断开 Connection aborted by network.
2120e2c2e8bS裕依     ENETRESET = 102,
21391e9d4abSLoGin     /// 连接已断开 Connection aborted.
2140e2c2e8bS裕依     ECONNABORTED = 103,
21591e9d4abSLoGin     /// 连接被重置 Connection reset.
2160e2c2e8bS裕依     ECONNRESET = 104,
21791e9d4abSLoGin     /// 缓冲区空间不足 No buffer space available.
2180e2c2e8bS裕依     ENOBUFS = 105,
21991e9d4abSLoGin     /// 套接字已连接 Socket is connected.
2200e2c2e8bS裕依     EISCONN = 106,
22191e9d4abSLoGin     /// 套接字未连接 The socket is not connected.
2220e2c2e8bS裕依     ENOTCONN = 107,
22391e9d4abSLoGin     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
2240e2c2e8bS裕依     ESHUTDOWN = 108,
22591e9d4abSLoGin     /// 引用太多:无法拼接 Too many references: cannot splice
2260e2c2e8bS裕依     ETOOMANYREFS = 109,
22791e9d4abSLoGin     /// 连接超时 Connection timed out.
2280e2c2e8bS裕依     ETIMEDOUT = 110,
22991e9d4abSLoGin     /// 连接被拒绝 Connection refused.
2300e2c2e8bS裕依     ECONNREFUSED = 111,
23191e9d4abSLoGin     /// 主机已关闭 Host is down
2320e2c2e8bS裕依     EHOSTDOWN = 112,
23391e9d4abSLoGin     /// 主机不可达 Host is unreachable.
2340e2c2e8bS裕依     EHOSTUNREACH = 113,
23591e9d4abSLoGin     /// 连接已经在处理 Connection already in progress.
2360e2c2e8bS裕依     EALREADY = 114,
23791e9d4abSLoGin     /// 操作正在处理 Operation in progress.
2380e2c2e8bS裕依     EINPROGRESS = 115,
23991e9d4abSLoGin     /// 保留 Reserved.
2400e2c2e8bS裕依     ESTALE = 116,
24191e9d4abSLoGin     /// 结构需要清理 Structure needs cleaning
2420e2c2e8bS裕依     EUCLEAN = 117,
24391e9d4abSLoGin     /// 不是XENIX命名类型文件 Not a XENIX named type file
2440e2c2e8bS裕依     ENOTNAM = 118,
24591e9d4abSLoGin     /// 没有可用的XENIX信号量 No XENIX semaphores available
2460e2c2e8bS裕依     ENAVAIL = 119,
24791e9d4abSLoGin     /// 是命名类型文件 Is a named type file
2480e2c2e8bS裕依     EISNAM = 120,
24991e9d4abSLoGin     /// 远程I/O错误 Remote I/O error
2500e2c2e8bS裕依     EREMOTEIO = 121,
25191e9d4abSLoGin     /// 保留使用 Reserved
2520e2c2e8bS裕依     EDQUOT = 122,
25391e9d4abSLoGin     /// 没有找到媒介 No medium found
2540e2c2e8bS裕依     ENOMEDIUM = 123,
25591e9d4abSLoGin     /// 介质类型错误 Wrong medium type
2560e2c2e8bS裕依     EMEDIUMTYPE = 124,
25791e9d4abSLoGin     /// 操作被取消 Operation canceled.
2580e2c2e8bS裕依     ECANCELED = 125,
25991e9d4abSLoGin     /// 所需的密钥不可用 Required key not available
2600e2c2e8bS裕依     ENOKEY = 126,
26191e9d4abSLoGin     /// 密钥已过期 Key has expired
2620e2c2e8bS裕依     EKEYEXPIRED = 127,
26391e9d4abSLoGin     /// 密钥已被撤销 Key has been revoked
2640e2c2e8bS裕依     EKEYREVOKED = 128,
26591e9d4abSLoGin     /// 密钥被服务拒绝 Key has been revoked
2660e2c2e8bS裕依     EKEYREJECTED = 129,
26791e9d4abSLoGin     /// 之前的拥有者挂了 Previous owner died.
2680e2c2e8bS裕依     EOWNERDEAD = 130,
26991e9d4abSLoGin     /// 状态不可恢复 State not recoverable.
2700e2c2e8bS裕依     ENOTRECOVERABLE = 131,
2710e2c2e8bS裕依     /// 由于射频终止,无法操作 Operation not possible due to RF-kill
2720e2c2e8bS裕依     ERFKILL = 132,
2730e2c2e8bS裕依     /// 内存页面有硬件错误 Memory page has hardware error
2740e2c2e8bS裕依     EHWPOISON = 133,
27591e9d4abSLoGin 
27691e9d4abSLoGin     // === 以下错误码不应该被用户态程序使用 ===
27791e9d4abSLoGin     ERESTARTSYS = 512,
2780e2c2e8bS裕依     // VMX on 虚拟化开启指令出错
2790e2c2e8bS裕依     EVMXONFailed = 513,
2800e2c2e8bS裕依     // VMX off 虚拟化关闭指令出错
2810e2c2e8bS裕依     EVMXOFFFailed = 514,
2820e2c2e8bS裕依     // VMX VMWRITE 写入虚拟化VMCS内存出错
2830e2c2e8bS裕依     EVMWRITEFailed = 515,
2840e2c2e8bS裕依     EVMREADFailed = 516,
2850e2c2e8bS裕依     EVMPRTLDFailed = 517,
2860e2c2e8bS裕依     EVMLAUNCHFailed = 518,
2870e2c2e8bS裕依     KVM_HVA_ERR_BAD = 519,
288*52da9a59SGnoCiYeH     /// 没有对应的ioctlcmd
289*52da9a59SGnoCiYeH     ENOIOCTLCMD = 520,
29091e9d4abSLoGin }
29191e9d4abSLoGin 
29291e9d4abSLoGin impl SystemError {
29391e9d4abSLoGin     /// @brief 把posix错误码转换为系统错误枚举类型。
29491e9d4abSLoGin     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
29591e9d4abSLoGin         // posix 错误码是小于0的
29691e9d4abSLoGin         if errno >= 0 {
29791e9d4abSLoGin             return None;
29891e9d4abSLoGin         }
29991e9d4abSLoGin         return <Self as num_traits::FromPrimitive>::from_i32(-errno);
30091e9d4abSLoGin     }
30191e9d4abSLoGin 
30291e9d4abSLoGin     /// @brief 把系统错误枚举类型转换为负数posix错误码。
30391e9d4abSLoGin     pub fn to_posix_errno(&self) -> i32 {
30491e9d4abSLoGin         return -<Self as num_traits::ToPrimitive>::to_i32(self).unwrap();
30591e9d4abSLoGin     }
30691e9d4abSLoGin }
30791e9d4abSLoGin 
30891e9d4abSLoGin #[cfg(test)]
30991e9d4abSLoGin mod tests {
31091e9d4abSLoGin     use super::*;
31191e9d4abSLoGin 
31291e9d4abSLoGin     #[test]
31391e9d4abSLoGin     fn it_works() {
31491e9d4abSLoGin         assert_eq!(SystemError::EPERM.to_posix_errno(), -1);
31591e9d4abSLoGin     }
31691e9d4abSLoGin }
317