1037b3c26Smrg/*
2037b3c26Smrg *  sync abstraction
3037b3c26Smrg *  Copyright 2015-2016 Collabora Ltd.
4037b3c26Smrg *
5037b3c26Smrg *  Based on the implementation from the Android Open Source Project,
6037b3c26Smrg *
7037b3c26Smrg *  Copyright 2012 Google, Inc
8037b3c26Smrg *
9037b3c26Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
10037b3c26Smrg *  copy of this software and associated documentation files (the "Software"),
11037b3c26Smrg *  to deal in the Software without restriction, including without limitation
12037b3c26Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13037b3c26Smrg *  and/or sell copies of the Software, and to permit persons to whom the
14037b3c26Smrg *  Software is furnished to do so, subject to the following conditions:
15037b3c26Smrg *
16037b3c26Smrg *  The above copyright notice and this permission notice shall be included in
17037b3c26Smrg *  all copies or substantial portions of the Software.
18037b3c26Smrg *
19037b3c26Smrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20037b3c26Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21037b3c26Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22037b3c26Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23037b3c26Smrg *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24037b3c26Smrg *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25037b3c26Smrg *  OTHER DEALINGS IN THE SOFTWARE.
26037b3c26Smrg */
27037b3c26Smrg
28037b3c26Smrg#ifndef _LIBSYNC_H
29037b3c26Smrg#define _LIBSYNC_H
30037b3c26Smrg
31037b3c26Smrg#include <assert.h>
32037b3c26Smrg#include <errno.h>
33037b3c26Smrg#include <stdint.h>
34037b3c26Smrg#include <string.h>
35037b3c26Smrg#include <sys/ioctl.h>
3648246ce7Smrg#include <poll.h>
37037b3c26Smrg#include <unistd.h>
38037b3c26Smrg
39037b3c26Smrg#if defined(__cplusplus)
40037b3c26Smrgextern "C" {
41037b3c26Smrg#endif
42037b3c26Smrg
43037b3c26Smrg#ifndef SYNC_IOC_MERGE
44037b3c26Smrg/* duplicated from linux/sync_file.h to avoid build-time dependency
45037b3c26Smrg * on new (v4.7) kernel headers.  Once distro's are mostly using
46037b3c26Smrg * something newer than v4.7 drop this and #include <linux/sync_file.h>
47037b3c26Smrg * instead.
48037b3c26Smrg */
49037b3c26Smrgstruct sync_merge_data {
50037b3c26Smrg	char	name[32];
51037b3c26Smrg	int32_t	fd2;
52037b3c26Smrg	int32_t	fence;
53037b3c26Smrg	uint32_t	flags;
54037b3c26Smrg	uint32_t	pad;
55037b3c26Smrg};
56037b3c26Smrg#define SYNC_IOC_MAGIC		'>'
57037b3c26Smrg#define SYNC_IOC_MERGE		_IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
58037b3c26Smrg#endif
59037b3c26Smrg
60037b3c26Smrg
61037b3c26Smrgstatic inline int sync_wait(int fd, int timeout)
62037b3c26Smrg{
639ac5b8b6Schristos	struct pollfd fds;
64037b3c26Smrg	int ret;
65037b3c26Smrg
66037b3c26Smrg	fds.fd = fd;
67037b3c26Smrg	fds.events = POLLIN;
689ac5b8b6Schristos	fds.revents = 0;
69037b3c26Smrg
70037b3c26Smrg	do {
71037b3c26Smrg		ret = poll(&fds, 1, timeout);
72037b3c26Smrg		if (ret > 0) {
73037b3c26Smrg			if (fds.revents & (POLLERR | POLLNVAL)) {
74037b3c26Smrg				errno = EINVAL;
75037b3c26Smrg				return -1;
76037b3c26Smrg			}
77037b3c26Smrg			return 0;
78037b3c26Smrg		} else if (ret == 0) {
79037b3c26Smrg			errno = ETIME;
80037b3c26Smrg			return -1;
81037b3c26Smrg		}
82037b3c26Smrg	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
83037b3c26Smrg
84037b3c26Smrg	return ret;
85037b3c26Smrg}
86037b3c26Smrg
87037b3c26Smrgstatic inline int sync_merge(const char *name, int fd1, int fd2)
88037b3c26Smrg{
899ac5b8b6Schristos	struct sync_merge_data data;
90037b3c26Smrg	int ret;
91037b3c26Smrg
92037b3c26Smrg	data.fd2 = fd2;
93037b3c26Smrg	strncpy(data.name, name, sizeof(data.name));
949ac5b8b6Schristos	data.fence = 0;
959ac5b8b6Schristos	data.flags = 0;
969ac5b8b6Schristos	data.pad = 0;
97037b3c26Smrg
98037b3c26Smrg	do {
99037b3c26Smrg		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
100037b3c26Smrg	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
101037b3c26Smrg
102037b3c26Smrg	if (ret < 0)
103037b3c26Smrg		return ret;
104037b3c26Smrg
105037b3c26Smrg	return data.fence;
106037b3c26Smrg}
107037b3c26Smrg
108037b3c26Smrg/* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
109037b3c26Smrg * otherwise sync_merge() and close the old *fd1.  This can be used
110037b3c26Smrg * to implement the pattern:
111037b3c26Smrg *
112037b3c26Smrg *    init()
113037b3c26Smrg *    {
114037b3c26Smrg *       batch.fence_fd = -1;
115037b3c26Smrg *    }
116037b3c26Smrg *
117037b3c26Smrg *    // does *NOT* take ownership of fd
118037b3c26Smrg *    server_sync(int fd)
119037b3c26Smrg *    {
120037b3c26Smrg *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
121037b3c26Smrg *          ... error ...
122037b3c26Smrg *       }
123037b3c26Smrg *    }
124037b3c26Smrg */
125037b3c26Smrgstatic inline int sync_accumulate(const char *name, int *fd1, int fd2)
126037b3c26Smrg{
127037b3c26Smrg	int ret;
128037b3c26Smrg
129037b3c26Smrg	assert(fd2 >= 0);
130037b3c26Smrg
131037b3c26Smrg	if (*fd1 < 0) {
132037b3c26Smrg		*fd1 = dup(fd2);
133037b3c26Smrg		return 0;
134037b3c26Smrg	}
135037b3c26Smrg
136037b3c26Smrg	ret = sync_merge(name, *fd1, fd2);
137037b3c26Smrg	if (ret < 0) {
138037b3c26Smrg		/* leave *fd1 as it is */
139037b3c26Smrg		return ret;
140037b3c26Smrg	}
141037b3c26Smrg
142037b3c26Smrg	close(*fd1);
143037b3c26Smrg	*fd1 = ret;
144037b3c26Smrg
145037b3c26Smrg	return 0;
146037b3c26Smrg}
147037b3c26Smrg
148037b3c26Smrg#if defined(__cplusplus)
149037b3c26Smrg}
150037b3c26Smrg#endif
151037b3c26Smrg
152037b3c26Smrg#endif
153