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

View file

@ -472,7 +472,7 @@ static int run_gdb(sd_journal *j) {
data = (const uint8_t*) data + 9;
len -= 9;
fd = mkostemp(path, O_WRONLY);
fd = mkostemp_safe(path, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
log_error("Failed to create temporary file: %m");
return -errno;

View file

@ -1286,7 +1286,7 @@ static int setup_keys(void) {
n /= arg_interval;
close_nointr_nofail(fd);
fd = mkostemp(k, O_WRONLY|O_CLOEXEC|O_NOCTTY);
fd = mkostemp_safe(k, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
log_error("Failed to open %s: %m", k);
r = -errno;

View file

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "util.h"
#include "log.h"
@ -45,7 +46,9 @@ static void test_import(Hashmap *h, struct strbuf *sb,
const char* contents, ssize_t size, int code) {
int r;
char name[] = "/tmp/test-catalog.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, contents, size) == size);
@ -98,10 +101,10 @@ static void test_catalog_importing(void) {
static const char* database = NULL;
static void test_catalog_update(void) {
static char name[] = "/tmp/test-catalog.XXXXXX";
int r;
static char name[] = "/tmp/test-catalog.XXXXXX";
r = mkstemp(name);
r = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert(r >= 0);
database = name;

View file

@ -22,6 +22,7 @@
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "macro.h"
@ -36,15 +37,15 @@ int main(int argc, char *argv[]) {
assert_se(m = mmap_cache_new());
x = mkstemp(px);
x = mkostemp_safe(px, O_RDWR|O_CLOEXEC);
assert(x >= 0);
unlink(px);
y = mkstemp(py);
y = mkostemp_safe(py, O_RDWR|O_CLOEXEC);
assert(y >= 0);
unlink(py);
z = mkstemp(pz);
z = mkostemp_safe(pz, O_RDWR|O_CLOEXEC);
assert(z >= 0);
unlink(pz);

View file

@ -325,10 +325,7 @@ int ask_password_agent(
mkdir_p_label("/run/systemd/ask-password", 0755);
RUN_WITH_UMASK(0022) {
fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY);
}
fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
log_error("Failed to create password file: %m");
r = -errno;

View file

@ -3893,7 +3893,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
t[k] = '.';
stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
fd = mkostemp(t, O_WRONLY|O_CLOEXEC);
fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
free(t);
return -errno;
@ -6115,11 +6115,15 @@ int mkostemp_safe(char *pattern, int flags) {
unsigned long tries = TMP_MAX;
char *s;
int r;
_cleanup_umask_ mode_t u;
assert(pattern);
u = umask(077);
/* This is much like like mkostemp() but avoids using any
* static variables, thus is async signal safe */
* static variables, thus is async signal safe. Also, it's not
* subject to umask(). */
s = endswith(pattern, "XXXXXX");
if (!s)

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");

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);

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));

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);