IPCUnix.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef ATOMIC_PLATFORM_WINDOWS
  2. #include "IPCUnix.h"
  3. namespace Atomic
  4. {
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <sys/socket.h>
  8. #include <libproc.h>
  9. #include <errno.h>
  10. #define HANDLE_EINTR(x) ({ \
  11. typeof(x) __eintr_result__; \
  12. do { \
  13. __eintr_result__ = x; \
  14. } while (__eintr_result__ == -1 && errno == EINTR); \
  15. __eintr_result__;\
  16. })
  17. bool SilenceSocket(int fd) {
  18. int nosigpipe = 1;
  19. // On OSX an attempt to read or write to a closed socket may generate a
  20. // SIGPIPE rather than returning -1. setsockopt will shut this off.
  21. if (0 != setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE,
  22. &nosigpipe, sizeof nosigpipe)) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. size_t ReadFromFD(int fd, char* buffer, size_t bytes) {
  28. fd_set set;
  29. struct timeval timeout;
  30. FD_ZERO(&set);
  31. FD_SET(fd, &set);
  32. timeout.tv_sec = 0;
  33. // 100ms
  34. timeout.tv_usec = 100000;
  35. // check if there is anything to read
  36. int rv = select(fd + 1, &set, NULL, NULL, &timeout);
  37. if (rv < 0)
  38. return -1;
  39. if (!rv)
  40. return 0;
  41. ssize_t bytes_read =
  42. HANDLE_EINTR(read(fd, buffer, bytes));
  43. if (bytes_read < 0) {
  44. return -1;
  45. }
  46. return bytes_read;
  47. }
  48. size_t WriteToFD(int fd, const char* data, size_t size) {
  49. // Allow for partial writes.
  50. ssize_t written_total = 0;
  51. for (ssize_t written_partial = 0; written_total < size; written_total += written_partial) {
  52. written_partial =
  53. HANDLE_EINTR(write(fd, data + written_total, size - written_total));
  54. if (written_partial < 0) {
  55. return -1;
  56. }
  57. }
  58. return written_total;
  59. }
  60. PipePair::PipePair() {
  61. fd_[0] = -1;
  62. fd_[1] = -1;
  63. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd_) !=0) {
  64. return;
  65. }
  66. };
  67. PipeUnix::PipeUnix() : fd_(-1) {
  68. }
  69. bool PipeUnix::OpenClient(int fd) {
  70. if (!SilenceSocket(fd)) {
  71. return false;
  72. }
  73. fd_ = fd;
  74. return true;
  75. }
  76. bool PipeUnix::OpenServer(int fd) {
  77. if (!SilenceSocket(fd)) {
  78. return false;
  79. }
  80. fd_ = fd;
  81. return true;
  82. }
  83. bool PipeUnix::Write(const void* buf, size_t sz) {
  84. if (sz == -1) {
  85. return false;
  86. }
  87. size_t written = WriteToFD(fd_, static_cast<const char*> (buf), sz);
  88. return (sz == written);
  89. }
  90. bool PipeUnix::Read(void* buf, size_t* sz) {
  91. size_t read = ReadFromFD(fd_, static_cast<char*> (buf), *sz);
  92. if (read == -1) {
  93. return false;
  94. }
  95. *sz = read;
  96. return true;
  97. }
  98. char* PipeTransport::Receive(size_t* size) {
  99. if (buf_.Size() < kBufferSz) {
  100. buf_.Resize(kBufferSz);
  101. }
  102. *size = kBufferSz;
  103. if (!Read(&buf_[0], size)) {
  104. return NULL;
  105. }
  106. return &buf_[0];
  107. }
  108. IPCProcess::IPCProcess(Context* context, int fd1, int fd2, int pid) : Object(context),
  109. pid_(pid),
  110. fd1_(fd1),
  111. fd2_(fd2)
  112. {
  113. }
  114. IPCProcess::~IPCProcess()
  115. {
  116. }
  117. bool IPCProcess::IsRunning()
  118. {
  119. if (pid_ == -1)
  120. return false;
  121. #ifdef __APPLE__
  122. char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
  123. int ret = proc_pidpath (pid_, pathbuf, sizeof(pathbuf));
  124. if ( ret > 0 )
  125. {
  126. return true;
  127. }
  128. #else
  129. // this doesn't seem to work on OSX?
  130. if (kill(pid_, 0) == 0)
  131. return true;
  132. #endif
  133. return false;
  134. }
  135. bool IPCProcess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  136. {
  137. assert(pid_ == -1);
  138. // We must not allocated memory after fork(),
  139. // therefore allocate all required buffers first.
  140. PODVector<char*> argv;
  141. argv.Resize(args.Size() + 2);
  142. int i = 0;
  143. argv[i++] = const_cast<char*>(command.CString());
  144. for (unsigned j = 0; j < args.Size(); j++)
  145. {
  146. argv[i++] = const_cast<char*>(args[j].CString());
  147. }
  148. argv[i] = NULL;
  149. const char* pInitialDirectory = initialDirectory.Empty() ? 0 : initialDirectory.CString();
  150. int pid = fork();
  151. if (pid < 0)
  152. {
  153. // error forking
  154. return false;
  155. }
  156. else if (pid == 0)
  157. {
  158. // child process
  159. if (pInitialDirectory)
  160. {
  161. if (chdir(pInitialDirectory) != 0)
  162. {
  163. _exit(72);
  164. }
  165. }
  166. // close all open file descriptors other than stdin, stdout, stderr
  167. // and the IPC child fd
  168. for (int i = 3; i < getdtablesize(); ++i)
  169. {
  170. if (i != fd2())
  171. close(i);
  172. }
  173. execvp(argv[0], &argv[0]);
  174. _exit(72);
  175. }
  176. else
  177. {
  178. // parent process
  179. pid_ = pid;
  180. }
  181. return true;
  182. }
  183. }
  184. #endif