diff options
| -rw-r--r-- | gnu/build/linux-container.scm | 22 | ||||
| -rw-r--r-- | tests/containers.scm | 27 |
2 files changed, 48 insertions, 1 deletions
diff --git a/gnu/build/linux-container.scm b/gnu/build/linux-container.scm index dd56a792320..95bfd92ddef 100644 --- a/gnu/build/linux-container.scm +++ b/gnu/build/linux-container.scm | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | 2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> |
| 3 | ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org> | ||
| 3 | ;;; | 4 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 6 | ;;; |
| @@ -32,7 +33,8 @@ | |||
| 32 | %namespaces | 33 | %namespaces |
| 33 | run-container | 34 | run-container |
| 34 | call-with-container | 35 | call-with-container |
| 35 | container-excursion)) | 36 | container-excursion |
| 37 | container-excursion*)) | ||
| 36 | 38 | ||
| 37 | (define (user-namespace-supported?) | 39 | (define (user-namespace-supported?) |
| 38 | "Return #t if user namespaces are supported on this system." | 40 | "Return #t if user namespaces are supported on this system." |
| @@ -326,3 +328,21 @@ return the exit status." | |||
| 326 | (match (waitpid pid) | 328 | (match (waitpid pid) |
| 327 | ((_ . status) | 329 | ((_ . status) |
| 328 | (status:exit-val status)))))) | 330 | (status:exit-val status)))))) |
| 331 | |||
| 332 | (define (container-excursion* pid thunk) | ||
| 333 | "Like 'container-excursion', but return the return value of THUNK." | ||
| 334 | (match (pipe) | ||
| 335 | ((in . out) | ||
| 336 | (match (container-excursion pid | ||
| 337 | (lambda () | ||
| 338 | (close-port in) | ||
| 339 | (write (thunk) out))) | ||
| 340 | (0 | ||
| 341 | (close-port out) | ||
| 342 | (let ((result (read in))) | ||
| 343 | (close-port in) | ||
| 344 | result)) | ||
| 345 | (_ ;maybe PID died already | ||
| 346 | (close-port out) | ||
| 347 | (close-port in) | ||
| 348 | #f))))) | ||
diff --git a/tests/containers.scm b/tests/containers.scm index 745b56b710d..0b3a4be12b9 100644 --- a/tests/containers.scm +++ b/tests/containers.scm | |||
| @@ -180,4 +180,31 @@ | |||
| 180 | (lambda () | 180 | (lambda () |
| 181 | (primitive-exit 42)))) | 181 | (primitive-exit 42)))) |
| 182 | 182 | ||
| 183 | (skip-if-unsupported) | ||
| 184 | (test-assert "container-excursion*" | ||
| 185 | (call-with-temporary-directory | ||
| 186 | (lambda (root) | ||
| 187 | (define (namespaces pid) | ||
| 188 | (let ((pid (number->string pid))) | ||
| 189 | (map (lambda (ns) | ||
| 190 | (readlink (string-append "/proc/" pid "/ns/" ns))) | ||
| 191 | '("user" "ipc" "uts" "net" "pid" "mnt")))) | ||
| 192 | |||
| 193 | (let* ((pid (run-container root '() | ||
| 194 | %namespaces 1 | ||
| 195 | (lambda () | ||
| 196 | (sleep 100)))) | ||
| 197 | (result (container-excursion* pid | ||
| 198 | (lambda () | ||
| 199 | (namespaces 1))))) | ||
| 200 | (kill pid SIGKILL) | ||
| 201 | (equal? result (namespaces pid)))))) | ||
| 202 | |||
| 203 | (skip-if-unsupported) | ||
| 204 | (test-equal "container-excursion*, same namespaces" | ||
| 205 | 42 | ||
| 206 | (container-excursion* (getpid) | ||
| 207 | (lambda () | ||
| 208 | (* 6 7)))) | ||
| 209 | |||
| 183 | (test-end) | 210 | (test-end) |
