Subject: [PATCH 1/5] build: syscalls: Add reboot.

From: Mathieu Othacehe <[email-masked]>
Date: Sat, 8 Apr 2017 18:03:25 +0200

* guix/build/syscalls.scm (static-or-ffi): New macro. Used to dispatch between
  static Guile core implementation and FFI version.
(reboot): New export procedure. Reimplemented from guile-linux-syscalls.patch.
(RB_AUTOBOOT, ..., RB_KEXEC): New exported flags replacing static Guile flags.
---
 guix/build/syscalls.scm | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 4bcb2a871..af5ec4b6a 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2015 David Thompson <[email-masked]>
 ;;; Copyright © 2015 Mark H Weaver <[email-masked]>
+;;; Copyright © 2017 Mathieu Othacehe <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -144,7 +145,15 @@
             utmpx-address
             login-type
             utmpx-entries
-            (read-utmpx-from-port . read-utmpx)))
+            (read-utmpx-from-port . read-utmpx))
+  #:replace (RB_AUTOBOOT
+             RB_HALT_SYSTEM
+             RB_ENABLED_CAD
+             RB_DISABLE_CAD
+             RB_POWER_OFF
+             RB_SW_SUSPEND
+             RB_KEXEC
+             reboot))
 
 ;;; Commentary:
 ;;;
@@ -409,6 +418,13 @@ the returned procedure is called."
         (error (format #f "~a: syscall->procedure failed: ~s"
                        name args))))))
 
+(define-syntax-rule (static-or-ffi symbol ffi-procedure)
+  "If SYMBOL is defined in the core Guile module, return the associated
+procedure, otherwise return FFI-PROCEDURE."
+  (if (module-defined? the-scm-module symbol)
+      (module-ref the-scm-module symbol)
+      ffi-procedure))
+
 
 ;;;
 ;;; File systems.
@@ -547,6 +563,24 @@ constants from <sys/mount.h>."
                  (list device (strerror err))
                  (list err)))))))
 
+(define RB_AUTOBOOT    #x01234567)
+(define RB_HALT_SYSTEM #xcdef0123)
+(define RB_ENABLED_CAD #x89abcdef)
+(define RB_DISABLE_CAD 0)
+(define RB_POWER_OFF   #x4321fedc)
+(define RB_SW_SUSPEND  #xd000fce2)
+(define RB_KEXEC       #x45584543)
+
+(define reboot
+  (static-or-ffi
+   'reboot
+   (let ((proc (syscall->procedure int "reboot" (list int))))
+     (lambda* (#:optional (cmd RB_AUTOBOOT))
+       (let-values (((ret err) (proc cmd)))
+         (unless (zero? ret)
+           (throw 'system-error "reboot" "~S: ~A"
+                  (list cmd (strerror err))
+                  (list err))))))))
 (define (kernel? pid)
   "Return #t if PID designates a \"kernel thread\" rather than a normal
 user-land process."
-- 
2.12.2