f1c3eaef6a1ab5524a6f59df7696d5e2eafd3c22
[apps/camera-gstreamer.git] / app / utils.cpp
1 #include <sys/mman.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <assert.h>
5 #include <cstdlib>
6 #include <cstring>
7 #include <cerrno>
8
9 #include "utils.h"
10
11 static int
12 os_fd_set_cloexec(int fd)
13 {
14         long flags;
15
16         if (fd == -1)
17                 return -1;
18
19         flags = fcntl(fd, F_GETFD);
20         if (flags == -1)
21                 return -1;
22
23         if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
24                 return -1;
25
26         return 0;
27 }
28
29 static int
30 set_cloexec_or_close(int fd)
31 {
32         if (os_fd_set_cloexec(fd) != 0) {
33                 close(fd);
34                 return -1;
35         }
36         return fd;
37 }
38
39 static int
40 create_tmpfile_cloexec(char *tmpname)
41 {
42         int fd;
43
44 #ifdef HAVE_MKOSTEMP
45         fd = mkostemp(tmpname, O_CLOEXEC);
46         if (fd >= 0)
47                 unlink(tmpname);
48 #else
49         fd = mkstemp(tmpname);
50         if (fd >= 0) {
51                 fd = set_cloexec_or_close(fd);
52                 unlink(tmpname);
53         }
54 #endif
55
56         return fd;
57 }
58
59 /*
60  * Create a new, unique, anonymous file of the given size, and
61  * return the file descriptor for it. The file descriptor is set
62  * CLOEXEC. The file is immediately suitable for mmap()'ing
63  * the given size at offset zero.
64  *
65  * The file should not have a permanent backing store like a disk,
66  * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
67  *
68  * The file name is deleted from the file system.
69  *
70  * The file is suitable for buffer sharing between processes by
71  * transmitting the file descriptor over Unix sockets using the
72  * SCM_RIGHTS methods.
73  *
74  * If the C library implements posix_fallocate(), it is used to
75  * guarantee that disk space is available for the file at the
76  * given size. If disk space is insufficient, errno is set to ENOSPC.
77  * If posix_fallocate() is not supported, program may receive
78  * SIGBUS on accessing mmap()'ed file contents instead.
79  *
80  * If the C library implements memfd_create(), it is used to create the
81  * file purely in memory, without any backing file name on the file
82  * system, and then sealing off the possibility of shrinking it.  This
83  * can then be checked before accessing mmap()'ed file contents, to
84  * make sure SIGBUS can't happen.  It also avoids requiring
85  * XDG_RUNTIME_DIR.
86  */
87 int
88 os_create_anonymous_file(off_t size)
89 {
90         static const char weston_template[] = "/weston-shared-XXXXXX";
91         const char *path;
92         char *name;
93         int fd;
94         int ret;
95
96 #ifdef HAVE_MEMFD_CREATE
97         fd = memfd_create("weston-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING);
98         if (fd >= 0) {
99                 /* We can add this seal before calling posix_fallocate(), as
100                  * the file is currently zero-sized anyway.
101                  *
102                  * There is also no need to check for the return value, we
103                  * couldn't do anything with it anyway.
104                  */
105                 fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
106         } else
107 #endif
108         {
109                 path = getenv("XDG_RUNTIME_DIR");
110                 if (!path) {
111                         errno = ENOENT;
112                         return -1;
113                 }
114
115                 name = static_cast<char *>(malloc(strlen(path) + sizeof(weston_template)));
116                 if (!name)
117                         return -1;
118
119                 strcpy(name, path);
120                 strcat(name, weston_template);
121
122                 fd = create_tmpfile_cloexec(name);
123
124                 free(name);
125
126                 if (fd < 0)
127                         return -1;
128         }
129
130 #ifdef HAVE_POSIX_FALLOCATE
131         do {
132                 ret = posix_fallocate(fd, 0, size);
133         } while (ret == EINTR);
134         if (ret != 0) {
135                 close(fd);
136                 errno = ret;
137                 return -1;
138         }
139 #else
140         do {
141                 ret = ftruncate(fd, size);
142         } while (ret < 0 && errno == EINTR);
143         if (ret < 0) {
144                 close(fd);
145                 return -1;
146         }
147 #endif
148
149         return fd;
150 }