blob: ef7a6f54d9ee4f5d4828fb27791c017490476d84 [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>
Mathias Agopian7b5be952012-04-02 17:02:19 -070020#include <sys/socket.h>
Dan Stoza27c81152017-03-31 16:30:42 -070021#include <sys/types.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 -070030namespace android {
Dan Stoza27c81152017-03-31 16:30:42 -070031namespace gui {
Mathias Agopian5cae0d02011-10-20 18:42:02 -070032
Dan Stoza27c81152017-03-31 16:30:42 -070033// Socket buffer size. The default is typically about 128KB, which is much larger than we really
34// need. So we make it smaller.
Mathias Agopian90ed3e82013-09-09 23:36:25 -070035static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
Mathias Agopian7b5be952012-04-02 17:02:19 -070036
Dan Stoza7d290172017-03-31 17:10:06 -070037BitTube::BitTube(size_t bufsize) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070038 init(bufsize, bufsize);
Mathias Agopian5cae0d02011-10-20 18:42:02 -070039}
40
Dan Stoza7d290172017-03-31 17:10:06 -070041BitTube::BitTube(DefaultSizeType) : BitTube(DEFAULT_SOCKET_BUFFER_SIZE) {}
Mathias Agopian5cae0d02011-10-20 18:42:02 -070042
Dan Stoza7d290172017-03-31 17:10:06 -070043BitTube::BitTube(const Parcel& data) {
44 readFromParcel(&data);
Mathias Agopian5cae0d02011-10-20 18:42:02 -070045}
46
Mathias Agopian90ed3e82013-09-09 23:36:25 -070047void BitTube::init(size_t rcvbuf, size_t sndbuf) {
48 int sockets[2];
49 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) == 0) {
50 size_t size = DEFAULT_SOCKET_BUFFER_SIZE;
51 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
52 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
Dan Stoza27c81152017-03-31 16:30:42 -070053 // since we don't use the "return channel", we keep it small...
Mathias Agopian90ed3e82013-09-09 23:36:25 -070054 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
55 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
56 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
57 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
Dan Stoza7d290172017-03-31 17:10:06 -070058 mReceiveFd.reset(sockets[0]);
59 mSendFd.reset(sockets[1]);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070060 } else {
Dan Stoza7d290172017-03-31 17:10:06 -070061 mReceiveFd.reset();
62 ALOGE("BitTube: pipe creation failed (%s)", strerror(errno));
Mathias Agopian90ed3e82013-09-09 23:36:25 -070063 }
64}
65
Dan Stoza27c81152017-03-31 16:30:42 -070066status_t BitTube::initCheck() const {
Mathias Agopian5cae0d02011-10-20 18:42:02 -070067 if (mReceiveFd < 0) {
68 return status_t(mReceiveFd);
69 }
70 return NO_ERROR;
71}
72
Dan Stoza27c81152017-03-31 16:30:42 -070073int BitTube::getFd() const {
Mathias Agopian5cae0d02011-10-20 18:42:02 -070074 return mReceiveFd;
75}
76
Dan Stoza27c81152017-03-31 16:30:42 -070077int BitTube::getSendFd() const {
Aravind Akella56ae4262014-07-10 16:01:10 -070078 return mSendFd;
79}
80
Dan Stoza6b698e42017-04-03 13:09:08 -070081base::unique_fd BitTube::moveReceiveFd() {
82 return std::move(mReceiveFd);
83}
84
85void BitTube::setReceiveFd(base::unique_fd&& receiveFd) {
86 mReceiveFd = std::move(receiveFd);
87}
88
Dan Stoza27c81152017-03-31 16:30:42 -070089ssize_t BitTube::write(void const* vaddr, size_t size) {
Mathias Agopian5cae0d02011-10-20 18:42:02 -070090 ssize_t err, len;
91 do {
Mathias Agopian7b5be952012-04-02 17:02:19 -070092 len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070093 // cannot return less than size, since we're using SOCK_SEQPACKET
Mathias Agopian5cae0d02011-10-20 18:42:02 -070094 err = len < 0 ? errno : 0;
95 } while (err == EINTR);
96 return err == 0 ? len : -err;
Mathias Agopian5cae0d02011-10-20 18:42:02 -070097}
98
Dan Stoza27c81152017-03-31 16:30:42 -070099ssize_t BitTube::read(void* vaddr, size_t size) {
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700100 ssize_t err, len;
101 do {
Mathias Agopian7b5be952012-04-02 17:02:19 -0700102 len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700103 err = len < 0 ? errno : 0;
104 } while (err == EINTR);
Mathias Agopian3ad38072011-11-29 11:42:32 -0800105 if (err == EAGAIN || err == EWOULDBLOCK) {
Dan Stoza27c81152017-03-31 16:30:42 -0700106 // EAGAIN means that we have non-blocking I/O but there was no data to be read. Nothing the
107 // client should care about.
Mathias Agopian3ad38072011-11-29 11:42:32 -0800108 return 0;
109 }
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700110 return err == 0 ? len : -err;
111}
112
Dan Stoza27c81152017-03-31 16:30:42 -0700113status_t BitTube::writeToParcel(Parcel* reply) const {
114 if (mReceiveFd < 0) return -EINVAL;
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700115
116 status_t result = reply->writeDupFileDescriptor(mReceiveFd);
Dan Stoza7d290172017-03-31 17:10:06 -0700117 mReceiveFd.reset();
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700118 return result;
119}
120
Dan Stoza7d290172017-03-31 17:10:06 -0700121status_t BitTube::readFromParcel(const Parcel* parcel) {
122 mReceiveFd.reset(dup(parcel->readFileDescriptor()));
123 if (mReceiveFd < 0) {
124 mReceiveFd.reset();
125 int error = errno;
126 ALOGE("BitTube::readFromParcel: can't dup file descriptor (%s)", strerror(error));
127 return -error;
128 }
129 return NO_ERROR;
130}
131
Dan Stoza6b698e42017-04-03 13:09:08 -0700132ssize_t BitTube::sendObjects(BitTube* tube, void const* events, size_t count, size_t objSize) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700133 const char* vaddr = reinterpret_cast<const char*>(events);
Dan Stoza27c81152017-03-31 16:30:42 -0700134 ssize_t size = tube->write(vaddr, count * objSize);
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700135
136 // should never happen because of SOCK_SEQPACKET
Dan Stozad723bd72014-11-18 10:24:03 -0800137 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
Dan Stoza27c81152017-03-31 16:30:42 -0700138 "BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were "
139 "sent!)",
140 count, objSize, size);
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700141
Dan Stoza27c81152017-03-31 16:30:42 -0700142 // ALOGE_IF(size<0, "error %d sending %d events", size, count);
Dan Stozad723bd72014-11-18 10:24:03 -0800143 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700144}
145
Dan Stoza6b698e42017-04-03 13:09:08 -0700146ssize_t BitTube::recvObjects(BitTube* tube, void* events, size_t count, size_t objSize) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700147 char* vaddr = reinterpret_cast<char*>(events);
Dan Stoza27c81152017-03-31 16:30:42 -0700148 ssize_t size = tube->read(vaddr, count * objSize);
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700149
150 // should never happen because of SOCK_SEQPACKET
Dan Stozad723bd72014-11-18 10:24:03 -0800151 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
Dan Stoza27c81152017-03-31 16:30:42 -0700152 "BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were "
153 "received!)",
154 count, objSize, size);
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700155
Dan Stoza27c81152017-03-31 16:30:42 -0700156 // ALOGE_IF(size<0, "error %d receiving %d events", size, count);
Dan Stozad723bd72014-11-18 10:24:03 -0800157 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700158}
159
Dan Stoza27c81152017-03-31 16:30:42 -0700160} // namespace gui
161} // namespace android