PostgreSQL Source Code  git master
ps_status.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DEFAULT_UPDATE_PROCESS_TITLE   true
 

Functions

char ** save_ps_display_args (int argc, char **argv)
 
void init_ps_display (const char *fixed_part)
 
void set_ps_display (const char *activity)
 
const char * get_ps_display (int *displen)
 

Variables

PGDLLIMPORT bool update_process_title
 

Macro Definition Documentation

◆ DEFAULT_UPDATE_PROCESS_TITLE

#define DEFAULT_UPDATE_PROCESS_TITLE   true

Definition at line 19 of file ps_status.h.

Function Documentation

◆ get_ps_display()

const char* get_ps_display ( int *  displen)

Definition at line 414 of file ps_status.c.

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 }

Referenced by LockBufferForCleanup(), log_status_format(), ResolveRecoveryConflictWithVirtualXIDs(), SyncRepWaitForLSN(), WaitOnLock(), write_csvlog(), and write_jsonlog().

◆ init_ps_display()

void init_ps_display ( const char *  fixed_part)

Definition at line 251 of file ps_status.c.

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 }
bool IsUnderPostmaster
Definition: globals.c:113
char * cluster_name
Definition: guc_tables.c:502
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:251
BackendType MyBackendType
Definition: miscinit.c:63
#define snprintf
Definition: port.h:238
static int save_argc
Definition: ps_status.c:106
bool update_process_title
Definition: ps_status.c:35
void set_ps_display(const char *activity)
Definition: ps_status.c:342
static char ** save_argv
Definition: ps_status.c:107

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

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMain(), BackendInitialize(), StartBackgroundWorker(), and SysLoggerMain().

◆ save_ps_display_args()

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

Definition at line 124 of file ps_status.c.

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 }
#define write_stderr(str)
Definition: parallel.c:184
#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()

void set_ps_display ( const char *  activity)

Definition at line 342 of file ps_status.c.

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 }
#define MemSet(start, val, len)
Definition: c.h:953
const char * name
Definition: encode.c:561
int MyProcPid
Definition: globals.c:44
#define sprintf
Definition: port.h:240
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define PS_PADDING
Definition: ps_status.c:82

References IsUnderPostmaster, MemSet, MyProcPid, name, PS_PADDING, sprintf, strlcpy(), and update_process_title.

Referenced by AutoVacWorkerMain(), BackendInitialize(), exec_bind_message(), exec_execute_message(), exec_parse_message(), exec_replication_command(), exec_simple_query(), init_ps_display(), LockBufferForCleanup(), PerformAuthentication(), pgarch_archiveXlog(), PostgresMain(), ProcessIncomingNotify(), ResolveRecoveryConflictWithVirtualXIDs(), SendBaseBackup(), StartupXLOG(), SyncRepWaitForLSN(), update_checkpoint_display(), WaitOnLock(), WalRcvWaitForStartPosition(), XLogFileRead(), XLogSendPhysical(), and XLogWalRcvFlush().

Variable Documentation

◆ update_process_title