1 /*
2 * Copyright © 2013 Christian Persch
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "vice.h"
20
21 #include "vteutils.h"
22
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26
27 #include <glib.h>
28
29 /* Temporary define until glibc release catches up */
30 #ifdef __linux__
31
32 #include <sys/ioctl.h>
33 #include <linux/fs.h>
34
35 #ifndef O_TMPFILE
36 #ifndef __O_TMPFILE
37 #define __O_TMPFILE 020000000
38 #endif
39 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
40 #endif
41
42 #endif /* __linux__ */
43
44 int
_vte_mkstemp(void)45 _vte_mkstemp (void)
46 {
47 int fd;
48 gchar *file_name;
49
50 #ifdef O_TMPFILE
51 fd = open (g_get_tmp_dir (),
52 O_TMPFILE | O_EXCL | O_RDWR | O_NOATIME | O_CLOEXEC,
53 0600);
54 if (fd != -1)
55 goto done;
56
57 /* Try again with g_file_open_tmp */
58 #endif
59
60 fd = g_file_open_tmp ("vteXXXXXX", &file_name, NULL);
61 if (fd == -1)
62 return -1;
63
64 unlink (file_name);
65 g_free (file_name);
66
67 #ifdef O_NOATIME
68 do { } while (fcntl (fd, F_SETFL, O_NOATIME) == -1 && errno == EINTR);
69 #endif
70
71 #ifdef O_TMPFILE
72 done:
73 #endif
74
75 #ifdef __linux__
76 {
77 /* Mark the tmpfile as no-cow on file systems that support it.
78 *
79 * (Note that the definition of the ioctls make you think @flags would
80 * be long instead of int, but it turns out that this is not the case;
81 * see http://lwn.net/Articles/575846/ ).
82 */
83 int flags;
84
85 if (ioctl (fd, FS_IOC_GETFLAGS, &flags) == 0) {
86 flags |= FS_SECRM_FL | FS_NOATIME_FL | FS_NOCOMP_FL | FS_NOCOW_FL | FS_NODUMP_FL;
87
88 ioctl (fd, FS_IOC_SETFLAGS, &flags);
89 }
90 }
91 #endif /* __linux__ */
92
93 return fd;
94 }
95