| 24 | #[external_methods(init_std_fds, unwrap, as_raw_fd, create, to_owned, clone)] |
| 25 | #[external_calls(open, forget, get_homedir_fd, from)] |
| 26 | pub fn fresh_ctx(homedir: String) -> VmCtx { |
| 27 | let memlen = LINEAR_MEM_SIZE; |
| 28 | let mem = vec![0; memlen]; |
| 29 | let mut fdmap = FdMap::new(); |
| 30 | fdmap.init_std_fds(); |
| 31 | let homedir_host_fd = get_homedir_fd(&homedir) as usize; |
| 32 | // let homedir_file = std::fs::File::open(&homedir).unwrap(); |
| 33 | // let homedir_fd = homedir_file.as_raw_fd(); |
| 34 | if homedir_host_fd >= 0 { |
| 35 | fdmap.create(HostFd::from_raw(homedir_host_fd)); |
| 36 | } |
| 37 | // Need to forget file to make sure it does not get auto-closed |
| 38 | // when it gets out of scope |
| 39 | // std::mem::forget(homedir_file); |
| 40 | // let log_path = "".to_owned(); |
| 41 | // let log_path = String::new(); |
| 42 | |
| 43 | let arg_buffer = Vec::new(); |
| 44 | let argc = 0; |
| 45 | let env_buffer = Vec::new(); |
| 46 | let envc = 0; |
| 47 | |
| 48 | let netlist = empty_netlist(); |
| 49 | VmCtx { |
| 50 | mem, |
| 51 | memlen, |
| 52 | fdmap, |
| 53 | homedir, |
| 54 | homedir_host_fd: HostFd::from_raw(homedir_host_fd), |
| 55 | // errno: Success, |
| 56 | arg_buffer, |
| 57 | argc, |
| 58 | env_buffer, |
| 59 | envc, |
| 60 | // log_path, |
| 61 | netlist, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl VmCtx { |
| 66 | /// Check whether sandbox pointer is actually inside the sandbox |