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 
write(void const * vaddr,size_t size)89 ssize_t BitTube::write(void const* vaddr, size_t size) {
90     ssize_t err, len;
91     do {
92         len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
93         // cannot return less than size, since we're using SOCK_SEQPACKET
94         err = len < 0 ? errno : 0;
95     } while (err == EINTR);
96     return err == 0 ? len : -err;
97 }
98 
read(void * vaddr,size_t size)99 ssize_t BitTube::read(void* vaddr, size_t size) {
100     ssize_t err, len;
101     do {
102         len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
103         err = len < 0 ? errno : 0;
104     } while (err == EINTR);
105     if (err == EAGAIN || err == EWOULDBLOCK) {
106         // EAGAIN means that we have non-blocking I/O but there was no data to be read. Nothing the
107         // client should care about.
108         return 0;
109     }
110     return err == 0 ? len : -err;
111 }
112 
writeToParcel(Parcel * reply) const113 status_t BitTube::writeToParcel(Parcel* reply) const {
114     if (mReceiveFd < 0) return -EINVAL;
115 
116     status_t result = reply->writeDupFileDescriptor(mReceiveFd);
117     mReceiveFd.reset();
118     return result;
119 }
120 
readFromParcel(const Parcel * parcel)121 status_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 
sendObjects(BitTube * tube,void const * events,size_t count,size_t objSize)132 ssize_t BitTube::sendObjects(BitTube* tube, void const* events, size_t count, size_t objSize) {
133     const char* vaddr = reinterpret_cast<const char*>(events);
134     ssize_t size = tube->write(vaddr, count * objSize);
135 
136     // should never happen because of SOCK_SEQPACKET
137     LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
138                         "BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were "
139                         "sent!)",
140                         count, objSize, size);
141 
142     // ALOGE_IF(size<0, "error %d sending %d events", size, count);
143     return size < 0 ? size : size / static_cast<ssize_t>(objSize);
144 }
145 
recvObjects(BitTube * tube,void * events,size_t count,size_t objSize)146 ssize_t BitTube::recvObjects(BitTube* tube, void* events, size_t count, size_t objSize) {
147     char* vaddr = reinterpret_cast<char*>(events);
148     ssize_t size = tube->read(vaddr, count * objSize);
149 
150     // should never happen because of SOCK_SEQPACKET
151     LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
152                         "BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were "
153                         "received!)",
154                         count, objSize, size);
155 
156     // ALOGE_IF(size<0, "error %d receiving %d events", size, count);
157     return size < 0 ? size : size / static_cast<ssize_t>(objSize);
158 }
159 
160 } // namespace gui
161 } // namespace android
162