[bug#70494,13/23] syscalls: Add unshare.

Message ID c75be97b19a39c26af414d2c1278e9cdb92048ef.1713692561.git.mail@cbaines.net
State New
Headers
Series Groundwork for the Guile guix-daemon |

Commit Message

Christopher Baines April 21, 2024, 9:42 a.m. UTC
* guix/build/syscalls.scm (unshare): New procedure.

Change-Id: I7caad207117b17b349290e680277f650c51d2f3b
---
 guix/build/syscalls.scm | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Comments

Ludovic Courtès May 16, 2024, 4:14 p.m. UTC | #1
Christopher Baines <mail@cbaines.net> skribis:

> * guix/build/syscalls.scm (unshare): New procedure.
>
> Change-Id: I7caad207117b17b349290e680277f650c51d2f3b

[...]

> +(define unshare
> +  (false-if-exception
> +   (let ((proc (syscall->procedure int "unshare" (list int))))
> +     (lambda (flags)
> +       (let ((ret err (proc flags)))
> +         (unless (zero? ret)
> +           (throw 'system-error "unshare" "~d ~d: ~A"
> +                  (list flags (strerror err))
> +                  (list err))))))))

Please remove ‘false-if-exception’, add a docstring, and add a test or
two.

(I find that unshare(2) is not that useful because a process cannot
unshare(2) its PID namespace: it has to fork to do that.  At that point,
one might as well call clone(CLONE_NEWPID) directly.)

Ludo’.
  

Patch

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 487ee68b43..492a229938 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -36,6 +36,7 @@  (define-module (guix build syscalls)
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-19)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-71)
   #:use-module (ice-9 rdelim)
   #:use-module (ice-9 regex)
   #:use-module (ice-9 match)
@@ -121,6 +122,7 @@  (define-module (guix build syscalls)
             mkdtemp!
             fdatasync
             pivot-root
+            unshare
             scandir*
             getxattr
             setxattr
@@ -1183,6 +1185,16 @@  (define pivot-root
                  (list new-root put-old (strerror err))
                  (list err)))))))
 
+(define unshare
+  (false-if-exception
+   (let ((proc (syscall->procedure int "unshare" (list int))))
+     (lambda (flags)
+       (let ((ret err (proc flags)))
+         (unless (zero? ret)
+           (throw 'system-error "unshare" "~d ~d: ~A"
+                  (list flags (strerror err))
+                  (list err))))))))
+
 
 ;;;
 ;;; Opendir & co.