Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / utils / misc / ps_status.c
blobba602cafd9014bbfbd0f1f7969f12457c3dd4def
1 /*--------------------------------------------------------------------
2 * ps_status.c
4 * Routines to support changing the ps display of PostgreSQL backends
5 * to contain some useful information. Mechanism differs wildly across
6 * platforms.
8 * $PostgreSQL$
10 * Copyright (c) 2000-2008, PostgreSQL Global Development Group
11 * various details abducted from various places
12 *--------------------------------------------------------------------
15 #include "postgres.h"
17 #include <unistd.h>
18 #ifdef HAVE_SYS_PSTAT_H
19 #include <sys/pstat.h> /* for HP-UX */
20 #endif
21 #ifdef HAVE_PS_STRINGS
22 #include <machine/vmparam.h> /* for old BSD */
23 #include <sys/exec.h>
24 #endif
25 #if defined(__darwin__)
26 #include <crt_externs.h>
27 #endif
29 #include "libpq/libpq.h"
30 #include "miscadmin.h"
31 #include "utils/ps_status.h"
33 extern char **environ;
34 bool update_process_title = true;
38 * Alternative ways of updating ps display:
40 * PS_USE_SETPROCTITLE
41 * use the function setproctitle(const char *, ...)
42 * (newer BSD systems)
43 * PS_USE_PSTAT
44 * use the pstat(PSTAT_SETCMD, )
45 * (HPUX)
46 * PS_USE_PS_STRINGS
47 * assign PS_STRINGS->ps_argvstr = "string"
48 * (some BSD systems)
49 * PS_USE_CHANGE_ARGV
50 * assign argv[0] = "string"
51 * (some other BSD systems)
52 * PS_USE_CLOBBER_ARGV
53 * write over the argv and environment area
54 * (most SysV-like systems)
55 * PS_USE_WIN32
56 * push the string out as the name of a Windows event
57 * PS_USE_NONE
58 * don't update ps display
59 * (This is the default, as it is safest.)
61 #if defined(HAVE_SETPROCTITLE)
62 #define PS_USE_SETPROCTITLE
63 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
64 #define PS_USE_PSTAT
65 #elif defined(HAVE_PS_STRINGS)
66 #define PS_USE_PS_STRINGS
67 #elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__)
68 #define PS_USE_CHANGE_ARGV
69 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__)
70 #define PS_USE_CLOBBER_ARGV
71 #elif defined(WIN32)
72 #define PS_USE_WIN32
73 #else
74 #define PS_USE_NONE
75 #endif
78 /* Different systems want the buffer padded differently */
79 #if defined(_AIX) || defined(__linux__) || defined(__svr4__)
80 #define PS_PADDING '\0'
81 #else
82 #define PS_PADDING ' '
83 #endif
86 #ifndef PS_USE_CLOBBER_ARGV
87 /* all but one options need a buffer to write their ps line in */
88 #define PS_BUFFER_SIZE 256
89 static char ps_buffer[PS_BUFFER_SIZE];
90 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
91 #else /* PS_USE_CLOBBER_ARGV */
92 static char *ps_buffer; /* will point to argv area */
93 static size_t ps_buffer_size; /* space determined at run time */
94 static size_t last_status_len; /* use to minimize length of clobber */
95 #endif /* PS_USE_CLOBBER_ARGV */
97 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
99 /* save the original argv[] location here */
100 static int save_argc;
101 static char **save_argv;
105 * Call this early in startup to save the original argc/argv values.
106 * If needed, we make a copy of the original argv[] array to preserve it
107 * from being clobbered by subsequent ps_display actions.
109 * (The original argv[] will not be overwritten by this routine, but may be
110 * overwritten during init_ps_display. Also, the physical location of the
111 * environment strings may be moved, so this should be called before any code
112 * that might try to hang onto a getenv() result.)
114 char **
115 save_ps_display_args(int argc, char **argv)
117 save_argc = argc;
118 save_argv = argv;
120 #if defined(PS_USE_CLOBBER_ARGV)
123 * If we're going to overwrite the argv area, count the available space.
124 * Also move the environment to make additional room.
127 char *end_of_area = NULL;
128 char **new_environ;
129 int i;
132 * check for contiguous argv strings
134 for (i = 0; i < argc; i++)
136 if (i == 0 || end_of_area + 1 == argv[i])
137 end_of_area = argv[i] + strlen(argv[i]);
140 if (end_of_area == NULL) /* probably can't happen? */
142 ps_buffer = NULL;
143 ps_buffer_size = 0;
144 return argv;
148 * check for contiguous environ strings following argv
150 for (i = 0; environ[i] != NULL; i++)
152 if (end_of_area + 1 == environ[i])
153 end_of_area = environ[i] + strlen(environ[i]);
156 ps_buffer = argv[0];
157 last_status_len = ps_buffer_size = end_of_area - argv[0];
160 * move the environment out of the way
162 new_environ = (char **) malloc((i + 1) * sizeof(char *));
163 for (i = 0; environ[i] != NULL; i++)
164 new_environ[i] = strdup(environ[i]);
165 new_environ[i] = NULL;
166 environ = new_environ;
168 #endif /* PS_USE_CLOBBER_ARGV */
170 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
173 * If we're going to change the original argv[] then make a copy for
174 * argument parsing purposes.
176 * (NB: do NOT think to remove the copying of argv[], even though
177 * postmaster.c finishes looking at argv[] long before we ever consider
178 * changing the ps display. On some platforms, getopt() keeps pointers
179 * into the argv array, and will get horribly confused when it is
180 * re-called to analyze a subprocess' argument string if the argv storage
181 * has been clobbered meanwhile. Other platforms have other dependencies
182 * on argv[].
185 char **new_argv;
186 int i;
188 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
189 for (i = 0; i < argc; i++)
190 new_argv[i] = strdup(argv[i]);
191 new_argv[argc] = NULL;
193 #if defined(__darwin__)
196 * Darwin (and perhaps other NeXT-derived platforms?) has a static
197 * copy of the argv pointer, which we may fix like so:
199 *_NSGetArgv() = new_argv;
200 #endif
202 argv = new_argv;
204 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
206 return argv;
210 * Call this once during subprocess startup to set the identification
211 * values. At this point, the original argv[] array may be overwritten.
213 void
214 init_ps_display(const char *username, const char *dbname,
215 const char *host_info, const char *initial_str)
217 Assert(username);
218 Assert(dbname);
219 Assert(host_info);
221 #ifndef PS_USE_NONE
222 /* no ps display for stand-alone backend */
223 if (!IsUnderPostmaster)
224 return;
226 /* no ps display if you didn't call save_ps_display_args() */
227 if (!save_argv)
228 return;
229 #ifdef PS_USE_CLOBBER_ARGV
230 /* If ps_buffer is a pointer, it might still be null */
231 if (!ps_buffer)
232 return;
233 #endif
236 * Overwrite argv[] to point at appropriate space, if needed
239 #ifdef PS_USE_CHANGE_ARGV
240 save_argv[0] = ps_buffer;
241 save_argv[1] = NULL;
242 #endif /* PS_USE_CHANGE_ARGV */
244 #ifdef PS_USE_CLOBBER_ARGV
246 int i;
248 /* make extra argv slots point at end_of_area (a NUL) */
249 for (i = 1; i < save_argc; i++)
250 save_argv[i] = ps_buffer + ps_buffer_size;
252 #endif /* PS_USE_CLOBBER_ARGV */
255 * Make fixed prefix of ps display.
258 #ifdef PS_USE_SETPROCTITLE
261 * apparently setproctitle() already adds a `progname:' prefix to the ps
262 * line
264 snprintf(ps_buffer, ps_buffer_size,
265 "%s %s %s ",
266 username, dbname, host_info);
267 #else
268 snprintf(ps_buffer, ps_buffer_size,
269 "postgres: %s %s %s ",
270 username, dbname, host_info);
271 #endif
273 ps_buffer_fixed_size = strlen(ps_buffer);
275 set_ps_display(initial_str, true);
276 #endif /* not PS_USE_NONE */
282 * Call this to update the ps status display to a fixed prefix plus an
283 * indication of what you're currently doing passed in the argument.
285 void
286 set_ps_display(const char *activity, bool force)
289 if (!force && !update_process_title)
290 return;
292 #ifndef PS_USE_NONE
293 /* no ps display for stand-alone backend */
294 if (!IsUnderPostmaster)
295 return;
297 #ifdef PS_USE_CLOBBER_ARGV
298 /* If ps_buffer is a pointer, it might still be null */
299 if (!ps_buffer)
300 return;
301 #endif
303 /* Update ps_buffer to contain both fixed part and activity */
304 strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
305 ps_buffer_size - ps_buffer_fixed_size);
307 /* Transmit new setting to kernel, if necessary */
309 #ifdef PS_USE_SETPROCTITLE
310 setproctitle("%s", ps_buffer);
311 #endif
313 #ifdef PS_USE_PSTAT
315 union pstun pst;
317 pst.pst_command = ps_buffer;
318 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
320 #endif /* PS_USE_PSTAT */
322 #ifdef PS_USE_PS_STRINGS
323 PS_STRINGS->ps_nargvstr = 1;
324 PS_STRINGS->ps_argvstr = ps_buffer;
325 #endif /* PS_USE_PS_STRINGS */
327 #ifdef PS_USE_CLOBBER_ARGV
329 int buflen;
331 /* pad unused memory */
332 buflen = strlen(ps_buffer);
333 /* clobber remainder of old status string */
334 if (last_status_len > buflen)
335 MemSet(ps_buffer + buflen, PS_PADDING, last_status_len - buflen);
336 last_status_len = buflen;
338 #endif /* PS_USE_CLOBBER_ARGV */
340 #ifdef PS_USE_WIN32
343 * Win32 does not support showing any changed arguments. To make it at
344 * all possible to track which backend is doing what, we create a
345 * named object that can be viewed with for example Process Explorer.
347 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
348 char name[PS_BUFFER_SIZE + 32];
350 if (ident_handle != INVALID_HANDLE_VALUE)
351 CloseHandle(ident_handle);
353 sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
355 ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
357 #endif /* PS_USE_WIN32 */
358 #endif /* not PS_USE_NONE */
363 * Returns what's currently in the ps display, in case someone needs
364 * it. Note that only the activity part is returned. On some platforms
365 * the string will not be null-terminated, so return the effective
366 * length into *displen.
368 const char *
369 get_ps_display(int *displen)
371 #ifdef PS_USE_CLOBBER_ARGV
372 size_t offset;
374 /* If ps_buffer is a pointer, it might still be null */
375 if (!ps_buffer)
377 *displen = 0;
378 return "";
381 /* Remove any trailing spaces to offset the effect of PS_PADDING */
382 offset = ps_buffer_size;
383 while (offset > ps_buffer_fixed_size && ps_buffer[offset - 1] == PS_PADDING)
384 offset--;
386 *displen = offset - ps_buffer_fixed_size;
387 #else
388 *displen = strlen(ps_buffer + ps_buffer_fixed_size);
389 #endif
391 return ps_buffer + ps_buffer_fixed_size;