PostgreSQL Source Code  git master
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-2023, 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_PS_STRINGS
19 #include <machine/vmparam.h> /* for old BSD */
20 #include <sys/exec.h>
21 #endif
22 #if defined(__darwin__)
23 #include <crt_externs.h>
24 #endif
25 
26 #include "libpq/libpq.h"
27 #include "miscadmin.h"
28 #include "pgstat.h"
29 #include "utils/guc.h"
30 #include "utils/ps_status.h"
31 
32 extern char **environ;
33 
34 /* GUC variable */
36 
37 /*
38  * Alternative ways of updating ps display:
39  *
40  * PS_USE_SETPROCTITLE_FAST
41  * use the function setproctitle_fast(const char *, ...)
42  * (newer FreeBSD systems)
43  * PS_USE_SETPROCTITLE
44  * use the function setproctitle(const char *, ...)
45  * (newer BSD systems)
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_FAST)
62 #define PS_USE_SETPROCTITLE_FAST
63 #elif defined(HAVE_SETPROCTITLE)
64 #define PS_USE_SETPROCTITLE
65 #elif defined(HAVE_PS_STRINGS)
66 #define PS_USE_PS_STRINGS
67 #elif (defined(BSD) || defined(__hurd__)) && !defined(__darwin__)
68 #define PS_USE_CHANGE_ARGV
69 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || 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(__darwin__)
80 #define PS_PADDING '\0'
81 #else
82 #define PS_PADDING ' '
83 #endif
84 
85 
86 #ifndef PS_USE_NONE
87 
88 #ifndef PS_USE_CLOBBER_ARGV
89 /* all but one option need a buffer to write their ps line in */
90 #define PS_BUFFER_SIZE 256
91 static char ps_buffer[PS_BUFFER_SIZE];
92 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
93 #else /* PS_USE_CLOBBER_ARGV */
94 static char *ps_buffer; /* will point to argv area */
95 static size_t ps_buffer_size; /* space determined at run time */
96 static size_t last_status_len; /* use to minimize length of clobber */
97 #endif /* PS_USE_CLOBBER_ARGV */
98 
99 static size_t ps_buffer_cur_len; /* nominal strlen(ps_buffer) */
100 
101 static size_t ps_buffer_fixed_size; /* size of the constant prefix */
102 
103 #endif /* not PS_USE_NONE */
104 
105 /* save the original argv[] location here */
106 static int save_argc;
107 static char **save_argv;
108 
109 
110 /*
111  * Call this early in startup to save the original argc/argv values.
112  * If needed, we make a copy of the original argv[] array to preserve it
113  * from being clobbered by subsequent ps_display actions.
114  *
115  * (The original argv[] will not be overwritten by this routine, but may be
116  * overwritten during init_ps_display. Also, the physical location of the
117  * environment strings may be moved, so this should be called before any code
118  * that might try to hang onto a getenv() result.)
119  *
120  * Note that in case of failure this cannot call elog() as that is not
121  * initialized yet. We rely on write_stderr() instead.
122  */
123 char **
124 save_ps_display_args(int argc, char **argv)
125 {
126  save_argc = argc;
127  save_argv = argv;
128 
129 #if defined(PS_USE_CLOBBER_ARGV)
130 
131  /*
132  * If we're going to overwrite the argv area, count the available space.
133  * Also move the environment to make additional room.
134  */
135  {
136  char *end_of_area = NULL;
137  char **new_environ;
138  int i;
139 
140  /*
141  * check for contiguous argv strings
142  */
143  for (i = 0; i < argc; i++)
144  {
145  if (i == 0 || end_of_area + 1 == argv[i])
146  end_of_area = argv[i] + strlen(argv[i]);
147  }
148 
149  if (end_of_area == NULL) /* probably can't happen? */
150  {
151  ps_buffer = NULL;
152  ps_buffer_size = 0;
153  return argv;
154  }
155 
156  /*
157  * check for contiguous environ strings following argv
158  */
159  for (i = 0; environ[i] != NULL; i++)
160  {
161  if (end_of_area + 1 == environ[i])
162  end_of_area = environ[i] + strlen(environ[i]);
163  }
164 
165  ps_buffer = argv[0];
166  last_status_len = ps_buffer_size = end_of_area - argv[0];
167 
168  /*
169  * move the environment out of the way
170  */
171  new_environ = (char **) malloc((i + 1) * sizeof(char *));
172  if (!new_environ)
173  {
174  write_stderr("out of memory\n");
175  exit(1);
176  }
177  for (i = 0; environ[i] != NULL; i++)
178  {
179  new_environ[i] = strdup(environ[i]);
180  if (!new_environ[i])
181  {
182  write_stderr("out of memory\n");
183  exit(1);
184  }
185  }
186  new_environ[i] = NULL;
187  environ = new_environ;
188  }
189 #endif /* PS_USE_CLOBBER_ARGV */
190 
191 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
192 
193  /*
194  * If we're going to change the original argv[] then make a copy for
195  * argument parsing purposes.
196  *
197  * (NB: do NOT think to remove the copying of argv[], even though
198  * postmaster.c finishes looking at argv[] long before we ever consider
199  * changing the ps display. On some platforms, getopt() keeps pointers
200  * into the argv array, and will get horribly confused when it is
201  * re-called to analyze a subprocess' argument string if the argv storage
202  * has been clobbered meanwhile. Other platforms have other dependencies
203  * on argv[].
204  */
205  {
206  char **new_argv;
207  int i;
208 
209  new_argv = (char **) malloc((argc + 1) * sizeof(char *));
210  if (!new_argv)
211  {
212  write_stderr("out of memory\n");
213  exit(1);
214  }
215  for (i = 0; i < argc; i++)
216  {
217  new_argv[i] = strdup(argv[i]);
218  if (!new_argv[i])
219  {
220  write_stderr("out of memory\n");
221  exit(1);
222  }
223  }
224  new_argv[argc] = NULL;
225 
226 #if defined(__darwin__)
227 
228  /*
229  * macOS (and perhaps other NeXT-derived platforms?) has a static copy
230  * of the argv pointer, which we may fix like so:
231  */
232  *_NSGetArgv() = new_argv;
233 #endif
234 
235  argv = new_argv;
236  }
237 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
238 
239  return argv;
240 }
241 
242 /*
243  * Call this once during subprocess startup to set the identification
244  * values.
245  *
246  * If fixed_part is NULL, a default will be obtained from MyBackendType.
247  *
248  * At this point, the original argv[] array may be overwritten.
249  */
250 void
251 init_ps_display(const char *fixed_part)
252 {
253 #ifndef PS_USE_NONE
254  bool save_update_process_title;
255 #endif
256 
257  Assert(fixed_part || MyBackendType);
258  if (!fixed_part)
259  fixed_part = GetBackendTypeDesc(MyBackendType);
260 
261 #ifndef PS_USE_NONE
262  /* no ps display for stand-alone backend */
263  if (!IsUnderPostmaster)
264  return;
265 
266  /* no ps display if you didn't call save_ps_display_args() */
267  if (!save_argv)
268  return;
269 
270 #ifdef PS_USE_CLOBBER_ARGV
271  /* If ps_buffer is a pointer, it might still be null */
272  if (!ps_buffer)
273  return;
274 #endif
275 
276  /*
277  * Overwrite argv[] to point at appropriate space, if needed
278  */
279 
280 #ifdef PS_USE_CHANGE_ARGV
281  save_argv[0] = ps_buffer;
282  save_argv[1] = NULL;
283 #endif /* PS_USE_CHANGE_ARGV */
284 
285 #ifdef PS_USE_CLOBBER_ARGV
286  {
287  int i;
288 
289  /* make extra argv slots point at end_of_area (a NUL) */
290  for (i = 1; i < save_argc; i++)
291  save_argv[i] = ps_buffer + ps_buffer_size;
292  }
293 #endif /* PS_USE_CLOBBER_ARGV */
294 
295  /*
296  * Make fixed prefix of ps display.
297  */
298 
299 #if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
300 
301  /*
302  * apparently setproctitle() already adds a `progname:' prefix to the ps
303  * line
304  */
305 #define PROGRAM_NAME_PREFIX ""
306 #else
307 #define PROGRAM_NAME_PREFIX "postgres: "
308 #endif
309 
310  if (*cluster_name == '\0')
311  {
312  snprintf(ps_buffer, ps_buffer_size,
313  PROGRAM_NAME_PREFIX "%s ",
314  fixed_part);
315  }
316  else
317  {
318  snprintf(ps_buffer, ps_buffer_size,
319  PROGRAM_NAME_PREFIX "%s: %s ",
320  cluster_name, fixed_part);
321  }
322 
323  ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
324 
325  /*
326  * On the first run, force the update.
327  */
328  save_update_process_title = update_process_title;
329  update_process_title = true;
330  set_ps_display("");
331  update_process_title = save_update_process_title;
332 #endif /* not PS_USE_NONE */
333 }
334 
335 
336 
337 /*
338  * Call this to update the ps status display to a fixed prefix plus an
339  * indication of what you're currently doing passed in the argument.
340  */
341 void
342 set_ps_display(const char *activity)
343 {
344 #ifndef PS_USE_NONE
345  /* update_process_title=off disables updates */
347  return;
348 
349  /* no ps display for stand-alone backend */
350  if (!IsUnderPostmaster)
351  return;
352 
353 #ifdef PS_USE_CLOBBER_ARGV
354  /* If ps_buffer is a pointer, it might still be null */
355  if (!ps_buffer)
356  return;
357 #endif
358 
359  /* Update ps_buffer to contain both fixed part and activity */
360  strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
361  ps_buffer_size - ps_buffer_fixed_size);
362  ps_buffer_cur_len = strlen(ps_buffer);
363 
364  /* Transmit new setting to kernel, if necessary */
365 
366 #ifdef PS_USE_SETPROCTITLE
367  setproctitle("%s", ps_buffer);
368 #elif defined(PS_USE_SETPROCTITLE_FAST)
369  setproctitle_fast("%s", ps_buffer);
370 #endif
371 
372 #ifdef PS_USE_PS_STRINGS
373  PS_STRINGS->ps_nargvstr = 1;
374  PS_STRINGS->ps_argvstr = ps_buffer;
375 #endif /* PS_USE_PS_STRINGS */
376 
377 #ifdef PS_USE_CLOBBER_ARGV
378  /* pad unused memory; need only clobber remainder of old status string */
379  if (last_status_len > ps_buffer_cur_len)
380  MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
381  last_status_len - ps_buffer_cur_len);
382  last_status_len = ps_buffer_cur_len;
383 #endif /* PS_USE_CLOBBER_ARGV */
384 
385 #ifdef PS_USE_WIN32
386  {
387  /*
388  * Win32 does not support showing any changed arguments. To make it at
389  * all possible to track which backend is doing what, we create a
390  * named object that can be viewed with for example Process Explorer.
391  */
392  static HANDLE ident_handle = INVALID_HANDLE_VALUE;
393  char name[PS_BUFFER_SIZE + 32];
394 
395  if (ident_handle != INVALID_HANDLE_VALUE)
396  CloseHandle(ident_handle);
397 
398  sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
399 
400  ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
401  }
402 #endif /* PS_USE_WIN32 */
403 #endif /* not PS_USE_NONE */
404 }
405 
406 
407 /*
408  * Returns what's currently in the ps display, in case someone needs
409  * it. Note that only the activity part is returned. On some platforms
410  * the string will not be null-terminated, so return the effective
411  * length into *displen.
412  */
413 const char *
414 get_ps_display(int *displen)
415 {
416 #ifdef PS_USE_CLOBBER_ARGV
417  /* If ps_buffer is a pointer, it might still be null */
418  if (!ps_buffer)
419  {
420  *displen = 0;
421  return "";
422  }
423 #endif
424 
425 #ifndef PS_USE_NONE
426  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
427 
428  return ps_buffer + ps_buffer_fixed_size;
429 #else
430  *displen = 0;
431  return "";
432 #endif
433 }
#define write_stderr(str)
Definition: parallel.c:184
#define MemSet(start, val, len)
Definition: c.h:1004
const char * name
Definition: encode.c:571
int MyProcPid
Definition: globals.c:44
bool IsUnderPostmaster
Definition: globals.c:113
char * cluster_name
Definition: guc_tables.c:508
#define malloc(a)
Definition: header.h:50
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
exit(1)
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:256
BackendType MyBackendType
Definition: miscinit.c:63
#define sprintf
Definition: port.h:240
#define snprintf
Definition: port.h:238
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define PS_PADDING
Definition: ps_status.c:82
const char * get_ps_display(int *displen)
Definition: ps_status.c:414
static int save_argc
Definition: ps_status.c:106
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:251
char ** environ
bool update_process_title
Definition: ps_status.c:35
char ** save_ps_display_args(int argc, char **argv)
Definition: ps_status.c:124
void set_ps_display(const char *activity)
Definition: ps_status.c:342
static char ** save_argv
Definition: ps_status.c:107
#define DEFAULT_UPDATE_PROCESS_TITLE
Definition: ps_status.h:19