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