diff options
| author | Christopher Baines <mail@cbaines.net> | 2022-06-25 18:14:07 +0100 |
|---|---|---|
| committer | Christopher Baines <mail@cbaines.net> | 2022-07-08 13:51:34 +0100 |
| commit | b4c4a6acb1204ee53e95744236ee89985db32f91 (patch) | |
| tree | 55ac03203aea7ccc904e9dd706c7ff22fb19e63c | |
| parent | 37dd7e53b9bf635b62b36cd6b028497048481288 (diff) | |
guix: inferior: Fix the behaviour of open-inferior #:error-port.
I'm looking at this as the Guix Data Service uses this behaviour to record and
display logs from inferior processes.
* guix/inferior.scm (open-bidirectional-pipe): Call dup2 for file descriptor
2, passing either the file number for the current error port, or a file
descriptor for /dev/null.
* tests/inferior.scm ("#:error-port stderr", "#:error-port pipe"): Add two new
tests that cover some of the #:error-port behaviour.
| -rw-r--r-- | guix/inferior.scm | 12 | ||||
| -rw-r--r-- | tests/inferior.scm | 39 |
2 files changed, 47 insertions, 4 deletions
diff --git a/guix/inferior.scm b/guix/inferior.scm index 54200b75e41..20a86bbfda7 100644 --- a/guix/inferior.scm +++ b/guix/inferior.scm | |||
| @@ -156,12 +156,18 @@ custom binary port)." | |||
| 156 | (close-port parent) | 156 | (close-port parent) |
| 157 | (close-fdes 0) | 157 | (close-fdes 0) |
| 158 | (close-fdes 1) | 158 | (close-fdes 1) |
| 159 | (close-fdes 2) | ||
| 159 | (dup2 (fileno child) 0) | 160 | (dup2 (fileno child) 0) |
| 160 | (dup2 (fileno child) 1) | 161 | (dup2 (fileno child) 1) |
| 161 | ;; Mimic 'open-pipe*'. | 162 | ;; Mimic 'open-pipe*'. |
| 162 | (unless (file-port? (current-error-port)) | 163 | (if (file-port? (current-error-port)) |
| 163 | (close-fdes 2) | 164 | (let ((error-port-fileno |
| 164 | (dup2 (open-fdes "/dev/null" O_WRONLY) 2)) | 165 | (fileno (current-error-port)))) |
| 166 | (unless (eq? error-port-fileno 2) | ||
| 167 | (dup2 error-port-fileno | ||
| 168 | 2))) | ||
| 169 | (dup2 (open-fdes "/dev/null" O_WRONLY) | ||
| 170 | 2)) | ||
| 165 | (apply execlp command command args)) | 171 | (apply execlp command command args)) |
| 166 | (lambda () | 172 | (lambda () |
| 167 | (primitive-_exit 127)))) | 173 | (primitive-_exit 127)))) |
diff --git a/tests/inferior.scm b/tests/inferior.scm index 56b2fcb7bce..963d405e33f 100644 --- a/tests/inferior.scm +++ b/tests/inferior.scm | |||
| @@ -30,7 +30,8 @@ | |||
| 30 | #:use-module (srfi srfi-1) | 30 | #:use-module (srfi srfi-1) |
| 31 | #:use-module (srfi srfi-34) | 31 | #:use-module (srfi srfi-34) |
| 32 | #:use-module (srfi srfi-64) | 32 | #:use-module (srfi srfi-64) |
| 33 | #:use-module (ice-9 match)) | 33 | #:use-module (ice-9 match) |
| 34 | #:use-module (ice-9 rdelim)) | ||
| 34 | 35 | ||
| 35 | (define %top-srcdir | 36 | (define %top-srcdir |
| 36 | (dirname (search-path %load-path "guix.scm"))) | 37 | (dirname (search-path %load-path "guix.scm"))) |
| @@ -315,4 +316,40 @@ | |||
| 315 | (close-inferior inferior) | 316 | (close-inferior inferior) |
| 316 | (map manifest-entry->list (manifest-entries manifest)))) | 317 | (map manifest-entry->list (manifest-entries manifest)))) |
| 317 | 318 | ||
| 319 | (test-equal "#:error-port stderr" | ||
| 320 | 42 | ||
| 321 | ;; There's a special case in open-bidirectional-pipe for | ||
| 322 | ;; (current-error-port) being stderr, so this test just checks that | ||
| 323 | ;; open-inferior doesn't raise an exception | ||
| 324 | (let ((inferior (open-inferior %top-builddir | ||
| 325 | #:command "scripts/guix" | ||
| 326 | #:error-port (current-error-port)))) | ||
| 327 | (and (inferior? inferior) | ||
| 328 | (inferior-eval '(display "test" (current-error-port)) inferior) | ||
| 329 | (let ((result (inferior-eval '(apply * '(6 7)) inferior))) | ||
| 330 | (close-inferior inferior) | ||
| 331 | result)))) | ||
| 332 | |||
| 333 | (test-equal "#:error-port pipe" | ||
| 334 | "42" | ||
| 335 | (match (pipe) | ||
| 336 | ((port-to-read-from . port-to-write-to) | ||
| 337 | |||
| 338 | (setvbuf port-to-read-from 'line) | ||
| 339 | (setvbuf port-to-write-to 'line) | ||
| 340 | |||
| 341 | (let ((inferior (open-inferior %top-builddir | ||
| 342 | #:command "scripts/guix" | ||
| 343 | #:error-port port-to-write-to))) | ||
| 344 | (and (inferior? inferior) | ||
| 345 | (begin | ||
| 346 | (inferior-eval '(display "42\n" (current-error-port)) inferior) | ||
| 347 | |||
| 348 | (let loop ((line (read-line port-to-read-from))) | ||
| 349 | (if (string=? line "42") | ||
| 350 | (begin | ||
| 351 | (close-inferior inferior) | ||
| 352 | line) | ||
| 353 | (loop (read-line port-to-read-from)))))))))) | ||
| 354 | |||
| 318 | (test-end "inferior") | 355 | (test-end "inferior") |
