IPCWindows.cpp 6.7 KB

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