PostgreSQL Source Code  git master
ps_status.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "miscadmin.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
Include dependency graph for ps_status.c:

Go to the source code of this file.

Macros

#define PS_USE_NONE
 
#define PS_PADDING   ' '
 

Functions

char ** save_ps_display_args (int argc, char **argv)
 
void init_ps_display (const char *fixed_part)
 
void set_ps_display_suffix (const char *suffix)
 
void set_ps_display_remove_suffix (void)
 
void set_ps_display_with_len (const char *activity, size_t len)
 
const char * get_ps_display (int *displen)
 

Variables

char ** environ
 
bool update_process_title = DEFAULT_UPDATE_PROCESS_TITLE
 
static int save_argc
 
static char ** save_argv
 

Macro Definition Documentation

◆ PS_PADDING

#define PS_PADDING   ' '

Definition at line 66 of file ps_status.c.

◆ PS_USE_NONE

#define PS_USE_NONE

Definition at line 58 of file ps_status.c.

Function Documentation

◆ get_ps_display()

const char* get_ps_display ( int *  displen)

Definition at line 530 of file ps_status.c.

531 {
532 #ifdef PS_USE_CLOBBER_ARGV
533  /* If ps_buffer is a pointer, it might still be null */
534  if (!ps_buffer)
535  {
536  *displen = 0;
537  return "";
538  }
539 #endif
540 
541 #ifndef PS_USE_NONE
542  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
543 
544  return ps_buffer + ps_buffer_fixed_size;
545 #else
546  *displen = 0;
547  return "";
548 #endif
549 }

Referenced by log_status_format(), write_csvlog(), and write_jsonlog().

◆ init_ps_display()

void init_ps_display ( const char *  fixed_part)

Definition at line 267 of file ps_status.c.

268 {
269 #ifndef PS_USE_NONE
270  bool save_update_process_title;
271 #endif
272 
273  Assert(fixed_part || MyBackendType);
274  if (!fixed_part)
275  fixed_part = GetBackendTypeDesc(MyBackendType);
276 
277 #ifndef PS_USE_NONE
278  /* no ps display for stand-alone backend */
279  if (!IsUnderPostmaster)
280  return;
281 
282  /* no ps display if you didn't call save_ps_display_args() */
283  if (!save_argv)
284  return;
285 
286 #ifdef PS_USE_CLOBBER_ARGV
287  /* If ps_buffer is a pointer, it might still be null */
288  if (!ps_buffer)
289  return;
290 
291  /* make extra argv slots point at end_of_area (a NUL) */
292  for (int i = 1; i < save_argc; i++)
293  save_argv[i] = ps_buffer + ps_buffer_size;
294 #endif /* PS_USE_CLOBBER_ARGV */
295 
296  /*
297  * Make fixed prefix of ps display.
298  */
299 
300 #if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
301 
302  /*
303  * apparently setproctitle() already adds a `progname:' prefix to the ps
304  * line
305  */
306 #define PROGRAM_NAME_PREFIX ""
307 #else
308 #define PROGRAM_NAME_PREFIX "postgres: "
309 #endif
310 
311  if (*cluster_name == '\0')
312  {
313  snprintf(ps_buffer, ps_buffer_size,
314  PROGRAM_NAME_PREFIX "%s ",
315  fixed_part);
316  }
317  else
318  {
319  snprintf(ps_buffer, ps_buffer_size,
320  PROGRAM_NAME_PREFIX "%s: %s ",
321  cluster_name, fixed_part);
322  }
323 
324  ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
325 
326  /*
327  * On the first run, force the update.
328  */
329  save_update_process_title = update_process_title;
330  update_process_title = true;
331  set_ps_display("");
332  update_process_title = save_update_process_title;
333 #endif /* not PS_USE_NONE */
334 }
#define Assert(condition)
Definition: c.h:837
bool IsUnderPostmaster
Definition: globals.c:119
char * cluster_name
Definition: guc_tables.c:537
int i
Definition: isn.c:72
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:270
BackendType MyBackendType
Definition: miscinit.c:64
#define snprintf
Definition: port.h:238
static int save_argc
Definition: ps_status.c:98
bool update_process_title
Definition: ps_status.c:29
static char ** save_argv
Definition: ps_status.c:99
static void set_ps_display(const char *activity)
Definition: ps_status.h:40

