diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-04-16 19:26:11 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-05-01 21:30:34 +0200 |
| commit | a76fa226c8761dd349aaacca7ba041429bce4e73 (patch) | |
| tree | 80f2d7a2017e8234267fbea1ce48ec5ba647bab2 /gnu/build/linux-container.scm | |
| parent | eaebc7f2b715093baf0d4a8c86d7e1a6af20ebc8 (diff) | |
linux-container: 'call-with-container' relays SIGTERM and SIGINT.
* gnu/build/linux-container.scm (call-with-container): Add #:relayed-signals.
[install-signal-handlers]: New procedure.
Call it.
Diffstat (limited to 'gnu/build/linux-container.scm')
| -rw-r--r-- | gnu/build/linux-container.scm | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/gnu/build/linux-container.scm b/gnu/build/linux-container.scm index bdeca2cdb9f..03c01439cea 100644 --- a/gnu/build/linux-container.scm +++ b/gnu/build/linux-container.scm | |||
| @@ -303,6 +303,7 @@ delete it when leaving the dynamic extent of this call." | |||
| 303 | 303 | ||
| 304 | (define* (call-with-container mounts thunk #:key (namespaces %namespaces) | 304 | (define* (call-with-container mounts thunk #:key (namespaces %namespaces) |
| 305 | (host-uids 1) (guest-uid 0) (guest-gid 0) | 305 | (host-uids 1) (guest-uid 0) (guest-gid 0) |
| 306 | (relayed-signals (list SIGINT SIGTERM)) | ||
| 306 | (process-spawned-hook (const #t))) | 307 | (process-spawned-hook (const #t))) |
| 307 | "Run THUNK in a new container process and return its exit status; call | 308 | "Run THUNK in a new container process and return its exit status; call |
| 308 | PROCESS-SPAWNED-HOOK with the PID of the new process that has been spawned. | 309 | PROCESS-SPAWNED-HOOK with the PID of the new process that has been spawned. |
| @@ -320,20 +321,27 @@ can map more than a single uid/gid. | |||
| 320 | GUEST-UID and GUEST-GID specify the first UID (respectively GID) that host | 321 | GUEST-UID and GUEST-GID specify the first UID (respectively GID) that host |
| 321 | UIDs (respectively GIDs) map to in the namespace. | 322 | UIDs (respectively GIDs) map to in the namespace. |
| 322 | 323 | ||
| 324 | RELAYED-SIGNALS is the list of signals that are \"relayed\" to the container | ||
| 325 | process when caught by its parent. | ||
| 326 | |||
| 323 | Note that if THUNK needs to load any additional Guile modules, the relevant | 327 | Note that if THUNK needs to load any additional Guile modules, the relevant |
| 324 | module files must be present in one of the mappings in MOUNTS and the Guile | 328 | module files must be present in one of the mappings in MOUNTS and the Guile |
| 325 | load path must be adjusted as needed." | 329 | load path must be adjusted as needed." |
| 330 | (define (install-signal-handlers pid) | ||
| 331 | ;; Install handlers that forward signals to PID. | ||
| 332 | (define (relay-signal signal) | ||
| 333 | (false-if-exception (kill pid signal))) | ||
| 334 | |||
| 335 | (for-each (lambda (signal) | ||
| 336 | (sigaction signal relay-signal)) | ||
| 337 | relayed-signals)) | ||
| 338 | |||
| 326 | (call-with-temporary-directory | 339 | (call-with-temporary-directory |
| 327 | (lambda (root) | 340 | (lambda (root) |
| 328 | (let ((pid (run-container root mounts namespaces host-uids thunk | 341 | (let ((pid (run-container root mounts namespaces host-uids thunk |
| 329 | #:guest-uid guest-uid | 342 | #:guest-uid guest-uid |
| 330 | #:guest-gid guest-gid))) | 343 | #:guest-gid guest-gid))) |
| 331 | ;; Catch SIGINT and kill the container process. | 344 | (install-signal-handlers pid) |
| 332 | (sigaction SIGINT | ||
| 333 | (lambda (signum) | ||
| 334 | (false-if-exception | ||
| 335 | (kill pid SIGKILL)))) | ||
| 336 | |||
| 337 | (process-spawned-hook pid) | 345 | (process-spawned-hook pid) |
| 338 | (match (waitpid pid) | 346 | (match (waitpid pid) |
| 339 | ((_ . status) status)))))) | 347 | ((_ . status) status)))))) |
