ps_title.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include "ps_title.h"
  31. #include <stdio.h>
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #ifdef PHP_WIN32
  38. #include "config.w32.h"
  39. #include <windows.h>
  40. #include <process.h>
  41. #else
  42. #include "php_config.h"
  43. extern char** environ;
  44. #endif
  45. #ifdef HAVE_SYS_PSTAT_H
  46. #include <sys/pstat.h> /* for HP-UX */
  47. #endif
  48. #ifdef HAVE_PS_STRINGS
  49. #include <machine/vmparam.h> /* for old BSD */
  50. #include <sys/exec.h>
  51. #endif
  52. #if defined(DARWIN)
  53. #include <crt_externs.h>
  54. #endif
  55. /*
  56. * Ways of updating ps display:
  57. *
  58. * PS_USE_SETPROCTITLE
  59. * use the function setproctitle(const char *, ...)
  60. * (newer BSD systems)
  61. * PS_USE_PSTAT
  62. * use the pstat(PSTAT_SETCMD, )
  63. * (HPUX)
  64. * PS_USE_PS_STRINGS
  65. * assign PS_STRINGS->ps_argvstr = "string"
  66. * (some BSD systems)
  67. * PS_USE_CHANGE_ARGV
  68. * assign argv[0] = "string"
  69. * (some other BSD systems)
  70. * PS_USE_CLOBBER_ARGV
  71. * write over the argv and environment area
  72. * (Linux and most SysV-like systems)
  73. * PS_USE_WIN32
  74. * push the string out as the name of a Windows event
  75. * PS_USE_NONE
  76. * don't update ps display
  77. * (This is the default, as it is safest.)
  78. */
  79. #if defined(HAVE_SETPROCTITLE)
  80. #define PS_USE_SETPROCTITLE
  81. #elif defined(HAVE_SYS_PSTAT_H) && defined(PSTAT_SETCMD)
  82. #define PS_USE_PSTAT
  83. #elif defined(HAVE_PS_STRINGS)
  84. #define PS_USE_PS_STRINGS
  85. #elif defined(BSD) && !defined(DARWIN)
  86. #define PS_USE_CHANGE_ARGV
  87. #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__osf__) || defined(DARWIN)
  88. #define PS_USE_CLOBBER_ARGV
  89. #elif defined(PHP_WIN32)
  90. #define PS_USE_WIN32
  91. #else
  92. #define PS_USE_NONE
  93. #endif
  94. /* Different systems want the buffer padded differently */
  95. #if defined(_AIX) || defined(__linux__) || defined(DARWIN)
  96. #define PS_PADDING '\0'
  97. #else
  98. #define PS_PADDING ' '
  99. #endif
  100. #ifdef PS_USE_WIN32
  101. static char windows_error_details[64];
  102. static char ps_buffer[MAX_PATH];
  103. static const size_t ps_buffer_size = MAX_PATH;
  104. #elif defined(PS_USE_CLOBBER_ARGV)
  105. static char *ps_buffer; /* will point to argv area */
  106. static size_t ps_buffer_size; /* space determined at run time */
  107. static char *empty_environ[] = {0}; /* empty environment */
  108. #else
  109. #define PS_BUFFER_SIZE 256
  110. static char ps_buffer[PS_BUFFER_SIZE];
  111. static const size_t ps_buffer_size = PS_BUFFER_SIZE;
  112. #endif
  113. static size_t ps_buffer_cur_len; /* actual string length in ps_buffer */
  114. /* save the original argv[] location here */
  115. static int save_argc;
  116. static char** save_argv;
  117. /*
  118. * This holds the 'locally' allocated environ from the save_ps_args method.
  119. * This is subsequently free'd at exit.
  120. */
  121. static char** frozen_environ, **new_environ;
  122. /*
  123. * Call this method early, before any code has used the original argv passed in
  124. * from main().
  125. * If needed, this code will make deep copies of argv and environ and return
  126. * these to the caller for further use. The original argv is then 'clobbered'
  127. * to store the process title.
  128. */
  129. char** save_ps_args(int argc, char** argv)
  130. {
  131. save_argc = argc;
  132. save_argv = argv;
  133. #if defined(PS_USE_CLOBBER_ARGV)
  134. /*
  135. * If we're going to overwrite the argv area, count the available space.
  136. * Also move the environment to make additional room.
  137. */
  138. {
  139. char* end_of_area = NULL;
  140. int non_contiguous_area = 0;
  141. int i;
  142. /*
  143. * check for contiguous argv strings
  144. */
  145. for (i = 0; (non_contiguous_area == 0) && (i < argc); i++)
  146. {
  147. if (i != 0 && end_of_area + 1 != argv[i])
  148. non_contiguous_area = 1;
  149. end_of_area = argv[i] + strlen(argv[i]);
  150. }
  151. /*
  152. * check for contiguous environ strings following argv
  153. */
  154. for (i = 0; (non_contiguous_area == 0) && (environ[i] != NULL); i++)
  155. {
  156. if (end_of_area + 1 != environ[i])
  157. non_contiguous_area = 1;
  158. end_of_area = environ[i] + strlen(environ[i]);
  159. }
  160. if (non_contiguous_area != 0)
  161. goto clobber_error;
  162. ps_buffer = argv[0];
  163. ps_buffer_size = end_of_area - argv[0];
  164. /*
  165. * move the environment out of the way
  166. */
  167. new_environ = (char **) malloc((i + 1) * sizeof(char *));
  168. frozen_environ = (char **) malloc((i + 1) * sizeof(char *));
  169. if (!new_environ || !frozen_environ)
  170. goto clobber_error;
  171. for (i = 0; environ[i] != NULL; i++)
  172. {
  173. new_environ[i] = strdup(environ[i]);
  174. if (!new_environ[i])
  175. goto clobber_error;
  176. }
  177. new_environ[i] = NULL;
  178. environ = new_environ;
  179. memcpy((char *)frozen_environ, (char *)new_environ, sizeof(char *) * (i + 1));
  180. }
  181. #endif /* PS_USE_CLOBBER_ARGV */
  182. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  183. /*
  184. * If we're going to change the original argv[] then make a copy for
  185. * argument parsing purposes.
  186. *
  187. * (NB: do NOT think to remove the copying of argv[]!
  188. * On some platforms, getopt() keeps pointers into the argv array, and
  189. * will get horribly confused when it is re-called to analyze a subprocess'
  190. * argument string if the argv storage has been clobbered meanwhile.
  191. * Other platforms have other dependencies on argv[].)
  192. */
  193. {
  194. char** new_argv;
  195. int i;
  196. new_argv = (char **) malloc((argc + 1) * sizeof(char *));
  197. if (!new_argv)
  198. goto clobber_error;
  199. for (i = 0; i < argc; i++)
  200. {
  201. new_argv[i] = strdup(argv[i]);
  202. if (!new_argv[i])
  203. goto clobber_error;
  204. }
  205. new_argv[argc] = NULL;
  206. #if defined(DARWIN)
  207. /*
  208. * Darwin (and perhaps other NeXT-derived platforms?) has a static
  209. * copy of the argv pointer, which we may fix like so:
  210. */
  211. *_NSGetArgv() = new_argv;
  212. #endif
  213. argv = new_argv;
  214. }
  215. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  216. #if defined(PS_USE_CLOBBER_ARGV)
  217. {
  218. /* make extra argv slots point at end_of_area (a NUL) */
  219. int i;
  220. for (i = 1; i < save_argc; i++)
  221. save_argv[i] = ps_buffer + ps_buffer_size;
  222. }
  223. #endif /* PS_USE_CLOBBER_ARGV */
  224. #ifdef PS_USE_CHANGE_ARGV
  225. save_argv[0] = ps_buffer; /* ps_buffer here is a static const array of size PS_BUFFER_SIZE */
  226. save_argv[1] = NULL;
  227. #endif /* PS_USE_CHANGE_ARGV */
  228. return argv;
  229. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  230. clobber_error:
  231. /* probably can't happen?!
  232. * if we ever get here, argv still points to originally passed
  233. * in argument
  234. */
  235. save_argv = NULL;
  236. save_argc = 0;
  237. ps_buffer = NULL;
  238. ps_buffer_size = 0;
  239. return argv;
  240. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  241. }
  242. /*
  243. * Returns PS_TITLE_SUCCESS if the OS supports this functionality
  244. * and the init function was called.
  245. * Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
  246. */
  247. int is_ps_title_available()
  248. {
  249. #ifdef PS_USE_NONE
  250. return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
  251. #endif
  252. if (!save_argv)
  253. return PS_TITLE_NOT_INITIALIZED;
  254. #ifdef PS_USE_CLOBBER_ARGV
  255. if (!ps_buffer)
  256. return PS_TITLE_BUFFER_NOT_AVAILABLE;
  257. #endif /* PS_USE_CLOBBER_ARGV */
  258. return PS_TITLE_SUCCESS;
  259. }
  260. /*
  261. * Convert error codes into error strings
  262. */
  263. const char* ps_title_errno(int rc)
  264. {
  265. switch(rc)
  266. {
  267. case PS_TITLE_SUCCESS:
  268. return "Success";
  269. case PS_TITLE_NOT_AVAILABLE:
  270. return "Not available on this OS";
  271. case PS_TITLE_NOT_INITIALIZED:
  272. return "Not initialized correctly";
  273. case PS_TITLE_BUFFER_NOT_AVAILABLE:
  274. return "Buffer not contiguous";
  275. #ifdef PS_USE_WIN32
  276. case PS_TITLE_WINDOWS_ERROR:
  277. sprintf(windows_error_details, "Windows error code: %d", GetLastError());
  278. return windows_error_details;
  279. #endif
  280. }
  281. return "Unknown error code";
  282. }
  283. /*
  284. * Set a new process title.
  285. * Returns the appropriate error code if if there's an error
  286. * (like the functionality is compile time disabled, or the
  287. * save_ps_args() was not called.
  288. * Else returns 0 on success.
  289. */
  290. int set_ps_title(const char* title)
  291. {
  292. int rc = is_ps_title_available();
  293. if (rc != PS_TITLE_SUCCESS)
  294. return rc;
  295. strncpy(ps_buffer, title, ps_buffer_size);
  296. ps_buffer[ps_buffer_size - 1] = '\0';
  297. ps_buffer_cur_len = strlen(ps_buffer);
  298. #ifdef PS_USE_SETPROCTITLE
  299. setproctitle("%s", ps_buffer);
  300. #endif
  301. #ifdef PS_USE_PSTAT
  302. {
  303. union pstun pst;
  304. pst.pst_command = ps_buffer;
  305. pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
  306. }
  307. #endif /* PS_USE_PSTAT */
  308. #ifdef PS_USE_PS_STRINGS
  309. PS_STRINGS->ps_nargvstr = 1;
  310. PS_STRINGS->ps_argvstr = ps_buffer;
  311. #endif /* PS_USE_PS_STRINGS */
  312. #ifdef PS_USE_CLOBBER_ARGV
  313. /* pad unused memory */
  314. if (ps_buffer_cur_len < ps_buffer_size)
  315. {
  316. memset(ps_buffer + ps_buffer_cur_len, PS_PADDING,
  317. ps_buffer_size - ps_buffer_cur_len);
  318. }
  319. #endif /* PS_USE_CLOBBER_ARGV */
  320. #ifdef PS_USE_WIN32
  321. {
  322. if (!SetConsoleTitle(ps_buffer))
  323. return PS_TITLE_WINDOWS_ERROR;
  324. }
  325. #endif /* PS_USE_WIN32 */
  326. return PS_TITLE_SUCCESS;
  327. }
  328. /*
  329. * Returns the current ps_buffer value into string. On some platforms
  330. * the string will not be null-terminated, so return the effective
  331. * length into *displen.
  332. * The return code indicates the error.
  333. */
  334. int get_ps_title(int *displen, const char** string)
  335. {
  336. int rc = is_ps_title_available();
  337. if (rc != PS_TITLE_SUCCESS)
  338. return rc;
  339. #ifdef PS_USE_WIN32
  340. if (!(ps_buffer_cur_len = GetConsoleTitle(ps_buffer, ps_buffer_size)))
  341. return PS_TITLE_WINDOWS_ERROR;
  342. #endif
  343. *displen = (int)ps_buffer_cur_len;
  344. *string = ps_buffer;
  345. return PS_TITLE_SUCCESS;
  346. }
  347. /*
  348. * Clean up the allocated argv and environ if applicable. Only call
  349. * this right before exiting.
  350. * This isn't needed per-se because the OS will clean-up anyway, but
  351. * having and calling this will ensure Valgrind doesn't output 'false
  352. * positives'.
  353. */
  354. void cleanup_ps_args(char **argv)
  355. {
  356. #ifndef PS_USE_NONE
  357. if (save_argv)
  358. {
  359. save_argv = NULL;
  360. save_argc = 0;
  361. #ifdef PS_USE_CLOBBER_ARGV
  362. {
  363. int i;
  364. for (i = 0; frozen_environ[i] != NULL; i++)
  365. free(frozen_environ[i]);
  366. free(frozen_environ);
  367. free(new_environ);
  368. /* leave a sane environment behind since some atexit() handlers
  369. call getenv(). */
  370. environ = empty_environ;
  371. }
  372. #endif /* PS_USE_CLOBBER_ARGV */
  373. #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
  374. {
  375. int i;
  376. for (i=0; argv[i] != NULL; i++)
  377. free(argv[i]);
  378. free(argv);
  379. }
  380. #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
  381. }
  382. #endif /* PS_USE_NONE */
  383. return;
  384. }