signal.rs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. use crate::{
  2. arch::{sched::sched, CurrentIrqArch},
  3. exception::InterruptArch,
  4. kerror,
  5. process::ProcessManager,
  6. };
  7. /// 信号最大值
  8. pub const MAX_SIG_NUM: usize = 64;
  9. #[allow(dead_code)]
  10. #[derive(Eq)]
  11. #[repr(usize)]
  12. #[allow(non_camel_case_types)]
  13. #[atomic_enum]
  14. pub enum Signal {
  15. INVALID = 0,
  16. SIGHUP = 1,
  17. SIGINT,
  18. SIGQUIT,
  19. SIGILL,
  20. SIGTRAP,
  21. /// SIGABRT和SIGIOT共用这个号码
  22. SIGABRT_OR_IOT,
  23. SIGBUS,
  24. SIGFPE,
  25. SIGKILL,
  26. SIGUSR1,
  27. SIGSEGV = 11,
  28. SIGUSR2,
  29. SIGPIPE,
  30. SIGALRM,
  31. SIGTERM,
  32. SIGSTKFLT,
  33. SIGCHLD,
  34. SIGCONT,
  35. SIGSTOP,
  36. SIGTSTP,
  37. SIGTTIN = 21,
  38. SIGTTOU,
  39. SIGURG,
  40. SIGXCPU,
  41. SIGXFSZ,
  42. SIGVTALRM,
  43. SIGPROF,
  44. SIGWINCH,
  45. /// SIGIO和SIGPOLL共用这个号码
  46. SIGIO_OR_POLL,
  47. SIGPWR,
  48. SIGSYS = 31,
  49. SIGRTMIN = 32,
  50. SIGRTMAX = 64,
  51. }
  52. /// 为Signal实现判断相等的trait
  53. impl PartialEq for Signal {
  54. fn eq(&self, other: &Signal) -> bool {
  55. *self as usize == *other as usize
  56. }
  57. }
  58. impl From<usize> for Signal {
  59. fn from(value: usize) -> Self {
  60. if value <= MAX_SIG_NUM {
  61. let ret: Signal = unsafe { core::mem::transmute(value) };
  62. return ret;
  63. } else {
  64. kerror!("Try to convert an invalid number to Signal");
  65. return Signal::INVALID;
  66. }
  67. }
  68. }
  69. impl Into<usize> for Signal {
  70. fn into(self) -> usize {
  71. self as usize
  72. }
  73. }
  74. impl From<i32> for Signal {
  75. fn from(value: i32) -> Self {
  76. if value < 0 {
  77. kerror!("Try to convert an invalid number to Signal");
  78. return Signal::INVALID;
  79. } else {
  80. return Self::from(value as usize);
  81. }
  82. }
  83. }
  84. impl Into<SigSet> for Signal {
  85. fn into(self) -> SigSet {
  86. SigSet {
  87. bits: (1 << (self as usize - 1) as u64),
  88. }
  89. }
  90. }
  91. impl Signal {
  92. /// 判断一个数字是否为可用的信号
  93. #[inline]
  94. pub fn is_valid(&self) -> bool {
  95. return (*self) as usize <= MAX_SIG_NUM;
  96. }
  97. /// const convertor between `Signal` and `SigSet`
  98. pub const fn into_sigset(self) -> SigSet {
  99. SigSet {
  100. bits: (1 << (self as usize - 1) as u64),
  101. }
  102. }
  103. /// 判断一个信号是不是实时信号
  104. ///
  105. /// ## 返回值
  106. ///
  107. /// - `true` 这个信号是实时信号
  108. /// - `false` 这个信号不是实时信号
  109. #[inline]
  110. pub fn is_rt_signal(&self) -> bool {
  111. return (*self) as usize >= Signal::SIGRTMIN.into();
  112. }
  113. /// 调用信号的默认处理函数
  114. pub fn handle_default(&self) {
  115. match self {
  116. Signal::INVALID => {
  117. kerror!("attempting to handler an Invalid");
  118. }
  119. Signal::SIGHUP => sig_terminate(self.clone()),
  120. Signal::SIGINT => sig_terminate(self.clone()),
  121. Signal::SIGQUIT => sig_terminate_dump(self.clone()),
  122. Signal::SIGILL => sig_terminate_dump(self.clone()),
  123. Signal::SIGTRAP => sig_terminate_dump(self.clone()),
  124. Signal::SIGABRT_OR_IOT => sig_terminate_dump(self.clone()),
  125. Signal::SIGBUS => sig_terminate_dump(self.clone()),
  126. Signal::SIGFPE => sig_terminate_dump(self.clone()),
  127. Signal::SIGKILL => sig_terminate(self.clone()),
  128. Signal::SIGUSR1 => sig_terminate(self.clone()),
  129. Signal::SIGSEGV => sig_terminate_dump(self.clone()),
  130. Signal::SIGUSR2 => sig_terminate(self.clone()),
  131. Signal::SIGPIPE => sig_terminate(self.clone()),
  132. Signal::SIGALRM => sig_terminate(self.clone()),
  133. Signal::SIGTERM => sig_terminate(self.clone()),
  134. Signal::SIGSTKFLT => sig_terminate(self.clone()),
  135. Signal::SIGCHLD => sig_ignore(self.clone()),
  136. Signal::SIGCONT => sig_continue(self.clone()),
  137. Signal::SIGSTOP => sig_stop(self.clone()),
  138. Signal::SIGTSTP => sig_stop(self.clone()),
  139. Signal::SIGTTIN => sig_stop(self.clone()),
  140. Signal::SIGTTOU => sig_stop(self.clone()),
  141. Signal::SIGURG => sig_ignore(self.clone()),
  142. Signal::SIGXCPU => sig_terminate_dump(self.clone()),
  143. Signal::SIGXFSZ => sig_terminate_dump(self.clone()),
  144. Signal::SIGVTALRM => sig_terminate(self.clone()),
  145. Signal::SIGPROF => sig_terminate(self.clone()),
  146. Signal::SIGWINCH => sig_ignore(self.clone()),
  147. Signal::SIGIO_OR_POLL => sig_terminate(self.clone()),
  148. Signal::SIGPWR => sig_terminate(self.clone()),
  149. Signal::SIGSYS => sig_terminate(self.clone()),
  150. Signal::SIGRTMIN => sig_terminate(self.clone()),
  151. Signal::SIGRTMAX => sig_terminate(self.clone()),
  152. }
  153. }
  154. }
  155. /// siginfo中的si_code的可选值
  156. /// 请注意,当这个值小于0时,表示siginfo来自用户态,否则来自内核态
  157. #[derive(Copy, Debug, Clone)]
  158. #[repr(i32)]
  159. pub enum SigCode {
  160. /// sent by kill, sigsend, raise
  161. User = 0,
  162. /// sent by kernel from somewhere
  163. Kernel = 0x80,
  164. /// 通过sigqueue发送
  165. Queue = -1,
  166. /// 定时器过期时发送
  167. Timer = -2,
  168. /// 当实时消息队列的状态发生改变时发送
  169. Mesgq = -3,
  170. /// 当异步IO完成时发送
  171. AsyncIO = -4,
  172. /// sent by queued SIGIO
  173. SigIO = -5,
  174. }
  175. impl SigCode {
  176. /// 为SigCode这个枚举类型实现从i32转换到枚举类型的转换函数
  177. #[allow(dead_code)]
  178. pub fn from_i32(x: i32) -> SigCode {
  179. match x {
  180. 0 => Self::User,
  181. 0x80 => Self::Kernel,
  182. -1 => Self::Queue,
  183. -2 => Self::Timer,
  184. -3 => Self::Mesgq,
  185. -4 => Self::AsyncIO,
  186. -5 => Self::SigIO,
  187. _ => panic!("signal code not valid"),
  188. }
  189. }
  190. }
  191. bitflags! {
  192. #[repr(C,align(8))]
  193. #[derive(Default)]
  194. pub struct SigFlags:u32{
  195. const SA_NOCLDSTOP = 1;
  196. const SA_NOCLDWAIT = 2;
  197. const SA_SIGINFO = 4;
  198. const SA_ONSTACK = 0x08000000;
  199. const SA_RESTART = 0x10000000;
  200. const SA_NODEFER = 0x40000000;
  201. const SA_RESETHAND = 0x80000000;
  202. const SA_RESTORER =0x04000000;
  203. const SA_ALL = Self::SA_NOCLDSTOP.bits()|Self::SA_NOCLDWAIT.bits()|Self::SA_NODEFER.bits()|Self::SA_ONSTACK.bits()|Self::SA_RESETHAND.bits()|Self::SA_RESTART.bits()|Self::SA_SIGINFO.bits()|Self::SA_RESTORER.bits();
  204. }
  205. /// 请注意,sigset 这个bitmap, 第0位表示sig=1的信号。也就是说,Signal-1才是sigset_t中对应的位
  206. #[derive(Default)]
  207. pub struct SigSet:u64{
  208. const SIGHUP = 1<<0;
  209. const SIGINT = 1<<1;
  210. const SIGQUIT = 1<<2;
  211. const SIGILL = 1<<3;
  212. const SIGTRAP = 1<<4;
  213. /// SIGABRT和SIGIOT共用这个号码
  214. const SIGABRT_OR_IOT = 1<<5;
  215. const SIGBUS = 1<<6;
  216. const SIGFPE = 1<<7;
  217. const SIGKILL = 1<<8;
  218. const SIGUSR = 1<<9;
  219. const SIGSEGV = 1<<10;
  220. const SIGUSR2 = 1<<11;
  221. const SIGPIPE = 1<<12;
  222. const SIGALRM = 1<<13;
  223. const SIGTERM = 1<<14;
  224. const SIGSTKFLT= 1<<15;
  225. const SIGCHLD = 1<<16;
  226. const SIGCONT = 1<<17;
  227. const SIGSTOP = 1<<18;
  228. const SIGTSTP = 1<<19;
  229. const SIGTTIN = 1<<20;
  230. const SIGTTOU = 1<<21;
  231. const SIGURG = 1<<22;
  232. const SIGXCPU = 1<<23;
  233. const SIGXFSZ = 1<<24;
  234. const SIGVTALRM= 1<<25;
  235. const SIGPROF = 1<<26;
  236. const SIGWINCH = 1<<27;
  237. /// SIGIO和SIGPOLL共用这个号码
  238. const SIGIO_OR_POLL = 1<<28;
  239. const SIGPWR = 1<<29;
  240. const SIGSYS = 1<<30;
  241. const SIGRTMIN = 1<<31;
  242. // TODO 写上实时信号
  243. const SIGRTMAX = 1<<MAX_SIG_NUM-1;
  244. }
  245. }
  246. /// SIGCHLD si_codes
  247. #[derive(Debug, Clone, Copy, PartialEq, Eq, ToPrimitive)]
  248. #[allow(dead_code)]
  249. pub enum SigChildCode {
  250. /// child has exited
  251. ///
  252. /// CLD_EXITED
  253. Exited = 1,
  254. /// child was killed
  255. ///
  256. /// CLD_KILLED
  257. Killed = 2,
  258. /// child terminated abnormally
  259. ///
  260. /// CLD_DUMPED
  261. Dumped = 3,
  262. /// traced child has trapped
  263. ///
  264. /// CLD_TRAPPED
  265. Trapped = 4,
  266. /// child has stopped
  267. ///
  268. /// CLD_STOPPED
  269. Stopped = 5,
  270. /// stopped child has continued
  271. ///
  272. /// CLD_CONTINUED
  273. Continued = 6,
  274. }
  275. impl Into<i32> for SigChildCode {
  276. fn into(self) -> i32 {
  277. self as i32
  278. }
  279. }
  280. /// 信号默认处理函数——终止进程
  281. fn sig_terminate(sig: Signal) {
  282. ProcessManager::exit(sig as usize);
  283. }
  284. /// 信号默认处理函数——终止进程并生成 core dump
  285. fn sig_terminate_dump(sig: Signal) {
  286. ProcessManager::exit(sig as usize);
  287. // TODO 生成 coredump 文件
  288. }
  289. /// 信号默认处理函数——暂停进程
  290. fn sig_stop(sig: Signal) {
  291. let guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
  292. ProcessManager::mark_stop().unwrap_or_else(|e| {
  293. kerror!(
  294. "sleep error :{:?},failed to sleep process :{:?}, with signal :{:?}",
  295. e,
  296. ProcessManager::current_pcb(),
  297. sig
  298. );
  299. });
  300. drop(guard);
  301. sched();
  302. // TODO 暂停进程
  303. }
  304. /// 信号默认处理函数——继续进程
  305. fn sig_continue(sig: Signal) {
  306. ProcessManager::wakeup_stop(&ProcessManager::current_pcb()).unwrap_or_else(|_| {
  307. kerror!(
  308. "Failed to wake up process pid = {:?} with signal :{:?}",
  309. ProcessManager::current_pcb().pid(),
  310. sig
  311. );
  312. });
  313. }
  314. /// 信号默认处理函数——忽略
  315. fn sig_ignore(_sig: Signal) {
  316. return;
  317. }