1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

fileio: simplify mkostemp_safe() (#4090)

According to its manual page, flags given to mkostemp(3) shouldn't include
O_RDWR, O_CREAT or O_EXCL flags as these are always included. Beyond
those, the only flag that all callers (except a few tests where it
probably doesn't matter) use is O_CLOEXEC, so set that unconditionally.
This commit is contained in:
Topi Miettinen 2016-09-13 06:20:38 +00:00 committed by Martin Pitt
parent acb986015d
commit 646853bdd8
22 changed files with 60 additions and 60 deletions

View File

@ -1043,7 +1043,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
if (r < 0)
return r;
fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
fd = mkostemp_safe(t);
if (fd < 0) {
free(t);
return -errno;
@ -1076,7 +1076,7 @@ int fflush_and_check(FILE *f) {
}
/* This is much like mkostemp() but is subject to umask(). */
int mkostemp_safe(char *pattern, int flags) {
int mkostemp_safe(char *pattern) {
_cleanup_umask_ mode_t u = 0;
int fd;
@ -1084,7 +1084,7 @@ int mkostemp_safe(char *pattern, int flags) {
u = umask(077);
fd = mkostemp(pattern, flags);
fd = mkostemp(pattern, O_CLOEXEC);
if (fd < 0)
return -errno;
@ -1289,7 +1289,7 @@ int open_tmpfile_unlinkable(const char *directory, int flags) {
/* Fall back to unguessable name + unlinking */
p = strjoina(directory, "/systemd-tmp-XXXXXX");
fd = mkostemp_safe(p, flags);
fd = mkostemp_safe(p);
if (fd < 0)
return fd;

View File

@ -71,7 +71,7 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *root
int fflush_and_check(FILE *f);
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
int mkostemp_safe(char *pattern, int flags);
int mkostemp_safe(char *pattern);
int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
int tempfn_random(const char *p, const char *extra, char **ret);

View File

@ -620,7 +620,7 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
if (!temp)
return log_oom();
fdt = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
fdt = mkostemp_safe(temp);
if (fdt < 0)
return log_error_errno(fdt, "Failed to create temporary file: %m");
log_debug("Created temporary file %s", temp);

View File

@ -1632,7 +1632,7 @@ static int setup_keys(void) {
n /= arg_interval;
safe_close(fd);
fd = mkostemp_safe(k, O_WRONLY|O_CLOEXEC);
fd = mkostemp_safe(k);
if (fd < 0) {
r = log_error_errno(fd, "Failed to open %s: %m", k);
goto finish;

View File

@ -55,7 +55,7 @@ static Hashmap * test_import(const char* contents, ssize_t size, int code) {
assert_se(h = hashmap_new(&catalog_hash_ops));
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, contents, size) == size);
@ -182,7 +182,7 @@ static void test_catalog_update(void) {
static char name[] = "/tmp/test-catalog.XXXXXX";
int r;
r = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
r = mkostemp_safe(name);
assert_se(r >= 0);
database = name;

View File

@ -167,7 +167,7 @@ static void test_compress_stream(int compression,
log_debug("/* test compression */");
assert_se((dst = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC)) >= 0);
assert_se((dst = mkostemp_safe(pattern)) >= 0);
assert_se(compress(src, dst, -1) == 0);
@ -178,7 +178,7 @@ static void test_compress_stream(int compression,
log_debug("/* test decompression */");
assert_se((dst2 = mkostemp_safe(pattern2, O_RDWR|O_CLOEXEC)) >= 0);
assert_se((dst2 = mkostemp_safe(pattern2)) >= 0);
assert_se(stat(srcfile, &st) == 0);

View File

@ -36,15 +36,15 @@ int main(int argc, char *argv[]) {
assert_se(m = mmap_cache_new());
x = mkostemp_safe(px, O_RDWR|O_CLOEXEC);
x = mkostemp_safe(px);
assert_se(x >= 0);
unlink(px);
y = mkostemp_safe(py, O_RDWR|O_CLOEXEC);
y = mkostemp_safe(py);
assert_se(y >= 0);
unlink(py);
z = mkostemp_safe(pz, O_RDWR|O_CLOEXEC);
z = mkostemp_safe(pz);
assert_se(z >= 0);
unlink(pz);

View File

@ -484,7 +484,7 @@ int ask_password_agent(
(void) mkdir_p_label("/run/systemd/ask-password", 0755);
fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
fd = mkostemp_safe(temp);
if (fd < 0) {
r = fd;
goto finish;

View File

@ -35,7 +35,7 @@ static void test_add_acls_for_user(void) {
uid_t uid;
int r;
fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
/* Use the mode that user journal files use */

View File

@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
int fd;
char name[] = "/tmp/test-asynchronous_close.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
asynchronous_close(fd);

View File

@ -55,7 +55,7 @@ static void test_clock_is_localtime(void) {
/* without an adjtime file we default to UTC */
assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
fd = mkostemp_safe(adjtime, O_WRONLY|O_CLOEXEC);
fd = mkostemp_safe(adjtime);
assert_se(fd >= 0);
log_info("adjtime test file: %s", adjtime);
f = fdopen(fd, "w");

View File

@ -42,11 +42,11 @@ static void test_copy_file(void) {
log_info("%s", __func__);
fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
close(fd);
fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(fn_copy);
assert_se(fd >= 0);
close(fd);
@ -71,9 +71,9 @@ static void test_copy_file_fd(void) {
log_info("%s", __func__);
in_fd = mkostemp_safe(in_fn, O_RDWR);
in_fd = mkostemp_safe(in_fn);
assert_se(in_fd >= 0);
out_fd = mkostemp_safe(out_fn, O_RDWR);
out_fd = mkostemp_safe(out_fn);
assert_se(out_fd >= 0);
assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
@ -207,10 +207,10 @@ static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint
fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
assert_se(fd >= 0);
fd2 = mkostemp_safe(fn2, O_RDWR);
fd2 = mkostemp_safe(fn2);
assert_se(fd2 >= 0);
fd3 = mkostemp_safe(fn3, O_WRONLY);
fd3 = mkostemp_safe(fn3);
assert_se(fd3 >= 0);
r = copy_bytes(fd, fd2, max_bytes, try_reflink);

View File

@ -31,9 +31,9 @@ static void test_close_many(void) {
char name1[] = "/tmp/test-close-many.XXXXXX";
char name2[] = "/tmp/test-close-many.XXXXXX";
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);
fds[0] = mkostemp_safe(name0);
fds[1] = mkostemp_safe(name1);
fds[2] = mkostemp_safe(name2);
close_many(fds, 2);
@ -52,7 +52,7 @@ static void test_close_nointr(void) {
char name[] = "/tmp/test-test-close_nointr.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(close_nointr(fd) >= 0);
assert_se(close_nointr(fd) < 0);

View File

@ -31,7 +31,7 @@ static void test_fdset_new_fill(void) {
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(fdset_new_fill(&fdset) >= 0);
assert_se(fdset_contains(fdset, fd));
@ -45,7 +45,7 @@ static void test_fdset_put_dup(void) {
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -64,7 +64,7 @@ static void test_fdset_cloexec(void) {
int flags = -1;
char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -91,7 +91,7 @@ static void test_fdset_close_others(void) {
int flags = -1;
char name[] = "/tmp/test-fdset_close_others.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -113,7 +113,7 @@ static void test_fdset_remove(void) {
FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_remove.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -136,7 +136,7 @@ static void test_fdset_iterate(void) {
int c = 0;
int a;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -161,7 +161,7 @@ static void test_fdset_isempty(void) {
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_isempty.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();
@ -179,7 +179,7 @@ static void test_fdset_steal_first(void) {
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_steal_first.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
fdset = fdset_new();

View File

@ -45,11 +45,11 @@ static void test_parse_env_file(void) {
char **i;
unsigned k;
fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(p);
assert_se(fd >= 0);
close(fd);
fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(t);
assert_se(fd >= 0);
f = fdopen(fd, "w");
@ -158,11 +158,11 @@ static void test_parse_multiline_env_file(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
char **i;
fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(p);
assert_se(fd >= 0);
close(fd);
fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(t);
assert_se(fd >= 0);
f = fdopen(fd, "w");
@ -211,7 +211,7 @@ static void test_executable_is_script(void) {
FILE *f;
char *command;
fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(t);
assert_se(fd >= 0);
f = fdopen(fd, "w");
@ -300,7 +300,7 @@ static void test_write_string_stream(void) {
int fd;
char buf[64];
fd = mkostemp_safe(fn, O_RDWR);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
f = fdopen(fd, "r");
@ -334,7 +334,7 @@ static void test_write_string_file(void) {
char buf[64] = {};
_cleanup_close_ int fd;
fd = mkostemp_safe(fn, O_RDWR);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
@ -350,7 +350,7 @@ static void test_write_string_file_no_create(void) {
_cleanup_close_ int fd;
char buf[64] = {0};
fd = mkostemp_safe(fn, O_RDWR);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
@ -390,7 +390,7 @@ static void test_load_env_file_pairs(void) {
_cleanup_strv_free_ char **l = NULL;
char **k, **v;
fd = mkostemp_safe(fn, O_RDWR);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
r = write_string_file(fn,
@ -433,7 +433,7 @@ static void test_search_and_fopen(void) {
int r;
FILE *f;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
close(fd);
@ -469,7 +469,7 @@ static void test_search_and_fopen_nulstr(void) {
int r;
FILE *f;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
close(fd);
@ -504,7 +504,7 @@ static void test_writing_tmpfile(void) {
IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
IOVEC_SET_STRING(iov[2], "");
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
printf("tmpfile: %s", name);
r = writev(fd, iov, 3);

View File

@ -34,7 +34,7 @@ static void test_unlink_noerrno(void) {
char name[] = "/tmp/test-close_nointr.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(close_nointr(fd) >= 0);

View File

@ -30,7 +30,7 @@ static void test_glob_exists(void) {
int fd = -1;
int r;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
close(fd);

View File

@ -104,7 +104,7 @@ static void test_read_hostname_config(void) {
char *hostname;
int fd;
fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(path);
assert(fd > 0);
close(fd);

View File

@ -31,7 +31,7 @@ static void test_files_same(void) {
char name[] = "/tmp/test-files_same.XXXXXX";
char name_alias[] = "/tmp/test-files_same.alias";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(symlink(name, name_alias) >= 0);
@ -47,7 +47,7 @@ static void test_is_symlink(void) {
char name_link[] = "/tmp/test-is_symlink.link";
_cleanup_close_ int fd = -1;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(symlink(name, name_link) >= 0);

View File

@ -50,7 +50,7 @@ static void test_read_one_char(void) {
char name[] = "/tmp/test-read_one_char.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
file = fdopen(fd, "r+");
assert_se(file);

View File

@ -51,7 +51,7 @@ int main(int argc, char** argv) {
log_debug("link1: %s", ans);
assert_se(endswith(ans, " (deleted)"));
fd2 = mkostemp_safe(pattern, O_RDWR|O_CLOEXEC);
fd2 = mkostemp_safe(pattern);
assert_se(fd >= 0);
assert_se(unlink(pattern) == 0);

View File

@ -485,7 +485,7 @@ static void test_load_env_file_1(void) {
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
@ -508,7 +508,7 @@ static void test_load_env_file_2(void) {
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
@ -526,7 +526,7 @@ static void test_load_env_file_3(void) {
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
@ -542,7 +542,7 @@ static void test_load_env_file_4(void) {
_cleanup_close_ int fd;
int r;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
@ -562,7 +562,7 @@ static void test_load_env_file_5(void) {
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se(write(fd, env_file_5, sizeof(env_file_5)) == sizeof(env_file_5));