• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <private/gui/BitTube.h>
18 
19 #include <stdint.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 
23 #include <fcntl.h>
24 #include <unistd.h>
25 
26 #include <utils/Errors.h>
27 
28 #include <binder/Parcel.h>
29 
30 namespace android {
31 namespace gui {
32 
33 // Socket buffer size.  The default is typically about 128KB, which is much larger than we really
34 // need. So we make it smaller.
35 static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
36 
BitTube(size_t bufsize)37 BitTube::BitTube(size_t bufsize) {
38     init(bufsize, bufsize);
39 }
40 
BitTube(DefaultSizeType)41 BitTube::BitTube(DefaultSizeType) : BitTube(DEFAULT_SOCKET_BUFFER_SIZE) {}
42 
BitTube(const Parcel & data)43 BitTube::BitTube(const Parcel& data) {
44     readFromParcel(&data);
45 }
46 
init(size_t rcvbuf,size_t sndbuf)47 void 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));
53         // since we don't use the "return channel", we keep it small...
54         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);
58         mReceiveFd.reset(sockets[0]);
59         mSendFd.reset(sockets[1]);
60     } else {
61         mReceiveFd.reset();
62         ALOGE("BitTube: pipe creation failed (%s)", strerror(errno));
63     }
64 }
65 
initCheck() const66 status_t BitTube::initCheck() const {
67     if (mReceiveFd < 0) {
68         return status_t(mReceiveFd);
69     }
70     return NO_ERROR;
71 }
72 
getFd() const73 int BitTube::getFd() const {
74     return mReceiveFd;
75 }
76 
getSendFd() const77 int BitTube::getSendFd() const {
78     return mSendFd;
79 }
80 
moveReceiveFd()81 base::unique_fd BitTube::moveReceiveFd() {
82     return std::move(mReceiveFd);
83 }
84 
setReceiveFd(base::unique_fd && receiveFd)85 void BitTube::setReceiveFd(base::unique_fd&& receiveFd) {
86     mReceiveFd = std::move(receiveFd);
87 }
88 
setSendFd(base::unique_fd && sendFd)89 void BitTube::setSendFd(base::unique_fd&& sendFd) {
90     mSendFd = std::move(sendFd);
91 }
92 
write(void const * vaddr,size_t size)93 ssize_t BitTube::write(void const* vaddr, size_t size) {
94     ssize_t err, len;
95     do {
96         len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
97         // cannot return less than size, since we're using SOCK_SEQPACKET
98         err = len < 0 ? errno : 0;
99     } while (err == EINTR);
100     return err == 0 ? len : -err;
101 }
102 
read(void * vaddr,size_t size)103 ssize_t BitTube::read(void* vaddr, size_t size) {
104     ssize_t err, len;
105     do {
106         len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
107         err = len < 0 ? errno : 0;
108     } while (err == EINTR);
109     if (err == EAGAIN || err == EWOULDBLOCK) {
110         // EAGAIN means that we have non-blocking I/O but there was no data to be read. Nothing the
111         // client should care about.
112         return 0;
113     }
114     return err == 0 ? len : -err;
115 }
116 
writeToParcel(Parcel * reply) const117 status_t BitTube::writeToParcel(Parcel* reply) const {
118     if (mReceiveFd < 0) return -EINVAL;
119 
120     status_t result = reply->writeDupFileDescriptor(mReceiveFd);
121     mReceiveFd.reset();
122     if (result != NO_ERROR) {
123         return result;
124     }
125     result = reply->writeDupFileDescriptor(mSendFd);
126     mSendFd.reset();
127     return result;
128 }
129 
readFromParcel(const Parcel * parcel)130 status_t BitTube::readFromParcel(const Parcel* parcel) {
131     mReceiveFd.reset(dup(parcel->readFileDescriptor()));
132     if (mReceiveFd < 0) {
133         mReceiveFd.reset();
134         int error = errno;
135         ALOGE("BitTube::readFromParcel: can't dup file descriptor (%s)", strerror(error));
136         return -error;
137     }
138     mSendFd.reset(dup(parcel->readFileDescriptor()));
139     if (mSendFd < 0) {
140         mSendFd.reset();
141         int error = errno;
142         ALOGE("BitTube::readFromParcel: can't dup file descriptor (%s)", strerror(error));
143         return -error;
144     }
145     return NO_ERROR;
146 }
147 
sendObjects(BitTube * tube,void const * events,size_t count,size_t objSize)148 ssize_t BitTube::sendObjects(BitTube* tube, void const* events, size_t count, size_t objSize) {
149     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
153     LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
154                         "BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were "
155                         "sent!)",
156                         count, objSize, size);
157 
158     // ALOGE_IF(size<0, "error %d sending %d events", size, count);
159     return size < 0 ? size : size / static_cast<ssize_t>(objSize);
160 }
161 
recvObjects(BitTube * tube,void * events,size_t count,size_t objSize)162 ssize_t BitTube::recvObjects(BitTube* tube, void* events, size_t count, size_t objSize) {
163     char* vaddr = reinterpret_cast<char*>(events);
164     ssize_t size = tube->read(vaddr, count * objSize);
165 
166     // should never happen because of SOCK_SEQPACKET
167     LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
168                         "BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were "
169                         "received!)",
170                         count, objSize, size);
171 
172     // ALOGE_IF(size<0, "error %d receiving %d events", size, count);
173     return size < 0 ? size : size / static_cast<ssize_t>(objSize);
174 }
175 
176 } // namespace gui
177 } // namespace android
178