lib.rs 11 KB

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