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.36 2007/11/15 21:14:41 momjian Exp $
10 * Copyright (c) 2000-2007, 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 static size_t last_status_len; /* use to minimize length of clobber */
95 #endif /* PS_USE_CLOBBER_ARGV */
97 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
99 /* save the original argv[] location here */
100 static int save_argc;
101 static char **save_argv;
105 * Call this early in startup to save the original argc/argv values.
106 * If needed, we make a copy of the original argv[] array to preserve it
107 * from being clobbered by subsequent ps_display actions.
109 * (The original argv[] will not be overwritten by this routine, but may be
110 * overwritten during init_ps_display. Also, the physical location of the
111 * environment strings may be moved, so this should be called before any code
112 * that might try to hang onto a getenv() result.)
115 save_ps_display_args(int argc, char **argv)
120 #if defined(PS_USE_CLOBBER_ARGV)
123 * If we're going to overwrite the argv area, count the available space.
124 * Also move the environment to make additional room.
127 char *end_of_area = NULL;
132 * check for contiguous argv strings
134 for (i = 0; i < argc; i++)
136 if (i == 0 || end_of_area + 1 == argv[i])
137 end_of_area = argv[i] + strlen(argv[i]);
140 if (end_of_area == NULL) /* probably can't happen? */
148 * check for contiguous environ strings following argv
150 for (i = 0; environ[i] != NULL; i++)
152 if (end_of_area + 1 == environ[i])
153 end_of_area = environ[i] + strlen(environ[i]);
157 last_status_len = ps_buffer_size = end_of_area - argv[0];
160 * move the environment out of the way
162 new_environ = (char **) malloc((i + 1) * sizeof(char *));
163 for (i = 0; environ[i] != NULL; i++)
164 new_environ[i] = strdup(environ[i]);
165 new_environ[i] = NULL;
166 environ = new_environ;
168 #endif /* PS_USE_CLOBBER_ARGV */
170 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
173 * If we're going to change the original argv[] then make a copy for
174 * argument parsing purposes.
176 * (NB: do NOT think to remove the copying of argv[], even though
177 * postmaster.c finishes looking at argv[] long before we ever consider
178 * changing the ps display. On some platforms, getopt() keeps pointers
179 * into the argv array, and will get horribly confused when it is
180 * re-called to analyze a subprocess' argument string if the argv storage
181 * has been clobbered meanwhile. Other platforms have other dependencies
188 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
189 for (i = 0; i < argc; i++)
190 new_argv[i] = strdup(argv[i]);
191 new_argv[argc] = NULL;
193 #if defined(__darwin__)
196 * Darwin (and perhaps other NeXT-derived platforms?) has a static
197 * copy of the argv pointer, which we may fix like so:
199 *_NSGetArgv() = new_argv;
204 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
210 * Call this once during subprocess startup to set the identification
211 * values. At this point, the original argv[] array may be overwritten.
214 init_ps_display(const char *username, const char *dbname,
215 const char *host_info, const char *initial_str)
222 /* no ps display for stand-alone backend */
223 if (!IsUnderPostmaster)
226 /* no ps display if you didn't call save_ps_display_args() */
229 #ifdef PS_USE_CLOBBER_ARGV
230 /* If ps_buffer is a pointer, it might still be null */
236 * Overwrite argv[] to point at appropriate space, if needed
239 #ifdef PS_USE_CHANGE_ARGV
240 save_argv[0] = ps_buffer;
242 #endif /* PS_USE_CHANGE_ARGV */
244 #ifdef PS_USE_CLOBBER_ARGV
248 /* make extra argv slots point at end_of_area (a NUL) */
249 for (i = 1; i < save_argc; i++)
250 save_argv[i] = ps_buffer + ps_buffer_size;
252 #endif /* PS_USE_CLOBBER_ARGV */
255 * Make fixed prefix of ps display.
258 #ifdef PS_USE_SETPROCTITLE
261 * apparently setproctitle() already adds a `progname:' prefix to the ps
264 snprintf(ps_buffer, ps_buffer_size,
266 username, dbname, host_info);
268 snprintf(ps_buffer, ps_buffer_size,
269 "postgres: %s %s %s ",
270 username, dbname, host_info);
273 ps_buffer_fixed_size = strlen(ps_buffer);
275 set_ps_display(initial_str, true);
276 #endif /* not PS_USE_NONE */
282 * Call this to update the ps status display to a fixed prefix plus an
283 * indication of what you're currently doing passed in the argument.
286 set_ps_display(const char *activity, bool force)
289 if (!force && !update_process_title)
293 /* no ps display for stand-alone backend */
294 if (!IsUnderPostmaster)
297 #ifdef PS_USE_CLOBBER_ARGV
298 /* If ps_buffer is a pointer, it might still be null */
303 /* Update ps_buffer to contain both fixed part and activity */
304 strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
305 ps_buffer_size - ps_buffer_fixed_size);
307 /* Transmit new setting to kernel, if necessary */
309 #ifdef PS_USE_SETPROCTITLE
310 setproctitle("%s", ps_buffer);
317 pst.pst_command = ps_buffer;
318 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
320 #endif /* PS_USE_PSTAT */
322 #ifdef PS_USE_PS_STRINGS
323 PS_STRINGS->ps_nargvstr = 1;
324 PS_STRINGS->ps_argvstr = ps_buffer;
325 #endif /* PS_USE_PS_STRINGS */
327 #ifdef PS_USE_CLOBBER_ARGV
331 /* pad unused memory */
332 buflen = strlen(ps_buffer);
333 /* clobber remainder of old status string */
334 if (last_status_len > buflen)
335 MemSet(ps_buffer + buflen, PS_PADDING, last_status_len - buflen);
336 last_status_len = buflen;
338 #endif /* PS_USE_CLOBBER_ARGV */
343 * Win32 does not support showing any changed arguments. To make it at
344 * all possible to track which backend is doing what, we create a
345 * named object that can be viewed with for example Process Explorer.
347 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
348 char name[PS_BUFFER_SIZE + 32];
350 if (ident_handle != INVALID_HANDLE_VALUE)
351 CloseHandle(ident_handle);
353 sprintf(name, "pgident: %s", ps_buffer);
355 ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
357 #endif /* PS_USE_WIN32 */
358 #endif /* not PS_USE_NONE */
363 * Returns what's currently in the ps display, in case someone needs
364 * it. Note that only the activity part is returned. On some platforms
365 * the string will not be null-terminated, so return the effective
366 * length into *displen.
369 get_ps_display(int *displen)
371 #ifdef PS_USE_CLOBBER_ARGV
374 /* If ps_buffer is a pointer, it might still be null */
381 /* Remove any trailing spaces to offset the effect of PS_PADDING */
382 offset = ps_buffer_size;
383 while (offset > ps_buffer_fixed_size && ps_buffer[offset - 1] == PS_PADDING)
386 *displen = offset - ps_buffer_fixed_size;
388 *displen = strlen(ps_buffer + ps_buffer_fixed_size);
391 return ps_buffer + ps_buffer_fixed_size;