1/*
2 *  sync abstraction
3 *  Copyright 2015-2016 Collabora Ltd.
4 *
5 *  Based on the implementation from the Android Open Source Project,
6 *
7 *  Copyright 2012 Google, Inc
8 *
9 *  Permission is hereby granted, free of charge, to any person obtaining a
10 *  copy of this software and associated documentation files (the "Software"),
11 *  to deal in the Software without restriction, including without limitation
12 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 *  and/or sell copies of the Software, and to permit persons to whom the
14 *  Software is furnished to do so, subject to the following conditions:
15 *
16 *  The above copyright notice and this permission notice shall be included in
17 *  all copies or substantial portions of the Software.
18 *
19 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 *  OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef _LIBSYNC_H
29#define _LIBSYNC_H
30
31#include <assert.h>
32#include <errno.h>
33#include <stdbool.h>
34#include <stdint.h>
35#include <string.h>
36#include <sys/ioctl.h>
37#include <sys/poll.h>
38#include <unistd.h>
39
40#if defined(__cplusplus)
41extern "C" {
42#endif
43
44#ifdef ANDROID
45/* On Android, rely on the system's libsync instead of rolling our own
46 * sync_wait() and sync_merge().  This gives us compatibility with pre-4.7
47 * Android kernels.
48 */
49#include <android/sync.h>
50
51/**
52 * Check if the fd represents a valid fence-fd.
53 *
54 * The android variant of this debug helper is implemented on top of the
55 * system's libsync for compatibility with pre-4.7 android kernels.
56 */
57static inline bool
58sync_valid_fd(int fd)
59{
60	/* sync_file_info() only available in SDK 26. */
61#if ANDROID_API_LEVEL >= 26
62	struct sync_file_info *info = sync_file_info(fd);
63	if (!info)
64		return false;
65	sync_file_info_free(info);
66#endif
67	return true;
68}
69#else
70
71#ifndef SYNC_IOC_MERGE
72/* duplicated from linux/sync_file.h to avoid build-time dependency
73 * on new (v4.7) kernel headers.  Once distro's are mostly using
74 * something newer than v4.7 drop this and #include <linux/sync_file.h>
75 * instead.
76 */
77struct sync_merge_data {
78	char	name[32];
79	int32_t	fd2;
80	int32_t	fence;
81	uint32_t	flags;
82	uint32_t	pad;
83};
84
85struct sync_file_info {
86	char	name[32];
87	int32_t	status;
88	uint32_t	flags;
89	uint32_t	num_fences;
90	uint32_t	pad;
91
92	uint64_t	sync_fence_info;
93};
94
95#define SYNC_IOC_MAGIC		'>'
96#define SYNC_IOC_MERGE		_IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
97#define SYNC_IOC_FILE_INFO	_IOWR(SYNC_IOC_MAGIC, 4, struct sync_file_info)
98#endif
99
100
101static inline int sync_wait(int fd, int timeout)
102{
103	struct pollfd fds = {0};
104	int ret;
105
106	fds.fd = fd;
107	fds.events = POLLIN;
108
109	do {
110		ret = poll(&fds, 1, timeout);
111		if (ret > 0) {
112			if (fds.revents & (POLLERR | POLLNVAL)) {
113				errno = EINVAL;
114				return -1;
115			}
116			return 0;
117		} else if (ret == 0) {
118			errno = ETIME;
119			return -1;
120		}
121	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
122
123	return ret;
124}
125
126static inline int sync_merge(const char *name, int fd1, int fd2)
127{
128	struct sync_merge_data data = {0};
129	int ret;
130
131	data.fd2 = fd2;
132	strncpy(data.name, name, sizeof(data.name));
133
134	do {
135		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
136	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
137
138	if (ret < 0)
139		return ret;
140
141	return data.fence;
142}
143
144/**
145 * Check if the fd represents a valid fence-fd.
146 */
147static inline bool
148sync_valid_fd(int fd)
149{
150	struct sync_file_info info = {{0}};
151	return ioctl(fd, SYNC_IOC_FILE_INFO, &info) >= 0;
152}
153
154#endif /* !ANDROID */
155
156/* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
157 * otherwise sync_merge() and close the old *fd1.  This can be used
158 * to implement the pattern:
159 *
160 *    init()
161 *    {
162 *       batch.fence_fd = -1;
163 *    }
164 *
165 *    // does *NOT* take ownership of fd
166 *    server_sync(int fd)
167 *    {
168 *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
169 *          ... error ...
170 *       }
171 *    }
172 */
173static inline int sync_accumulate(const char *name, int *fd1, int fd2)
174{
175	int ret;
176
177	assert(fd2 >= 0);
178
179	if (*fd1 < 0) {
180		*fd1 = dup(fd2);
181		return 0;
182	}
183
184	ret = sync_merge(name, *fd1, fd2);
185	if (ret < 0) {
186		/* leave *fd1 as it is */
187		return ret;
188	}
189
190	close(*fd1);
191	*fd1 = ret;
192
193	return 0;
194}
195
196#if defined(__cplusplus)
197}
198#endif
199
200#endif
201