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