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