IPCWindows.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #include <Windows.h>
  24. #include <Sddl.h>
  25. #include <AccCtrl.h>
  26. #include <Aclapi.h>
  27. #include <string>
  28. #include "../Core/Timer.h"
  29. #include "IPCWindows.h"
  30. typedef std::wstring IPCWString;
  31. namespace Atomic
  32. {
  33. static const wchar_t kPipePrefix[] = L"\\\\.\\pipe\\";
  34. static const int kPipeBufferSz = 4 * 1024;
  35. static LONG g_pipe_seq = 0;
  36. HANDLE PipePair::OpenPipeServer(const wchar_t* name, bool read)
  37. {
  38. IPCWString pipename(kPipePrefix);
  39. pipename.append(name);
  40. DWORD openMode = read ? PIPE_ACCESS_INBOUND : PIPE_ACCESS_OUTBOUND;
  41. return ::CreateNamedPipeW(pipename.c_str(), openMode,
  42. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
  43. 1, kPipeBufferSz, kPipeBufferSz, 200, NULL);
  44. }
  45. HANDLE PipePair::OpenPipeClient(const wchar_t* name, bool read)
  46. {
  47. IPCWString pipename(kPipePrefix);
  48. pipename.append(name);
  49. SECURITY_ATTRIBUTES sa;
  50. sa.bInheritHandle = TRUE;
  51. sa.lpSecurityDescriptor = NULL;
  52. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  53. DWORD accessMode = read ? GENERIC_READ : GENERIC_WRITE;
  54. for (;;) {
  55. HANDLE pipe = ::CreateFileW(pipename.c_str(), accessMode, 0, &sa,
  56. OPEN_EXISTING, 0, NULL);
  57. if (INVALID_HANDLE_VALUE == pipe) {
  58. if (ERROR_PIPE_BUSY != ::GetLastError()) {
  59. return pipe;
  60. }
  61. // wait and retry.
  62. ::Sleep(25);
  63. }
  64. else {
  65. // success.
  66. return pipe;
  67. }
  68. }
  69. }
  70. PipePair::PipePair() :
  71. srvRead_(INVALID_IPCHANDLE_VALUE),
  72. srvWrite_(INVALID_IPCHANDLE_VALUE),
  73. clnRead_(INVALID_IPCHANDLE_VALUE),
  74. clnWrite_(INVALID_IPCHANDLE_VALUE)
  75. {
  76. // Come up with a reasonable unique name.
  77. const wchar_t kPipePattern[] = L"ko.%x.%x.%x";
  78. wchar_t serverReadName[8 * 3 + sizeof(kPipePattern)];
  79. ::wsprintfW(serverReadName, kPipePattern, ::GetCurrentProcessId(), ::GetTickCount(),
  80. ::InterlockedIncrement(&g_pipe_seq));
  81. wchar_t serverWriteName[8 * 3 + sizeof(kPipePattern)];
  82. ::wsprintfW(serverWriteName, kPipePattern, ::GetCurrentProcessId(), ::GetTickCount() + 1,
  83. ::InterlockedIncrement(&g_pipe_seq));
  84. srvRead_ = OpenPipeServer(serverReadName, true);
  85. srvWrite_ = OpenPipeServer(serverWriteName, false);
  86. // Don't allow client impersonation.
  87. clnRead_ = OpenPipeClient(serverWriteName, true);
  88. clnWrite_ = OpenPipeClient(serverReadName, false);
  89. /*
  90. if (INVALID_HANDLE_VALUE == client)
  91. {
  92. ::CloseHandle(server);
  93. return;
  94. }
  95. */
  96. if (!::ConnectNamedPipe(srvRead_, NULL))
  97. {
  98. if (ERROR_PIPE_CONNECTED != ::GetLastError())
  99. {
  100. // ::CloseHandle(server);
  101. //::CloseHandle(client);
  102. return;
  103. }
  104. }
  105. if (!::ConnectNamedPipe(srvWrite_, NULL))
  106. {
  107. if (ERROR_PIPE_CONNECTED != ::GetLastError())
  108. {
  109. // ::CloseHandle(server);
  110. //::CloseHandle(client);
  111. return;
  112. }
  113. }
  114. }
  115. PipeWin::PipeWin() : pipeRead_(INVALID_IPCHANDLE_VALUE), pipeWrite_(INVALID_IPCHANDLE_VALUE), readerThread_(this)
  116. {
  117. }
  118. PipeWin::~PipeWin()
  119. {
  120. readerThread_.Kill();
  121. if (pipeRead_ != INVALID_HANDLE_VALUE)
  122. {
  123. ::DisconnectNamedPipe(pipeRead_); // $$$ disconect is valid on the server side.
  124. ::CloseHandle(pipeRead_);
  125. }
  126. if (pipeWrite_ != INVALID_HANDLE_VALUE)
  127. {
  128. ::DisconnectNamedPipe(pipeWrite_); // $$$ disconect is valid on the server side.
  129. ::CloseHandle(pipeWrite_);
  130. }
  131. }
  132. bool PipeWin::OpenClient(IPCHandle pipeRead, IPCHandle pipeWrite)
  133. {
  134. pipeRead_ = pipeRead;
  135. pipeWrite_ = pipeWrite;
  136. readerThread_.Run();
  137. return true;
  138. }
  139. bool PipeWin::OpenServer(IPCHandle pipeRead, IPCHandle pipeWrite)
  140. {
  141. pipeRead_ = pipeRead;
  142. pipeWrite_ = pipeWrite;
  143. readerThread_.Run();
  144. return true;
  145. }
  146. bool PipeWin::Write(const void* buf, size_t sz)
  147. {
  148. DWORD written = 0;
  149. if (TRUE == ::WriteFile(pipeWrite_, buf, (DWORD) sz, &written, NULL))
  150. return true;
  151. return false;
  152. }
  153. void PipeWin::ReaderThread::Kill()
  154. {
  155. if (handle_)
  156. {
  157. BOOL result = TerminateThread((HANDLE)handle_, 0);
  158. result = CloseHandle((HANDLE)handle_);
  159. handle_ = 0;
  160. }
  161. }
  162. void PipeWin::ReaderThread::ThreadFunction()
  163. {
  164. while(shouldRun_)
  165. {
  166. if (readSize_)
  167. continue;
  168. DWORD bytesRead = 0;
  169. if (TRUE == ::ReadFile(pipeWin_->pipeRead_, &buf_[0], 4096, &bytesRead, NULL))
  170. {
  171. readSize_ = (unsigned) bytesRead;
  172. }
  173. Time::Sleep(100);
  174. }
  175. }
  176. bool PipeWin::Read(void* buf, size_t* sz)
  177. {
  178. *sz = 0;
  179. if (readerThread_.readSize_)
  180. {
  181. memcpy(buf, &readerThread_.buf_[0], readerThread_.readSize_);
  182. *sz = readerThread_.readSize_;
  183. readerThread_.readSize_ = 0;
  184. }
  185. return true;
  186. }
  187. char* PipeTransport::Receive(size_t* size)
  188. {
  189. if (buf_.Size() < kBufferSz)
  190. {
  191. buf_.Resize(kBufferSz);
  192. }
  193. *size = kBufferSz;
  194. if (!Read(&buf_[0], size))
  195. {
  196. return NULL;
  197. }
  198. return &buf_[0];
  199. }
  200. IPCProcess::IPCProcess(Context* context, IPCHandle clientRead, IPCHandle clientWrite, IPCHandle pid) : Object(context),
  201. pid_(pid),
  202. clientRead_(clientRead),
  203. clientWrite_(clientWrite)
  204. {
  205. }
  206. IPCProcess::~IPCProcess()
  207. {
  208. }
  209. bool IPCProcess::IsRunning()
  210. {
  211. DWORD exitCode;
  212. if (!GetExitCodeProcess(pid_, &exitCode))
  213. return false;
  214. return exitCode == STILL_ACTIVE;
  215. }
  216. bool IPCProcess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  217. {
  218. STARTUPINFOW si = { sizeof(si) };
  219. PROCESS_INFORMATION pi = { 0 };
  220. // CreateProcess wants a single string
  221. String sargs;
  222. sargs.Join(args, " ");
  223. // convert to wide
  224. WString wcommand(command);
  225. // prepend the command and convert to wide
  226. WString wargs("\"" + command + "\" " + sargs);
  227. // The child process inherits the pipe handle.
  228. if (!::CreateProcessW(wcommand.CString(), (LPWSTR) wargs.CString(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
  229. return false;
  230. }
  231. pid_ = pi.hProcess;
  232. ::CloseHandle(pi.hThread);
  233. return true;
  234. }
  235. }
  236. #endif