diff options
Diffstat (limited to '')
| -rw-r--r-- | src/tools/shell.rs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/tools/shell.rs b/src/tools/shell.rs index 91f2dd9..c38f032 100644 --- a/src/tools/shell.rs +++ b/src/tools/shell.rs | |||
| @@ -18,6 +18,20 @@ const MAX_FILE_BYTES: u64 = 64 * 1024 * 1024; | |||
| 18 | 18 | ||
| 19 | static COUNTER: AtomicU64 = AtomicU64::new(0); | 19 | static COUNTER: AtomicU64 = AtomicU64::new(0); |
| 20 | 20 | ||
| 21 | /// Removes both temp files when dropped, so an early `?` on the spawn/read paths | ||
| 22 | /// cannot leak `agent686-…-{out,err}.log` files into $TMPDIR. | ||
| 23 | struct TempGuard { | ||
| 24 | out: PathBuf, | ||
| 25 | err: PathBuf, | ||
| 26 | } | ||
| 27 | |||
| 28 | impl Drop for TempGuard { | ||
| 29 | fn drop(&mut self) { | ||
| 30 | let _ = std::fs::remove_file(&self.out); | ||
| 31 | let _ = std::fs::remove_file(&self.err); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 21 | pub fn bash( | 35 | pub fn bash( |
| 22 | args: &Value, | 36 | args: &Value, |
| 23 | timeout: Duration, | 37 | timeout: Duration, |
| @@ -44,6 +58,10 @@ pub fn bash( | |||
| 44 | let serial = COUNTER.fetch_add(1, Ordering::Relaxed); | 58 | let serial = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 45 | let out_path = temp_path("out", serial); | 59 | let out_path = temp_path("out", serial); |
| 46 | let err_path = temp_path("err", serial); | 60 | let err_path = temp_path("err", serial); |
| 61 | let _guard = TempGuard { | ||
| 62 | out: out_path.clone(), | ||
| 63 | err: err_path.clone(), | ||
| 64 | }; | ||
| 47 | let mut out_file = create(&out_path)?; | 65 | let mut out_file = create(&out_path)?; |
| 48 | let mut err_file = create(&err_path)?; | 66 | let mut err_file = create(&err_path)?; |
| 49 | let stdout = out_file.try_clone().map_err(|e| e.to_string())?; | 67 | let stdout = out_file.try_clone().map_err(|e| e.to_string())?; |
| @@ -104,15 +122,17 @@ pub fn bash( | |||
| 104 | } | 122 | } |
| 105 | }; | 123 | }; |
| 106 | 124 | ||
| 107 | // A child that blew the file-size limit was killed by SIGXFSZ. | 125 | // SIGXFSZ lands on whichever process crossed the limit — usually a |
| 108 | let over_limit = status.signal() == Some(libc::SIGXFSZ); | 126 | // grandchild of `sh` — so `sh` exits 128+25 rather than being signalled. |
| 127 | // The reliable signal is the file itself having reached the limit. | ||
| 128 | let over_limit = status.signal() == Some(libc::SIGXFSZ) | ||
| 129 | || out_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES | ||
| 130 | || err_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES; | ||
| 109 | 131 | ||
| 110 | let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; | 132 | let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; |
| 111 | let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; | 133 | let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; |
| 112 | drop(out_file); | 134 | drop(out_file); |
| 113 | drop(err_file); | 135 | drop(err_file); |
| 114 | let _ = std::fs::remove_file(&out_path); | ||
| 115 | let _ = std::fs::remove_file(&err_path); | ||
| 116 | 136 | ||
| 117 | let marker = if over_limit { | 137 | let marker = if over_limit { |
| 118 | format!( | 138 | format!( |
