cmdlinerunner.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at",
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License
  14. #define CMDLINERUNNER_EXPORTS
  15. #include <windows.h>
  16. #include <tchar.h>
  17. #include <strsafe.h>
  18. #include "cmdlinerunner.h"
  19. #define LINESIZE 1024
  20. #ifdef WITH_DEBUG
  21. #include <stdio.h>
  22. #define DEBUGV(...) do { fprintf(stderr, __VA_ARG__); } while(0)
  23. #else
  24. #define DEBUGV(...)
  25. #endif
  26. struct proc_instance_s {
  27. PROCESS_INFORMATION child_process;
  28. HANDLE pipe_server_handle;
  29. HANDLE pipe_client_handle;
  30. };
  31. #ifdef WITH_DEBUG
  32. static void print_last_error(void)
  33. {
  34. DWORD dw;
  35. TCHAR errmsg[LINESIZE];
  36. dw = GetLastError();
  37. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  38. NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  39. errmsg, sizeof(errmsg) - 1, NULL );
  40. DEBUGV("error %d: %s\n", dw, errmsg);
  41. }
  42. #define PRINT_LAST_ERROR() print_last_error()
  43. #else
  44. #define PRINT_LAST_ERROR()
  45. #endif
  46. static proc_instance_t *proc_instance_allocate(void)
  47. {
  48. return (proc_instance_t*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(proc_instance_t));
  49. }
  50. static void proc_instance_free(proc_instance_t *instance)
  51. {
  52. if (instance->pipe_server_handle) {
  53. CloseHandle(instance->pipe_server_handle);
  54. }
  55. if (instance->pipe_client_handle) {
  56. CloseHandle(instance->pipe_client_handle);
  57. }
  58. if (instance->child_process.hProcess) {
  59. TerminateProcess(instance->child_process.hProcess, 1);
  60. CloseHandle(instance->child_process.hProcess);
  61. CloseHandle(instance->child_process.hThread);
  62. }
  63. HeapFree(GetProcessHeap(), 0, instance);
  64. }
  65. void proc_end(proc_instance_t *inst)
  66. {
  67. if (inst == NULL) {
  68. return;
  69. }
  70. proc_instance_free(inst);
  71. }
  72. CMDLINERUNNER_API proc_instance_t * proc_start(LPCTSTR cmdline, LPCTSTR workdir)
  73. {
  74. proc_instance_t *inst = proc_instance_allocate();
  75. if (inst == NULL) {
  76. return NULL;
  77. }
  78. SECURITY_ATTRIBUTES sec_attr = {
  79. .nLength = sizeof(SECURITY_ATTRIBUTES),
  80. .bInheritHandle = TRUE,
  81. .lpSecurityDescriptor = NULL
  82. };
  83. LPCTSTR pipename = TEXT("\\\\.\\pipe\\cmdlinerunner_pipe");
  84. inst->pipe_server_handle = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
  85. PIPE_TYPE_BYTE | PIPE_WAIT, 1, 1024 * 16, 1024 * 16,
  86. NMPWAIT_WAIT_FOREVER, &sec_attr);
  87. if (inst->pipe_server_handle == INVALID_HANDLE_VALUE) {
  88. DEBUGV("inst->pipe_server_handle == INVALID_HANDLE_VALUE\n");
  89. goto error;
  90. }
  91. inst->pipe_client_handle = CreateFile(pipename, GENERIC_WRITE | GENERIC_READ,
  92. 0, &sec_attr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  93. if (inst->pipe_client_handle == INVALID_HANDLE_VALUE) {
  94. DEBUGV("inst->pipe_client_handle == INVALID_HANDLE_VALUE\n");
  95. goto error;
  96. }
  97. DWORD new_mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
  98. if (!SetNamedPipeHandleState(inst->pipe_server_handle, &new_mode, NULL,
  99. NULL)) {
  100. DEBUGV("SetNamedPipeHandleState failed\n");
  101. goto error;
  102. }
  103. if (!SetHandleInformation(inst->pipe_server_handle, HANDLE_FLAG_INHERIT, 0)) {
  104. DEBUGV("SetHandleInformation failed\n");
  105. goto error;
  106. }
  107. if (!SetHandleInformation(inst->pipe_client_handle, HANDLE_FLAG_INHERIT,
  108. HANDLE_FLAG_INHERIT)) {
  109. DEBUGV("SetHandleInformation failed\n");
  110. goto error;
  111. }
  112. STARTUPINFO siStartInfo = {
  113. .cb = sizeof(STARTUPINFO),
  114. .hStdError = inst->pipe_client_handle,
  115. .hStdOutput = inst->pipe_client_handle,
  116. .hStdInput = inst->pipe_client_handle,
  117. .dwFlags = STARTF_USESTDHANDLES
  118. };
  119. size_t workdir_len = 0;
  120. StringCbLength(workdir, STRSAFE_MAX_CCH * sizeof(TCHAR), &workdir_len);
  121. if (workdir_len == 0) {
  122. workdir = NULL;
  123. }
  124. TCHAR cmdline_tmp[LINESIZE];
  125. StringCbCopy(cmdline_tmp, sizeof(cmdline_tmp), cmdline);
  126. if (!CreateProcess(NULL, cmdline_tmp,
  127. NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, workdir, &siStartInfo,
  128. &inst->child_process)) {
  129. DEBUGV("CreateProcess failed\n");
  130. goto error;
  131. }
  132. return inst;
  133. error:
  134. PRINT_LAST_ERROR();
  135. proc_instance_free(inst);
  136. return NULL;
  137. }
  138. int proc_get_exit_code(proc_instance_t *inst)
  139. {
  140. DWORD result;
  141. if (!GetExitCodeProcess(inst->child_process.hProcess, &result)) {
  142. return -2;
  143. }
  144. if (result == STILL_ACTIVE) {
  145. return -1;
  146. }
  147. return (int) result;
  148. }
  149. DWORD proc_get_output(proc_instance_t *inst, LPSTR dest, DWORD sz)
  150. {
  151. DWORD read_bytes;
  152. BOOL res = ReadFile(inst->pipe_server_handle, dest,
  153. sz - 1, &read_bytes, NULL);
  154. if (!res) {
  155. if (GetLastError() == ERROR_NO_DATA) {
  156. return 0;
  157. } else {
  158. PRINT_LAST_ERROR();
  159. return 0;
  160. }
  161. }
  162. dest[read_bytes] = 0;
  163. return read_bytes;
  164. }
  165. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
  166. {
  167. return TRUE;
  168. }