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 "config.h"
20 
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE /* for O_TMPFILE */
23 #endif
24 
25 #include "vteutils.h"
26 
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 
31 #include <glib.h>
32 
33 /* Temporary define until glibc release catches up */
34 #ifdef __linux__
35 
36 #include <sys/ioctl.h>
37 #include <linux/fs.h>
38 
39 #ifndef O_TMPFILE
40 #ifndef __O_TMPFILE
41 #define __O_TMPFILE     020000000
42 #endif
43 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
44 #endif
45 
46 #endif /* __linux__ */
47 
48 int
_vte_mkstemp(void)49 _vte_mkstemp (void)
50 {
51         int fd;
52         gchar *file_name;
53 
54 #ifdef O_TMPFILE
55         fd = open (g_get_tmp_dir (),
56                    O_TMPFILE | O_EXCL | O_RDWR | O_NOATIME | O_CLOEXEC,
57                    0600);
58         if (fd != -1)
59                 goto done;
60 
61         /* Try again with g_file_open_tmp */
62 #endif
63 
64         fd = g_file_open_tmp ("vteXXXXXX", &file_name, NULL);
65         if (fd == -1)
66                 return -1;
67 
68         unlink (file_name);
69         g_free (file_name);
70 
71 #ifdef O_NOATIME
72         do { } while (fcntl (fd, F_SETFL, O_NOATIME) == -1 && errno == EINTR);
73 #endif
74 
75 #ifdef O_TMPFILE
76  done:
77 #endif
78 
79 #ifdef __linux__
80 {
81         /* Mark the tmpfile as no-cow on file systems that support it.
82          *
83          * (Note that the definition of the ioctls make you think @flags would
84          * be long instead of int, but it turns out that this is not the case;
85          * see http://lwn.net/Articles/575846/ ).
86          */
87         int flags;
88 
89         if (ioctl (fd, FS_IOC_GETFLAGS, &flags) == 0) {
90                 flags |= FS_SECRM_FL | FS_NOATIME_FL | FS_NOCOMP_FL | FS_NOCOW_FL | FS_NODUMP_FL;
91 
92                 ioctl (fd, FS_IOC_SETFLAGS, &flags);
93         }
94 }
95 #endif /* __linux__ */
96 
97         return fd;
98 }
99