PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

const char* get_ps_display ( int *  displen)

Definition at line 405 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().

406 {
407 #ifdef PS_USE_CLOBBER_ARGV
408  /* If ps_buffer is a pointer, it might still be null */
409  if (!ps_buffer)
410  {
411  *displen = 0;
412  return "";
413  }
414 #endif
415 
416  *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
417 
419 }
static size_t ps_buffer_cur_len
Definition: ps_status.c:98
static size_t ps_buffer_fixed_size
Definition: ps_status.c:100
static char ps_buffer[PS_BUFFER_SIZE]
Definition: ps_status.c:90
void init_ps_display ( const char *  username,
const char *  dbname,
const char *  host_info,
const char *  initial_str 
)

Definition at line 244 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().

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

Definition at line 121 of file ps_status.c.

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

Referenced by main().

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

Definition at line 326 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, 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().

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

Variable Documentation