1 /*--------------------------------------------------------------------
4 * Routines to support changing the ps display of PostgreSQL backends
5 * to contain some useful information. Differs wildly across
8 * $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.4 2001/03/22 04:00:06 momjian Exp $
10 * Copyright 2000 by PostgreSQL Global Development Group
11 * various details abducted from various places
12 *--------------------------------------------------------------------
20 #ifdef HAVE_SYS_PSTAT_H
21 #include <sys/pstat.h> /* for HP-UX */
23 #ifdef HAVE_PS_STRINGS
24 #include <machine/vmparam.h> /* for old BSD */
28 #include "miscadmin.h"
30 #include "utils/ps_status.h"
32 extern char **environ;
36 * Alternative ways of updating ps display:
39 * use the function setproctitle(const char *, ...)
42 * use the pstat(PSTAT_SETCMD, )
45 * assign PS_STRINGS->ps_argvstr = "string"
48 * assign argv[0] = "string"
49 * (some other BSD systems)
51 * write over the argv and environment area
52 * (most SysV-like systems)
54 * don't update ps display
55 * (This is the default, as it is safest.)
57 #if defined(HAVE_SETPROCTITLE)
58 #define PS_USE_SETPROCTITLE
59 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
61 #elif defined(HAVE_PS_STRINGS)
62 #define PS_USE_PS_STRINGS
63 #elif defined(BSD) || defined(__bsdi__) || defined(__hurd__)
64 #define PS_USE_CHANGE_ARGV
65 #elif defined(__linux__) || defined(_AIX4) || defined(_AIX3) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__QNX__) || defined(__svr4__) || defined(__svr5__)
66 #define PS_USE_CLOBBER_ARGV
72 /* Different systems want the buffer padded differently */
73 #if defined(_AIX3) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
74 #define PS_PADDING '\0'
76 #define PS_PADDING ' '
80 #ifndef PS_USE_CLOBBER_ARGV
81 /* all but one options need a buffer to write their ps line in */
82 #define PS_BUFFER_SIZE 256
83 static char ps_buffer[PS_BUFFER_SIZE];
84 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
86 #else /* PS_USE_CLOBBER_ARGV */
87 static char *ps_buffer; /* will point to argv area */
88 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 */
97 * Call this once at backend start.
100 init_ps_display(int argc, char *argv[],
101 const char *username, const char *dbname,
102 const char *host_info)
108 /* no ps display for stand-alone backend */
109 if (!IsUnderPostmaster)
112 #ifdef PS_USE_CHANGE_ARGV
115 #endif /* PS_USE_CHANGE_ARGV */
117 #ifdef PS_USE_CLOBBER_ARGV
120 * If we're going to overwrite the argv area, count the space.
123 char *end_of_area = NULL;
128 * check for contiguous argv strings
130 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 * check for contiguous environ strings following argv
137 for (i = 0; end_of_area != NULL && environ[i] != NULL; i++)
138 if (end_of_area + 1 == environ[i])
139 end_of_area = environ[i] + strlen(environ[i]);
141 if (end_of_area == NULL)
149 ps_buffer_size = end_of_area - argv[0] - 1;
154 * move the environment out of the way
156 for (i = 0; environ[i] != NULL; i++)
158 new_environ = malloc(sizeof(char *) * (i + 1));
159 for (i = 0; environ[i] != NULL; i++)
160 new_environ[i] = strdup(environ[i]);
161 new_environ[i] = NULL;
162 environ = new_environ;
164 #endif /* PS_USE_CLOBBER_ARGV */
169 #ifdef PS_USE_SETPROCTITLE
172 * apparently setproctitle() already adds a `progname:' prefix to the
175 snprintf(ps_buffer, ps_buffer_size,
177 username, dbname, host_info);
179 snprintf(ps_buffer, ps_buffer_size,
180 "postgres: %s %s %s ",
181 username, dbname, host_info);
184 ps_buffer_fixed_size = strlen(ps_buffer);
185 #endif /* not PS_USE_NONE */
191 * Call this to update the ps status display to a fixed prefix plus an
192 * indication of what you're currently doing passed in the argument.
195 set_ps_display(const char *value)
198 /* no ps display for stand-alone backend */
199 if (!IsUnderPostmaster)
202 #ifdef PS_USE_CLOBBER_ARGV
203 /* If ps_buffer is a pointer, it might still be null */
208 /* Update ps_buffer to contain both fixed part and value */
209 StrNCpy(ps_buffer + ps_buffer_fixed_size, value,
210 ps_buffer_size - ps_buffer_fixed_size);
212 /* Transmit new setting to kernel, if necessary */
214 #ifdef PS_USE_SETPROCTITLE
215 setproctitle("%s", ps_buffer);
222 pst.pst_command = ps_buffer;
223 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
225 #endif /* PS_USE_PSTAT */
227 #ifdef PS_USE_PS_STRINGS
228 PS_STRINGS->ps_nargvstr = 1;
229 PS_STRINGS->ps_argvstr = ps_buffer;
230 #endif /* PS_USE_PS_STRINGS */
232 #ifdef PS_USE_CLOBBER_ARGV
236 /* pad unused memory */
237 for (cp = ps_buffer + strlen(ps_buffer);
238 cp < ps_buffer + ps_buffer_size;
242 #endif /* PS_USE_CLOBBER_ARGV */
244 #endif /* not PS_USE_NONE */
249 * Returns what's currently in the ps display, in case someone needs
250 * it. Note that only the variable part is returned.
255 #ifdef PS_USE_CLOBBER_ARGV
256 /* If ps_buffer is a pointer, it might still be null */
261 return ps_buffer + ps_buffer_fixed_size;