summaryrefslogtreecommitdiff
path: root/src/backend/utils/misc/ps_status.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/misc/ps_status.c')
-rw-r--r--src/backend/utils/misc/ps_status.c114
1 files changed, 65 insertions, 49 deletions
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 98fff4db5b4..9441aa00561 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -5,7 +5,7 @@
* to contain some useful information. Mechanism differs wildly across
* platforms.
*
- * $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.6 2001/10/21 03:25:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.7 2001/10/22 19:41:38 tgl Exp $
*
* Copyright 2000 by PostgreSQL Global Development Group
* various details abducted from various places
@@ -95,45 +95,22 @@ static char **save_argv;
/*
* Call this early in startup to save the original argc/argv values.
+ *
* argv[] will not be overwritten by this routine, but may be overwritten
- * during init_ps_display.
+ * during init_ps_display. Also, the physical location of the environment
+ * strings may be moved, so this should be called before any code that
+ * might try to hang onto a getenv() result.
*/
void
save_ps_display_args(int argc, char *argv[])
{
save_argc = argc;
save_argv = argv;
-}
-
-/*
- * Call this once during subprocess startup to set the identification
- * values. At this point, the original argv[] array may be overwritten.
- */
-void
-init_ps_display(const char *username, const char *dbname,
- const char *host_info)
-{
-#ifndef PS_USE_NONE
- Assert(username);
- Assert(dbname);
-
- /* no ps display for stand-alone backend */
- if (!IsUnderPostmaster)
- return;
-
- /* no ps display if you didn't call save_ps_display_args() */
- if (!save_argv)
- return;
-
-#ifdef PS_USE_CHANGE_ARGV
- save_argv[0] = ps_buffer;
- save_argv[1] = NULL;
-#endif /* PS_USE_CHANGE_ARGV */
#ifdef PS_USE_CLOBBER_ARGV
-
/*
- * If we're going to overwrite the argv area, count the space.
+ * If we're going to overwrite the argv area, count the available
+ * space. Also move the environment to make additional room.
*/
{
char *end_of_area = NULL;
@@ -143,35 +120,34 @@ init_ps_display(const char *username, const char *dbname,
/*
* check for contiguous argv strings
*/
- for (i = 0; i < save_argc; i++)
- if (i == 0 || end_of_area + 1 == save_argv[i])
- end_of_area = save_argv[i] + strlen(save_argv[i]);
-
- /*
- * check for contiguous environ strings following argv
- */
- for (i = 0; end_of_area != NULL && environ[i] != NULL; i++)
- if (end_of_area + 1 == environ[i])
- end_of_area = environ[i] + strlen(environ[i]);
+ for (i = 0; i < argc; i++)
+ {
+ if (i == 0 || end_of_area + 1 == argv[i])
+ end_of_area = argv[i] + strlen(argv[i]);
+ }
- if (end_of_area == NULL)
+ if (end_of_area == NULL) /* probably can't happen? */
{
ps_buffer = NULL;
ps_buffer_size = 0;
return;
}
- else
+
+ /*
+ * check for contiguous environ strings following argv
+ */
+ for (i = 0; environ[i] != NULL; i++)
{
- ps_buffer = save_argv[0];
- ps_buffer_size = end_of_area - save_argv[0] - 1;
+ if (end_of_area + 1 == environ[i])
+ end_of_area = environ[i] + strlen(environ[i]);
}
- save_argv[1] = NULL;
+
+ ps_buffer = argv[0];
+ ps_buffer_size = end_of_area - argv[0] - 1;
/*
* move the environment out of the way
*/
- for (i = 0; environ[i] != NULL; i++)
- ;
new_environ = malloc(sizeof(char *) * (i + 1));
for (i = 0; environ[i] != NULL; i++)
new_environ[i] = strdup(environ[i]);
@@ -179,12 +155,52 @@ init_ps_display(const char *username, const char *dbname,
environ = new_environ;
}
#endif /* PS_USE_CLOBBER_ARGV */
+}
+
+/*
+ * Call this once during subprocess startup to set the identification
+ * values. At this point, the original argv[] array may be overwritten.
+ */
+void
+init_ps_display(const char *username, const char *dbname,
+ const char *host_info)
+{
+ Assert(username);
+ Assert(dbname);
+ Assert(host_info);
+
+#ifndef PS_USE_NONE
+ /* no ps display for stand-alone backend */
+ if (!IsUnderPostmaster)
+ return;
+
+ /* no ps display if you didn't call save_ps_display_args() */
+ if (!save_argv)
+ return;
+#ifdef PS_USE_CLOBBER_ARGV
+ /* If ps_buffer is a pointer, it might still be null */
+ if (!ps_buffer)
+ return;
+#endif
+
+ /*
+ * Overwrite argv[] to point at appropriate space, if needed
+ */
+
+#ifdef PS_USE_CHANGE_ARGV
+ save_argv[0] = ps_buffer;
+ save_argv[1] = NULL;
+#endif /* PS_USE_CHANGE_ARGV */
+
+#ifdef PS_USE_CLOBBER_ARGV
+ save_argv[1] = NULL;
+#endif /* PS_USE_CLOBBER_ARGV */
/*
- * Make fixed prefix
+ * Make fixed prefix of ps display.
*/
-#ifdef PS_USE_SETPROCTITLE
+#ifdef PS_USE_SETPROCTITLE
/*
* apparently setproctitle() already adds a `progname:' prefix to the
* ps line