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 <sys/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 = {0};
64	int ret;
65
66	fds.fd = fd;
67	fds.events = POLLIN;
68
69	do {
70		ret = poll(&fds, 1, timeout);
71		if (ret > 0) {
72			if (fds.revents & (POLLERR | POLLNVAL)) {
73				errno = EINVAL;
74				return -1;
75			}
76			return 0;
77		} else if (ret == 0) {
78			errno = ETIME;
79			return -1;
80		}
81	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
82
83	return ret;
84}
85
86static inline int sync_merge(const char *name, int fd1, int fd2)
87{
88	struct sync_merge_data data = {0};
89	int ret;
90
91	data.fd2 = fd2;
92	strncpy(data.name, name, sizeof(data.name));
93
94	do {
95		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
96	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
97
98	if (ret < 0)
99		return ret;
100
101	return data.fence;
102}
103
104/* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
105 * otherwise sync_merge() and close the old *fd1.  This can be used
106 * to implement the pattern:
107 *
108 *    init()
109 *    {
110 *       batch.fence_fd = -1;
111 *    }
112 *
113 *    // does *NOT* take ownership of fd
114 *    server_sync(int fd)
115 *    {
116 *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
117 *          ... error ...
118 *       }
119 *    }
120 */
121static inline int sync_accumulate(const char *name, int *fd1, int fd2)
122{
123	int ret;
124
125	assert(fd2 >= 0);
126
127	if (*fd1 < 0) {
128		*fd1 = dup(fd2);
129		return 0;
130	}
131
132	ret = sync_merge(name, *fd1, fd2);
133	if (ret < 0) {
134		/* leave *fd1 as it is */
135		return ret;
136	}
137
138	close(*fd1);
139	*fd1 = ret;
140
141	return 0;
142}
143
144#if defined(__cplusplus)
145}
146#endif
147
148#endif
149