ps_title.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * PostgreSQL is released under the PostgreSQL License, a liberal Open Source
  3. * license, similar to the BSD or MIT licenses.
  4. * PostgreSQL Database Management System (formerly known as Postgres, then as
  5. * Postgres95)
  6. *
  7. * Portions Copyright (c) 1996-2015, The PostgreSQL Global Development Group
  8. *
  9. * Portions Copyright (c) 1994, The Regents of the University of California
  10. *
  11. * Permission to use, copy, modify, and distribute this software and its
  12. * documentation for any purpose, without fee, and without a written
  13. * agreement is hereby granted, provided that the above copyright notice
  14. * and this paragraph and the following two paragraphs appear in all copies.
  15. *
  16. * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
  18. * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
  19. * EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  20. * SUCH DAMAGE.
  21. *
  22. * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  24. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  25. * "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
  26. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27. *
  28. * The following code is adopted from the PostgreSQL's ps_status(.h/.c).
  29. */
  30. #ifdef PHP_WIN32
  31. #include "config.w32.h"
  32. #include <windows.h>
  33. #include <process.h>
  34. #include "win32/codepage.h"
  35. #else
  36. #include "php_config.h"
  37. extern char** environ;
  38. #endif
  39. #include "ps_title.h"
  40. #include <stdio.h>
  41. #ifdef HAVE_UNISTD_H
  42. #include <sys/types.h>
  43. #include <unistd.h>
  44. #endif
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #ifdef HAVE_SYS_PSTAT_H
  48. #include <sys/pstat.h> /* for HP-UX */
  49. #endif
  50. #ifdef HAVE_PS_STRINGS
  51. #include <machine/vmparam.h> /* for old BSD */
  52. #include <sys/exec.h>
  53. #endif
  54. #if defined(DARWIN)
  55. #include <crt_externs.h>
  56. #endif
  57. /*
  58. * Ways of updating ps display:
  59. *
  60. * PS_USE_SETPROCTITLE
  61. * use the function setproctitle(const char *, ...)
  62. * (newer BSD systems)
  63. * PS_USE_PSTAT
  64. * use the pstat(PSTAT_SETCMD, )
  65. * (HPUX)
  66. * PS_USE_PS_STRINGS
  67. * assign PS_STRINGS->ps_argvstr = "string"
  68. * (some BSD systems)
  69. * PS_USE_CHANGE_ARGV
  70. * assign argv[0] = "string"
  71. * (some other BSD systems)
  72. * PS_USE_CLOBBER_ARGV
  73. * write over the argv and environment area
  74. * (Linux and most SysV-like systems)
  75. * PS_USE_WIN32
  76. * push the string out as the name of a Windows event
  77. * PS_USE_NONE
  78. * don't update ps display
  79. * (This is the default, as it is safest.)
  80. */
  81. #if defined(HAVE_SETPROCTITLE)
  82. #define PS_USE_SETPROCTITLE
  83. #elif defined(HAVE_SYS_PSTAT_H) && defined(PSTAT_SETCMD)
  84. #define PS_USE_PSTAT
  85. #elif defined(HAVE_PS_STRINGS)
  86. #define PS_USE_PS_STRINGS
  87. #elif defined(BSD) && !defined(DARWIN)
  88. #define PS_USE_CHANGE_ARGV
  89. #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__osf__) || defined(DARWIN)
  90. #define PS_USE_CLOBBER_ARGV
  91. #elif defined(PHP_WIN32)
  92. #define PS_USE_WIN32
  93. #else
  94. #define PS_USE_NONE
  95. #endif
  96. /* Different systems want the buffer padded differently */
  97. #if defined(_AIX) || defined(__linux__) || defined(DARWIN)
  98. #define PS_PADDING '\0'
  99. #else
  100. #define PS_PADDING ' '
  101. #endif
  102. #ifdef PS_USE_WIN32
  103. static char windows_error_details[64];
  104. static char ps_buffer[MAX_PATH];
  105. static const size_t ps_buffer_size = MAX_PATH;
  106. #elif defined(PS_USE_CLOBBER_ARGV)
  107. static char *ps_buffer; /* will point to argv area */
  108. static size_t ps_buffer_size; /* space determined at run time */
  109. static char *empty_environ[] = {0}; /* empty environment */
  110. #else
  111. #define PS_BUFFER_SIZE 256
  112. static char ps_buffer[PS_BUFFER_SIZE];
  113. static const size_t ps_buffer_size = PS_BUFFER_SIZE;
  114. #endif
  115. static size_t ps_buffer_cur_len; /* actual string length in ps_buffer */
  116. /* save the original argv[] location here */
  117. static int save_argc;
  118. static char** save_argv;
  119. /*
  120. * This holds the 'locally' allocated environ from the save_ps_args method.
  121. * This is subsequently free'd at exit.
  122. */
  123. #if defined(PS_USE_CLOBBER_ARGV)
  124. static char** frozen_environ, **new_environ;
  125. #endif
  126. /*
  127. * Call this method early, before any code has used the original argv passed in
  128. * from main().
  129. * If needed, this code will make deep copies of argv and environ and return
  130. * these to the caller for further use. The original argv is then 'clobbered'
  131. * to store the process title.
  132. */
  133. char** save_ps_args(int argc, char** argv)
  134. {
  135. save_argc = argc;
  136. save_argv = argv;
  137. #if defined(PS_USE_CLOBBER_ARGV)
  138. /*
  139. * If we're going to overwrite the argv area, count the available space.
  140. * Also move the environment to make additional room.
  141. */
  142. {
  143. char* end_of_area = NULL;
  144. int non_contiguous_area = 0;
  145. int i;
  146. /*
  147. * check for contiguous argv strings
  148. */
  149. for (i = 0; (non_contiguous_area == 0) && (i < argc); i++)
  150. {
  151. if (i != 0 && end_of_area + 1 != argv[i])
  152. non_contiguous_area = 1;
  153. end_of_area = argv[i] + strlen(argv[i]);
  154. }
  155. /*
  156. * check for contiguous environ strings following argv
  157. */
  158. for (i = 0; (non_contiguous_area == 0) && (environ[i] != NULL); i++)
  159. {
  160. if (end_of_area + 1 != environ[i])
  161. non_contiguous_area = 1;
  162. end_of_area = environ[i] + strlen(environ[i]);
  163. }
  164. if (non_contiguous_area != 0)
  165. goto clobber_error;
  166. ps_buffer = argv[0];
  167. ps_buffer_size = end_of_area - argv[0];
  168. /*
  169. * move the environment out of the way
  170. */
  171. new_environ = (char **) malloc((i + 1) * sizeof(char *));
  172. frozen_environ = (char **) malloc((i + 1) * sizeof(char *));
  173. if (!new_environ || !frozen_environ)
  174. goto clobber_error;
  175. for (i = 0; environ[i] != NULL; i++)
  176. {
  177. new_environ[i] = strdup(environ[i]);
  178. if (!new_environ[i])
  179. goto clobber_error;
  180. }
  181. new_environ[i] = NULL;
  182. environ = new_environ;
  183. memcpy((char *)frozen_environ, (char *)new_environ, sizeof(char *) * (i + 1));
  184. }
  185. #endif /* PS_USE_CLOBBER_ARGV */
  186. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  187. /*
  188. * If we're going to change the original argv[] then make a copy for
  189. * argument parsing purposes.
  190. *
  191. * (NB: do NOT think to remove the copying of argv[]!
  192. * On some platforms, getopt() keeps pointers into the argv array, and
  193. * will get horribly confused when it is re-called to analyze a subprocess'
  194. * argument string if the argv storage has been clobbered meanwhile.
  195. * Other platforms have other dependencies on argv[].)
  196. */
  197. {
  198. char** new_argv;
  199. int i;
  200. new_argv = (char **) malloc((argc + 1) * sizeof(char *));
  201. if (!new_argv)
  202. goto clobber_error;
  203. for (i = 0; i < argc; i++)
  204. {
  205. new_argv[i] = strdup(argv[i]);
  206. if (!new_argv[i]) {
  207. free(new_argv);
  208. goto clobber_error;
  209. }
  210. }
  211. new_argv[argc] = NULL;
  212. #if defined(DARWIN)
  213. /*
  214. * Darwin (and perhaps other NeXT-derived platforms?) has a static
  215. * copy of the argv pointer, which we may fix like so:
  216. */
  217. *_NSGetArgv() = new_argv;
  218. #endif
  219. argv = new_argv;
  220. }
  221. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  222. #if defined(PS_USE_CLOBBER_ARGV)
  223. {
  224. /* make extra argv slots point at end_of_area (a NUL) */
  225. int i;
  226. for (i = 1; i < save_argc; i++)
  227. save_argv[i] = ps_buffer + ps_buffer_size;
  228. }
  229. #endif /* PS_USE_CLOBBER_ARGV */
  230. #ifdef PS_USE_CHANGE_ARGV
  231. save_argv[0] = ps_buffer; /* ps_buffer here is a static const array of size PS_BUFFER_SIZE */
  232. save_argv[1] = NULL;
  233. #endif /* PS_USE_CHANGE_ARGV */
  234. return argv;
  235. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  236. clobber_error:
  237. /* probably can't happen?!
  238. * if we ever get here, argv still points to originally passed
  239. * in argument
  240. */
  241. save_argv = NULL;
  242. save_argc = 0;
  243. ps_buffer = NULL;
  244. ps_buffer_size = 0;
  245. return argv;
  246. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  247. }
  248. /*
  249. * Returns PS_TITLE_SUCCESS if the OS supports this functionality
  250. * and the init function was called.
  251. * Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
  252. */
  253. int is_ps_title_available()
  254. {
  255. #ifdef PS_USE_NONE
  256. return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
  257. #endif
  258. if (!save_argv)
  259. return PS_TITLE_NOT_INITIALIZED;
  260. #ifdef PS_USE_CLOBBER_ARGV
  261. if (!ps_buffer)
  262. return PS_TITLE_BUFFER_NOT_AVAILABLE;
  263. #endif /* PS_USE_CLOBBER_ARGV */
  264. return PS_TITLE_SUCCESS;
  265. }
  266. /*
  267. * Convert error codes into error strings
  268. */
  269. const char* ps_title_errno(int rc)
  270. {
  271. switch(rc)
  272. {
  273. case PS_TITLE_SUCCESS:
  274. return "Success";
  275. case PS_TITLE_NOT_AVAILABLE:
  276. return "Not available on this OS";
  277. case PS_TITLE_NOT_INITIALIZED:
  278. return "Not initialized correctly";
  279. case PS_TITLE_BUFFER_NOT_AVAILABLE:
  280. return "Buffer not contiguous";
  281. #ifdef PS_USE_WIN32
  282. case PS_TITLE_WINDOWS_ERROR:
  283. sprintf(windows_error_details, "Windows error code: %lu", GetLastError());
  284. return windows_error_details;
  285. #endif
  286. }
  287. return "Unknown error code";
  288. }
  289. /*
  290. * Set a new process title.
  291. * Returns the appropriate error code if if there's an error
  292. * (like the functionality is compile time disabled, or the
  293. * save_ps_args() was not called.
  294. * Else returns 0 on success.
  295. */
  296. int set_ps_title(const char* title)
  297. {
  298. int rc = is_ps_title_available();
  299. if (rc != PS_TITLE_SUCCESS)
  300. return rc;
  301. strncpy(ps_buffer, title, ps_buffer_size);
  302. ps_buffer[ps_buffer_size - 1] = '\0';
  303. ps_buffer_cur_len = strlen(ps_buffer);
  304. #ifdef PS_USE_SETPROCTITLE
  305. setproctitle("%s", ps_buffer);
  306. #endif
  307. #ifdef PS_USE_PSTAT
  308. {
  309. union pstun pst;
  310. pst.pst_command = ps_buffer;
  311. pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
  312. }
  313. #endif /* PS_USE_PSTAT */
  314. #ifdef PS_USE_PS_STRINGS
  315. PS_STRINGS->ps_nargvstr = 1;
  316. PS_STRINGS->ps_argvstr = ps_buffer;
  317. #endif /* PS_USE_PS_STRINGS */
  318. #ifdef PS_USE_CLOBBER_ARGV
  319. /* pad unused memory */
  320. if (ps_buffer_cur_len < ps_buffer_size)
  321. {
  322. memset(ps_buffer + ps_buffer_cur_len, PS_PADDING,
  323. ps_buffer_size - ps_buffer_cur_len);
  324. }
  325. #endif /* PS_USE_CLOBBER_ARGV */
  326. #ifdef PS_USE_WIN32
  327. {
  328. wchar_t *ps_buffer_w = php_win32_cp_any_to_w(ps_buffer);
  329. if (!ps_buffer_w || !SetConsoleTitleW(ps_buffer_w)) {
  330. return PS_TITLE_WINDOWS_ERROR;
  331. }
  332. free(ps_buffer_w);
  333. }
  334. #endif /* PS_USE_WIN32 */
  335. return PS_TITLE_SUCCESS;
  336. }
  337. /*
  338. * Returns the current ps_buffer value into string. On some platforms
  339. * the string will not be null-terminated, so return the effective
  340. * length into *displen.
  341. * The return code indicates the error.
  342. */
  343. int get_ps_title(int *displen, const char** string)
  344. {
  345. int rc = is_ps_title_available();
  346. if (rc != PS_TITLE_SUCCESS)
  347. return rc;
  348. #ifdef PS_USE_WIN32
  349. {
  350. wchar_t ps_buffer_w[MAX_PATH];
  351. char *tmp;
  352. if (!(ps_buffer_cur_len = GetConsoleTitleW(ps_buffer_w, (DWORD)sizeof(ps_buffer_w)))) {
  353. return PS_TITLE_WINDOWS_ERROR;
  354. }
  355. tmp = php_win32_cp_conv_w_to_any(ps_buffer_w, PHP_WIN32_CP_IGNORE_LEN, &ps_buffer_cur_len);
  356. if (!tmp) {
  357. return PS_TITLE_WINDOWS_ERROR;
  358. }
  359. ps_buffer_cur_len = ps_buffer_cur_len > sizeof(ps_buffer)-1 ? sizeof(ps_buffer)-1 : ps_buffer_cur_len;
  360. memmove(ps_buffer, tmp, ps_buffer_cur_len);
  361. free(tmp);
  362. }
  363. #endif
  364. *displen = (int)ps_buffer_cur_len;
  365. *string = ps_buffer;
  366. return PS_TITLE_SUCCESS;
  367. }
  368. /*
  369. * Clean up the allocated argv and environ if applicable. Only call
  370. * this right before exiting.
  371. * This isn't needed per-se because the OS will clean-up anyway, but
  372. * having and calling this will ensure Valgrind doesn't output 'false
  373. * positives'.
  374. */
  375. void cleanup_ps_args(char **argv)
  376. {
  377. #ifndef PS_USE_NONE
  378. if (save_argv)
  379. {
  380. save_argv = NULL;
  381. save_argc = 0;
  382. #ifdef PS_USE_CLOBBER_ARGV
  383. {
  384. int i;
  385. for (i = 0; frozen_environ[i] != NULL; i++)
  386. free(frozen_environ[i]);
  387. free(frozen_environ);
  388. free(new_environ);
  389. /* leave a sane environment behind since some atexit() handlers
  390. call getenv(). */
  391. environ = empty_environ;
  392. }
  393. #endif /* PS_USE_CLOBBER_ARGV */
  394. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  395. {
  396. int i;
  397. for (i=0; argv[i] != NULL; i++)
  398. free(argv[i]);
  399. free(argv);
  400. }
  401. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  402. }
  403. #endif /* PS_USE_NONE */
  404. return;
  405. }