18 #if defined(__darwin__)
19 #include <crt_externs.h>
49 #if defined(HAVE_SETPROCTITLE_FAST)
50 #define PS_USE_SETPROCTITLE_FAST
51 #elif defined(HAVE_SETPROCTITLE)
52 #define PS_USE_SETPROCTITLE
53 #elif defined(__linux__) || defined(__sun) || defined(__darwin__)
54 #define PS_USE_CLOBBER_ARGV
63 #if defined(__linux__) || defined(__darwin__)
64 #define PS_PADDING '\0'
66 #define PS_PADDING ' '
72 #ifndef PS_USE_CLOBBER_ARGV
74 #define PS_BUFFER_SIZE 256
75 static char ps_buffer[PS_BUFFER_SIZE];
76 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
78 static char *ps_buffer;
79 static size_t ps_buffer_size;
80 static size_t last_status_len;
83 static size_t ps_buffer_cur_len;
85 static size_t ps_buffer_fixed_size;
91 static size_t ps_buffer_nosuffix_len;
93 static void flush_ps_display(
void);
122 #if defined(PS_USE_CLOBBER_ARGV)
129 char *end_of_area = NULL;
136 for (
i = 0;
i < argc;
i++)
138 if (
i == 0 || end_of_area + 1 == argv[
i])
139 end_of_area = argv[
i] + strlen(argv[
i]);
142 if (end_of_area == NULL)
165 #if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__))
166 if (strncmp(
environ[
i],
"LD_LIBRARY_PATH=", 16) == 0)
185 last_status_len = ps_buffer_size = end_of_area - argv[0];
190 new_environ = (
char **)
malloc((
i + 1) *
sizeof(
char *));
205 new_environ[
i] = NULL;
225 new_argv = (
char **)
malloc((argc + 1) *
sizeof(
char *));
231 for (
i = 0;
i < argc;
i++)
233 new_argv[
i] = strdup(argv[
i]);
240 new_argv[argc] = NULL;
242 #if defined(__darwin__)
248 *_NSGetArgv() = new_argv;
270 bool save_update_process_title;
286 #ifdef PS_USE_CLOBBER_ARGV
300 #if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
306 #define PROGRAM_NAME_PREFIX ""
308 #define PROGRAM_NAME_PREFIX "postgres: "
314 PROGRAM_NAME_PREFIX
"%s ",
320 PROGRAM_NAME_PREFIX
"%s: %s ",
324 ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
343 update_ps_display_precheck(
void)
353 #ifdef PS_USE_CLOBBER_ARGV
375 if (!update_ps_display_precheck())
379 if (ps_buffer_nosuffix_len > 0)
380 ps_buffer_cur_len = ps_buffer_nosuffix_len;
382 ps_buffer_nosuffix_len = ps_buffer_cur_len;
384 len = strlen(suffix);
387 if (ps_buffer_cur_len +
len + 1 >= ps_buffer_size)
390 if (ps_buffer_cur_len < ps_buffer_size - 1)
393 ps_buffer[ps_buffer_cur_len++] =
' ';
396 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
397 ps_buffer_size - ps_buffer_cur_len - 1);
398 ps_buffer[ps_buffer_size - 1] =
'\0';
399 ps_buffer_cur_len = ps_buffer_size - 1;
404 ps_buffer[ps_buffer_cur_len++] =
' ';
405 memcpy(ps_buffer + ps_buffer_cur_len, suffix,
len + 1);
406 ps_buffer_cur_len = ps_buffer_cur_len +
len;
409 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
425 if (!update_ps_display_precheck())
429 if (ps_buffer_nosuffix_len == 0)
433 ps_buffer[ps_buffer_nosuffix_len] =
'\0';
434 ps_buffer_cur_len = ps_buffer_nosuffix_len;
435 ps_buffer_nosuffix_len = 0;
437 Assert(ps_buffer_cur_len == strlen(ps_buffer));
457 if (!update_ps_display_precheck())
461 ps_buffer_nosuffix_len = 0;
464 if (ps_buffer_fixed_size +
len >= ps_buffer_size)
467 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
468 ps_buffer_size - ps_buffer_fixed_size - 1);
469 ps_buffer[ps_buffer_size - 1] =
'\0';
470 ps_buffer_cur_len = ps_buffer_size - 1;
474 memcpy(ps_buffer + ps_buffer_fixed_size, activity,
len + 1);
475 ps_buffer_cur_len = ps_buffer_fixed_size +
len;
477 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
486 flush_ps_display(
void)
488 #ifdef PS_USE_SETPROCTITLE
489 setproctitle(
"%s", ps_buffer);
490 #elif defined(PS_USE_SETPROCTITLE_FAST)
491 setproctitle_fast(
"%s", ps_buffer);
494 #ifdef PS_USE_CLOBBER_ARGV
496 if (last_status_len > ps_buffer_cur_len)
498 last_status_len - ps_buffer_cur_len);
499 last_status_len = ps_buffer_cur_len;
509 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
510 char name[PS_BUFFER_SIZE + 32];
512 if (ident_handle != INVALID_HANDLE_VALUE)
513 CloseHandle(ident_handle);
517 ident_handle = CreateEvent(NULL, TRUE, FALSE,
name);
532 #ifdef PS_USE_CLOBBER_ARGV
542 *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
544 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