From a3cabedada9dbe49082bd915ffc7fe8cfc5b9682 Mon Sep 17 00:00:00 2001 From: Matt Staveley-Taylor Date: Tue, 15 Oct 2024 22:01:37 +0200 Subject: [PATCH] tests/rtld: add stdio flush-on-exit test --- tests/rtld/destroy/meson.build | 0 tests/rtld/destroy1/meson.build | 1 + tests/rtld/{destroy => destroy1}/test.c | 0 tests/rtld/destroy2/meson.build | 1 + tests/rtld/destroy2/test.c | 43 +++++++++++++++++++++++++ tests/rtld/meson.build | 3 +- 6 files changed, 47 insertions(+), 1 deletion(-) delete mode 100644 tests/rtld/destroy/meson.build create mode 100644 tests/rtld/destroy1/meson.build rename tests/rtld/{destroy => destroy1}/test.c (100%) create mode 100644 tests/rtld/destroy2/meson.build create mode 100644 tests/rtld/destroy2/test.c diff --git a/tests/rtld/destroy/meson.build b/tests/rtld/destroy/meson.build deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/rtld/destroy1/meson.build b/tests/rtld/destroy1/meson.build new file mode 100644 index 000000000..0e7d79e30 --- /dev/null +++ b/tests/rtld/destroy1/meson.build @@ -0,0 +1 @@ +# only the test executable is required diff --git a/tests/rtld/destroy/test.c b/tests/rtld/destroy1/test.c similarity index 100% rename from tests/rtld/destroy/test.c rename to tests/rtld/destroy1/test.c diff --git a/tests/rtld/destroy2/meson.build b/tests/rtld/destroy2/meson.build new file mode 100644 index 000000000..0e7d79e30 --- /dev/null +++ b/tests/rtld/destroy2/meson.build @@ -0,0 +1 @@ +# only the test executable is required diff --git a/tests/rtld/destroy2/test.c b/tests/rtld/destroy2/test.c new file mode 100644 index 000000000..53e51781d --- /dev/null +++ b/tests/rtld/destroy2/test.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +// Make sure files are flushed on process exit. + +int main() { + int pipefd[2]; + if (pipe(pipefd) < 0) { + perror("pipe"); + return -1; + } + + pid_t child = fork(); + if (child < 0) { + perror("fork"); + return -1; + } + + // This should sit in libc's buffer and be flushed on exit. + const char *message = "hello world"; + + if (child == 0) { + // Child + dup2(pipefd[1], STDOUT_FILENO); + printf("%s", message); + return 0; + } + + // Parent + wait(NULL); + + char buffer[64]; + ssize_t len = read(pipefd[0], buffer, sizeof(buffer)); + assert(len > 0); + buffer[len] = 0; + + return strcmp(buffer, message) != 0; +} + diff --git a/tests/rtld/meson.build b/tests/rtld/meson.build index 13f465cb2..68c082532 100644 --- a/tests/rtld/meson.build +++ b/tests/rtld/meson.build @@ -6,7 +6,8 @@ rtld_test_cases = [ 'rtld_next', 'soname', 'preinit', - 'destroy', + 'destroy1', + 'destroy2', 'scope1', 'scope2', 'scope3',