blob: 51a8d671188d9ba23f76d010bacddd89bb38c171 [file] [log] [blame]
Mathias Agopian5cae0d02011-10-20 18:42:02 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopian801ea092017-03-06 15:05:04 -080017#include <private/gui/BitTube.h>
18
Mathias Agopian5cae0d02011-10-20 18:42:02 -070019#include <stdint.h>
20#include <sys/types.h>
Mathias Agopian7b5be952012-04-02 17:02:19 -070021#include <sys/socket.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070022
Mathias Agopian5cae0d02011-10-20 18:42:02 -070023#include <fcntl.h>
Mathias Agopian99fe3c62012-02-06 19:10:04 -080024#include <unistd.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070025
26#include <utils/Errors.h>
27
28#include <binder/Parcel.h>
29
Mathias Agopian5cae0d02011-10-20 18:42:02 -070030
31namespace android {
32// ----------------------------------------------------------------------------
33
Mathias Agopian7b5be952012-04-02 17:02:19 -070034// Socket buffer size. The default is typically about 128KB, which is much larger than
35// we really need. So we make it smaller.
Mathias Agopian90ed3e82013-09-09 23:36:25 -070036static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
Mathias Agopian7b5be952012-04-02 17:02:19 -070037
38
Mathias Agopian5cae0d02011-10-20 18:42:02 -070039BitTube::BitTube()
40 : mSendFd(-1), mReceiveFd(-1)
41{
Mathias Agopian90ed3e82013-09-09 23:36:25 -070042 init(DEFAULT_SOCKET_BUFFER_SIZE, DEFAULT_SOCKET_BUFFER_SIZE);
43}
44
45BitTube::BitTube(size_t bufsize)
46 : mSendFd(-1), mReceiveFd(-1)
47{
48 init(bufsize, bufsize);
Mathias Agopian5cae0d02011-10-20 18:42:02 -070049}
50
51BitTube::BitTube(const Parcel& data)
52 : mSendFd(-1), mReceiveFd(-1)
53{
54 mReceiveFd = dup(data.readFileDescriptor());
Mathias Agopian90ed3e82013-09-09 23:36:25 -070055 if (mReceiveFd < 0) {
Mathias Agopian5cae0d02011-10-20 18:42:02 -070056 mReceiveFd = -errno;
Steve Blocke6f43dd2012-01-06 19:20:56 +000057 ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
Mathias Agopian5cae0d02011-10-20 18:42:02 -070058 strerror(-mReceiveFd));
59 }
60}
61
62BitTube::~BitTube()
63{
64 if (mSendFd >= 0)
65 close(mSendFd);
66
67 if (mReceiveFd >= 0)
68 close(mReceiveFd);
69}
70
Mathias Agopian90ed3e82013-09-09 23:36:25 -070071void BitTube::init(size_t rcvbuf, size_t sndbuf) {
72 int sockets[2];
73 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) == 0) {
74 size_t size = DEFAULT_SOCKET_BUFFER_SIZE;
75 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
76 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
77 // sine we don't use the "return channel", we keep it small...
78 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
79 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
80 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
81 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
82 mReceiveFd = sockets[0];
83 mSendFd = sockets[1];
84 } else {
85 mReceiveFd = -errno;
86 ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd));
87 }
88}
89
Mathias Agopian5cae0d02011-10-20 18:42:02 -070090status_t BitTube::initCheck() const
91{
92 if (mReceiveFd < 0) {
93 return status_t(mReceiveFd);
94 }
95 return NO_ERROR;
96}
97
98int BitTube::getFd() const
99{
100 return mReceiveFd;
101}
102
Aravind Akella56ae4262014-07-10 16:01:10 -0700103int BitTube::getSendFd() const
104{
105 return mSendFd;
106}
107
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700108ssize_t BitTube::write(void const* vaddr, size_t size)
109{
110 ssize_t err, len;
111 do {
Mathias Agopian7b5be952012-04-02 17:02:19 -0700112 len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700113 // cannot return less than size, since we're using SOCK_SEQPACKET
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700114 err = len < 0 ? errno : 0;
115 } while (err == EINTR);
116 return err == 0 ? len : -err;
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700117}
118
119ssize_t BitTube::read(void* vaddr, size_t size)
120{
121 ssize_t err, len;
122 do {
Mathias Agopian7b5be952012-04-02 17:02:19 -0700123 len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700124 err = len < 0 ? errno : 0;
125 } while (err == EINTR);
Mathias Agopian3ad38072011-11-29 11:42:32 -0800126 if (err == EAGAIN || err == EWOULDBLOCK) {
127 // EAGAIN means that we have non-blocking I/O but there was
128 // no data to be read. Nothing the client should care about.
129 return 0;
130 }
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700131 return err == 0 ? len : -err;
132}
133
134status_t BitTube::writeToParcel(Parcel* reply) const
135{
136 if (mReceiveFd < 0)
137 return -EINVAL;
138
139 status_t result = reply->writeDupFileDescriptor(mReceiveFd);
140 close(mReceiveFd);
141 mReceiveFd = -1;
142 return result;
143}
144
Mathias Agopian7b5be952012-04-02 17:02:19 -0700145
146ssize_t BitTube::sendObjects(const sp<BitTube>& tube,
147 void const* events, size_t count, size_t objSize)
148{
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700149 const char* vaddr = reinterpret_cast<const char*>(events);
150 ssize_t size = tube->write(vaddr, count*objSize);
151
152 // should never happen because of SOCK_SEQPACKET
Dan Stozad723bd72014-11-18 10:24:03 -0800153 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
Mark Salyzyn91100452014-06-09 14:27:45 -0700154 "BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were sent!)",
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700155 count, objSize, size);
156
157 //ALOGE_IF(size<0, "error %d sending %d events", size, count);
Dan Stozad723bd72014-11-18 10:24:03 -0800158 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700159}
160
161ssize_t BitTube::recvObjects(const sp<BitTube>& tube,
162 void* events, size_t count, size_t objSize)
163{
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700164 char* vaddr = reinterpret_cast<char*>(events);
165 ssize_t size = tube->read(vaddr, count*objSize);
166
167 // should never happen because of SOCK_SEQPACKET
Dan Stozad723bd72014-11-18 10:24:03 -0800168 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
Mark Salyzyn91100452014-06-09 14:27:45 -0700169 "BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were received!)",
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700170 count, objSize, size);
171
172 //ALOGE_IF(size<0, "error %d receiving %d events", size, count);
Dan Stozad723bd72014-11-18 10:24:03 -0800173 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700174}
175
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700176// ----------------------------------------------------------------------------
177}; // namespace android