From 2d5bdf5bc0e4714d42e5999a4e37553a6bf83575 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 28 Jan 2014 13:47:35 +0100 Subject: always use the same code for creating temporary files Let's unify our code here, and also always specifiy O_CLOEXEC. --- src/test/test-fileio.c | 10 +++++----- src/test/test-tmpfiles.c | 4 ++-- src/test/test-unit-file.c | 18 +++++++++++++----- src/test/test-util.c | 8 ++++---- 4 files changed, 24 insertions(+), 16 deletions(-) (limited to 'src/test') diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index eb4fbc91d2..47a0907f90 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -41,11 +41,11 @@ static void test_parse_env_file(void) { char **i; unsigned k; - fd = mkstemp(p); + fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); close(fd); - fd = mkostemp(t, O_CLOEXEC); + fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); f = fdopen(fd, "w"); @@ -154,11 +154,11 @@ static void test_parse_multiline_env_file(void) { _cleanup_strv_free_ char **a = NULL, **b = NULL; char **i; - fd = mkstemp(p); + fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); close(fd); - fd = mkostemp(t, O_CLOEXEC); + fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); f = fdopen(fd, "w"); @@ -207,7 +207,7 @@ static void test_executable_is_script(void) { FILE *f; char *command; - fd = mkostemp(t, O_CLOEXEC); + fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); f = fdopen(fd, "w"); diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c index f25a0dca52..565f0f8b40 100644 --- a/src/test/test-tmpfiles.c +++ b/src/test/test-tmpfiles.c @@ -34,13 +34,13 @@ int main(int argc, char** argv) { _cleanup_close_ int fd, fd2; _cleanup_free_ char *cmd, *cmd2; - fd = open_tmpfile(p, O_RDWR); + fd = open_tmpfile(p, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd) > 0); system(cmd); - fd2 = mkostemp_safe(pattern, O_RDWR); + fd2 = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(unlink(pattern) == 0); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 1b4133b5d3..a8d5b65e6d 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "install.h" #include "install-printf.h" @@ -221,7 +222,9 @@ static void test_load_env_file_1(void) { int r; char name[] = "/tmp/test-load-env-file.XXXXXX"; - _cleanup_close_ int fd = mkstemp(name); + _cleanup_close_ int fd; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1)); @@ -242,7 +245,9 @@ static void test_load_env_file_2(void) { int r; char name[] = "/tmp/test-load-env-file.XXXXXX"; - _cleanup_close_ int fd = mkstemp(name); + _cleanup_close_ int fd; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2)); @@ -258,7 +263,9 @@ static void test_load_env_file_3(void) { int r; char name[] = "/tmp/test-load-env-file.XXXXXX"; - _cleanup_close_ int fd = mkstemp(name); + _cleanup_close_ int fd; + + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3)); @@ -270,10 +277,11 @@ static void test_load_env_file_3(void) { static void test_load_env_file_4(void) { _cleanup_strv_free_ char **data = NULL; + char name[] = "/tmp/test-load-env-file.XXXXXX"; + _cleanup_close_ int fd; int r; - char name[] = "/tmp/test-load-env-file.XXXXXX"; - _cleanup_close_ int fd = mkstemp(name); + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); assert(fd >= 0); assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4)); diff --git a/src/test/test-util.c b/src/test/test-util.c index f819589b52..43bb0249a2 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -56,9 +56,9 @@ static void test_close_many(void) { char name1[] = "/tmp/test-close-many.XXXXXX"; char name2[] = "/tmp/test-close-many.XXXXXX"; - fds[0] = mkstemp(name0); - fds[1] = mkstemp(name1); - fds[2] = mkstemp(name2); + fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC); + fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC); + fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC); close_many(fds, 2); @@ -591,7 +591,7 @@ static void test_writev_safe(void) { IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n"); IOVEC_SET_STRING(iov[2], ""); - fd = mkstemp(name); + fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); printf("test_writev_safe: %s", name); r = writev_safe(fd, iov, 3); -- cgit v1.2.3-54-g00ecf