]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/ps_status.c
pgindent run for 9.0, second run
[postgresql] / src / backend / utils / misc / ps_status.c
1 /*--------------------------------------------------------------------
2  * ps_status.c
3  *
4  * Routines to support changing the ps display of PostgreSQL backends
5  * to contain some useful information. Mechanism differs wildly across
6  * platforms.
7  *
8  * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.42 2010/07/06 19:18:59 momjian Exp $
9  *
10  * Copyright (c) 2000-2010, PostgreSQL Global Development Group
11  * various details abducted from various places
12  *--------------------------------------------------------------------
13  */
14
15 #include "postgres.h"
16
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
28
29 #include "libpq/libpq.h"
30 #include "miscadmin.h"
31 #include "utils/ps_status.h"
32
33 extern char **environ;
34 bool            update_process_title = true;
35
36
37 /*
38  * Alternative ways of updating ps display:
39  *
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  *         (Linux and 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.)
60  */
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
76
77
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
84
85
86 #ifndef PS_USE_CLOBBER_ARGV
87 /* all but one option 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 */
96
97 static size_t ps_buffer_cur_len;        /* nominal strlen(ps_buffer) */
98
99 static size_t ps_buffer_fixed_size;             /* size of the constant prefix */
100
101 /* save the original argv[] location here */
102 static int      save_argc;
103 static char **save_argv;
104
105
106 /*
107  * Call this early in startup to save the original argc/argv values.
108  * If needed, we make a copy of the original argv[] array to preserve it
109  * from being clobbered by subsequent ps_display actions.
110  *
111  * (The original argv[] will not be overwritten by this routine, but may be
112  * overwritten during init_ps_display.  Also, the physical location of the
113  * environment strings may be moved, so this should be called before any code
114  * that might try to hang onto a getenv() result.)
115  */
116 char      **
117 save_ps_display_args(int argc, char **argv)
118 {
119         save_argc = argc;
120         save_argv = argv;
121
122 #if defined(PS_USE_CLOBBER_ARGV)
123
124         /*
125          * If we're going to overwrite the argv area, count the available space.
126          * Also move the environment to make additional room.
127          */
128         {
129                 char       *end_of_area = NULL;
130                 char      **new_environ;
131                 int                     i;
132
133                 /*
134                  * check for contiguous argv strings
135                  */
136                 for (i = 0; i < argc; i++)
137                 {
138                         if (i == 0 || end_of_area + 1 == argv[i])
139                                 end_of_area = argv[i] + strlen(argv[i]);
140                 }
141
142                 if (end_of_area == NULL)        /* probably can't happen? */
143                 {
144                         ps_buffer = NULL;
145                         ps_buffer_size = 0;
146                         return argv;
147                 }
148
149                 /*
150                  * check for contiguous environ strings following argv
151                  */
152                 for (i = 0; environ[i] != NULL; i++)
153                 {
154                         if (end_of_area + 1 == environ[i])
155                                 end_of_area = environ[i] + strlen(environ[i]);
156                 }
157
158                 ps_buffer = argv[0];
159                 last_status_len = ps_buffer_size = end_of_area - argv[0];
160
161                 /*
162                  * move the environment out of the way
163                  */
164                 new_environ = (char **) malloc((i + 1) * sizeof(char *));
165                 for (i = 0; environ[i] != NULL; i++)
166                         new_environ[i] = strdup(environ[i]);
167                 new_environ[i] = NULL;
168                 environ = new_environ;
169         }
170 #endif   /* PS_USE_CLOBBER_ARGV */
171
172 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
173
174         /*
175          * If we're going to change the original argv[] then make a copy for
176          * argument parsing purposes.
177          *
178          * (NB: do NOT think to remove the copying of argv[], even though
179          * postmaster.c finishes looking at argv[] long before we ever consider
180          * changing the ps display.  On some platforms, getopt() keeps pointers
181          * into the argv array, and will get horribly confused when it is
182          * re-called to analyze a subprocess' argument string if the argv storage
183          * has been clobbered meanwhile.  Other platforms have other dependencies
184          * on argv[].
185          */
186         {
187                 char      **new_argv;
188                 int                     i;
189
190                 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
191                 for (i = 0; i < argc; i++)
192                         new_argv[i] = strdup(argv[i]);
193                 new_argv[argc] = NULL;
194
195 #if defined(__darwin__)
196
197                 /*
198                  * Darwin (and perhaps other NeXT-derived platforms?) has a static
199                  * copy of the argv pointer, which we may fix like so:
200                  */
201                 *_NSGetArgv() = new_argv;
202 #endif
203
204                 argv = new_argv;
205         }
206 #endif   /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
207
208         return argv;
209 }
210
211 /*
212  * Call this once during subprocess startup to set the identification
213  * values.      At this point, the original argv[] array may be overwritten.
214  */
215 void
216 init_ps_display(const char *username, const char *dbname,
217                                 const char *host_info, const char *initial_str)
218 {
219         Assert(username);
220         Assert(dbname);
221         Assert(host_info);
222
223 #ifndef PS_USE_NONE
224         /* no ps display for stand-alone backend */
225         if (!IsUnderPostmaster)
226                 return;
227
228         /* no ps display if you didn't call save_ps_display_args() */
229         if (!save_argv)
230                 return;
231
232 #ifdef PS_USE_CLOBBER_ARGV
233         /* If ps_buffer is a pointer, it might still be null */
234         if (!ps_buffer)
235                 return;
236 #endif
237
238         /*
239          * Overwrite argv[] to point at appropriate space, if needed
240          */
241
242 #ifdef PS_USE_CHANGE_ARGV
243         save_argv[0] = ps_buffer;
244         save_argv[1] = NULL;
245 #endif   /* PS_USE_CHANGE_ARGV */
246
247 #ifdef PS_USE_CLOBBER_ARGV
248         {
249                 int                     i;
250
251                 /* make extra argv slots point at end_of_area (a NUL) */
252                 for (i = 1; i < save_argc; i++)
253                         save_argv[i] = ps_buffer + ps_buffer_size;
254         }
255 #endif   /* PS_USE_CLOBBER_ARGV */
256
257         /*
258          * Make fixed prefix of ps display.
259          */
260
261 #ifdef PS_USE_SETPROCTITLE
262
263         /*
264          * apparently setproctitle() already adds a `progname:' prefix to the ps
265          * line
266          */
267         snprintf(ps_buffer, ps_buffer_size,
268                          "%s %s %s ",
269                          username, dbname, host_info);
270 #else
271         snprintf(ps_buffer, ps_buffer_size,
272                          "postgres: %s %s %s ",
273                          username, dbname, host_info);
274 #endif
275
276         ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
277
278         set_ps_display(initial_str, true);
279 #endif   /* not PS_USE_NONE */
280 }
281
282
283
284 /*
285  * Call this to update the ps status display to a fixed prefix plus an
286  * indication of what you're currently doing passed in the argument.
287  */
288 void
289 set_ps_display(const char *activity, bool force)
290 {
291 #ifndef PS_USE_NONE
292         /* update_process_title=off disables updates, unless force = true */
293         if (!force && !update_process_title)
294                 return;
295
296         /* no ps display for stand-alone backend */
297         if (!IsUnderPostmaster)
298                 return;
299
300 #ifdef PS_USE_CLOBBER_ARGV
301         /* If ps_buffer is a pointer, it might still be null */
302         if (!ps_buffer)
303                 return;
304 #endif
305
306         /* Update ps_buffer to contain both fixed part and activity */
307         strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
308                         ps_buffer_size - ps_buffer_fixed_size);
309         ps_buffer_cur_len = strlen(ps_buffer);
310
311         /* Transmit new setting to kernel, if necessary */
312
313 #ifdef PS_USE_SETPROCTITLE
314         setproctitle("%s", ps_buffer);
315 #endif
316
317 #ifdef PS_USE_PSTAT
318         {
319                 union pstun pst;
320
321                 pst.pst_command = ps_buffer;
322                 pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
323         }
324 #endif   /* PS_USE_PSTAT */
325
326 #ifdef PS_USE_PS_STRINGS
327         PS_STRINGS->ps_nargvstr = 1;
328         PS_STRINGS->ps_argvstr = ps_buffer;
329 #endif   /* PS_USE_PS_STRINGS */
330
331 #ifdef PS_USE_CLOBBER_ARGV
332         /* pad unused memory; need only clobber remainder of old status string */
333         if (last_status_len > ps_buffer_cur_len)
334                 MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
335                            last_status_len - ps_buffer_cur_len);
336         last_status_len = ps_buffer_cur_len;
337 #endif   /* PS_USE_CLOBBER_ARGV */
338
339 #ifdef PS_USE_WIN32
340         {
341                 /*
342                  * Win32 does not support showing any changed arguments. To make it at
343                  * all possible to track which backend is doing what, we create a
344                  * named object that can be viewed with for example Process Explorer.
345                  */
346                 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
347                 char            name[PS_BUFFER_SIZE + 32];
348
349                 if (ident_handle != INVALID_HANDLE_VALUE)
350                         CloseHandle(ident_handle);
351
352                 sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
353
354                 ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
355         }
356 #endif   /* PS_USE_WIN32 */
357 #endif   /* not PS_USE_NONE */
358 }
359
360
361 /*
362  * Returns what's currently in the ps display, in case someone needs
363  * it.  Note that only the activity part is returned.  On some platforms
364  * the string will not be null-terminated, so return the effective
365  * length into *displen.
366  */
367 const char *
368 get_ps_display(int *displen)
369 {
370 #ifdef PS_USE_CLOBBER_ARGV
371         /* If ps_buffer is a pointer, it might still be null */
372         if (!ps_buffer)
373         {
374                 *displen = 0;
375                 return "";
376         }
377 #endif
378
379         *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
380
381         return ps_buffer + ps_buffer_fixed_size;
382 }