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.

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

bool update_process_title
 

Function Documentation

◆ get_ps_display()

const char* get_ps_display ( int *  displen)

Definition at line 430 of file ps_status.c.

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

431 {
432 #ifdef PS_USE_CLOBBER_ARGV
433  /* If ps_buffer is a pointer, it might still be null */
434  if (!ps_buffer)
435  {
436  *displen = 0;
437  return "";
438  }
439 #endif
440 
441 #ifndef PS_USE_NONE
442  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
443 
444  return ps_buffer + ps_buffer_fixed_size;
445 #else
446  return "";
447 #endif
448 }

◆ init_ps_display()

void init_ps_display ( const char *  fixed_part)

Definition at line 258 of file ps_status.c.

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(), PgArchiverMain(), PgstatCollectorMain(), StartBackgroundWorker(), and SysLoggerMain().

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

◆ save_ps_display_args()

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

Definition at line 131 of file ps_status.c.

References environ, i, malloc, save_argc, save_argv, and write_stderr.

Referenced by main().

132 {
133  save_argc = argc;
134  save_argv = argv;
135 
136 #if defined(PS_USE_CLOBBER_ARGV)
137 
138  /*
139  * If we're going to overwrite the argv area, count the available space.
140  * Also move the environment to make additional room.
141  */
142  {
143  char *end_of_area = NULL;
144  char **new_environ;
145  int i;
146 
147  /*
148  * check for contiguous argv strings
149  */
150  for (i = 0; i < argc; i++)
151  {
152  if (i == 0 || end_of_area + 1 == argv[i])
153  end_of_area = argv[i] + strlen(argv[i]);
154  }
155 
156  if (end_of_area == NULL) /* probably can't happen? */
157  {
158  ps_buffer = NULL;
159  ps_buffer_size = 0;
160  return argv;
161  }
162 
163  /*
164  * check for contiguous environ strings following argv
165  */
166  for (i = 0; environ[i] != NULL; i++)
167  {
168  if (end_of_area + 1 == environ[i])
169  end_of_area = environ[i] + strlen(environ[i]);
170  }
171 
172  ps_buffer = argv[0];
173  last_status_len = ps_buffer_size = end_of_area - argv[0];
174 
175  /*
176  * move the environment out of the way
177  */
178  new_environ = (char **) malloc((i + 1) * sizeof(char *));
179  if (!new_environ)
180  {
181  write_stderr("out of memory\n");
182  exit(1);
183  }
184  for (i = 0; environ[i] != NULL; i++)
185  {
186  new_environ[i] = strdup(environ[i]);
187  if (!new_environ[i])
188  {
189  write_stderr("out of memory\n");
190  exit(1);
191  }
192  }
193  new_environ[i] = NULL;
194  environ = new_environ;
195  }
196 #endif /* PS_USE_CLOBBER_ARGV */
197 
198 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
199 
200  /*
201  * If we're going to change the original argv[] then make a copy for
202  * argument parsing purposes.
203  *
204  * (NB: do NOT think to remove the copying of argv[], even though
205  * postmaster.c finishes looking at argv[] long before we ever consider
206  * changing the ps display. On some platforms, getopt() keeps pointers
207  * into the argv array, and will get horribly confused when it is
208  * re-called to analyze a subprocess' argument string if the argv storage
209  * has been clobbered meanwhile. Other platforms have other dependencies
210  * on argv[].
211  */
212  {
213  char **new_argv;
214  int i;
215 
216  new_argv = (char **) malloc((argc + 1) * sizeof(char *));
217  if (!new_argv)
218  {
219  write_stderr("out of memory\n");
220  exit(1);
221  }
222  for (i = 0; i < argc; i++)
223  {
224  new_argv[i] = strdup(argv[i]);
225  if (!new_argv[i])
226  {
227  write_stderr("out of memory\n");
228  exit(1);
229  }
230  }
231  new_argv[argc] = NULL;
232 
233 #if defined(__darwin__)
234 
235  /*
236  * macOS (and perhaps other NeXT-derived platforms?) has a static copy
237  * of the argv pointer, which we may fix like so:
238  */
239  *_NSGetArgv() = new_argv;
240 #endif
241 
242  argv = new_argv;
243  }
244 #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
245 
246  return argv;
247 }
static int save_argc
Definition: ps_status.c:113
#define write_stderr(str)
Definition: parallel.c:186
#define malloc(a)
Definition: header.h:50
int i
char ** environ
static char ** save_argv
Definition: ps_status.c:114

