(argc: usize, argv: &[&str])
| 8 | |
| 9 | #[no_mangle] |
| 10 | pub fn main(argc: usize, argv: &[&str]) -> i32 { |
| 11 | assert_eq!(argc, 3, "argc must be 3!"); |
| 12 | let timeout_ms = argv[2] |
| 13 | .parse::<isize>() |
| 14 | .expect("Error when parsing timeout!"); |
| 15 | let pid = fork() as usize; |
| 16 | if pid == 0 { |
| 17 | if exec(argv[1], &[core::ptr::null::<u8>()]) != 0 { |
| 18 | println!("Error when executing '{}'", argv[1]); |
| 19 | return -4; |
| 20 | } |
| 21 | } else { |
| 22 | let start_time = get_time(); |
| 23 | let mut child_exited = false; |
| 24 | let mut exit_code: i32 = 0; |
| 25 | loop { |
| 26 | if get_time() - start_time > timeout_ms { |
| 27 | break; |
| 28 | } |
| 29 | if waitpid_nb(pid, &mut exit_code) as usize == pid { |
| 30 | child_exited = true; |
| 31 | println!( |
| 32 | "child exited in {}ms, exit_code = {}", |
| 33 | get_time() - start_time, |
| 34 | exit_code, |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | if !child_exited { |
| 39 | println!("child has run for {}ms, kill it!", timeout_ms); |
| 40 | kill(pid, SignalFlags::SIGINT.bits()); |
| 41 | assert_eq!(waitpid(pid, &mut exit_code) as usize, pid); |
| 42 | println!("exit code of the child is {}", exit_code); |
| 43 | } |
| 44 | } |
| 45 | 0 |
| 46 | } |
nothing calls this directly
no test coverage detected