IPCUnix.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifndef ATOMIC_PLATFORM_WINDOWS
  23. #include "IPCUnix.h"
  24. namespace Atomic
  25. {
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <sys/socket.h>
  29. #include <errno.h>
  30. #ifdef ATOMIC_PLATFORM_OSX
  31. #include <libproc.h>
  32. #endif
  33. #define HANDLE_EINTR(x) ({ \
  34. typeof(x) __eintr_result__; \
  35. do { \
  36. __eintr_result__ = x; \
  37. } while (__eintr_result__ == -1 && errno == EINTR); \
  38. __eintr_result__;\
  39. })
  40. bool SilenceSocket(int fd) {
  41. #ifdef ATOMIC_PLATFORM_OSX
  42. int nosigpipe = 1;
  43. // On OSX an attempt to read or write to a closed socket may generate a
  44. // SIGPIPE rather than returning -1. setsockopt will shut this off.
  45. if (0 != setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE,
  46. &nosigpipe, sizeof nosigpipe)) {
  47. return false;
  48. }
  49. #endif
  50. return true;
  51. }
  52. size_t ReadFromFD(int fd, char* buffer, size_t bytes) {
  53. fd_set set;
  54. struct timeval timeout;
  55. FD_ZERO(&set);
  56. FD_SET(fd, &set);
  57. timeout.tv_sec = 0;
  58. // 100ms
  59. timeout.tv_usec = 100000;
  60. // check if there is anything to read
  61. int rv = select(fd + 1, &set, NULL, NULL, &timeout);
  62. if (rv < 0)
  63. return -1;
  64. if (!rv)
  65. return 0;
  66. ssize_t bytes_read =
  67. HANDLE_EINTR(read(fd, buffer, bytes));
  68. if (bytes_read < 0) {
  69. return -1;
  70. }
  71. return bytes_read;
  72. }
  73. size_t WriteToFD(int fd, const char* data, size_t size) {
  74. // Allow for partial writes.
  75. ssize_t written_total = 0;
  76. for (ssize_t written_partial = 0; written_total < size; written_total += written_partial) {
  77. written_partial =
  78. HANDLE_EINTR(write(fd, data + written_total, size - written_total));
  79. if (written_partial < 0) {
  80. return -1;
  81. }
  82. }
  83. return written_total;
  84. }
  85. PipePair::PipePair() {
  86. fd_[0] = -1;
  87. fd_[1] = -1;
  88. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd_) !=0) {
  89. return;
  90. }
  91. };
  92. PipeUnix::PipeUnix() : fd_(-1) {
  93. }
  94. bool PipeUnix::OpenClient(int fd) {
  95. if (!SilenceSocket(fd)) {
  96. return false;
  97. }
  98. fd_ = fd;
  99. return true;
  100. }
  101. bool PipeUnix::OpenServer(int fd) {
  102. if (!SilenceSocket(fd)) {
  103. return false;
  104. }
  105. fd_ = fd;
  106. return true;
  107. }
  108. bool PipeUnix::Write(const void* buf, size_t sz) {
  109. if (sz == -1) {
  110. return false;
  111. }
  112. size_t written = WriteToFD(fd_, static_cast<const char*> (buf), sz);
  113. return (sz == written);
  114. }
  115. bool PipeUnix::Read(void* buf, size_t* sz) {
  116. size_t read = ReadFromFD(fd_, static_cast<char*> (buf), *sz);
  117. if (read == -1) {
  118. return false;
  119. }
  120. *sz = read;
  121. return true;
  122. }
  123. char* PipeTransport::Receive(size_t* size) {
  124. if (buf_.Size() < kBufferSz) {
  125. buf_.Resize(kBufferSz);
  126. }
  127. *size = kBufferSz;
  128. if (!Read(&buf_[0], size)) {
  129. return NULL;
  130. }
  131. return &buf_[0];
  132. }
  133. IPCProcess::IPCProcess(Context* context, int fd1, int fd2, int pid) : Object(context),
  134. pid_(pid),
  135. fd1_(fd1),
  136. fd2_(fd2)
  137. {
  138. }
  139. IPCProcess::~IPCProcess()
  140. {
  141. }
  142. bool IPCProcess::IsRunning()
  143. {
  144. if (pid_ == -1)
  145. return false;
  146. #ifdef __APPLE__
  147. char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
  148. int ret = proc_pidpath (pid_, pathbuf, sizeof(pathbuf));
  149. if ( ret > 0 )
  150. {
  151. return true;
  152. }
  153. #else
  154. // this doesn't seem to work on OSX?
  155. if (kill(pid_, 0) == 0)
  156. return true;
  157. #endif
  158. return false;
  159. }
  160. bool IPCProcess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  161. {
  162. assert(pid_ == -1);
  163. // We must not allocated memory after fork(),
  164. // therefore allocate all required buffers first.
  165. PODVector<char*> argv;
  166. argv.Resize(args.Size() + 2);
  167. int i = 0;
  168. argv[i++] = const_cast<char*>(command.CString());
  169. for (unsigned j = 0; j < args.Size(); j++)
  170. {
  171. argv[i++] = const_cast<char*>(args[j].CString());
  172. }
  173. argv[i] = NULL;
  174. const char* pInitialDirectory = initialDirectory.Empty() ? 0 : initialDirectory.CString();
  175. int pid = fork();
  176. if (pid < 0)
  177. {
  178. // error forking
  179. return false;
  180. }
  181. else if (pid == 0)
  182. {
  183. // child process
  184. if (pInitialDirectory)
  185. {
  186. if (chdir(pInitialDirectory) != 0)
  187. {
  188. _exit(72);
  189. }
  190. }
  191. // close all open file descriptors other than stdin, stdout, stderr
  192. // and the IPC child fd
  193. for (int i = 3; i < getdtablesize(); ++i)
  194. {
  195. if (i != fd2())
  196. close(i);
  197. }
  198. execvp(argv[0], &argv[0]);
  199. _exit(72);
  200. }
  201. else
  202. {
  203. // parent process
  204. pid_ = pid;
  205. }
  206. return true;
  207. }
  208. }
  209. #endif