client: Don't forget to init and destroy mutex
[profile/ivi/wayland.git] / src / wayland-os.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 #define _GNU_SOURCE
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
32 #include "../config.h"
33 #include "wayland-os.h"
34
35 static int
36 set_cloexec_or_close(int fd)
37 {
38         long flags;
39
40         if (fd == -1)
41                 return -1;
42
43         flags = fcntl(fd, F_GETFD);
44         if (flags == -1)
45                 goto err;
46
47         if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
48                 goto err;
49
50         return fd;
51
52 err:
53         close(fd);
54         return -1;
55 }
56
57 int
58 wl_os_socket_cloexec(int domain, int type, int protocol)
59 {
60         int fd;
61
62         fd = socket(domain, type | SOCK_CLOEXEC, protocol);
63         if (fd >= 0)
64                 return fd;
65         if (errno != EINVAL)
66                 return -1;
67
68         fd = socket(domain, type, protocol);
69         return set_cloexec_or_close(fd);
70 }
71
72 int
73 wl_os_dupfd_cloexec(int fd, long minfd)
74 {
75         int newfd;
76
77         newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
78         if (newfd >= 0)
79                 return newfd;
80         if (errno != EINVAL)
81                 return -1;
82
83         newfd = fcntl(fd, F_DUPFD, minfd);
84         return set_cloexec_or_close(newfd);
85 }
86
87 static ssize_t
88 recvmsg_cloexec_fallback(int sockfd, struct msghdr *msg, int flags)
89 {
90         ssize_t len;
91         struct cmsghdr *cmsg;
92         unsigned char *data;
93         int *fd;
94         int *end;
95
96         len = recvmsg(sockfd, msg, flags);
97         if (len == -1)
98                 return -1;
99
100         if (!msg->msg_control || msg->msg_controllen == 0)
101                 return len;
102
103         cmsg = CMSG_FIRSTHDR(msg);
104         for (; cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
105                 if (cmsg->cmsg_level != SOL_SOCKET ||
106                     cmsg->cmsg_type != SCM_RIGHTS)
107                         continue;
108
109                 data = CMSG_DATA(cmsg);
110                 end = (int *)(data + cmsg->cmsg_len - CMSG_LEN(0));
111                 for (fd = (int *)data; fd < end; ++fd)
112                         *fd = set_cloexec_or_close(*fd);
113         }
114
115         return len;
116 }
117
118 ssize_t
119 wl_os_recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags)
120 {
121         ssize_t len;
122
123         len = recvmsg(sockfd, msg, flags | MSG_CMSG_CLOEXEC);
124         if (len >= 0)
125                 return len;
126         if (errno != EINVAL)
127                 return -1;
128
129         return recvmsg_cloexec_fallback(sockfd, msg, flags);
130 }
131
132 int
133 wl_os_epoll_create_cloexec(void)
134 {
135         int fd;
136
137 #ifdef EPOLL_CLOEXEC
138         fd = epoll_create1(EPOLL_CLOEXEC);
139         if (fd >= 0)
140                 return fd;
141         if (errno != EINVAL)
142                 return -1;
143 #endif
144
145         fd = epoll_create(1);
146         return set_cloexec_or_close(fd);
147 }
148
149 int
150 wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
151 {
152         int fd;
153
154 #ifdef HAVE_ACCEPT4
155         fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
156         if (fd >= 0)
157                 return fd;
158         if (errno != ENOSYS)
159                 return -1;
160 #endif
161
162         fd = accept(sockfd, addr, addrlen);
163         return set_cloexec_or_close(fd);
164 }