1 /*
2 * Create a temporary file in the current directory.
3 *
4 * Copyright (c) 2020 Dmitry V. Levin <ldv@strace.io>
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "tests.h"
11 #include <fcntl.h>
12 #include <unistd.h>
13
14 #ifndef O_TMPFILE
15 # if defined __hppa__
16 # define O_TMPFILE 040000000
17 # elif defined __alpha__
18 # define O_TMPFILE 0100000000
19 # elif defined __sparc__
20 # define O_TMPFILE 0200000000
21 # else
22 # define O_TMPFILE 020000000
23 # endif
24 #endif
25
26 int
27 create_tmpfile(unsigned int flags)
28 {
29 int fd = open(".", O_TMPFILE | O_DIRECTORY | O_EXCL | flags, 0600);
30
31 if (fd < 0) {
32 /*
33 * Since every test is executed in a separate directory,
34 * there is no need to protect from race conditions.
35 */
36 static const char fname[] = "create_tmpfile";
37
38 fd = open(fname, O_CREAT | O_EXCL | flags, 0600);
39 if (fd < 0)
40 perror_msg_and_fail("open: %s", fname);
41 if (unlink(fname))
42 perror_msg_and_fail("unlink: %s", fname);
43 }
44
45 return fd;
46 }