xref: /DragonOS/kernel/crates/system_error/src/lib.rs (revision 81294aa2e6b257f0de5e3c28c3f3c89798330836)
1 #![no_std]
2 
3 use num_derive::{FromPrimitive, ToPrimitive};
4 
5 #[repr(i32)]
6 #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
7 #[allow(dead_code, non_camel_case_types)]
8 pub enum SystemError {
9     /// 操作不被允许 Operation not permitted.
10     EPERM = 1,
11     /// 没有指定的文件或目录 No such file or directory.
12     ENOENT = 2,
13     /// 没有这样的进程 No such process.
14     ESRCH = 3,
15     /// 被中断的函数 Interrupted function.
16     EINTR = 4,
17     /// I/O错误 I/O error.
18     EIO = 5,
19     /// 没有这样的设备或地址 No such device or address.
20     ENXIO = 6,
21     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
22     E2BIG = 7,
23     /// 可执行文件格式错误 Executable file format error
24     ENOEXEC = 8,
25     /// 错误的文件描述符 Bad file descriptor.
26     EBADF = 9,
27     /// 没有子进程 No child processes.
28     ECHILD = 10,
29     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
30     ///
31     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
32     EAGAIN_OR_EWOULDBLOCK = 11,
33     /// 没有足够的空间 Not enough space.
34     ENOMEM = 12,
35     /// 访问被拒绝 Permission denied
36     EACCES = 13,
37     /// 错误的地址 Bad address
38     EFAULT = 14,
39     /// 需要块设备 Block device required
40     ENOTBLK = 15,
41     /// 设备或资源忙 Device or resource busy.
42     EBUSY = 16,
43     /// 文件已存在 File exists.
44     EEXIST = 17,
45     /// 跨设备连接 Cross-device link.
46     EXDEV = 18,
47     /// 没有指定的设备 No such device.
48     ENODEV = 19,
49     /// 不是目录 Not a directory.
50     ENOTDIR = 20,
51     /// 是一个目录 Is a directory
52     EISDIR = 21,
53     /// 不可用的参数 Invalid argument.
54     EINVAL = 22,
55     /// 系统中打开的文件过多 Too many files open in system.
56     ENFILE = 23,
57     /// 文件描述符的值过大 File descriptor value too large.
58     EMFILE = 24,
59     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
60     ENOTTY = 25,
61     /// 文本文件忙 Text file busy.
62     ETXTBSY = 26,
63     /// 文件太大 File too large.
64     EFBIG = 27,
65     /// 设备上没有空间 No space left on device.
66     ENOSPC = 28,
67     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
68     ESPIPE = 29,
69     /// 只读的文件系统 Read-only file system.
70     EROFS = 30,
71     /// 链接数过多 Too many links.
72     EMLINK = 31,
73     /// 断开的管道 Broken pipe.
74     EPIPE = 32,
75     /// 数学参数超出作用域 Mathematics argument out of domain of function.
76     EDOM = 33,
77     /// 结果过大 Result too large.
78     ERANGE = 34,
79     /// 资源死锁将要发生 Resource deadlock would occur.
80     EDEADLK = 35,
81     /// 文件名过长 Filename too long.
82     ENAMETOOLONG = 36,
83     /// 没有可用的锁 No locks available.
84     ENOLCK = 37,
85     /// 功能不支持 Function not supported.
86     ENOSYS = 38,
87     /// 目录非空 Directory not empty.
88     ENOTEMPTY = 39,
89     /// 符号链接级别过多 Too many levels of symbolic links.
90     ELOOP = 40,
91     /// 没有期待类型的消息 No message of the desired type.
92     ENOMSG = 41,
93     /// 标志符被移除 Identifier removed.
94     EIDRM = 42,
95     /// 通道号超出范围 Channel number out of range
96     ECHRNG = 43,
97     /// 二级不同步 Level 2 not synchronized
98     EL2NSYNC = 44,
99     /// 三级暂停 Level 3 halted
100     EL3HLT = 45,
101     /// 三级重置 Level 3 reset
102     EL3RST = 46,
103     /// 链接号超出范围 Link number out of range
104     ELNRNG = 47,
105     /// 未连接协议驱动程序 Protocol driver not attached
106     EUNATCH = 48,
107     /// 没有可用的CSI结构 No CSI structure available
108     ENOCSI = 49,
109     /// 二级暂停 Level 2 halted
110     EL2HLT = 50,
111     /// 无效交换 Invalid exchange
112     EBADE = 51,
113     /// 无效的请求描述符 Invalid request descriptor
114     EBADR = 52,
115     /// 交换满 Exchange full
116     EXFULL = 53,
117     /// 无阳极 No anode
118     ENOANO = 54,
119     /// 请求码无效 Invalid request code
120     EBADRQC = 55,
121     /// 无效插槽 Invalid slot
122     EBADSLT = 56,
123     /// 资源死锁 Resource deadlock would occur
124     EDEADLOCK = 57,
125     /// 错误的字体文件格式 Bad font file format
126     EBFONT = 58,
127     /// 不是STREAM Not a STREAM
128     ENOSTR = 59,
129     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
130     ENODATA = 60,
131     /// 流式ioctl()超时 Stream ioctl() timeout
132     ETIME = 61,
133     /// 没有STREAM资源  No STREAM resources.
134     ENOSR = 62,
135     /// 机器不在网络上 Machine is not on the network
136     ENONET = 63,
137     /// 未安装软件包 Package not installed
138     ENOPKG = 64,
139     /// 远程对象 Object is remote
140     EREMOTE = 65,
141     /// 保留 Reserved.
142     ENOLINK = 66,
143     /// 外设错误 Advertise error.
144     EADV = 67,
145     /// 安装错误 Srmount error
146     ESRMNT = 68,
147     /// 发送时发生通信错误 Communication error on send
148     ECOMM = 69,
149     /// 协议错误 Protocol error.
150     EPROTO = 70,
151     /// 保留使用 Reserved.
152     EMULTIHOP = 71,
153     /// RFS特定错误 RFS specific error
154     EDOTDOT = 72,
155     /// 错误的消息 Bad message.
156     EBADMSG = 73,
157     /// 数值过大,产生溢出 Value too large to be stored in data type.
158     EOVERFLOW = 74,
159     /// 名称在网络上不是唯一的 Name not unique on network
160     ENOTUNIQ = 75,
161     /// 处于不良状态的文件描述符 File descriptor in bad state
162     EBADFD = 76,
163     /// 远程地址已更改 Remote address changed
164     EREMCHG = 77,
165     /// 无法访问所需的共享库 Can not access a needed shared library
166     ELIBACC = 78,
167     /// 访问损坏的共享库 Accessing a corrupted shared library
168     ELIBBAD = 79,
169     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
170     ELIBSCN = 80,
171     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
172     ELIBMAX = 81,
173     /// 无法直接执行共享库 Cannot exec a shared library directly
174     ELIBEXEC = 82,
175     /// 不合法的字符序列 Illegal byte sequence.
176     EILSEQ = 83,
177     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
178     ERESTART = 84,
179     /// 流管道错误 Streams pipe error
180     ESTRPIPE = 85,
181     /// 用户太多 Too many users
182     EUSERS = 86,
183     /// 不是一个套接字 Not a socket.
184     ENOTSOCK = 87,
185     /// 需要目标地址 Destination address required.
186     EDESTADDRREQ = 88,
187     /// 消息过大 Message too large.
188     EMSGSIZE = 89,
189     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
190     EPROTOTYPE = 90,
191     /// 协议不可用 Protocol not available.
192     ENOPROTOOPT = 91,
193     /// 协议不被支持 Protocol not supported.
194     EPROTONOSUPPORT = 92,
195     /// 不支持套接字类型 Socket type not supported
196     ESOCKTNOSUPPORT = 93,
197     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
198     ///
199     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
200     EOPNOTSUPP_OR_ENOTSUP = 94,
201     /// 不支持协议系列 Protocol family not supported
202     EPFNOSUPPORT = 95,
203     /// 地址family不支持 Address family not supported.
204     EAFNOSUPPORT = 96,
205     /// 地址正在被使用 Address in use.
206     EADDRINUSE = 97,
207     /// 地址不可用 Address  not available.
208     EADDRNOTAVAIL = 98,
209     /// 网络已关闭 Network is down.
210     ENETDOWN = 99,
211     /// 网络不可达 Network unreachable.
212     ENETUNREACH = 100,
213     /// 网络连接已断开 Connection aborted by network.
214     ENETRESET = 101,
215     /// 连接已断开 Connection aborted.
216     ECONNABORTED = 102,
217     /// 连接被重置 Connection reset.
218     ECONNRESET = 103,
219     /// 缓冲区空间不足 No buffer space available.
220     ENOBUFS = 104,
221     /// 套接字已连接 Socket is connected.
222     EISCONN = 105,
223     /// 套接字未连接 The socket is not connected.
224     ENOTCONN = 106,
225     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
226     ESHUTDOWN = 107,
227     /// 引用太多:无法拼接 Too many references: cannot splice
228     ETOOMANYREFS = 108,
229     /// 连接超时 Connection timed out.
230     ETIMEDOUT = 109,
231     /// 连接被拒绝 Connection refused.
232     ECONNREFUSED = 110,
233     /// 主机已关闭 Host is down
234     EHOSTDOWN = 111,
235     /// 主机不可达 Host is unreachable.
236     EHOSTUNREACH = 112,
237     /// 连接已经在处理 Connection already in progress.
238     EALREADY = 113,
239     /// 操作正在处理 Operation in progress.
240     EINPROGRESS = 114,
241     /// 保留 Reserved.
242     ESTALE = 115,
243     /// 结构需要清理 Structure needs cleaning
244     EUCLEAN = 116,
245     /// 不是XENIX命名类型文件 Not a XENIX named type file
246     ENOTNAM = 117,
247     /// 没有可用的XENIX信号量 No XENIX semaphores available
248     ENAVAIL = 118,
249     /// 是命名类型文件 Is a named type file
250     EISNAM = 119,
251     /// 远程I/O错误 Remote I/O error
252     EREMOTEIO = 120,
253     /// 保留使用 Reserved
254     EDQUOT = 121,
255     /// 没有找到媒介 No medium found
256     ENOMEDIUM = 122,
257     /// 介质类型错误 Wrong medium type
258     EMEDIUMTYPE = 123,
259     /// 操作被取消 Operation canceled.
260     ECANCELED = 124,
261     /// 所需的密钥不可用 Required key not available
262     ENOKEY = 125,
263     /// 密钥已过期 Key has expired
264     EKEYEXPIRED = 126,
265     /// 密钥已被撤销 Key has been revoked
266     EKEYREVOKED = 127,
267     /// 密钥被服务拒绝 Key has been revoked
268     EKEYREJECTED = 128,
269     /// 之前的拥有者挂了 Previous owner died.
270     EOWNERDEAD = 129,
271     /// 状态不可恢复 State not recoverable.
272     ENOTRECOVERABLE = 130,
273     // VMX on 虚拟化开启指令出错
274     EVMXONFailed = 131,
275     // VMX off 虚拟化关闭指令出错
276     EVMXOFFFailed = 132,
277     // VMX VMWRITE 写入虚拟化VMCS内存出错
278     EVMWRITEFailed = 133,
279     EVMREADFailed = 134,
280     EVMPRTLDFailed = 135,
281     EVMLAUNCHFailed = 136,
282     KVM_HVA_ERR_BAD = 137,
283 
284     // === 以下错误码不应该被用户态程序使用 ===
285     ERESTARTSYS = 512,
286 }
287 
288 impl SystemError {
289     /// @brief 把posix错误码转换为系统错误枚举类型。
290     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
291         // posix 错误码是小于0的
292         if errno >= 0 {
293             return None;
294         }
295         return <Self as num_traits::FromPrimitive>::from_i32(-errno);
296     }
297 
298     /// @brief 把系统错误枚举类型转换为负数posix错误码。
299     pub fn to_posix_errno(&self) -> i32 {
300         return -<Self as num_traits::ToPrimitive>::to_i32(self).unwrap();
301     }
302 }
303 
304 #[cfg(test)]
305 mod tests {
306     use super::*;
307 
308     #[test]
309     fn it_works() {
310         assert_eq!(SystemError::EPERM.to_posix_errno(), -1);
311     }
312 }
313