-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LibOS] Wake up thread that can handle SIGTERM on host-injected SIGTERM
Some apps (e.g. MongoDB) create a separate thread that has a single job -- to wait for a SIGTERM signal and then perform graceful app termination. This special thread issues `sigtimedwait(SIGTERM)` and waits forever for this signal. Other threads instead block this signal (add it to their sigmask), so that they don't need to care about reacting to this signal. Previously, our LibOS would try to handle SIGTERM on the thread that got chosen by the host Linux (the thread is arbitrarily chosen). But with high probability, on such apps this thread will be the not-special-one, and the app would effectively ignore SIGTERM. Signed-off-by: Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
- Loading branch information
Showing
10 changed files
with
152 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-or-later */ | ||
/* Copyright (C) 2024 Intel Corporation */ | ||
|
||
#define _XOPEN_SOURCE 700 | ||
#include <pthread.h> | ||
#include <signal.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#include "common.h" | ||
|
||
static bool thread_started = false; | ||
|
||
static void pthread_check(int x) { | ||
if (x) { | ||
errx(1, "pthread failed with %d", x); | ||
} | ||
} | ||
|
||
static void ignore_sigterm(void) { | ||
sigset_t blocked; | ||
sigemptyset(&blocked); | ||
sigaddset(&blocked, SIGTERM); | ||
CHECK(sigprocmask(SIG_SETMASK, &blocked, NULL)); | ||
} | ||
|
||
static void* thread_func(void* arg) { | ||
ignore_sigterm(); | ||
__atomic_store_n(&thread_started, true, __ATOMIC_SEQ_CST); | ||
|
||
sigset_t waitset; | ||
sigemptyset(&waitset); | ||
sigaddset(&waitset, SIGTERM); | ||
int ret = sigwaitinfo(&waitset, /*info=*/NULL); | ||
if (ret != SIGTERM) | ||
errx(1, "expected SIGTERM but sigwaitinfo returned %d", ret); | ||
|
||
exit(0); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
ignore_sigterm(); | ||
|
||
pthread_t th; | ||
pthread_check(pthread_create(&th, NULL, thread_func, NULL)); | ||
|
||
while (!__atomic_load_n(&thread_started, __ATOMIC_SEQ_CST)) | ||
; | ||
|
||
/* helper thread started and waits for SIGTERM; inform the wrapper shell script */ | ||
puts("READY"); | ||
fflush(stdout); | ||
|
||
/* emulate some processing; note that we can't use smth like `pause()` because in this case, | ||
* both threads would wait in blocking host syscalls indefinitely, and Gramine currently has a | ||
* limitation that signals are delivered when some thread returns from syscall to the app */ | ||
while (true) { | ||
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000 * 1000 }; /* 1ms */ | ||
nanosleep(&ts, NULL); | ||
} | ||
return 0; | ||
} |
21 changes: 21 additions & 0 deletions
21
libos/test/regression/sigterm_multithread.manifest.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
loader.entrypoint = "file:{{ gramine.libos }}" | ||
libos.entrypoint = "{{ entrypoint }}" | ||
|
||
loader.env.LD_LIBRARY_PATH = "/lib" | ||
|
||
sys.enable_sigterm_injection = true | ||
|
||
fs.mounts = [ | ||
{ path = "/lib", uri = "file:{{ gramine.runtimedir(libc) }}" }, | ||
{ path = "/{{ entrypoint }}", uri = "file:{{ binary_dir }}/{{ entrypoint }}" }, | ||
] | ||
|
||
sgx.max_threads = {{ '1' if env.get('EDMM', '0') == '1' else '16' }} | ||
sgx.debug = true | ||
sgx.edmm_enable = {{ 'true' if env.get('EDMM', '0') == '1' else 'false' }} | ||
|
||
sgx.trusted_files = [ | ||
"file:{{ gramine.libos }}", | ||
"file:{{ gramine.runtimedir(libc) }}/", | ||
"file:{{ binary_dir }}/{{ entrypoint }}", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
rm -f tmp/shell_fifo | ||
mkfifo tmp/shell_fifo | ||
|
||
$@ 2>&1 >tmp/shell_fifo & | ||
pid=$! | ||
|
||
while read line; do | ||
case "$line" in | ||
*READY*) | ||
break | ||
;; | ||
*) | ||
;; | ||
esac | ||
done <tmp/shell_fifo | ||
|
||
kill -TERM $pid | ||
wait $pid | ||
|
||
echo "SHELL OK" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters