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.18 2004/03/09 04:43:07 momjian Exp $
10 * Copyright (c) 2000-2003, 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
73 /* Different systems want the buffer padded differently */
74 #if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
75 #define PS_PADDING '\0'
77 #define PS_PADDING ' '
81 #ifndef PS_USE_CLOBBER_ARGV
82 /* all but one options need a buffer to write their ps line in */
83 #define PS_BUFFER_SIZE 256
84 static char ps_buffer[PS_BUFFER_SIZE];
85 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
87 #else /* PS_USE_CLOBBER_ARGV */
88 static char *ps_buffer; /* will point to argv area */
89 static size_t ps_buffer_size; /* space determined at run time */
90 #endif /* PS_USE_CLOBBER_ARGV */
92 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
94 /* save the original argv[] location here */
96 static char **save_argv;
100 * Call this early in startup to save the original argc/argv values.
101 * If needed, we make a copy of the original argv[] array to preserve it
102 * from being clobbered by subsequent ps_display actions.
104 * (The original argv[] will not be overwritten by this routine, but may be
105 * overwritten during init_ps_display. Also, the physical location of the
106 * environment strings may be moved, so this should be called before any code
107 * that might try to hang onto a getenv() result.)
110 save_ps_display_args(int argc, char **argv)
115 #if defined(PS_USE_CLOBBER_ARGV)
118 * If we're going to overwrite the argv area, count the available
119 * space. Also move the environment to make additional room.
122 char *end_of_area = NULL;
127 * check for contiguous argv strings
129 for (i = 0; i < argc; i++)
131 if (i == 0 || end_of_area + 1 == argv[i])
132 end_of_area = argv[i] + strlen(argv[i]);
135 if (end_of_area == NULL) /* probably can't happen? */
143 * check for contiguous environ strings following argv
145 for (i = 0; environ[i] != NULL; i++)
147 if (end_of_area + 1 == environ[i])
148 end_of_area = environ[i] + strlen(environ[i]);
152 ps_buffer_size = end_of_area - argv[0];
155 * move the environment out of the way
157 new_environ = (char **) malloc((i + 1) * sizeof(char *));
158 for (i = 0; environ[i] != NULL; i++)
159 new_environ[i] = strdup(environ[i]);
160 new_environ[i] = NULL;
161 environ = new_environ;
163 #endif /* PS_USE_CLOBBER_ARGV */
165 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
168 * If we're going to change the original argv[] then make a copy for
169 * argument parsing purposes.
171 * (NB: do NOT think to remove the copying of argv[], even though
172 * postmaster.c finishes looking at argv[] long before we ever
173 * consider changing the ps display. On some platforms, getopt()
174 * keeps pointers into the argv array, and will get horribly confused
175 * when it is re-called to analyze a subprocess' argument string if
176 * the argv storage has been clobbered meanwhile. Other platforms
177 * have other dependencies on argv[].
183 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
184 for (i = 0; i < argc; i++)
185 new_argv[i] = strdup(argv[i]);
186 new_argv[argc] = NULL;
188 #if defined(__darwin__)
190 * Darwin (and perhaps other NeXT-derived platforms?) has a static
191 * copy of the argv pointer, which we may fix like so:
193 *_NSGetArgv() = new_argv;
198 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
204 * Call this once during subprocess startup to set the identification
205 * values. At this point, the original argv[] array may be overwritten.
208 init_ps_display(const char *username, const char *dbname,
209 const char *host_info)
216 /* no ps display for stand-alone backend */
217 if (!IsUnderPostmaster)
220 /* no ps display if you didn't call save_ps_display_args() */
223 #ifdef PS_USE_CLOBBER_ARGV
224 /* If ps_buffer is a pointer, it might still be null */
230 * Overwrite argv[] to point at appropriate space, if needed
233 #ifdef PS_USE_CHANGE_ARGV
234 save_argv[0] = ps_buffer;
236 #endif /* PS_USE_CHANGE_ARGV */
238 #ifdef PS_USE_CLOBBER_ARGV
242 /* make extra argv slots point at end_of_area (a NUL) */
243 for (i = 1; i < save_argc; i++)
244 save_argv[i] = ps_buffer + ps_buffer_size;
246 #endif /* PS_USE_CLOBBER_ARGV */
249 * Make fixed prefix of ps display.
252 #ifdef PS_USE_SETPROCTITLE
255 * apparently setproctitle() already adds a `progname:' prefix to the
258 snprintf(ps_buffer, ps_buffer_size,
260 username, dbname, host_info);
262 snprintf(ps_buffer, ps_buffer_size,
263 "postgres: %s %s %s ",
264 username, dbname, host_info);
267 ps_buffer_fixed_size = strlen(ps_buffer);
268 #endif /* not PS_USE_NONE */
274 * Call this to update the ps status display to a fixed prefix plus an
275 * indication of what you're currently doing passed in the argument.
278 set_ps_display(const char *activity)
280 /* no ps display for stand-alone backend */
281 if (!IsUnderPostmaster)
284 /* save it for logging context */
286 MyProcPort->commandTag = (char *) activity;
290 #ifdef PS_USE_CLOBBER_ARGV
291 /* If ps_buffer is a pointer, it might still be null */
296 /* Update ps_buffer to contain both fixed part and activity */
297 StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
298 ps_buffer_size - ps_buffer_fixed_size);
300 /* Transmit new setting to kernel, if necessary */
302 #ifdef PS_USE_SETPROCTITLE
303 setproctitle("%s", ps_buffer);
310 pst.pst_command = ps_buffer;
311 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
313 #endif /* PS_USE_PSTAT */
315 #ifdef PS_USE_PS_STRINGS
316 PS_STRINGS->ps_nargvstr = 1;
317 PS_STRINGS->ps_argvstr = ps_buffer;
318 #endif /* PS_USE_PS_STRINGS */
320 #ifdef PS_USE_CLOBBER_ARGV
324 /* pad unused memory */
325 buflen = strlen(ps_buffer);
326 MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
328 #endif /* PS_USE_CLOBBER_ARGV */
329 #endif /* not PS_USE_NONE */
334 * Returns what's currently in the ps display, in case someone needs
335 * it. Note that only the activity part is returned.
340 #ifdef PS_USE_CLOBBER_ARGV
341 /* If ps_buffer is a pointer, it might still be null */
346 return ps_buffer + ps_buffer_fixed_size;