IPCWindows.cpp 5.7 KB

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