mod.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //! signal implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html
  2. use core::{mem, ptr};
  3. use header::errno;
  4. use platform;
  5. use platform::types::*;
  6. use platform::{PalSignal, Sys};
  7. pub use self::sys::*;
  8. #[cfg(target_os = "linux")]
  9. #[path = "linux.rs"]
  10. pub mod sys;
  11. #[cfg(target_os = "redox")]
  12. #[path = "redox.rs"]
  13. pub mod sys;
  14. const SIG_ERR: usize = !0;
  15. pub const SIG_BLOCK: c_int = 0;
  16. pub const SIG_UNBLOCK: c_int = 1;
  17. pub const SIG_SETMASK: c_int = 2;
  18. // Need both here and in platform because cbindgen :(
  19. #[repr(C)]
  20. #[derive(Clone)]
  21. pub struct sigaction {
  22. // I don't actually want these to be optional. They can have more than just
  23. // one invalid value. But because of rust's non-null optimization, this
  24. // causes Some(sigaction) with a null sa_handler to become None. Maybe
  25. // these should be usizes and transmuted when needed... However, then I
  26. // couldn't let cbindgen do its job.
  27. pub sa_handler: Option<extern "C" fn(c_int)>,
  28. pub sa_flags: c_ulong,
  29. pub sa_restorer: Option<unsafe extern "C" fn()>,
  30. pub sa_mask: sigset_t,
  31. }
  32. pub const NSIG: usize = 64;
  33. pub type sigset_t = c_ulong;
  34. #[no_mangle]
  35. pub extern "C" fn kill(pid: pid_t, sig: c_int) -> c_int {
  36. Sys::kill(pid, sig)
  37. }
  38. #[no_mangle]
  39. pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
  40. Sys::killpg(pgrp, sig)
  41. }
  42. #[no_mangle]
  43. pub extern "C" fn raise(sig: c_int) -> c_int {
  44. Sys::raise(sig)
  45. }
  46. #[no_mangle]
  47. pub unsafe extern "C" fn sigaction(
  48. sig: c_int,
  49. act: *const sigaction,
  50. oact: *mut sigaction,
  51. ) -> c_int {
  52. let mut _sigaction = None;
  53. let ptr = if !act.is_null() {
  54. _sigaction = Some((*act).clone());
  55. _sigaction.as_mut().unwrap().sa_flags |= SA_RESTORER as c_ulong;
  56. _sigaction.as_mut().unwrap() as *mut _ as *mut platform::types::sigaction
  57. } else {
  58. ptr::null_mut()
  59. };
  60. Sys::sigaction(sig, ptr, oact as *mut platform::types::sigaction)
  61. }
  62. #[no_mangle]
  63. pub extern "C" fn sigaddset(set: *mut sigset_t, signo: c_int) -> c_int {
  64. if signo <= 0 || signo as usize > NSIG {
  65. unsafe {
  66. platform::errno = errno::EINVAL;
  67. }
  68. return -1;
  69. }
  70. let signo = signo as usize - 1; // 0-indexed usize, please!
  71. unsafe {
  72. *set |= 1 << (signo & (8 * mem::size_of::<sigset_t>() - 1));
  73. }
  74. 0
  75. }
  76. #[no_mangle]
  77. pub extern "C" fn sigdelset(set: *mut sigset_t, signo: c_int) -> c_int {
  78. if signo <= 0 || signo as usize > NSIG {
  79. unsafe {
  80. platform::errno = errno::EINVAL;
  81. }
  82. return -1;
  83. }
  84. let signo = signo as usize - 1; // 0-indexed usize, please!
  85. unsafe {
  86. *set &= !(1 << (signo & (8 * mem::size_of::<sigset_t>() - 1)));
  87. }
  88. 0
  89. }
  90. #[no_mangle]
  91. pub extern "C" fn sigemptyset(set: *mut sigset_t) -> c_int {
  92. unsafe {
  93. *set = 0;
  94. }
  95. 0
  96. }
  97. #[no_mangle]
  98. pub extern "C" fn sigfillset(set: *mut sigset_t) -> c_int {
  99. unsafe {
  100. *set = c_ulong::max_value();
  101. }
  102. 0
  103. }
  104. // #[no_mangle]
  105. pub extern "C" fn sighold(sig: c_int) -> c_int {
  106. unimplemented!();
  107. }
  108. // #[no_mangle]
  109. pub extern "C" fn sigignore(sig: c_int) -> c_int {
  110. unimplemented!();
  111. }
  112. // #[no_mangle]
  113. pub extern "C" fn siginterrupt(sig: c_int, flag: c_int) -> c_int {
  114. unimplemented!();
  115. }
  116. // #[no_mangle]
  117. pub extern "C" fn sigismember(set: *const sigset_t, signo: c_int) -> c_int {
  118. unimplemented!();
  119. }
  120. extern "C" {
  121. // Defined in assembly inside platform/x/mod.rs
  122. fn __restore_rt();
  123. }
  124. #[no_mangle]
  125. pub extern "C" fn signal(
  126. sig: c_int,
  127. func: Option<extern "C" fn(c_int)>,
  128. ) -> Option<extern "C" fn(c_int)> {
  129. let sa = sigaction {
  130. sa_handler: func,
  131. sa_flags: SA_RESTART as c_ulong,
  132. sa_restorer: Some(__restore_rt),
  133. sa_mask: sigset_t::default(),
  134. };
  135. let mut old_sa = unsafe { mem::uninitialized() };
  136. if unsafe { sigaction(sig, &sa, &mut old_sa) } < 0 {
  137. mem::forget(old_sa);
  138. return unsafe { mem::transmute(SIG_ERR) };
  139. }
  140. old_sa.sa_handler
  141. }
  142. // #[no_mangle]
  143. pub extern "C" fn sigpause(sig: c_int) -> c_int {
  144. unimplemented!();
  145. }
  146. // #[no_mangle]
  147. pub extern "C" fn sigpending(set: *mut sigset_t) -> c_int {
  148. unimplemented!();
  149. }
  150. #[no_mangle]
  151. pub extern "C" fn sigprocmask(how: c_int, set: *const sigset_t, oset: *mut sigset_t) -> c_int {
  152. Sys::sigprocmask(how, set, oset)
  153. }
  154. // #[no_mangle]
  155. pub extern "C" fn sigrelse(sig: c_int) -> c_int {
  156. unimplemented!();
  157. }
  158. // #[no_mangle]
  159. pub extern "C" fn sigset(sig: c_int, func: fn(c_int)) -> fn(c_int) {
  160. unimplemented!();
  161. }
  162. // #[no_mangle]
  163. pub extern "C" fn sigsuspend(sigmask: *const sigset_t) -> c_int {
  164. unimplemented!();
  165. }
  166. // #[no_mangle]
  167. pub extern "C" fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int {
  168. unimplemented!();
  169. }
  170. pub const _signal_strings: [&'static str; 32] = [
  171. "Unknown signal\0",
  172. "Hangup\0",
  173. "Interrupt\0",
  174. "Quit\0",
  175. "Illegal instruction\0",
  176. "Trace/breakpoint trap\0",
  177. "Aborted\0",
  178. "Bus error\0",
  179. "Arithmetic exception\0",
  180. "Killed\0",
  181. "User defined signal 1\0",
  182. "Segmentation fault\0",
  183. "User defined signal 2\0",
  184. "Broken pipe\0",
  185. "Alarm clock\0",
  186. "Terminated\0",
  187. "Stack fault\0",
  188. "Child process status\0",
  189. "Continued\0",
  190. "Stopped (signal)\0",
  191. "Stopped\0",
  192. "Stopped (tty input)\0",
  193. "Stopped (tty output)\0",
  194. "Urgent I/O condition\0",
  195. "CPU time limit exceeded\0",
  196. "File size limit exceeded\0",
  197. "Virtual timer expired\0",
  198. "Profiling timer expired\0",
  199. "Window changed\0",
  200. "I/O possible\0",
  201. "Power failure\0",
  202. "Bad system call\0",
  203. ];