References Assert, cluster_name, GetBackendTypeDesc(), i, IsUnderPostmaster, MyBackendType, save_argc, save_argv, set_ps_display(), snprintf, and update_process_title.

Referenced by AutoVacWorkerMain(), AuxiliaryProcessMainCommon(), BackendInitialize(), BackgroundWorkerMain(), ReplSlotSyncWorkerMain(), and SysLoggerMain().

◆ save_ps_display_args()

char** save_ps_display_args ( int  argc,
char **  argv 
)

Definition at line 117 of file ps_status.c.

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 strings 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  {
156  /*
157  * The musl dynamic linker keeps a static pointer to the
158  * initial value of LD_LIBRARY_PATH, if that is defined in the
159  * process's environment. Therefore, we must not overwrite the
160  * value of that setting and thus cannot advance end_of_area
161  * beyond it. Musl does not define any identifying compiler
162  * symbol, so we have to do this unless we see a symbol
163  * identifying a Linux libc we know is safe.
164  */
165 #if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__))
166  if (strncmp(environ[i], "LD_LIBRARY_PATH=", 16) == 0)
167  {
168  /*
169  * We can overwrite the name, but stop at the equals sign.
170  * Future loop iterations will not find any more
171  * contiguous space, but we don't break early because we
172  * need to count the total number of environ[] entries.
173  */
174  end_of_area = environ[i] + 15;
175  }
176  else
177 #endif
178  {
179  end_of_area = environ[i] + strlen(environ[i]);
180  }
181  }
182  }
183 
184  ps_buffer = argv[0];
185  last_status_len = ps_buffer_size = end_of_area - argv[0];
186 
187  /*
188  * move the environment out of the way
189  */
190  new_environ = (char **) malloc((i + 1) * sizeof(char *));
191  if (!new_environ)
192  {
193  write_stderr("out of memory\n");
194  exit(1);
195  }
196  for (i = 0; environ[i] != NULL; i++)
197  {
198  new_environ[i] = strdup(environ[i]);
199  if (!new_environ[i])
200  {
201  write_stderr("out of memory\n");
202  exit(1);
203  }
204  }
205  new_environ[i] = NULL;
206  environ = new_environ;
207  }
208 
209  /*
210  * If we're going to change the original argv[] then make a copy for
211  * argument parsing purposes.
212  *
213  * NB: do NOT think to remove the copying of argv[], even though
214  * postmaster.c finishes looking at argv[] long before we ever consider
215  * changing the ps display. On some platforms, getopt() keeps pointers
216  * into the argv array, and will get horribly confused when it is
217  * re-called to analyze a subprocess' argument string if the argv storage
218  * has been clobbered meanwhile. Other platforms have other dependencies
219  * on argv[].
220  */
221  {
222  char **new_argv;
223  int i;
224 
225  new_argv = (char **) malloc((argc + 1) * sizeof(char *));
226  if (!new_argv)
227  {
228  write_stderr("out of memory\n");
229  exit(1);
230  }
231  for (i = 0; i < argc; i++)
232  {
233  new_argv[i] = strdup(argv[i]);
234  if (!new_argv[i])
235  {
236  write_stderr("out of memory\n");
237  exit(1);
238  }
239  }
240  new_argv[argc] = NULL;
241 
242 #if defined(__darwin__)
243 
244  /*
245  * macOS has a static copy of the argv pointer, which we may fix like
246  * so:
247  */
248  *_NSGetArgv() = new_argv;
249 #endif
250 
251  argv = new_argv;
252  }
253 #endif /* PS_USE_CLOBBER_ARGV */
254 
255  return argv;
256 }
#define write_stderr(str)
Definition: parallel.c:186
#define malloc(a)
Definition: header.h:50
exit(1)
char ** environ

References environ, exit(), i, malloc, save_argc, save_argv, and write_stderr.

Referenced by main().

◆ set_ps_display_remove_suffix()

void set_ps_display_remove_suffix ( void  )

Definition at line 421 of file ps_status.c.

422 {
423 #ifndef PS_USE_NONE
424  /* first, check if we need to update the process title */
425  if (!update_ps_display_precheck())
426  return;
427 
428  /* check we added a suffix */
429  if (ps_buffer_nosuffix_len == 0)
430  return; /* no suffix */
431 
432  /* remove the suffix from ps_buffer */
433  ps_buffer[ps_buffer_nosuffix_len] = '\0';
434  ps_buffer_cur_len = ps_buffer_nosuffix_len;
435  ps_buffer_nosuffix_len = 0;
436 
437  Assert(ps_buffer_cur_len == strlen(ps_buffer));
438 
439  /* and set the new title */
440  flush_ps_display();
441 #endif /* not PS_USE_NONE */
442 }

