Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 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 <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Mathias Agopian | 99fe3c6 | 2012-02-06 19:10:04 -0800 | [diff] [blame] | 21 | #include <signal.h> |
| 22 | #include <unistd.h> |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 23 | |
| 24 | #include <utils/Errors.h> |
| 25 | |
| 26 | #include <binder/Parcel.h> |
| 27 | |
| 28 | #include <gui/BitTube.h> |
| 29 | |
| 30 | namespace android { |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | BitTube::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 Agopian | 99fe3c6 | 2012-02-06 19:10:04 -0800 | [diff] [blame] | 42 | // ignore SIGPIPE, we handle write errors through EPIPE instead |
| 43 | signal(SIGPIPE, SIG_IGN); |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 44 | } else { |
| 45 | mReceiveFd = -errno; |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 46 | ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd)); |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | BitTube::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 Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 58 | ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)", |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 59 | strerror(-mReceiveFd)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | BitTube::~BitTube() |
| 64 | { |
| 65 | if (mSendFd >= 0) |
| 66 | close(mSendFd); |
| 67 | |
| 68 | if (mReceiveFd >= 0) |
| 69 | close(mReceiveFd); |
| 70 | } |
| 71 | |
| 72 | status_t BitTube::initCheck() const |
| 73 | { |
| 74 | if (mReceiveFd < 0) { |
| 75 | return status_t(mReceiveFd); |
| 76 | } |
| 77 | return NO_ERROR; |
| 78 | } |
| 79 | |
| 80 | int BitTube::getFd() const |
| 81 | { |
| 82 | return mReceiveFd; |
| 83 | } |
| 84 | |
| 85 | ssize_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 | |
| 96 | ssize_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 Agopian | 3ad3807 | 2011-11-29 11:42:32 -0800 | [diff] [blame] | 103 | 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 Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 108 | return err == 0 ? len : -err; |
| 109 | } |
| 110 | |
| 111 | status_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 |