PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ps_status.c
Go to the documentation of this file.
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  * src/backend/utils/misc/ps_status.c
9  *
10  * Copyright (c) 2000-2014, 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 #include "utils/guc.h"
33 
34 extern char **environ;
36 
37 
38 /*
39  * Alternative ways of updating ps display:
40  *
41  * PS_USE_SETPROCTITLE
42  * use the function setproctitle(const char *, ...)
43  * (newer BSD systems)
44  * PS_USE_PSTAT
45  * use the pstat(PSTAT_SETCMD, )
46  * (HPUX)
47  * PS_USE_PS_STRINGS
48  * assign PS_STRINGS->ps_argvstr = "string"
49  * (some BSD systems)
50  * PS_USE_CHANGE_ARGV
51  * assign argv[0] = "string"
52  * (some other BSD systems)
53  * PS_USE_CLOBBER_ARGV
54  * write over the argv and environment area
55  * (Linux and most SysV-like systems)
56  * PS_USE_WIN32
57  * push the string out as the name of a Windows event
58  * PS_USE_NONE
59  * don't update ps display
60  * (This is the default, as it is safest.)
61  */
62 #if defined(HAVE_SETPROCTITLE)
63 #define PS_USE_SETPROCTITLE
64 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
65 #define PS_USE_PSTAT
66 #elif defined(HAVE_PS_STRINGS)
67 #define PS_USE_PS_STRINGS
68 #elif (defined(BSD) || defined(__hurd__)) && !defined(__darwin__)
69 #define PS_USE_CHANGE_ARGV
70 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(__svr5__) || defined(__darwin__)
71 #define PS_USE_CLOBBER_ARGV
72 #elif defined(WIN32)
73 #define PS_USE_WIN32
74 #else
75 #define PS_USE_NONE
76 #endif
77 
78 
79 /* Different systems want the buffer padded differently */
80 #if defined(_AIX) || defined(__linux__) || defined(__darwin__)
81 #define PS_PADDING '\0'
82 #else
83 #define PS_PADDING ' '
84 #endif
85 
86 
87 #ifndef PS_USE_CLOBBER_ARGV
88 /* all but one option need a buffer to write their ps line in */
89 #define PS_BUFFER_SIZE 256
91 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
92 #else /* PS_USE_CLOBBER_ARGV */
93 static char *ps_buffer; /* will point to argv area */
94 static size_t ps_buffer_size; /* space determined at run time */
95 static size_t last_status_len; /* use to minimize length of clobber */
96 #endif /* PS_USE_CLOBBER_ARGV */
97 
98 static size_t ps_buffer_cur_len; /* nominal strlen(ps_buffer) */
99 
100 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
101 
102 /* save the original argv[] location here */
103 static int save_argc;
104 static char **save_argv;
105 
106 
107 /*
108  * Call this early in startup to save the original argc/argv values.
109  * If needed, we make a copy of the original argv[] array to preserve it
110  * from being clobbered by subsequent ps_display actions.
111  *
112  * (The original argv[] will not be overwritten by this routine, but may be
113  * overwritten during init_ps_display. Also, the physical location of the
114  * environment strings may be moved, so this should be called before any code
115  * that might try to hang onto a getenv() result.)
116  */
117 char **
118 save_ps_display_args(int argc, char **argv)
119 {
120  save_argc = argc;
121  save_argv = argv;
122 
123 #if defined(PS_USE_CLOBBER_ARGV)
124 
125  /*
126  * If we're going to overwrite the argv area, count the available space.
127  * Also move the environment to make additional room.
128  */
129  {
130  char *end_of_area = NULL;
131  char **new_environ;
132  int i;
133 
134  /*
135  * check for contiguous argv strings
136  */
137  for (i = 0; i < argc; i++)
138  {
139  if (i == 0 || end_of_area + 1 == argv[i])
140  end_of_area = argv[i] + strlen(argv[i]);
141  }
142 
143  if (end_of_area == NULL) /* probably can't happen? */
144  {
145  ps_buffer = NULL;
146  ps_buffer_size = 0;
147  return argv;
148  }
149 
150  /*
151  * check for contiguous environ strings following argv
152  */
153  for (i = 0; environ[i] != NULL; i++)
154  {
155  if (end_of_area + 1 == environ[i])
156  end_of_area = environ[i] + strlen(environ[i]);
157  }
158 
159  ps_buffer = argv[0];
160  last_status_len = ps_buffer_size = end_of_area - argv[0];
161 
162  /*
163  * move the environment out of the way
164  */
165  new_environ = (char **) malloc((i + 1) * sizeof(char *));
166  for (i = 0; environ[i] != NULL; i++)
167  new_environ[i] = strdup(environ[i]);
168  new_environ[i] = NULL;
169  environ = new_environ;
170  }
171 #endif /* PS_USE_CLOBBER_ARGV */
172 
173 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
174 
175  /*
176  * If we're going to change the original argv[] then make a copy for
177  * argument parsing purposes.
178  *
179  * (NB: do NOT think to remove the copying of argv[], even though
180  * postmaster.c finishes looking at argv[] long before we ever consider
181  * changing the ps display. On some platforms, getopt() keeps pointers
182  * into the argv array, and will get horribly confused when it is
183  * re-called to analyze a subprocess' argument string if the argv storage
184  * has been clobbered meanwhile. Other platforms have other dependencies
185  * on argv[].
186  */
187  {
188  char **new_argv;
189  int i;
190 
191  new_argv = (char **) malloc((argc + 1) * sizeof(char *));
192  for (i = 0; i < argc; i++)
193  new_argv[i] = strdup(argv[i]);
194  new_argv[argc] = NULL;
195 
196 #if defined(__darwin__)
197 
198  /*
199  * Darwin (and perhaps other NeXT-derived platforms?) has a static
200  * copy of the argv pointer, which we may fix like so:
201  */
202  *_NSGetArgv() = new_argv;
203 #endif
204 
205  argv = new_argv;
206  }
207 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
208 
209  return argv;
210 }
211 
212 /*
213  * Call this once during subprocess startup to set the identification
214  * values. At this point, the original argv[] array may be overwritten.
215  */
216 void
217 init_ps_display(const char *username, const char *dbname,
218  const char *host_info, const char *initial_str)
219 {
220  Assert(username);
221  Assert(dbname);
222  Assert(host_info);
223 
224 #ifndef PS_USE_NONE
225  /* no ps display for stand-alone backend */
226  if (!IsUnderPostmaster)
227  return;
228 
229  /* no ps display if you didn't call save_ps_display_args() */
230  if (!save_argv)
231  return;
232 
233 #ifdef PS_USE_CLOBBER_ARGV
234  /* If ps_buffer is a pointer, it might still be null */
235  if (!ps_buffer)
236  return;
237 #endif
238 
239  /*
240  * Overwrite argv[] to point at appropriate space, if needed
241  */
242 
243 #ifdef PS_USE_CHANGE_ARGV
244  save_argv[0] = ps_buffer;
245  save_argv[1] = NULL;
246 #endif /* PS_USE_CHANGE_ARGV */
247 
248 #ifdef PS_USE_CLOBBER_ARGV
249  {
250  int i;
251 
252  /* make extra argv slots point at end_of_area (a NUL) */
253  for (i = 1; i < save_argc; i++)
255  }
256 #endif /* PS_USE_CLOBBER_ARGV */
257 
258  /*
259  * Make fixed prefix of ps display.
260  */
261 
262 #ifdef PS_USE_SETPROCTITLE
263 
264  /*
265  * apparently setproctitle() already adds a `progname:' prefix to the ps
266  * line
267  */
268 #define PROGRAM_NAME_PREFIX ""
269 #else
270 #define PROGRAM_NAME_PREFIX "postgres: "
271 #endif
272 
273  if (*cluster_name == '\0')
274  {
276  PROGRAM_NAME_PREFIX "%s %s %s ",
277  username, dbname, host_info);
278  }
279  else
280  {
282  PROGRAM_NAME_PREFIX "%s: %s %s %s ",
283  cluster_name, username, dbname, host_info);
284  }
285 
287 
288  set_ps_display(initial_str, true);
289 #endif /* not PS_USE_NONE */
290 }
291 
292 
293 
294 /*
295  * Call this to update the ps status display to a fixed prefix plus an
296  * indication of what you're currently doing passed in the argument.
297  */
298 void
299 set_ps_display(const char *activity, bool force)
300 {
301 #ifndef PS_USE_NONE
302  /* update_process_title=off disables updates, unless force = true */
303  if (!force && !update_process_title)
304  return;
305 
306  /* no ps display for stand-alone backend */
307  if (!IsUnderPostmaster)
308  return;
309 
310 #ifdef PS_USE_CLOBBER_ARGV
311  /* If ps_buffer is a pointer, it might still be null */
312  if (!ps_buffer)
313  return;
314 #endif
315 
316  /* Update ps_buffer to contain both fixed part and activity */
319  ps_buffer_cur_len = strlen(ps_buffer);
320 
321  /* Transmit new setting to kernel, if necessary */
322 
323 #ifdef PS_USE_SETPROCTITLE
324  setproctitle("%s", ps_buffer);
325 #endif
326 
327 #ifdef PS_USE_PSTAT
328  {
329  union pstun pst;
330 
331  pst.pst_command = ps_buffer;
332  pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
333  }
334 #endif /* PS_USE_PSTAT */
335 
336 #ifdef PS_USE_PS_STRINGS
337  PS_STRINGS->ps_nargvstr = 1;
338  PS_STRINGS->ps_argvstr = ps_buffer;
339 #endif /* PS_USE_PS_STRINGS */
340 
341 #ifdef PS_USE_CLOBBER_ARGV
342  /* pad unused memory; need only clobber remainder of old status string */
343  if (last_status_len > ps_buffer_cur_len)
345  last_status_len - ps_buffer_cur_len);
346  last_status_len = ps_buffer_cur_len;
347 #endif /* PS_USE_CLOBBER_ARGV */
348 
349 #ifdef PS_USE_WIN32
350  {
351  /*
352  * Win32 does not support showing any changed arguments. To make it at
353  * all possible to track which backend is doing what, we create a
354  * named object that can be viewed with for example Process Explorer.
355  */
356  static HANDLE ident_handle = INVALID_HANDLE_VALUE;
357  char name[PS_BUFFER_SIZE + 32];
358 
359  if (ident_handle != INVALID_HANDLE_VALUE)
360  CloseHandle(ident_handle);
361 
362  sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
363 
364  ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
365  }
366 #endif /* PS_USE_WIN32 */
367 #endif /* not PS_USE_NONE */
368 }
369 
370 
371 /*
372  * Returns what's currently in the ps display, in case someone needs
373  * it. Note that only the activity part is returned. On some platforms
374  * the string will not be null-terminated, so return the effective
375  * length into *displen.
376  */
377 const char *
378 get_ps_display(int *displen)
379 {
380 #ifdef PS_USE_CLOBBER_ARGV
381  /* If ps_buffer is a pointer, it might still be null */
382  if (!ps_buffer)
383  {
384  *displen = 0;
385  return "";
386  }
387 #endif
388 
389  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
390 
392 }