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 412 of file ps_status.c.

References ps_buffer, ps_buffer_cur_len, and ps_buffer_fixed_size.

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

413 {
414 #ifdef PS_USE_CLOBBER_ARGV
415  /* If ps_buffer is a pointer, it might still be null */
416  if (!ps_buffer)
417  {
418  *displen = 0;
419  return "";
420  }
421 #endif
422 
423  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
424 
426 }
static size_t ps_buffer_cur_len
Definition: ps_status.c:103
static size_t ps_buffer_fixed_size
Definition: ps_status.c:105
static char ps_buffer[PS_BUFFER_SIZE]
Definition: ps_status.c:95

◆ init_ps_display()

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

Definition at line 249 of file ps_status.c.

References Assert, cluster_name, i, IsUnderPostmaster, ps_buffer, ps_buffer_cur_len, ps_buffer_fixed_size, ps_buffer_size, save_argc, save_argv, set_ps_display(), and snprintf.

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

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

◆ save_ps_display_args()

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

Definition at line 126 of file ps_status.c.

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

Referenced by main().

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

◆ set_ps_display()

void set_ps_display ( const char *  activity,
bool  force 
)

Definition at line 331 of file ps_status.c.

References FALSE, IsUnderPostmaster, MemSet, MyProcPid, name, ps_buffer, ps_buffer_cur_len, ps_buffer_fixed_size, PS_BUFFER_SIZE, ps_buffer_size, PS_PADDING, sprintf, strlcpy(), TRUE, 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().

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

Variable Documentation

◆ update_process_title