blob: 55f41784215bd039bf90529c94834493c728546b [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
17#include <stdint.h>
18#include <sys/types.h>
19
Mathias Agopian5cae0d02011-10-20 18:42:02 -070020#include <fcntl.h>
Mathias Agopian99fe3c62012-02-06 19:10:04 -080021#include <signal.h>
22#include <unistd.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070023
24#include <utils/Errors.h>
25
26#include <binder/Parcel.h>
27
28#include <gui/BitTube.h>
29
30namespace android {
31// ----------------------------------------------------------------------------
32
33BitTube::BitTube()
34 : mSendFd(-1), mReceiveFd(-1)
35{
36 int fds[2];
37 if (pipe(fds) == 0) {
38 mReceiveFd = fds[0];
39 mSendFd = fds[1];
40 fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
41 fcntl(mSendFd, F_SETFL, O_NONBLOCK);
Mathias Agopian99fe3c62012-02-06 19:10:04 -080042 // ignore SIGPIPE, we handle write errors through EPIPE instead
43 signal(SIGPIPE, SIG_IGN);
Mathias Agopian5cae0d02011-10-20 18:42:02 -070044 } else {
45 mReceiveFd = -errno;
Steve Blocke6f43dd2012-01-06 19:20:56 +000046 ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd));
Mathias Agopian5cae0d02011-10-20 18:42:02 -070047 }
48}
49
50BitTube::BitTube(const Parcel& data)
51 : mSendFd(-1), mReceiveFd(-1)
52{
53 mReceiveFd = dup(data.readFileDescriptor());
54 if (mReceiveFd >= 0) {
55 fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
56 } else {
57 mReceiveFd = -errno;
Steve Blocke6f43dd2012-01-06 19:20:56 +000058 ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
Mathias Agopian5cae0d02011-10-20 18:42:02 -070059 strerror(-mReceiveFd));
60 }
61}
62
63BitTube::~BitTube()
64{
65 if (mSendFd >= 0)
66 close(mSendFd);
67
68 if (mReceiveFd >= 0)
69 close(mReceiveFd);
70}
71
72status_t BitTube::initCheck() const
73{
74 if (mReceiveFd < 0) {
75 return status_t(mReceiveFd);
76 }
77 return NO_ERROR;
78}
79
80int BitTube::getFd() const
81{
82 return mReceiveFd;
83}
84
85ssize_t BitTube::write(void const* vaddr, size_t size)
86{
87 ssize_t err, len;
88 do {
89 len = ::write(mSendFd, vaddr, size);
90 err = len < 0 ? errno : 0;
91 } while (err == EINTR);
92 return err == 0 ? len : -err;
93
94}
95
96ssize_t BitTube::read(void* vaddr, size_t size)
97{
98 ssize_t err, len;
99 do {
100 len = ::read(mReceiveFd, vaddr, size);
101 err = len < 0 ? errno : 0;
102 } while (err == EINTR);
Mathias Agopian3ad38072011-11-29 11:42:32 -0800103 if (err == EAGAIN || err == EWOULDBLOCK) {
104 // EAGAIN means that we have non-blocking I/O but there was
105 // no data to be read. Nothing the client should care about.
106 return 0;
107 }
Mathias Agopian5cae0d02011-10-20 18:42:02 -0700108 return err == 0 ? len : -err;
109}
110
111status_t BitTube::writeToParcel(Parcel* reply) const
112{
113 if (mReceiveFd < 0)
114 return -EINVAL;
115
116 status_t result = reply->writeDupFileDescriptor(mReceiveFd);
117 close(mReceiveFd);
118 mReceiveFd = -1;
119 return result;
120}
121
122// ----------------------------------------------------------------------------
123}; // namespace android