References Assert.

Referenced by LockBufferForCleanup(), ResolveRecoveryConflictWithVirtualXIDs(), SyncRepWaitForLSN(), and WaitOnLock().

◆ set_ps_display_suffix()

void set_ps_display_suffix ( const char *  suffix)

Definition at line 369 of file ps_status.c.

370 {
371 #ifndef PS_USE_NONE
372  size_t len;
373 
374  /* first, check if we need to update the process title */
375  if (!update_ps_display_precheck())
376  return;
377 
378  /* if there's already a suffix, overwrite it */
379  if (ps_buffer_nosuffix_len > 0)
380  ps_buffer_cur_len = ps_buffer_nosuffix_len;
381  else
382  ps_buffer_nosuffix_len = ps_buffer_cur_len;
383 
384  len = strlen(suffix);
385 
386  /* check if we have enough space to append the suffix */
387  if (ps_buffer_cur_len + len + 1 >= ps_buffer_size)
388  {
389  /* not enough space. Check the buffer isn't full already */
390  if (ps_buffer_cur_len < ps_buffer_size - 1)
391  {
392  /* append a space before the suffix */
393  ps_buffer[ps_buffer_cur_len++] = ' ';
394 
395  /* just add what we can and fill the ps_buffer */
396  memcpy(ps_buffer + ps_buffer_cur_len, suffix,
397  ps_buffer_size - ps_buffer_cur_len - 1);
398  ps_buffer[ps_buffer_size - 1] = '\0';
399  ps_buffer_cur_len = ps_buffer_size - 1;
400  }
401  }
402  else
403  {
404  ps_buffer[ps_buffer_cur_len++] = ' ';
405  memcpy(ps_buffer + ps_buffer_cur_len, suffix, len + 1);
406  ps_buffer_cur_len = ps_buffer_cur_len + len;
407  }
408 
409  Assert(strlen(ps_buffer) == ps_buffer_cur_len);
410 
411  /* and set the new title */
412  flush_ps_display();
413 #endif /* not PS_USE_NONE */
414 }
const void size_t len

References Assert, and len.

Referenced by LockBufferForCleanup(), ResolveRecoveryConflictWithVirtualXIDs(), SyncRepWaitForLSN(), and WaitOnLock().

◆ set_ps_display_with_len()

void set_ps_display_with_len ( const char *  activity,
size_t  len 
)

Definition at line 451 of file ps_status.c.

452 {
453  Assert(strlen(activity) == len);
454 
455 #ifndef PS_USE_NONE
456  /* first, check if we need to update the process title */
457  if (!update_ps_display_precheck())
458  return;
459 
460  /* wipe out any suffix when the title is completely changed */
461  ps_buffer_nosuffix_len = 0;
462 
463  /* Update ps_buffer to contain both fixed part and activity */
464  if (ps_buffer_fixed_size + len >= ps_buffer_size)
465  {
466  /* handle the case where ps_buffer doesn't have enough space */
467  memcpy(ps_buffer + ps_buffer_fixed_size, activity,
468  ps_buffer_size - ps_buffer_fixed_size - 1);
469  ps_buffer[ps_buffer_size - 1] = '\0';
470  ps_buffer_cur_len = ps_buffer_size - 1;
471  }
472  else
473  {
474  memcpy(ps_buffer + ps_buffer_fixed_size, activity, len + 1);
475  ps_buffer_cur_len = ps_buffer_fixed_size + len;
476  }
477  Assert(strlen(ps_buffer) == ps_buffer_cur_len);
478 
479  /* Transmit new setting to kernel, if necessary */
480  flush_ps_display();
481 #endif /* not PS_USE_NONE */
482 }

References Assert, and len.

Referenced by exec_execute_message(), exec_simple_query(), and set_ps_display().

Variable Documentation

◆ environ

char** environ
extern

◆ save_argc

int save_argc
static

Definition at line 98 of file ps_status.c.

Referenced by init_ps_display(), and save_ps_display_args().

◆ save_argv

char** save_argv
static

Definition at line 99 of file ps_status.c.

Referenced by init_ps_display(), and save_ps_display_args().

◆ update_process_title