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 *username, const char *dbname, const char *host_info, const char *initial_str)
 
void set_ps_display (const char *activity, bool force)
 
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 416 of file ps_status.c.

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

417 {
418 #ifdef PS_USE_CLOBBER_ARGV
419  /* If ps_buffer is a pointer, it might still be null */
420  if (!ps_buffer)
421  {
422  *displen = 0;
423  return "";
424  }
425 #endif
426 
427 #ifndef PS_USE_NONE
428  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
429 
430  return ps_buffer + ps_buffer_fixed_size;
431 #else
432  return "";
433 #endif
434 }

◆ init_ps_display()

void init_ps_display ( const char *  username,
const char *  dbname,
const char *  host_info,
const char *  initial_str 
)

Definition at line 253 of file ps_status.c.

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

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

255 {
256  Assert(username);
257  Assert(dbname);
258  Assert(host_info);
259 
260 #ifndef PS_USE_NONE
261  /* no ps display for stand-alone backend */
262  if (!IsUnderPostmaster)
263  return;
264 
265  /* no ps display if you didn't call save_ps_display_args() */
266  if (!save_argv)
267  return;
268 
269 #ifdef PS_USE_CLOBBER_ARGV
270  /* If ps_buffer is a pointer, it might still be null */
271  if (!ps_buffer)
272  return;
273 #endif
274 
275  /*
276  * Overwrite argv[] to point at appropriate space, if needed
277  */
278 
279 #ifdef PS_USE_CHANGE_ARGV
280  save_argv[0] = ps_buffer;
281  save_argv[1] = NULL;
282 #endif /* PS_USE_CHANGE_ARGV */
283 
284 #ifdef PS_USE_CLOBBER_ARGV
285  {
286  int i;
287 
288  /* make extra argv slots point at end_of_area (a NUL) */
289  for (i = 1; i < save_argc; i++)
290  save_argv[i] = ps_buffer + ps_buffer_size;
291  }
292 #endif /* PS_USE_CLOBBER_ARGV */
293 
294  /*
295  * Make fixed prefix of ps display.
296  */
297 
298 #if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
299 
300  /*
301  * apparently setproctitle() already adds a `progname:' prefix to the ps
302  * line
303  */
304 #define PROGRAM_NAME_PREFIX ""
305 #else
306 #define PROGRAM_NAME_PREFIX "postgres: "
307 #endif
308 
309  if (*cluster_name == '\0')
310  {
311  snprintf(ps_buffer, ps_buffer_size,
312  PROGRAM_NAME_PREFIX "%s %s %s ",
313  username, dbname, host_info);
314  }
315  else
316  {
317  snprintf(ps_buffer, ps_buffer_size,
318  PROGRAM_NAME_PREFIX "%s: %s %s %s ",
319  cluster_name, username, dbname, host_info);
320  }
321 
322  ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
323 
324  set_ps_display(initial_str, true);
325 #endif /* not PS_USE_NONE */
326 }
static int save_argc
Definition: ps_status.c:112
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:335
bool IsUnderPostmaster
Definition: globals.c:109
char * cluster_name
Definition: guc.c:534
static char * username
Definition: initdb.c:133
#define Assert(condition)
Definition: c.h:739
char * dbname
Definition: streamutil.c:50
int i
#define snprintf
Definition: port.h:192
static char ** save_argv
Definition: ps_status.c:113

◆ save_ps_display_args()

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

Definition at line 130 of file ps_status.c.

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

Referenced by main().

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

◆ set_ps_display()

void set_ps_display ( const char *  activity,
bool  force 
)

Definition at line 335 of file ps_status.c.

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

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

336 {
337 #ifndef PS_USE_NONE
338  /* update_process_title=off disables updates, unless force = true */
339  if (!force && !update_process_title)
340  return;
341 
342  /* no ps display for stand-alone backend */
343  if (!IsUnderPostmaster)
344  return;
345 
346 #ifdef PS_USE_CLOBBER_ARGV
347  /* If ps_buffer is a pointer, it might still be null */
348  if (!ps_buffer)
349  return;
350 #endif
351 
352  /* Update ps_buffer to contain both fixed part and activity */
353  strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
354  ps_buffer_size - ps_buffer_fixed_size);
355  ps_buffer_cur_len = strlen(ps_buffer);
356 
357  /* Transmit new setting to kernel, if necessary */
358 
359 #ifdef PS_USE_SETPROCTITLE
360  setproctitle("%s", ps_buffer);
361 #elif defined(PS_USE_SETPROCTITLE_FAST)
362  setproctitle_fast("%s", ps_buffer);
363 #endif
364 
365 #ifdef PS_USE_PSTAT
366  {
367  union pstun pst;
368 
369  pst.pst_command = ps_buffer;
370  pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
371  }
372 #endif /* PS_USE_PSTAT */
373 
374 #ifdef PS_USE_PS_STRINGS
375  PS_STRINGS->ps_nargvstr = 1;
376  PS_STRINGS->ps_argvstr = ps_buffer;
377 #endif /* PS_USE_PS_STRINGS */
378 
379 #ifdef PS_USE_CLOBBER_ARGV
380  /* pad unused memory; need only clobber remainder of old status string */
381  if (last_status_len > ps_buffer_cur_len)
382  MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
383  last_status_len - ps_buffer_cur_len);
384  last_status_len = ps_buffer_cur_len;
385 #endif /* PS_USE_CLOBBER_ARGV */
386 
387 #ifdef PS_USE_WIN32
388  {
389  /*
390  * Win32 does not support showing any changed arguments. To make it at
391  * all possible to track which backend is doing what, we create a
392  * named object that can be viewed with for example Process Explorer.
393  */
394  static HANDLE ident_handle = INVALID_HANDLE_VALUE;
395  char name[PS_BUFFER_SIZE + 32];
396 
397  if (ident_handle != INVALID_HANDLE_VALUE)
398  CloseHandle(ident_handle);
399 
400  sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
401 
402  ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
403  }
404 #endif /* PS_USE_WIN32 */
405 #endif /* not PS_USE_NONE */
406 }
int MyProcPid
Definition: globals.c:40
bool update_process_title
Definition: ps_status.c:35
#define PS_PADDING
Definition: ps_status.c:88
#define MemSet(start, val, len)
Definition: c.h:962
#define sprintf
Definition: port.h:194
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:521

Variable Documentation

◆ update_process_title