IPCUnix.cpp 6.0 KB

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