| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 |
|
| 29 | (define-module (gnu build shepherd)
|
| 30 | #:use-module (gnu system file-systems)
|
| 31 | #:use-module (gnu build linux-container)
|
| 32 | #:use-module (guix build utils)
|
| 33 | #:use-module (srfi srfi-1)
|
| 34 | #:use-module (srfi srfi-26)
|
| 35 | #:use-module (ice-9 match)
|
| 36 | |
| 37 | #:autoload (shepherd service) (fork+exec-command
|
| 38 | read-pid-file
|
| 39 | exec-command
|
| 40 | %precious-signals)
|
| 41 | #:autoload (shepherd system) (unblock-signals)
|
| 42 | #:export (default-mounts
|
| 43 | fork+exec-command/container))
|
| 44 |
|
| 45 | (define (clean-up file)
|
| 46 | (when file
|
| 47 | (catch 'system-error
|
| 48 | (lambda ()
|
| 49 | (delete-file file))
|
| 50 | (lambda args
|
| 51 | (unless (= ENOENT (system-error-errno args))
|
| 52 | (apply throw args))))))
|
| 53 |
|
| 54 | (define-syntax-rule (catch-system-error exp)
|
| 55 | (catch 'system-error
|
| 56 | (lambda ()
|
| 57 | exp)
|
| 58 | (const #f)))
|
| 59 |
|
| 60 | (define (default-namespaces args)
|
| 61 | |
| 62 | |
| 63 | (fold delq %namespaces '(net user)))
|
| 64 |
|
| 65 | (define* (default-mounts #:key (namespaces (default-namespaces '())))
|
| 66 | (define (tmpfs directory)
|
| 67 | (file-system
|
| 68 | (device "none")
|
| 69 | (mount-point directory)
|
| 70 | (type "tmpfs")
|
| 71 | (check? #f)))
|
| 72 |
|
| 73 | (define accounts
|
| 74 | |
| 75 | |
| 76 | (list (file-system-mapping
|
| 77 | (source "/etc/passwd") (target source))
|
| 78 | (file-system-mapping
|
| 79 | (source "/etc/group") (target source))))
|
| 80 |
|
| 81 | (append (cons (tmpfs "/tmp") %container-file-systems)
|
| 82 | (let ((mappings `(,@(if (memq 'net namespaces)
|
| 83 | '()
|
| 84 | %network-file-mappings)
|
| 85 | ,@(if (and (memq 'mnt namespaces)
|
| 86 | (not (memq 'user namespaces)))
|
| 87 | accounts
|
| 88 | '())
|
| 89 |
|
| 90 | |
| 91 | |
| 92 | |
| 93 | ,(file-system-mapping
|
| 94 | (source "/etc/localtime")
|
| 95 | (target source))
|
| 96 |
|
| 97 | ,%store-mapping))) |
| 98 | (map file-system-mapping->bind-mount
|
| 99 | (filter (lambda (mapping)
|
| 100 | (file-exists? (file-system-mapping-source mapping)))
|
| 101 | mappings)))))
|
| 102 |
|
| 103 | (define* (exec-command* command #:key user group log-file pid-file
|
| 104 | (supplementary-groups '())
|
| 105 | (directory "/") (environment-variables (environ)))
|
| 106 | "Like 'exec-command', but first restore signal handles modified by
|
| 107 | shepherd (PID 1)."
|
| 108 | |
| 109 | (for-each (cut sigaction <> SIG_DFL) %precious-signals)
|
| 110 |
|
| 111 | |
| 112 | (unblock-signals %precious-signals)
|
| 113 |
|
| 114 | (mkdir-p "/var/run")
|
| 115 | (clean-up pid-file)
|
| 116 |
|
| 117 | (exec-command command
|
| 118 | #:user user
|
| 119 | #:group group
|
| 120 | #:supplementary-groups supplementary-groups
|
| 121 | #:log-file log-file
|
| 122 | #:directory directory
|
| 123 | #:environment-variables environment-variables))
|
| 124 |
|
| 125 | (define* (fork+exec-command/container command
|
| 126 | #:key pid
|
| 127 | #:allow-other-keys
|
| 128 | #:rest args)
|
| 129 | "This is a variant of 'fork+exec-command' procedure, that joins the
|
| 130 | namespaces of process PID beforehand. If there is no support for containers,
|
| 131 | on Hurd systems for instance, fallback to direct forking."
|
| 132 | (define (strip-pid args)
|
| 133 | |
| 134 | |
| 135 | (let loop ((args args)
|
| 136 | (result '()))
|
| 137 | (match args
|
| 138 | (()
|
| 139 | (reverse result))
|
| 140 | ((#:pid _ . rest)
|
| 141 | (loop rest result))
|
| 142 | ((head . rest)
|
| 143 | (loop rest (cons head result))))))
|
| 144 |
|
| 145 | (let ((container-support? (file-exists? "/proc/self/ns")))
|
| 146 | (if (and container-support?
|
| 147 | (not (and pid (= pid (getpid)))))
|
| 148 | (container-excursion* pid
|
| 149 | (lambda ()
|
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | (match (primitive-fork)
|
| 155 | (0 (dynamic-wind
|
| 156 | (const #t)
|
| 157 | (lambda ()
|
| 158 | (apply exec-command* command (strip-pid args)))
|
| 159 | (lambda ()
|
| 160 | (primitive-_exit 127))))
|
| 161 | (pid pid)))) |
| 162 | (apply fork+exec-command command (strip-pid args)))))
|
| 163 |
|
| 164 | |
| 165 | |
| 166 | |
| 167 |
|
| 168 | |