exec.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix scripts container exec)
  19. #:use-module (ice-9 match)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-11)
  22. #:use-module (srfi srfi-37)
  23. #:use-module (guix scripts)
  24. #:use-module (guix ui)
  25. #:use-module (guix utils)
  26. #:use-module (gnu build linux-container)
  27. #:export (guix-container-exec))
  28. (define %options
  29. (list (option '(#\h "help") #f #f
  30. (lambda args
  31. (show-help)
  32. (exit 0)))
  33. (option '(#\V "version") #f #f
  34. (lambda args
  35. (show-version-and-exit "guix container exec")))))
  36. (define (show-help)
  37. (display (G_ "Usage: guix container exec PID COMMAND [ARGS...]
  38. Execute COMMAND within the container process PID.\n"))
  39. (newline)
  40. (display (G_ "
  41. -h, --help display this help and exit"))
  42. (display (G_ "
  43. -V, --version display version information and exit"))
  44. (newline)
  45. (show-bug-report-information))
  46. (define (partition-args args)
  47. "Split ARGS into two lists; one containing the arguments for this program,
  48. and the other containing arguments for the command to be executed."
  49. (define (number-string? str)
  50. (false-if-exception (string->number str)))
  51. (let loop ((a '())
  52. (b args))
  53. (match b
  54. (()
  55. (values (reverse a) '()))
  56. (((? number-string? head) . tail)
  57. (values (reverse (cons head a)) tail))
  58. ((head . tail)
  59. (loop (cons head a) tail)))))
  60. (define (guix-container-exec . args)
  61. (define (handle-argument arg result)
  62. (if (assoc-ref result 'pid)
  63. (leave (G_ "~a: extraneous argument~%") arg)
  64. (alist-cons 'pid (string->number* arg) result)))
  65. (with-error-handling
  66. (let-values (((args command) (partition-args args)))
  67. (let* ((opts (parse-command-line args %options '(())
  68. #:argument-handler
  69. handle-argument))
  70. (pid (assoc-ref opts 'pid))
  71. (environment (filter-map (lambda (name)
  72. (let ((value (getenv name)))
  73. (and value (cons name value))))
  74. ;; Pass through the TERM environment
  75. ;; variable to inform processes about
  76. ;; the capabilities of the terminal.
  77. '("TERM"))))
  78. (unless pid
  79. (leave (G_ "no pid specified~%")))
  80. (when (null? command)
  81. (leave (G_ "no command specified~%")))
  82. (unless (file-exists? (string-append "/proc/" (number->string pid)))
  83. (leave (G_ "no such process ~d~%") pid))
  84. (let ((result (container-excursion pid
  85. (lambda ()
  86. (match command
  87. ((program . program-args)
  88. (for-each (match-lambda
  89. ((name . value)
  90. (setenv name value)))
  91. environment)
  92. (apply execlp program program program-args)))))))
  93. (unless (zero? result)
  94. (leave (G_ "exec failed with status ~d~%") result)))))))