From 168aba297866295d96779239e9662821ce9e66ae Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 4 Feb 2017 18:10:14 +0100 Subject: linux-container: Do not rely on 'isatty?'. This avoids problems where 'isatty?' return #t but 'ttyname' fails with ENOTTY or such. * gnu/build/linux-container.scm (mount-file-systems): Remove call of 'isatty?'. Directly call 'ttyname' and catch 'system-error'. --- gnu/build/linux-container.scm | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'gnu/build/linux-container.scm') diff --git a/gnu/build/linux-container.scm b/gnu/build/linux-container.scm index b71d6a5f88..cd71239527 100644 --- a/gnu/build/linux-container.scm +++ b/gnu/build/linux-container.scm @@ -128,13 +128,19 @@ for the process." "/dev/fuse")) ;; Setup the container's /dev/console by bind mounting the pseudo-terminal - ;; associated with standard input. - (let ((in (current-input-port)) - (console (scope "/dev/console"))) - (when (isatty? in) + ;; associated with standard input when there is one. + (let* ((in (current-input-port)) + (tty (catch 'system-error + (lambda () + ;; This call throws if IN does not correspond to a tty. + ;; This is more reliable than 'isatty?'. + (ttyname in)) + (const #f))) + (console (scope "/dev/console"))) + (when tty (touch console) (chmod console #o600) - (bind-mount (ttyname in) console))) + (bind-mount tty console))) ;; Setup standard input/output/error. (symlink "/proc/self/fd" (scope "/dev/fd")) -- cgit 1.4.1 From 36c4917c910f434524aae32725582d5bc51a44e0 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 4 Feb 2017 18:14:12 +0100 Subject: linux-container: Add comment on exception handling. * gnu/build/linux-container.scm (run-container): Add note about writing the exceptions. --- gnu/build/linux-container.scm | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gnu/build/linux-container.scm') diff --git a/gnu/build/linux-container.scm b/gnu/build/linux-container.scm index cd71239527..dd56a79232 100644 --- a/gnu/build/linux-container.scm +++ b/gnu/build/linux-container.scm @@ -235,6 +235,8 @@ host user identifiers to map into the user namespace." namespaces))) (lambda args ;; Forward the exception to the parent process. + ;; FIXME: SRFI-35 conditions and non-trivial objects + ;; cannot be 'read' so they shouldn't be written as is. (write args child) (primitive-exit 3)))) ;; TODO: Manage capabilities. -- cgit 1.4.1 From c90db25f4cf1f98f3f4f3af38d175a14ffb8c32a Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Mon, 6 Feb 2017 23:45:00 +0100 Subject: linux-container: Add 'container-excursion*'. * gnu/build/linux-container.scm (container-excursion*): New procedure. * tests/containers.scm ("container-excursion*") ("container-excursion*, same namespaces"): New tests. --- gnu/build/linux-container.scm | 22 +++++++++++++++++++++- tests/containers.scm | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) (limited to 'gnu/build/linux-container.scm') diff --git a/gnu/build/linux-container.scm b/gnu/build/linux-container.scm index dd56a79232..95bfd92dde 100644 --- a/gnu/build/linux-container.scm +++ b/gnu/build/linux-container.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson +;;; Copyright © 2017 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -32,7 +33,8 @@ %namespaces run-container call-with-container - container-excursion)) + container-excursion + container-excursion*)) (define (user-namespace-supported?) "Return #t if user namespaces are supported on this system." @@ -326,3 +328,21 @@ return the exit status." (match (waitpid pid) ((_ . status) (status:exit-val status)))))) + +(define (container-excursion* pid thunk) + "Like 'container-excursion', but return the return value of THUNK." + (match (pipe) + ((in . out) + (match (container-excursion pid + (lambda () + (close-port in) + (write (thunk) out))) + (0 + (close-port out) + (let ((result (read in))) + (close-port in) + result)) + (_ ;maybe PID died already + (close-port out) + (close-port in) + #f))))) diff --git a/tests/containers.scm b/tests/containers.scm index 745b56b710..0b3a4be12b 100644 --- a/tests/containers.scm +++ b/tests/containers.scm @@ -180,4 +180,31 @@ (lambda () (primitive-exit 42)))) +(skip-if-unsupported) +(test-assert "container-excursion*" + (call-with-temporary-directory + (lambda (root) + (define (namespaces pid) + (let ((pid (number->string pid))) + (map (lambda (ns) + (readlink (string-append "/proc/" pid "/ns/" ns))) + '("user" "ipc" "uts" "net" "pid" "mnt")))) + + (let* ((pid (run-container root '() + %namespaces 1 + (lambda () + (sleep 100)))) + (result (container-excursion* pid + (lambda () + (namespaces 1))))) + (kill pid SIGKILL) + (equal? result (namespaces pid)))))) + +(skip-if-unsupported) +(test-equal "container-excursion*, same namespaces" + 42 + (container-excursion* (getpid) + (lambda () + (* 6 7)))) + (test-end) -- cgit 1.4.1