1 /*--------------------------------------------------------------------
4 * Routines to support changing the ps display of PostgreSQL backends
5 * to contain some useful information. Mechanism differs wildly across
8 * $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.9 2001/10/28 06:25:57 momjian Exp $
10 * Copyright 2000 by 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 */
26 #include "miscadmin.h"
27 #include "utils/ps_status.h"
29 extern char **environ;
33 * Alternative ways of updating ps display:
36 * use the function setproctitle(const char *, ...)
39 * use the pstat(PSTAT_SETCMD, )
42 * assign PS_STRINGS->ps_argvstr = "string"
45 * assign argv[0] = "string"
46 * (some other BSD systems)
48 * write over the argv and environment area
49 * (most SysV-like systems)
51 * don't update ps display
52 * (This is the default, as it is safest.)
54 #if defined(HAVE_SETPROCTITLE)
55 #define PS_USE_SETPROCTITLE
56 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
58 #elif defined(HAVE_PS_STRINGS)
59 #define PS_USE_PS_STRINGS
60 #elif defined(BSD) || defined(__bsdi__) || defined(__hurd__)
61 #define PS_USE_CHANGE_ARGV
62 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__QNX__) || defined(__svr4__) || defined(__svr5__)
63 #define PS_USE_CLOBBER_ARGV
69 /* Different systems want the buffer padded differently */
70 #if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
71 #define PS_PADDING '\0'
73 #define PS_PADDING ' '
77 #ifndef PS_USE_CLOBBER_ARGV
78 /* all but one options need a buffer to write their ps line in */
79 #define PS_BUFFER_SIZE 256
80 static char ps_buffer[PS_BUFFER_SIZE];
81 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
83 #else /* PS_USE_CLOBBER_ARGV */
84 static char *ps_buffer; /* will point to argv area */
85 static size_t ps_buffer_size; /* space determined at run time */
86 #endif /* PS_USE_CLOBBER_ARGV */
88 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
90 /* save the original argv[] location here */
92 static char **save_argv;
96 * Call this early in startup to save the original argc/argv values.
98 * argv[] will not be overwritten by this routine, but may be overwritten
99 * during init_ps_display. Also, the physical location of the environment
100 * strings may be moved, so this should be called before any code that
101 * might try to hang onto a getenv() result.
104 save_ps_display_args(int argc, char *argv[])
109 #ifdef PS_USE_CLOBBER_ARGV
112 * If we're going to overwrite the argv area, count the available
113 * space. Also move the environment to make additional room.
116 char *end_of_area = NULL;
121 * check for contiguous argv strings
123 for (i = 0; i < argc; i++)
125 if (i == 0 || end_of_area + 1 == argv[i])
126 end_of_area = argv[i] + strlen(argv[i]);
129 if (end_of_area == NULL) /* probably can't happen? */
137 * check for contiguous environ strings following argv
139 for (i = 0; environ[i] != NULL; i++)
141 if (end_of_area + 1 == environ[i])
142 end_of_area = environ[i] + strlen(environ[i]);
146 ps_buffer_size = end_of_area - argv[0] - 1;
149 * move the environment out of the way
151 new_environ = malloc(sizeof(char *) * (i + 1));
152 for (i = 0; environ[i] != NULL; i++)
153 new_environ[i] = strdup(environ[i]);
154 new_environ[i] = NULL;
155 environ = new_environ;
157 #endif /* PS_USE_CLOBBER_ARGV */
161 * Call this once during subprocess startup to set the identification
162 * values. At this point, the original argv[] array may be overwritten.
165 init_ps_display(const char *username, const char *dbname,
166 const char *host_info)
173 /* no ps display for stand-alone backend */
174 if (!IsUnderPostmaster)
177 /* no ps display if you didn't call save_ps_display_args() */
180 #ifdef PS_USE_CLOBBER_ARGV
181 /* If ps_buffer is a pointer, it might still be null */
187 * Overwrite argv[] to point at appropriate space, if needed
190 #ifdef PS_USE_CHANGE_ARGV
191 save_argv[0] = ps_buffer;
193 #endif /* PS_USE_CHANGE_ARGV */
195 #ifdef PS_USE_CLOBBER_ARGV
197 #endif /* PS_USE_CLOBBER_ARGV */
200 * Make fixed prefix of ps display.
203 #ifdef PS_USE_SETPROCTITLE
206 * apparently setproctitle() already adds a `progname:' prefix to the
209 snprintf(ps_buffer, ps_buffer_size,
211 username, dbname, host_info);
213 snprintf(ps_buffer, ps_buffer_size,
214 "postgres: %s %s %s ",
215 username, dbname, host_info);
218 ps_buffer_fixed_size = strlen(ps_buffer);
219 #endif /* not PS_USE_NONE */
225 * Call this to update the ps status display to a fixed prefix plus an
226 * indication of what you're currently doing passed in the argument.
229 set_ps_display(const char *activity)
232 /* no ps display for stand-alone backend */
233 if (!IsUnderPostmaster)
236 #ifdef PS_USE_CLOBBER_ARGV
237 /* If ps_buffer is a pointer, it might still be null */
242 /* Update ps_buffer to contain both fixed part and activity */
243 StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
244 ps_buffer_size - ps_buffer_fixed_size);
246 /* Transmit new setting to kernel, if necessary */
248 #ifdef PS_USE_SETPROCTITLE
249 setproctitle("%s", ps_buffer);
256 pst.pst_command = ps_buffer;
257 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
259 #endif /* PS_USE_PSTAT */
261 #ifdef PS_USE_PS_STRINGS
262 PS_STRINGS->ps_nargvstr = 1;
263 PS_STRINGS->ps_argvstr = ps_buffer;
264 #endif /* PS_USE_PS_STRINGS */
266 #ifdef PS_USE_CLOBBER_ARGV
270 /* pad unused memory */
271 for (cp = ps_buffer + strlen(ps_buffer);
272 cp < ps_buffer + ps_buffer_size;
276 #endif /* PS_USE_CLOBBER_ARGV */
277 #endif /* not PS_USE_NONE */
282 * Returns what's currently in the ps display, in case someone needs
283 * it. Note that only the activity part is returned.
288 #ifdef PS_USE_CLOBBER_ARGV
289 /* If ps_buffer is a pointer, it might still be null */
294 return ps_buffer + ps_buffer_fixed_size;