18#if defined(__darwin__)
19#include <crt_externs.h>
26#if !defined(WIN32) || defined(_MSC_VER)
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
65#if defined(__linux__) || defined(__darwin__)
66#define PS_PADDING '\0'
74#ifndef PS_USE_CLOBBER_ARGV
76#define PS_BUFFER_SIZE 256
77static char ps_buffer[PS_BUFFER_SIZE];
78static const size_t ps_buffer_size = PS_BUFFER_SIZE;
80static char *ps_buffer;
81static size_t ps_buffer_size;
82static size_t last_status_len;
85static size_t ps_buffer_cur_len;
87static size_t ps_buffer_fixed_size;
93static size_t ps_buffer_nosuffix_len;
95static void flush_ps_display(
void);
124#if defined(PS_USE_CLOBBER_ARGV)
131 char *end_of_area = NULL;
138 for (
i = 0;
i < argc;
i++)
140 if (
i == 0 || end_of_area + 1 == argv[
i])
141 end_of_area = argv[
i] + strlen(argv[
i]);
144 if (end_of_area == NULL)
167#if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__))
168 if (strncmp(
environ[
i],
"LD_LIBRARY_PATH=", 16) == 0)
187 last_status_len = ps_buffer_size = end_of_area - argv[0];
192 new_environ = (
char **)
malloc((
i + 1) *
sizeof(
char *));
207 new_environ[
i] = NULL;
227 new_argv = (
char **)
malloc((argc + 1) *
sizeof(
char *));
233 for (
i = 0;
i < argc;
i++)
235 new_argv[
i] = strdup(argv[
i]);
242 new_argv[argc] = NULL;
244#if defined(__darwin__)
250 *_NSGetArgv() = new_argv;
272 bool save_update_process_title;
288#ifdef PS_USE_CLOBBER_ARGV
302#if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
308#define PROGRAM_NAME_PREFIX ""
310#define PROGRAM_NAME_PREFIX "postgres: "
316 PROGRAM_NAME_PREFIX
"%s ",
322 PROGRAM_NAME_PREFIX
"%s: %s ",
326 ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
345update_ps_display_precheck(
void)
355#ifdef PS_USE_CLOBBER_ARGV
377 if (!update_ps_display_precheck())
381 if (ps_buffer_nosuffix_len > 0)
382 ps_buffer_cur_len = ps_buffer_nosuffix_len;
384 ps_buffer_nosuffix_len = ps_buffer_cur_len;
386 len = strlen(suffix);
389 if (ps_buffer_cur_len +
len + 1 >= ps_buffer_size)
392 if (ps_buffer_cur_len < ps_buffer_size - 1)
395 ps_buffer[ps_buffer_cur_len++] =
' ';
398 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
399 ps_buffer_size - ps_buffer_cur_len - 1);
400 ps_buffer[ps_buffer_size - 1] =
'\0';
401 ps_buffer_cur_len = ps_buffer_size - 1;
406 ps_buffer[ps_buffer_cur_len++] =
' ';
407 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
len + 1);
408 ps_buffer_cur_len = ps_buffer_cur_len +
len;
411 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
427 if (!update_ps_display_precheck())
431 if (ps_buffer_nosuffix_len == 0)
435 ps_buffer[ps_buffer_nosuffix_len] =
'\0';
436 ps_buffer_cur_len = ps_buffer_nosuffix_len;
437 ps_buffer_nosuffix_len = 0;
439 Assert(ps_buffer_cur_len == strlen(ps_buffer));
459 if (!update_ps_display_precheck())
463 ps_buffer_nosuffix_len = 0;
466 if (ps_buffer_fixed_size +
len >= ps_buffer_size)
469 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
470 ps_buffer_size - ps_buffer_fixed_size - 1);
471 ps_buffer[ps_buffer_size - 1] =
'\0';
472 ps_buffer_cur_len = ps_buffer_size - 1;
476 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
len + 1);
477 ps_buffer_cur_len = ps_buffer_fixed_size +
len;
479 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
488flush_ps_display(
void)
490#ifdef PS_USE_SETPROCTITLE
491 setproctitle(
"%s", ps_buffer);
492#elif defined(PS_USE_SETPROCTITLE_FAST)
493 setproctitle_fast(
"%s", ps_buffer);
496#ifdef PS_USE_CLOBBER_ARGV
498 if (last_status_len > ps_buffer_cur_len)
500 last_status_len - ps_buffer_cur_len);
501 last_status_len = ps_buffer_cur_len;
511 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
512 char name[PS_BUFFER_SIZE + 32];
514 if (ident_handle != INVALID_HANDLE_VALUE)
515 CloseHandle(ident_handle);
519 ident_handle = CreateEvent(NULL, TRUE, FALSE,
name);
534#ifdef PS_USE_CLOBBER_ARGV
544 *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
546 return ps_buffer + ps_buffer_fixed_size;
#define write_stderr(str)
#define Assert(condition)
#define MemSet(start, val, len)
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