pipe_unix.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2010 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "pipe_unix.h"
  15. #include <sys/socket.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #define HANDLE_EINTR(x) ({ \
  19. typeof(x) __eintr_result__; \
  20. do { \
  21. __eintr_result__ = x; \
  22. } while (__eintr_result__ == -1 && errno == EINTR); \
  23. __eintr_result__;\
  24. })
  25. namespace {
  26. bool SilenceSocket(int fd) {
  27. int nosigpipe = 1;
  28. // On OSX an attempt to read or write to a closed socket may generate a
  29. // SIGPIPE rather than returning -1. setsockopt will shut this off.
  30. if (0 != setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE,
  31. &nosigpipe, sizeof nosigpipe)) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. size_t ReadFromFD(int fd, char* buffer, size_t bytes) {
  37. ssize_t bytes_read =
  38. HANDLE_EINTR(read(fd, buffer, bytes));
  39. if (bytes_read < 0) {
  40. return -1;
  41. }
  42. return bytes_read;
  43. }
  44. size_t WriteToFD(int fd, const char* data, size_t size) {
  45. // Allow for partial writes.
  46. ssize_t written_total = 0;
  47. for (ssize_t written_partial = 0; written_total < size; written_total += written_partial) {
  48. written_partial =
  49. HANDLE_EINTR(write(fd, data + written_total, size - written_total));
  50. if (written_partial < 0) {
  51. return -1;
  52. }
  53. }
  54. return written_total;
  55. }
  56. } // namespace
  57. PipePair::PipePair() {
  58. fd_[0] = -1;
  59. fd_[1] = -1;
  60. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd_) !=0) {
  61. return;
  62. }
  63. };
  64. PipeUnix::PipeUnix() : fd_(-1) {
  65. }
  66. bool PipeUnix::OpenClient(int fd) {
  67. if (!SilenceSocket(fd)) {
  68. return false;
  69. }
  70. fd_ = fd;
  71. return true;
  72. }
  73. bool PipeUnix::OpenServer(int fd) {
  74. if (!SilenceSocket(fd)) {
  75. return false;
  76. }
  77. fd_ = fd;
  78. return true;
  79. }
  80. bool PipeUnix::Write(const void* buf, size_t sz) {
  81. if (sz == -1) {
  82. return false;
  83. }
  84. size_t written = WriteToFD(fd_, static_cast<const char*> (buf), sz);
  85. return (sz == written);
  86. }
  87. bool PipeUnix::Read(void* buf, size_t* sz) {
  88. size_t read = ReadFromFD(fd_, static_cast<char*> (buf), *sz);
  89. if (read == -1) {
  90. return false;
  91. }
  92. *sz = read;
  93. return true;
  94. }
  95. char* PipeTransport::Receive(size_t* size) {
  96. if (buf_.size() < kBufferSz) {
  97. buf_.resize(kBufferSz);
  98. }
  99. *size = kBufferSz;
  100. if (!Read(&buf_[0], size)) {
  101. return NULL;
  102. }
  103. return &buf_[0];
  104. }