ps_status.c 6.54 KB
Newer Older
1 2 3 4
/*--------------------------------------------------------------------
 * ps_status.c
 *
 * Routines to support changing the ps display of PostgreSQL backends
5
 * to contain some useful information. Mechanism differs wildly across
6 7
 * platforms.
 *
8
 * $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.6 2001/10/21 03:25:35 tgl Exp $
9 10 11 12 13 14 15 16 17 18
 *
 * Copyright 2000 by PostgreSQL Global Development Group
 * various details abducted from various places
 *--------------------------------------------------------------------
 */

#include "postgres.h"

#include <unistd.h>
#ifdef HAVE_SYS_PSTAT_H
19
#include <sys/pstat.h>			/* for HP-UX */
20
#endif
21
#ifdef HAVE_PS_STRINGS
22 23
#include <machine/vmparam.h>	/* for old BSD */
#include <sys/exec.h>
24 25 26 27 28 29 30 31 32 33 34 35
#endif

#include "miscadmin.h"
#include "utils/ps_status.h"

extern char **environ;


/*------
 * Alternative ways of updating ps display:
 *
 * PS_USE_SETPROCTITLE
36 37
 *	   use the function setproctitle(const char *, ...)
 *	   (newer BSD systems)
38
 * PS_USE_PSTAT
39 40
 *	   use the pstat(PSTAT_SETCMD, )
 *	   (HPUX)
41
 * PS_USE_PS_STRINGS
42 43
 *	   assign PS_STRINGS->ps_argvstr = "string"
 *	   (some BSD systems)
44
 * PS_USE_CHANGE_ARGV
45 46
 *	   assign argv[0] = "string"
 *	   (some other BSD systems)
47
 * PS_USE_CLOBBER_ARGV
48 49
 *	   write over the argv and environment area
 *	   (most SysV-like systems)
50
 * PS_USE_NONE
51 52
 *	   don't update ps display
 *	   (This is the default, as it is safest.)
53 54
 */
#if defined(HAVE_SETPROCTITLE)
55
#define PS_USE_SETPROCTITLE
56
#elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
57
#define PS_USE_PSTAT
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
64
#else
65
#define PS_USE_NONE
66 67 68 69
#endif


/* Different systems want the buffer padded differently */
70
#if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
71
#define PS_PADDING '\0'
72
#else
73
#define PS_PADDING ' '
74 75 76 77 78 79 80 81 82
#endif


#ifndef PS_USE_CLOBBER_ARGV
/* all but one options need a buffer to write their ps line in */
#define PS_BUFFER_SIZE 256
static char ps_buffer[PS_BUFFER_SIZE];
static const size_t ps_buffer_size = PS_BUFFER_SIZE;

83 84 85
#else							/* PS_USE_CLOBBER_ARGV */
static char *ps_buffer;			/* will point to argv area */
static size_t ps_buffer_size;	/* space determined at run time */
86

87 88 89
#endif	 /* PS_USE_CLOBBER_ARGV */

static size_t ps_buffer_fixed_size;		/* size of the constant prefix */
90

91 92 93
/* save the original argv[] location here */
static int	save_argc;
static char **save_argv;
94 95 96


/*
97 98 99
 * 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.
100 101
 */
void
102 103 104 105 106 107 108 109 110 111 112 113
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,
114
				const char *host_info)
115 116
{
#ifndef PS_USE_NONE
117 118
	Assert(username);
	Assert(dbname);
119 120

	/* no ps display for stand-alone backend */
121 122
	if (!IsUnderPostmaster)
		return;
123

124 125 126 127
	/* no ps display if you didn't call save_ps_display_args() */
	if (!save_argv)
		return;

128
#ifdef PS_USE_CHANGE_ARGV
129 130
	save_argv[0] = ps_buffer;
	save_argv[1] = NULL;
131
#endif	 /* PS_USE_CHANGE_ARGV */
132

133 134 135 136 137
#ifdef PS_USE_CLOBBER_ARGV

	/*
	 * If we're going to overwrite the argv area, count the space.
	 */
138
	{
139 140 141
		char	   *end_of_area = NULL;
		char	  **new_environ;
		int			i;
142 143 144 145

		/*
		 * check for contiguous argv strings
		 */
146 147 148
		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]);
