1/*--------------------------------------------------------------------
4 * Routines to support changing the ps display of PostgreSQL backends
5 * to contain some useful information. Mechanism differs wildly across
8 * src/backend/utils/misc/ps_status.c
10 * Copyright (c) 2000-2025, PostgreSQL Global Development Group
11 * various details abducted from various places
12 *--------------------------------------------------------------------
18#if defined(__darwin__)
19#include <crt_externs.h>
26#if !defined(WIN32) || defined(_MSC_VER)
34 * Alternative ways of updating ps display:
36 * PS_USE_SETPROCTITLE_FAST
37 * use the function setproctitle_fast(const char *, ...)
40 * use the function setproctitle(const char *, ...)
43 * write over the argv and environment area
44 * (Linux and most SysV-like systems)
46 * push the string out as the name of a Windows event
48 * don't update ps display
49 * (This is the default, as it is safest.)
51#if defined(HAVE_SETPROCTITLE_FAST)
52#define PS_USE_SETPROCTITLE_FAST
53#elif defined(HAVE_SETPROCTITLE)
54#define PS_USE_SETPROCTITLE
55#elif defined(__linux__) || defined(__sun) || defined(__darwin__)
56#define PS_USE_CLOBBER_ARGV
64/* Different systems want the buffer padded differently */
65#if defined(__linux__) || defined(__darwin__)
66#define PS_PADDING '0円'
68 #define PS_PADDING ' '
74#ifndef PS_USE_CLOBBER_ARGV
75/* all but one option need a buffer to write their ps line in */
76#define PS_BUFFER_SIZE 256
77static char ps_buffer[PS_BUFFER_SIZE];
78static const size_t ps_buffer_size = PS_BUFFER_SIZE;
79#else /* PS_USE_CLOBBER_ARGV */
80static char *ps_buffer;
/* will point to argv area */
81static size_t ps_buffer_size;
/* space determined at run time */
82static size_t last_status_len;
/* use to minimize length of clobber */
83#endif /* PS_USE_CLOBBER_ARGV */
85static size_t ps_buffer_cur_len;
/* nominal strlen(ps_buffer) */
87static size_t ps_buffer_fixed_size;
/* size of the constant prefix */
90 * Length of ps_buffer before the suffix was appended to the end, or 0 if we
91 * didn't set a suffix.
93static size_t ps_buffer_nosuffix_len;
95static void flush_ps_display(
void);
97#endif /* not PS_USE_NONE */
99/* save the original argv[] location here */
104 * Valgrind seems not to consider the global "environ" variable as a valid
105 * root pointer; so when we allocate a new environment array, it claims that
106 * data is leaked. To fix that, keep our own statically-allocated copy of the
107 * pointer. (Oddly, this doesn't seem to be a problem for "argv".)
109#if defined(PS_USE_CLOBBER_ARGV) && defined(USE_VALGRIND)
110extern char **ps_status_new_environ;
111char **ps_status_new_environ;
116 * Call this early in startup to save the original argc/argv values.
117 * If needed, we make a copy of the original argv[] array to preserve it
118 * from being clobbered by subsequent ps_display actions.
120 * (The original argv[] will not be overwritten by this routine, but may be
121 * overwritten during init_ps_display. Also, the physical location of the
122 * environment strings may be moved, so this should be called before any code
123 * that might try to hang onto a getenv() result. But see hack for musl
126 * Note that in case of failure this cannot call elog() as that is not
127 * initialized yet. We rely on write_stderr() instead.
135#if defined(PS_USE_CLOBBER_ARGV)
138 * If we're going to overwrite the argv area, count the available space.
139 * Also move the environment strings to make additional room.
142 char *end_of_area = NULL;
147 * check for contiguous argv strings
149 for (
i = 0;
i < argc;
i++)
151 if (
i == 0 || end_of_area + 1 == argv[
i])
152 end_of_area = argv[
i] + strlen(argv[
i]);
155 if (end_of_area == NULL)
/* probably can't happen? */
163 * check for contiguous environ strings following argv
170 * The musl dynamic linker keeps a static pointer to the
171 * initial value of LD_LIBRARY_PATH, if that is defined in the
172 * process's environment. Therefore, we must not overwrite the
173 * value of that setting and thus cannot advance end_of_area
174 * beyond it. Musl does not define any identifying compiler
175 * symbol, so we have to do this unless we see a symbol
176 * identifying a Linux libc we know is safe.
178#if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__))
179 if (strncmp(
environ[
i],
"LD_LIBRARY_PATH=", 16) == 0)
182 * We can overwrite the name, but stop at the equals sign.
183 * Future loop iterations will not find any more
184 * contiguous space, but we don't break early because we
185 * need to count the total number of environ[] entries.
198 last_status_len = ps_buffer_size = end_of_area - argv[0];
201 * move the environment out of the way
203 new_environ = (
char **)
malloc((
i + 1) *
sizeof(
char *));
218 new_environ[
i] = NULL;
221 /* See notes about Valgrind above. */
223 ps_status_new_environ = new_environ;
228 * If we're going to change the original argv[] then make a copy for
229 * argument parsing purposes.
231 * NB: do NOT think to remove the copying of argv[], even though
232 * postmaster.c finishes looking at argv[] long before we ever consider
233 * changing the ps display. On some platforms, getopt() keeps pointers
234 * into the argv array, and will get horribly confused when it is
235 * re-called to analyze a subprocess' argument string if the argv storage
236 * has been clobbered meanwhile. Other platforms have other dependencies
243 new_argv = (
char **)
malloc((argc + 1) *
sizeof(
char *));
249 for (
i = 0;
i < argc;
i++)
251 new_argv[
i] = strdup(argv[
i]);
258 new_argv[argc] = NULL;
260#if defined(__darwin__)
263 * macOS has a static copy of the argv pointer, which we may fix like
266 *_NSGetArgv() = new_argv;
271#endif /* PS_USE_CLOBBER_ARGV */
277 * Call this once during subprocess startup to set the identification
280 * If fixed_part is NULL, a default will be obtained from MyBackendType.
282 * At this point, the original argv[] array may be overwritten.
288 bool save_update_process_title;
296 /* no ps display for stand-alone backend */
300 /* no ps display if you didn't call save_ps_display_args() */
304#ifdef PS_USE_CLOBBER_ARGV
305 /* If ps_buffer is a pointer, it might still be null */
309 /* make extra argv slots point at end_of_area (a NUL) */
312#endif /* PS_USE_CLOBBER_ARGV */
315 * Make fixed prefix of ps display.
318#if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
321 * apparently setproctitle() already adds a `progname:' prefix to the ps
324#define PROGRAM_NAME_PREFIX ""
326#define PROGRAM_NAME_PREFIX "postgres: "
332 PROGRAM_NAME_PREFIX
"%s ",
338 PROGRAM_NAME_PREFIX
"%s: %s ",
342 ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
345 * On the first run, force the update.
351#endif /* not PS_USE_NONE */
356 * update_ps_display_precheck
357 * Helper function to determine if updating the process title is
358 * something that we need to do.
361update_ps_display_precheck(
void)
363 /* update_process_title=off disables updates */
367 /* no ps display for stand-alone backend */
371#ifdef PS_USE_CLOBBER_ARGV
372 /* If ps_buffer is a pointer, it might still be null */
379#endif /* not PS_USE_NONE */
382 * set_ps_display_suffix
383 * Adjust the process title to append 'suffix' onto the end with a space
384 * between it and the current process title.
392 /* first, check if we need to update the process title */
393 if (!update_ps_display_precheck())
396 /* if there's already a suffix, overwrite it */
397 if (ps_buffer_nosuffix_len > 0)
398 ps_buffer_cur_len = ps_buffer_nosuffix_len;
400 ps_buffer_nosuffix_len = ps_buffer_cur_len;
402 len = strlen(suffix);
404 /* check if we have enough space to append the suffix */
405 if (ps_buffer_cur_len +
len + 1 >= ps_buffer_size)
407 /* not enough space. Check the buffer isn't full already */
408 if (ps_buffer_cur_len < ps_buffer_size - 1)
410 /* append a space before the suffix */
411 ps_buffer[ps_buffer_cur_len++] =
' ';
413 /* just add what we can and fill the ps_buffer */
414 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
415 ps_buffer_size - ps_buffer_cur_len - 1);
416 ps_buffer[ps_buffer_size - 1] =
'0円';
417 ps_buffer_cur_len = ps_buffer_size - 1;
422 ps_buffer[ps_buffer_cur_len++] =
' ';
423 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
len + 1);
424 ps_buffer_cur_len = ps_buffer_cur_len +
len;
427 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
429 /* and set the new title */
431#endif /* not PS_USE_NONE */
435 * set_ps_display_remove_suffix
436 * Remove the process display suffix added by set_ps_display_suffix
442 /* first, check if we need to update the process title */
443 if (!update_ps_display_precheck())
446 /* check we added a suffix */
447 if (ps_buffer_nosuffix_len == 0)
448 return;
/* no suffix */
450 /* remove the suffix from ps_buffer */
451 ps_buffer[ps_buffer_nosuffix_len] =
'0円';
452 ps_buffer_cur_len = ps_buffer_nosuffix_len;
453 ps_buffer_nosuffix_len = 0;
455 Assert(ps_buffer_cur_len == strlen(ps_buffer));
457 /* and set the new title */
459#endif /* not PS_USE_NONE */
463 * Call this to update the ps status display to a fixed prefix plus an
464 * indication of what you're currently doing passed in the argument.
466 * 'len' must be the same as strlen(activity)
474 /* first, check if we need to update the process title */
475 if (!update_ps_display_precheck())
478 /* wipe out any suffix when the title is completely changed */
479 ps_buffer_nosuffix_len = 0;
481 /* Update ps_buffer to contain both fixed part and activity */
482 if (ps_buffer_fixed_size +
len >= ps_buffer_size)
484 /* handle the case where ps_buffer doesn't have enough space */
485 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
486 ps_buffer_size - ps_buffer_fixed_size - 1);
487 ps_buffer[ps_buffer_size - 1] =
'0円';
488 ps_buffer_cur_len = ps_buffer_size - 1;
492 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
len + 1);
493 ps_buffer_cur_len = ps_buffer_fixed_size +
len;
495 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
497 /* Transmit new setting to kernel, if necessary */
499#endif /* not PS_USE_NONE */
504flush_ps_display(
void)
506#ifdef PS_USE_SETPROCTITLE
507 setproctitle(
"%s", ps_buffer);
508#elif defined(PS_USE_SETPROCTITLE_FAST)
509 setproctitle_fast(
"%s", ps_buffer);
512#ifdef PS_USE_CLOBBER_ARGV
513 /* pad unused memory; need only clobber remainder of old status string */
514 if (last_status_len > ps_buffer_cur_len)
516 last_status_len - ps_buffer_cur_len);
517 last_status_len = ps_buffer_cur_len;
518#endif /* PS_USE_CLOBBER_ARGV */
523 * Win32 does not support showing any changed arguments. To make it at
524 * all possible to track which backend is doing what, we create a
525 * named object that can be viewed with for example Process Explorer.
527 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
528 char name[PS_BUFFER_SIZE + 32];
530 if (ident_handle != INVALID_HANDLE_VALUE)
531 CloseHandle(ident_handle);
535 ident_handle = CreateEvent(NULL, TRUE, FALSE,
name);
537#endif /* PS_USE_WIN32 */
539#endif /* not PS_USE_NONE */
542 * Returns what's currently in the ps display, in case someone needs
543 * it. Note that only the activity part is returned. On some platforms
544 * the string will not be null-terminated, so return the effective
545 * length into *displen.
550#ifdef PS_USE_CLOBBER_ARGV
551 /* If ps_buffer is a pointer, it might still be null */
560 *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
562 return ps_buffer + ps_buffer_fixed_size;
#define write_stderr(str)
#define MemSet(start, val, len)
Assert(PointerIsAligned(start, uint64))
const char * GetBackendTypeDesc(BackendType backendType)
BackendType MyBackendType
const char * get_ps_display(int *displen)
void set_ps_display_remove_suffix(void)
void set_ps_display_with_len(const char *activity, size_t len)
void init_ps_display(const char *fixed_part)
void set_ps_display_suffix(const char *suffix)
bool update_process_title
char ** save_ps_display_args(int argc, char **argv)
static void set_ps_display(const char *activity)
#define DEFAULT_UPDATE_PROCESS_TITLE