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.27 2005/11/22 18:17:26 momjian Exp $
10 * Copyright (c) 2000-2005, 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;
37 * Alternative ways of updating ps display:
40 * use the function setproctitle(const char *, ...)
43 * use the pstat(PSTAT_SETCMD, )
46 * assign PS_STRINGS->ps_argvstr = "string"
49 * assign argv[0] = "string"
50 * (some other BSD systems)
52 * write over the argv and environment area
53 * (most SysV-like systems)
55 * don't update ps display
56 * (This is the default, as it is safest.)
58 #if defined(HAVE_SETPROCTITLE)
59 #define PS_USE_SETPROCTITLE
60 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
62 #elif defined(HAVE_PS_STRINGS)
63 #define PS_USE_PS_STRINGS
64 #elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__)
65 #define PS_USE_CHANGE_ARGV
66 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__QNX__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__)
67 #define PS_USE_CLOBBER_ARGV
75 /* Different systems want the buffer padded differently */
76 #if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
77 #define PS_PADDING '\0'
79 #define PS_PADDING ' '
83 #ifndef PS_USE_CLOBBER_ARGV
84 /* all but one options need a buffer to write their ps line in */
85 #define PS_BUFFER_SIZE 256
86 static char ps_buffer[PS_BUFFER_SIZE];
87 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
88 #else /* PS_USE_CLOBBER_ARGV */
89 static char *ps_buffer; /* will point to argv area */
90 static size_t ps_buffer_size; /* space determined at run time */
91 #endif /* PS_USE_CLOBBER_ARGV */
93 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
95 /* save the original argv[] location here */
97 static char **save_argv;
102 * Win32 does not support showing any changed arguments. To make it at all
103 * possible to track which backend is doing what, we create a named object
104 * that can be viewed with for example Process Explorer
106 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
108 pgwin32_update_ident(char *ident)
110 char name[PS_BUFFER_SIZE + 32];
112 if (ident_handle != INVALID_HANDLE_VALUE)
113 CloseHandle(ident_handle);
115 sprintf(name, "pgident: %s", ident);
117 ident_handle = CreateEvent(NULL,
125 * Call this early in startup to save the original argc/argv values.
126 * If needed, we make a copy of the original argv[] array to preserve it
127 * from being clobbered by subsequent ps_display actions.
129 * (The original argv[] will not be overwritten by this routine, but may be
130 * overwritten during init_ps_display. Also, the physical location of the
131 * environment strings may be moved, so this should be called before any code
132 * that might try to hang onto a getenv() result.)
135 save_ps_display_args(int argc, char **argv)
140 #if defined(PS_USE_CLOBBER_ARGV)
143 * If we're going to overwrite the argv area, count the available space.
144 * Also move the environment to make additional room.
147 char *end_of_area = NULL;
152 * check for contiguous argv strings
154 for (i = 0; i < argc; i++)
156 if (i == 0 || end_of_area + 1 == argv[i])
157 end_of_area = argv[i] + strlen(argv[i]);
160 if (end_of_area == NULL) /* probably can't happen? */
168 * check for contiguous environ strings following argv
170 for (i = 0; environ[i] != NULL; i++)
172 if (end_of_area + 1 == environ[i])
173 end_of_area = environ[i] + strlen(environ[i]);
177 ps_buffer_size = end_of_area - argv[0];
180 * move the environment out of the way
182 new_environ = (char **) malloc((i + 1) * sizeof(char *));
183 for (i = 0; environ[i] != NULL; i++)
184 new_environ[i] = strdup(environ[i]);
185 new_environ[i] = NULL;
186 environ = new_environ;
188 #endif /* PS_USE_CLOBBER_ARGV */
190 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
193 * If we're going to change the original argv[] then make a copy for
194 * argument parsing purposes.
196 * (NB: do NOT think to remove the copying of argv[], even though
197 * postmaster.c finishes looking at argv[] long before we ever consider
198 * changing the ps display. On some platforms, getopt() keeps pointers
199 * into the argv array, and will get horribly confused when it is
200 * re-called to analyze a subprocess' argument string if the argv storage
201 * has been clobbered meanwhile. Other platforms have other dependencies
208 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
209 for (i = 0; i < argc; i++)
210 new_argv[i] = strdup(argv[i]);
211 new_argv[argc] = NULL;
213 #if defined(__darwin__)
216 * Darwin (and perhaps other NeXT-derived platforms?) has a static
217 * copy of the argv pointer, which we may fix like so:
219 *_NSGetArgv() = new_argv;
224 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
230 * Call this once during subprocess startup to set the identification
231 * values. At this point, the original argv[] array may be overwritten.
234 init_ps_display(const char *username, const char *dbname,
235 const char *host_info)
242 /* no ps display for stand-alone backend */
243 if (!IsUnderPostmaster)
246 /* no ps display if you didn't call save_ps_display_args() */
249 #ifdef PS_USE_CLOBBER_ARGV
250 /* If ps_buffer is a pointer, it might still be null */
256 * Overwrite argv[] to point at appropriate space, if needed
259 #ifdef PS_USE_CHANGE_ARGV
260 save_argv[0] = ps_buffer;
262 #endif /* PS_USE_CHANGE_ARGV */
264 #ifdef PS_USE_CLOBBER_ARGV
268 /* make extra argv slots point at end_of_area (a NUL) */
269 for (i = 1; i < save_argc; i++)
270 save_argv[i] = ps_buffer + ps_buffer_size;
272 #endif /* PS_USE_CLOBBER_ARGV */
275 * Make fixed prefix of ps display.
278 #ifdef PS_USE_SETPROCTITLE
281 * apparently setproctitle() already adds a `progname:' prefix to the ps
284 snprintf(ps_buffer, ps_buffer_size,
286 username, dbname, host_info);
288 snprintf(ps_buffer, ps_buffer_size,
289 "postgres: %s %s %s ",
290 username, dbname, host_info);
293 ps_buffer_fixed_size = strlen(ps_buffer);
296 pgwin32_update_ident(ps_buffer);
298 #endif /* not PS_USE_NONE */
304 * Call this to update the ps status display to a fixed prefix plus an
305 * indication of what you're currently doing passed in the argument.
308 set_ps_display(const char *activity)
311 /* no ps display for stand-alone backend */
312 if (!IsUnderPostmaster)
315 #ifdef PS_USE_CLOBBER_ARGV
316 /* If ps_buffer is a pointer, it might still be null */
321 /* Update ps_buffer to contain both fixed part and activity */
322 StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
323 ps_buffer_size - ps_buffer_fixed_size);
325 /* Transmit new setting to kernel, if necessary */
327 #ifdef PS_USE_SETPROCTITLE
328 setproctitle("%s", ps_buffer);
335 pst.pst_command = ps_buffer;
336 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
338 #endif /* PS_USE_PSTAT */
340 #ifdef PS_USE_PS_STRINGS
341 PS_STRINGS->ps_nargvstr = 1;
342 PS_STRINGS->ps_argvstr = ps_buffer;
343 #endif /* PS_USE_PS_STRINGS */
345 #ifdef PS_USE_CLOBBER_ARGV
349 /* pad unused memory */
350 buflen = strlen(ps_buffer);
351 MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
353 #endif /* PS_USE_CLOBBER_ARGV */
356 pgwin32_update_ident(ps_buffer);
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;