1 /*--------------------------------------------------------------------
4 * Routines to support changing the ps display of PostgreSQL backends
5 * to contain some useful information. Mechanism differs wildly across
8 * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.31 2006/06/27 22:16:44 momjian Exp $
10 * Copyright (c) 2000-2006, PostgreSQL Global Development Group
11 * various details abducted from various places
12 *--------------------------------------------------------------------
18 #ifdef HAVE_SYS_PSTAT_H
19 #include <sys/pstat.h> /* for HP-UX */
21 #ifdef HAVE_PS_STRINGS
22 #include <machine/vmparam.h> /* for old BSD */
25 #if defined(__darwin__)
26 #include <crt_externs.h>
29 #include "libpq/libpq.h"
30 #include "miscadmin.h"
31 #include "utils/ps_status.h"
33 extern char **environ;
34 bool update_process_title = true;
38 * Alternative ways of updating ps display:
41 * use the function setproctitle(const char *, ...)
44 * use the pstat(PSTAT_SETCMD, )
47 * assign PS_STRINGS->ps_argvstr = "string"
50 * assign argv[0] = "string"
51 * (some other BSD systems)
53 * write over the argv and environment area
54 * (most SysV-like systems)
56 * push the string out as the name of a Windows event
58 * don't update ps display
59 * (This is the default, as it is safest.)
61 #if defined(HAVE_SETPROCTITLE)
62 #define PS_USE_SETPROCTITLE
63 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
65 #elif defined(HAVE_PS_STRINGS)
66 #define PS_USE_PS_STRINGS
67 #elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__)
68 #define PS_USE_CHANGE_ARGV
69 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__)
70 #define PS_USE_CLOBBER_ARGV
78 /* Different systems want the buffer padded differently */
79 #if defined(_AIX) || defined(__linux__) || defined(__svr4__)
80 #define PS_PADDING '\0'
82 #define PS_PADDING ' '
86 #ifndef PS_USE_CLOBBER_ARGV
87 /* all but one options need a buffer to write their ps line in */
88 #define PS_BUFFER_SIZE 256
89 static char ps_buffer[PS_BUFFER_SIZE];
90 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
91 #else /* PS_USE_CLOBBER_ARGV */
92 static char *ps_buffer; /* will point to argv area */
93 static size_t ps_buffer_size; /* space determined at run time */
94 #endif /* PS_USE_CLOBBER_ARGV */
96 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
98 /* save the original argv[] location here */
100 static char **save_argv;
104 * Call this early in startup to save the original argc/argv values.
105 * If needed, we make a copy of the original argv[] array to preserve it
106 * from being clobbered by subsequent ps_display actions.
108 * (The original argv[] will not be overwritten by this routine, but may be
109 * overwritten during init_ps_display. Also, the physical location of the
110 * environment strings may be moved, so this should be called before any code
111 * that might try to hang onto a getenv() result.)
114 save_ps_display_args(int argc, char **argv)
119 #if defined(PS_USE_CLOBBER_ARGV)
122 * If we're going to overwrite the argv area, count the available space.
123 * Also move the environment to make additional room.
126 char *end_of_area = NULL;
131 * check for contiguous argv strings
133 for (i = 0; i < argc; i++)
135 if (i == 0 || end_of_area + 1 == argv[i])
136 end_of_area = argv[i] + strlen(argv[i]);
139 if (end_of_area == NULL) /* probably can't happen? */
147 * check for contiguous environ strings following argv
149 for (i = 0; environ[i] != NULL; i++)
151 if (end_of_area + 1 == environ[i])
152 end_of_area = environ[i] + strlen(environ[i]);
156 ps_buffer_size = end_of_area - argv[0];
159 * move the environment out of the way
161 new_environ = (char **) malloc((i + 1) * sizeof(char *));
162 for (i = 0; environ[i] != NULL; i++)
163 new_environ[i] = strdup(environ[i]);
164 new_environ[i] = NULL;
165 environ = new_environ;
167 #endif /* PS_USE_CLOBBER_ARGV */
169 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
172 * If we're going to change the original argv[] then make a copy for
173 * argument parsing purposes.
175 * (NB: do NOT think to remove the copying of argv[], even though
176 * postmaster.c finishes looking at argv[] long before we ever consider
177 * changing the ps display. On some platforms, getopt() keeps pointers
178 * into the argv array, and will get horribly confused when it is
179 * re-called to analyze a subprocess' argument string if the argv storage
180 * has been clobbered meanwhile. Other platforms have other dependencies
187 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
188 for (i = 0; i < argc; i++)
189 new_argv[i] = strdup(argv[i]);
190 new_argv[argc] = NULL;
192 #if defined(__darwin__)
195 * Darwin (and perhaps other NeXT-derived platforms?) has a static
196 * copy of the argv pointer, which we may fix like so:
198 *_NSGetArgv() = new_argv;
203 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
209 * Call this once during subprocess startup to set the identification
210 * values. At this point, the original argv[] array may be overwritten.
213 init_ps_display(const char *username, const char *dbname,
214 const char *host_info, const char *initial_str)
221 /* no ps display for stand-alone backend */
222 if (!IsUnderPostmaster)
225 /* no ps display if you didn't call save_ps_display_args() */
228 #ifdef PS_USE_CLOBBER_ARGV
229 /* If ps_buffer is a pointer, it might still be null */
235 * Overwrite argv[] to point at appropriate space, if needed
238 #ifdef PS_USE_CHANGE_ARGV
239 save_argv[0] = ps_buffer;
241 #endif /* PS_USE_CHANGE_ARGV */
243 #ifdef PS_USE_CLOBBER_ARGV
247 /* make extra argv slots point at end_of_area (a NUL) */
248 for (i = 1; i < save_argc; i++)
249 save_argv[i] = ps_buffer + ps_buffer_size;
251 #endif /* PS_USE_CLOBBER_ARGV */
254 * Make fixed prefix of ps display.
257 #ifdef PS_USE_SETPROCTITLE
260 * apparently setproctitle() already adds a `progname:' prefix to the ps
263 snprintf(ps_buffer, ps_buffer_size,
265 username, dbname, host_info);
267 snprintf(ps_buffer, ps_buffer_size,
268 "postgres: %s %s %s ",
269 username, dbname, host_info);
272 ps_buffer_fixed_size = strlen(ps_buffer);
274 set_ps_display(initial_str, true);
275 #endif /* not PS_USE_NONE */
281 * Call this to update the ps status display to a fixed prefix plus an
282 * indication of what you're currently doing passed in the argument.
285 set_ps_display(const char *activity, bool force)
288 if (!force && !update_process_title)
292 /* no ps display for stand-alone backend */
293 if (!IsUnderPostmaster)
296 #ifdef PS_USE_CLOBBER_ARGV
297 /* If ps_buffer is a pointer, it might still be null */
302 /* Update ps_buffer to contain both fixed part and activity */
303 StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
304 ps_buffer_size - ps_buffer_fixed_size);
306 /* Transmit new setting to kernel, if necessary */
308 #ifdef PS_USE_SETPROCTITLE
309 setproctitle("%s", ps_buffer);
316 pst.pst_command = ps_buffer;
317 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
319 #endif /* PS_USE_PSTAT */
321 #ifdef PS_USE_PS_STRINGS
322 PS_STRINGS->ps_nargvstr = 1;
323 PS_STRINGS->ps_argvstr = ps_buffer;
324 #endif /* PS_USE_PS_STRINGS */
326 #ifdef PS_USE_CLOBBER_ARGV
330 /* pad unused memory */
331 buflen = strlen(ps_buffer);
332 MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
334 #endif /* PS_USE_CLOBBER_ARGV */
339 * Win32 does not support showing any changed arguments. To make it
340 * at all possible to track which backend is doing what, we create a
341 * named object that can be viewed with for example Process Explorer.
343 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
344 char name[PS_BUFFER_SIZE + 32];
346 if (ident_handle != INVALID_HANDLE_VALUE)
347 CloseHandle(ident_handle);
349 sprintf(name, "pgident: %s", ps_buffer);
351 ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
353 #endif /* PS_USE_WIN32 */
355 #endif /* not PS_USE_NONE */
360 * Returns what's currently in the ps display, in case someone needs
361 * it. Note that only the activity part is returned. On some platforms
362 * the string will not be null-terminated, so return the effective
363 * length into *displen.
366 get_ps_display(int *displen)
368 #ifdef PS_USE_CLOBBER_ARGV
371 /* If ps_buffer is a pointer, it might still be null */
378 /* Remove any trailing spaces to offset the effect of PS_PADDING */
379 offset = ps_buffer_size;
380 while (offset > ps_buffer_fixed_size && ps_buffer[offset - 1] == PS_PADDING)
383 *displen = offset - ps_buffer_fixed_size;
385 *displen = strlen(ps_buffer + ps_buffer_fixed_size);
388 return ps_buffer + ps_buffer_fixed_size;