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