OSDN Git Service

pgindent run for 8.3.
[pg-rex/syncrep.git] / 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.36 2007/11/15 21:14:41 momjian Exp $
9  *
10  * Copyright (c) 2000-2007, 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  *         (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 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 */
96
97 static size_t ps_buffer_fixed_size;             /* size of the constant prefix */
98
99 /* save the original argv[] location here */
100 static int      save_argc;
101 static char **save_argv;
102
103
104 /*
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.
108  *
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.)
113  */
114 char      **
115 save_ps_display_args(int argc, char **argv)
116 {
117         save_argc = argc;
118         save_argv = argv;
119
120 #if defined(PS_USE_CLOBBER_ARGV)
121
122         /*
123          * If we're going to overwrite the argv area, count the available space.
124          * Also move the environment to make additional room.
125          */
126         {
127                 char       *end_of_area = NULL;
128                 char      **new_environ;
129                 int                     i;
130
131                 /*
132                  * check for contiguous argv strings
133                  */
134                 for (i = 0; i < argc; i++)
135                 {
136                         if (i == 0 || end_of_area + 1 == argv[i])
137                                 end_of_area = argv[i] + strlen(argv[i]);
138                 }
139
140                 if (end_of_area == NULL)        /* probably can't happen? */
141                 {
142                         ps_buffer = NULL;
143                         ps_buffer_size = 0;
144                         return argv;
145                 }
146
147                 /*
148                  * check for contiguous environ strings following argv
149                  */
150                 for (i = 0; environ[i] != NULL; i++)
151                 {
152                         if (end_of_area + 1 == environ[i])
153                                 end_of_area = environ[i] + strlen(environ[i]);
154                 }
155
156                 ps_buffer = argv[0];
157                 last_status_len = ps_buffer_size = end_of_area - argv[0];
158
159                 /*
160                  * move the environment out of the way
161                  */
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;
167         }
168 #endif   /* PS_USE_CLOBBER_ARGV */
169
170 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
171
172         /*
173          * If we're going to change the original argv[] then make a copy for
174          * argument parsing purposes.
175          *
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[].
183          */
184         {
185                 char      **new_argv;
186                 int                     i;
187
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;
192
193 #if defined(__darwin__)
194
195                 /*
196                  * Darwin (and perhaps other NeXT-derived platforms?) has a static
197                  * copy of the argv pointer, which we may fix like so:
198                  */
199                 *_NSGetArgv() = new_argv;
200 #endif
201
202                 argv = new_argv;
203         }
204 #endif   /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
205
206         return argv;
207 }
208
209 /*
210  * Call this once during subprocess startup to set the identification
211  * values.      At this point, the original argv[] array may be overwritten.
212  */
213 void
214 init_ps_display(const char *username, const char *dbname,
215                                 const char *host_info, const char *initial_str)
216 {
217         Assert(username);
218         Assert(dbname);
219         Assert(host_info);
220
221 #ifndef PS_USE_NONE
222         /* no ps display for stand-alone backend */
223         if (!IsUnderPostmaster)
224                 return;
225
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
234
235         /*
236          * Overwrite argv[] to point at appropriate space, if needed
237          */
238
239 #ifdef PS_USE_CHANGE_ARGV
240         save_argv[0] = ps_buffer;
241         save_argv[1] = NULL;
242 #endif   /* PS_USE_CHANGE_ARGV */
243
244 #ifdef PS_USE_CLOBBER_ARGV
245         {
246                 int                     i;
247
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;
251         }
252 #endif   /* PS_USE_CLOBBER_ARGV */
253
254         /*
255          * Make fixed prefix of ps display.
256          */
257
258 #ifdef PS_USE_SETPROCTITLE
259
260         /*
261          * apparently setproctitle() already adds a `progname:' prefix to the ps
262          * line
263          */
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
272
273         ps_buffer_fixed_size = strlen(ps_buffer);
274
275         set_ps_display(initial_str, true);
276 #endif   /* not PS_USE_NONE */
277 }
278
279
280
281 /*
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.
284  */
285 void
286 set_ps_display(const char *activity, bool force)
287 {
288
289         if (!force && !update_process_title)
290                 return;
291
292 #ifndef PS_USE_NONE
293         /* no ps display for stand-alone backend */
294         if (!IsUnderPostmaster)
295                 return;
296
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
302
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);
306
307         /* Transmit new setting to kernel, if necessary */
308
309 #ifdef PS_USE_SETPROCTITLE
310         setproctitle("%s", ps_buffer);
311 #endif
312
313 #ifdef PS_USE_PSTAT
314         {
315                 union pstun pst;
316
317                 pst.pst_command = ps_buffer;
318                 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
319         }
320 #endif   /* PS_USE_PSTAT */
321
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 */
326
327 #ifdef PS_USE_CLOBBER_ARGV
328         {
329                 int                     buflen;
330
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;
337         }
338 #endif   /* PS_USE_CLOBBER_ARGV */
339
340 #ifdef PS_USE_WIN32
341         {
342                 /*
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.
346                  */
347                 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
348                 char            name[PS_BUFFER_SIZE + 32];
349
350                 if (ident_handle != INVALID_HANDLE_VALUE)
351                         CloseHandle(ident_handle);
352
353                 sprintf(name, "pgident: %s", ps_buffer);
354
355                 ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
356         }
357 #endif   /* PS_USE_WIN32 */
358 #endif   /* not PS_USE_NONE */
359 }
360
361
362 /*
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.
367  */
368 const char *
369 get_ps_display(int *displen)
370 {
371 #ifdef PS_USE_CLOBBER_ARGV
372         size_t          offset;
373
374         /* If ps_buffer is a pointer, it might still be null */
375         if (!ps_buffer)
376         {
377                 *displen = 0;
378                 return "";
379         }
380
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--;
385
386         *displen = offset - ps_buffer_fixed_size;
387 #else
388         *displen = strlen(ps_buffer + ps_buffer_fixed_size);
389 #endif
390
391         return ps_buffer + ps_buffer_fixed_size;
392 }