always use the same code for creating temporary files

Let's unify our code here, and also always specifiy O_CLOEXEC.
This commit is contained in:
Lennart Poettering
2014-01-28 13:47:35 +01:00
parent 7736202ce9
commit 2d5bdf5bc0
10 changed files with 43 additions and 30 deletions
+5 -5
View File
@@ -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");
+2 -2
View File
@@ -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);
+13 -5
View File
@@ -25,6 +25,7 @@
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#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));
+4 -4
View File
@@ -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);