downstream: cosmetic changes (tabulation etc)
[profile/ivi/weston-ivi-shell.git] / shared / os-compatibility.c
1 /*
2  * Copyright © 2012 Collabora, Ltd.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/epoll.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "os-compatibility.h"
35
36 static int
37 set_cloexec_or_close(int fd)
38 {
39         long flags;
40
41         if (fd == -1)
42                 return -1;
43
44         flags = fcntl(fd, F_GETFD);
45         if (flags == -1)
46                 goto err;
47
48         if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
49                 goto err;
50
51         return fd;
52
53 err:
54         close(fd);
55         return -1;
56 }
57
58 int
59 os_socketpair_cloexec(int domain, int type, int protocol, int *sv)
60 {
61         int ret;
62
63 #ifdef SOCK_CLOEXEC
64         ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
65         if (ret == 0 || errno != EINVAL)
66                 return ret;
67 #endif
68
69         ret = socketpair(domain, type, protocol, sv);
70         if (ret < 0)
71                 return ret;
72
73         sv[0] = set_cloexec_or_close(sv[0]);
74         sv[1] = set_cloexec_or_close(sv[1]);
75
76         if (sv[0] != -1 && sv[1] != -1)
77                 return 0;
78
79         close(sv[0]);
80         close(sv[1]);
81         return -1;
82 }
83
84 int
85 os_epoll_create_cloexec(void)
86 {
87         int fd;
88
89 #ifdef EPOLL_CLOEXEC
90         fd = epoll_create1(EPOLL_CLOEXEC);
91         if (fd >= 0)
92                 return fd;
93         if (errno != EINVAL)
94                 return -1;
95 #endif
96
97         fd = epoll_create(1);
98         return set_cloexec_or_close(fd);
99 }
100
101 static int
102 create_tmpfile_cloexec(char *tmpname)
103 {
104         int fd;
105
106 #ifdef HAVE_MKOSTEMP
107         fd = mkostemp(tmpname, O_CLOEXEC);
108         if (fd >= 0)
109                 unlink(tmpname);
110 #else
111         fd = mkstemp(tmpname);
112         if (fd >= 0) {
113                 fd = set_cloexec_or_close(fd);
114                 unlink(tmpname);
115         }
116 #endif
117
118         return fd;
119 }
120
121 /*
122  * Create a new, unique, anonymous file of the given size, and
123  * return the file descriptor for it. The file descriptor is set
124  * CLOEXEC. The file is immediately suitable for mmap()'ing
125  * the given size at offset zero.
126  *
127  * The file should not have a permanent backing store like a disk,
128  * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
129  *
130  * The file name is deleted from the file system.
131  *
132  * The file is suitable for buffer sharing between processes by
133  * transmitting the file descriptor over Unix sockets using the
134  * SCM_RIGHTS methods.
135  *
136  * If the C library implements posix_fallocate(), it is used to
137  * guarantee that disk space is available for the file at the
138  * given size. If disk space is insufficent, errno is set to ENOSPC.
139  * If posix_fallocate() is not supported, program may receive
140  * SIGBUS on accessing mmap()'ed file contents instead.
141  */
142 int
143 os_create_anonymous_file(off_t size)
144 {
145         static const char template[] = "/weston-shared-XXXXXX";
146         const char *path;
147         char *name;
148         int fd;
149         int ret;
150
151         path = getenv("XDG_RUNTIME_DIR");
152         if (!path) {
153                 errno = ENOENT;
154                 return -1;
155         }
156
157         name = malloc(strlen(path) + sizeof(template));
158         if (!name)
159                 return -1;
160
161         strcpy(name, path);
162         strcat(name, template);
163
164         fd = create_tmpfile_cloexec(name);
165
166         free(name);
167
168         if (fd < 0)
169                 return -1;
170
171 #ifdef HAVE_POSIX_FALLOCATE
172         ret = posix_fallocate(fd, 0, size);
173         if (ret != 0) {
174                 close(fd);
175                 errno = ret;
176                 return -1;
177         }
178 #else
179         ret = ftruncate(fd, size);
180         if (ret < 0) {
181                 close(fd);
182                 return -1;
183         }
184 #endif
185
186         return fd;
187 }
188
189 #ifndef HAVE_STRCHRNUL
190 char *
191 strchrnul(const char *s, int c)
192 {
193         while (*s && *s != c)
194                 s++;
195         return (char *)s;
196 }
197 #endif