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 <stdint.h>
34#include <string.h>
35#include <sys/ioctl.h>
36#include <poll.h>
37#include <unistd.h>
38
39#if defined(__cplusplus)
40extern "C" {
41#endif
42
43#ifndef SYNC_IOC_MERGE
44/* duplicated from linux/sync_file.h to avoid build-time dependency
45 * on new (v4.7) kernel headers.  Once distro's are mostly using
46 * something newer than v4.7 drop this and #include <linux/sync_file.h>
47 * instead.
48 */
49struct sync_merge_data {
50	char	name[32];
51	int32_t	fd2;
52	int32_t	fence;
53	uint32_t	flags;
54	uint32_t	pad;
55};
56#define SYNC_IOC_MAGIC		'>'
57#define SYNC_IOC_MERGE		_IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
58#endif
59
60
61static inline int sync_wait(int fd, int timeout)
62{
63	struct pollfd fds;
64	int ret;
65
66	fds.fd = fd;
67	fds.events = POLLIN;
68	fds.revents = 0;
69
70	do {
71		ret = poll(&fds, 1, timeout);
72		if (ret > 0) {
73			if (fds.revents & (POLLERR | POLLNVAL)) {
74				errno = EINVAL;
75				return -1;
76			}
77			return 0;
78		} else if (ret == 0) {
79			errno = ETIME;
80			return -1;
81		}
82	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
83
84	return ret;
85}
86
87static inline int sync_merge(const char *name, int fd1, int fd2)
88{
89	struct sync_merge_data data;
90	int ret;
91
92	data.fd2 = fd2;
93	strncpy(data.name, name, sizeof(data.name));
94	data.fence = 0;
95	data.flags = 0;
96	data.pad = 0;
97
98	do {
99		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
100	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
101
102	if (ret < 0)
103		return ret;
104
105	return data.fence;
106}
107
108/* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
109 * otherwise sync_merge() and close the old *fd1.  This can be used
110 * to implement the pattern:
111 *
112 *    init()
113 *    {
114 *       batch.fence_fd = -1;
115 *    }
116 *
117 *    // does *NOT* take ownership of fd
118 *    server_sync(int fd)
119 *    {
120 *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
121 *          ... error ...
122 *       }
123 *    }
124 */
125static inline int sync_accumulate(const char *name, int *fd1, int fd2)
126{
127	int ret;
128
129	assert(fd2 >= 0);
130
131	if (*fd1 < 0) {
132		*fd1 = dup(fd2);
133		return 0;
134	}
135
136	ret = sync_merge(name, *fd1, fd2);
137	if (ret < 0) {
138		/* leave *fd1 as it is */
139		return ret;
140	}
141
142	close(*fd1);
143	*fd1 = ret;
144
145	return 0;
146}
147
148#if defined(__cplusplus)
149}
150#endif
151
152#endif
153