149 150 151 152 153 154 155 156 157 158 159 160

		/*
		 * 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]);

		if (end_of_area == NULL)
		{
			ps_buffer = NULL;
			ps_buffer_size = 0;
161
			return;
162 163 164
		}
		else
		{
165 166
			ps_buffer = save_argv[0];
			ps_buffer_size = end_of_area - save_argv[0] - 1;
167
		}
168
		save_argv[1] = NULL;
169 170 171 172 173 174

		/*
		 * move the environment out of the way
		 */
		for (i = 0; environ[i] != NULL; i++)
			;
175
		new_environ = malloc(sizeof(char *) * (i + 1));
176
		for (i = 0; environ[i] != NULL; i++)
177
			new_environ[i] = strdup(environ[i]);
178 179 180
		new_environ[i] = NULL;
		environ = new_environ;
	}
181
#endif	 /* PS_USE_CLOBBER_ARGV */
182 183 184 185

	/*
	 * Make fixed prefix
	 */
186 187 188 189 190 191
#ifdef PS_USE_SETPROCTITLE

	/*
	 * apparently setproctitle() already adds a `progname:' prefix to the
	 * ps line
	 */
192 193 194
	snprintf(ps_buffer, ps_buffer_size,
			 "%s %s %s ",
			 username, dbname, host_info);
195
#else
196 197 198
	snprintf(ps_buffer, ps_buffer_size,
			 "postgres: %s %s %s ",
			 username, dbname, host_info);
199
#endif
200

201 202
	ps_buffer_fixed_size = strlen(ps_buffer);
#endif	 /* not PS_USE_NONE */
203 204 205 206 207 208 209 210 211
}



/*
 * Call this to update the ps status display to a fixed prefix plus an
 * indication of what you're currently doing passed in the argument.
 */
void
212
set_ps_display(const char *activity)
213
{
214
#ifndef PS_USE_NONE
215 216 217 218
	/* no ps display for stand-alone backend */
	if (!IsUnderPostmaster)
		return;

219
#ifdef PS_USE_CLOBBER_ARGV
220
	/* If ps_buffer is a pointer, it might still be null */
221 222
	if (!ps_buffer)
		return;
223
#endif
224

225 226
	/* Update ps_buffer to contain both fixed part and activity */
	StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
227
			ps_buffer_size - ps_buffer_fixed_size);
228

229
	/* Transmit new setting to kernel, if necessary */
230

231
#ifdef PS_USE_SETPROCTITLE
232
	setproctitle("%s", ps_buffer);
233
#endif
234

235 236 237
#ifdef PS_USE_PSTAT
	{
		union pstun pst;
238

239 240 241 242
		pst.pst_command = ps_buffer;
		pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
	}
#endif	 /* PS_USE_PSTAT */
243

244 245 246 247
#ifdef PS_USE_PS_STRINGS
	PS_STRINGS->ps_nargvstr = 1;
	PS_STRINGS->ps_argvstr = ps_buffer;
#endif	 /* PS_USE_PS_STRINGS */
248

249 250 251 252 253 254 255 256 257 258 259
#ifdef PS_USE_CLOBBER_ARGV
	{
		char	   *cp;

		/* pad unused memory */
		for (cp = ps_buffer + strlen(ps_buffer);
			 cp < ps_buffer + ps_buffer_size;
			 cp++)
			*cp = PS_PADDING;
	}
#endif	 /* PS_USE_CLOBBER_ARGV */
260

261
#endif	 /* not PS_USE_NONE */
262 263 264 265 266
}


/*
 * Returns what's currently in the ps display, in case someone needs
267
 * it.	Note that only the activity part is returned.
268 269 270 271
 */
const char *
get_ps_display(void)
{
272 273 274 275 276 277
#ifdef PS_USE_CLOBBER_ARGV
	/* If ps_buffer is a pointer, it might still be null */
	if (!ps_buffer)
		return "";
#endif

278 279
	return ps_buffer + ps_buffer_fixed_size;
}