◆ set_ps_display()

void set_ps_display ( const char *  activity)

Definition at line 349 of file ps_status.c.

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_simple_query(), init_ps_display(), LockBufferForCleanup(), PerformAuthentication(), pgarch_archiveXlog(), PostgresMain(), ProcessIncomingNotify(), ResolveRecoveryConflictWithVirtualXIDs(), SendBaseBackup(), SyncRepWaitForLSN(), WaitOnLock(), WalRcvWaitForStartPosition(), XLogFileRead(), XLogSendPhysical(), and XLogWalRcvFlush().

350 {
351 #ifndef PS_USE_NONE
352  /* update_process_title=off disables updates */
354  return;
355 
356  /* no ps display for stand-alone backend */
357  if (!IsUnderPostmaster)
358  return;
359 
360 #ifdef PS_USE_CLOBBER_ARGV
361  /* If ps_buffer is a pointer, it might still be null */
362  if (!ps_buffer)
363  return;
364 #endif
365 
366  /* Update ps_buffer to contain both fixed part and activity */
367  strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
368  ps_buffer_size - ps_buffer_fixed_size);
369  ps_buffer_cur_len = strlen(ps_buffer);
370 
371  /* Transmit new setting to kernel, if necessary */
372 
373 #ifdef PS_USE_SETPROCTITLE
374  setproctitle("%s", ps_buffer);
375 #elif defined(PS_USE_SETPROCTITLE_FAST)
376  setproctitle_fast("%s", ps_buffer);
377 #endif
378 
379 #ifdef PS_USE_PSTAT
380  {
381  union pstun pst;
382 
383  pst.pst_command = ps_buffer;
384  pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
385  }
386 #endif /* PS_USE_PSTAT */
387 
388 #ifdef PS_USE_PS_STRINGS
389  PS_STRINGS->ps_nargvstr = 1;
390  PS_STRINGS->ps_argvstr = ps_buffer;
391 #endif /* PS_USE_PS_STRINGS */
392 
393 #ifdef PS_USE_CLOBBER_ARGV
394  /* pad unused memory; need only clobber remainder of old status string */
395  if (last_status_len > ps_buffer_cur_len)
396  MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
397  last_status_len - ps_buffer_cur_len);
398  last_status_len = ps_buffer_cur_len;
399 #endif /* PS_USE_CLOBBER_ARGV */
400 
401 #ifdef PS_USE_WIN32
402  {
403  /*
404  * Win32 does not support showing any changed arguments. To make it at
405  * all possible to track which backend is doing what, we create a
406  * named object that can be viewed with for example Process Explorer.
407  */
408  static HANDLE ident_handle = INVALID_HANDLE_VALUE;
409  char name[PS_BUFFER_SIZE + 32];
410 
411  if (ident_handle != INVALID_HANDLE_VALUE)
412  CloseHandle(ident_handle);
413 
414  sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
415 
416  ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
417  }
418 #endif /* PS_USE_WIN32 */
419 #endif /* not PS_USE_NONE */
420 }
int MyProcPid
Definition: globals.c:40
bool update_process_title
Definition: ps_status.c:36
#define PS_PADDING
Definition: ps_status.c:89
#define MemSet(start, val, len)
Definition: c.h:978
#define sprintf
Definition: port.h:195
bool IsUnderPostmaster
Definition: globals.c:109
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
const char * name
Definition: encode.c:561

Variable Documentation

◆ update_process_title