POSIXUtils.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <unistd.h>
  23. #include <termios.h>
  24. #include <stdio.h>
  25. #include <sys/utsname.h>
  26. // for UnixCommandExecutor
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <sys/wait.h>
  30. #include <stdlib.h>
  31. #include "platformPOSIX/platformPOSIX.h"
  32. #include "platformPOSIX/POSIXUtils.h"
  33. UnixUtils *UUtils = NULL;
  34. UnixUtils utils;
  35. UnixUtils::UnixUtils()
  36. {
  37. UUtils = this;
  38. mUnameInfo = (struct utsname*)dRealMalloc(sizeof(struct utsname));;
  39. if (uname(mUnameInfo) == -1)
  40. {
  41. // oh well
  42. dRealFree(mUnameInfo);
  43. mUnameInfo = NULL;
  44. }
  45. }
  46. UnixUtils::~UnixUtils()
  47. {
  48. if (mUnameInfo != NULL)
  49. {
  50. dRealFree(mUnameInfo);
  51. mUnameInfo = NULL;
  52. }
  53. }
  54. const char* UnixUtils::getOSName()
  55. {
  56. if (mUnameInfo == NULL)
  57. return "";
  58. return mUnameInfo->sysname;
  59. }
  60. bool UnixUtils::inBackground()
  61. {
  62. int terminalGroupId = tcgetpgrp(fileno(stdin));
  63. int myPid = getpid();
  64. if (terminalGroupId != myPid)
  65. return true;
  66. else
  67. return false;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // UnixCommandExecutor
  71. void UnixCommandExecutor::clearFields()
  72. {
  73. mRet = -1;
  74. mStdoutSave = -1;
  75. mStderrSave = -1;
  76. mPipeFiledes[0] = -1;
  77. mPipeFiledes[1] = -1;
  78. mChildPID = -1;
  79. mBytesRead = 0;
  80. mStdoutClosed = false;
  81. mStderrClosed = false;
  82. mChildExited = false;
  83. }
  84. UnixCommandExecutor::UnixCommandExecutor()
  85. {
  86. clearFields();
  87. }
  88. UnixCommandExecutor::~UnixCommandExecutor()
  89. {
  90. cleanup();
  91. }
  92. int UnixCommandExecutor::exec(char* args[],
  93. char* stdoutCapture, int stdoutCaptureSize)
  94. {
  95. // check for shitty parameters
  96. if (args == NULL || stdoutCapture == NULL ||
  97. stdoutCaptureSize <= 0)
  98. return -666;
  99. // we're going to be redirecting stdout, so save it so that we can
  100. // restore it
  101. mRet = dup(1);
  102. if (mRet == -1)
  103. {
  104. cleanup();
  105. return mRet;
  106. }
  107. mStdoutSave = mRet;
  108. // save stderr
  109. mRet = dup(2);
  110. if (mRet == -1)
  111. {
  112. cleanup();
  113. return mRet;
  114. }
  115. mStderrSave = mRet;
  116. // we'll need some pipe action for communicating with subprocess
  117. mRet = pipe(mPipeFiledes);
  118. if (mRet == -1)
  119. {
  120. cleanup();
  121. return mRet;
  122. }
  123. // close stdout
  124. mRet = close(1);
  125. if (mRet == -1)
  126. {
  127. cleanup();
  128. return mRet;
  129. }
  130. mStdoutClosed = true;
  131. // stderr just gets closed and the output discarded
  132. mRet = close(2);
  133. if (mRet == -1)
  134. {
  135. cleanup();
  136. return mRet;
  137. }
  138. mStderrClosed = true;
  139. // dup the pipe output into stdout
  140. mRet = dup2(mPipeFiledes[1], 1);
  141. if (mRet == -1)
  142. {
  143. cleanup();
  144. return mRet;
  145. }
  146. // fork
  147. mRet = fork();
  148. if (mRet == -1)
  149. {
  150. cleanup();
  151. return mRet;
  152. }
  153. if (mRet == 0)
  154. {
  155. // child process
  156. //close(mPipeFiledes[0]);
  157. mRet = execvp(args[0], args);
  158. // if exec returns, some bad shit went down, so just
  159. // get outta here
  160. exit(mRet);
  161. }
  162. // parent process
  163. mChildPID = mRet;
  164. // need to suck in data from pipe while child is running,
  165. // otherwise child will eventually block on write and we'll
  166. // wait forever
  167. memset(stdoutCapture, 0, stdoutCaptureSize);
  168. // set input to be non blocking so that we don't block on read
  169. mRet = fcntl(mPipeFiledes[0], F_SETFL, O_NONBLOCK);
  170. if (mRet == -1)
  171. {
  172. cleanup();
  173. return mRet;
  174. }
  175. // check to see if child has exited
  176. mRet = waitpid(mChildPID, NULL, WNOHANG);
  177. while (mRet == 0)
  178. {
  179. // not exited, read some data
  180. mRet = read(mPipeFiledes[0], stdoutCapture + mBytesRead,
  181. stdoutCaptureSize - mBytesRead);
  182. // any error that isn't EAGAIN means we should exit
  183. if (mRet == -1 && errno != EAGAIN)
  184. {
  185. cleanup();
  186. return mRet;
  187. }
  188. // if the read was ok, increment bytes read
  189. if (mRet != -1)
  190. mBytesRead += mRet;
  191. // check again for child exit
  192. mRet = waitpid(mChildPID, NULL, WNOHANG);
  193. }
  194. // check for error from waitpid
  195. if (mRet == -1 && errno != ECHILD)
  196. {
  197. cleanup();
  198. return mRet;
  199. }
  200. // if we get here, the child exited
  201. mChildExited = true;
  202. // read final bit of data
  203. mRet = read(mPipeFiledes[0], stdoutCapture + mBytesRead,
  204. stdoutCaptureSize - mBytesRead);
  205. if (mRet == -1 && errno != EAGAIN)
  206. {
  207. cleanup();
  208. return mRet;
  209. }
  210. if (mRet != -1)
  211. mBytesRead += mRet;
  212. // done...cleanup
  213. cleanup();
  214. return 0;
  215. }
  216. void UnixCommandExecutor::cleanup()
  217. {
  218. // if child spawned and not exited, wait
  219. if (mChildPID > 0 && !mChildExited)
  220. waitpid(mChildPID, NULL, 0);
  221. // close pipe descriptors
  222. if (mPipeFiledes[0] != -1)
  223. close(mPipeFiledes[0]);
  224. if (mPipeFiledes[1] != -1)
  225. close(mPipeFiledes[1]);
  226. // if stdout is redirected, restore it
  227. if (mStdoutClosed && mStdoutSave != -1)
  228. dup2(mStdoutSave, 1);
  229. // close stdout save descriptor
  230. if (mStdoutSave != -1)
  231. close(mStdoutSave);
  232. // if stderr is redirected, restore it
  233. if (mStderrClosed && mStderrSave != -1)
  234. dup2(mStderrSave, 2);
  235. // close stderr save descriptor
  236. if (mStderrSave != -1)
  237. close(mStderrSave);
  238. clearFields();
  239. }
  240. /* Usage:
  241. UnixCommandExecutor exec;
  242. char* args[] = { "ps", "-aux", NULL };
  243. char data[20000];
  244. int ret = exec.exec(args, data, sizeof(data));
  245. printf("%s", data);
  246. */