/* Different systems want the buffer padded differently */ #ifdefined(__linux__) || defined(__darwin__) || defined(__GNU__) #define PS_PADDING '\0' #else #define PS_PADDING ' ' #endif
#ifndef PS_USE_NONE
#ifndef PS_USE_CLOBBER_ARGV /* all but one option need a buffer to write their ps line in */ #define PS_BUFFER_SIZE 256 staticchar ps_buffer[PS_BUFFER_SIZE]; staticconst size_t ps_buffer_size = PS_BUFFER_SIZE; #else/* PS_USE_CLOBBER_ARGV */ staticchar *ps_buffer; /* will point to argv area */ static size_t ps_buffer_size; /* space determined at run time */ static size_t last_status_len; /* use to minimize length of clobber */ #endif/* PS_USE_CLOBBER_ARGV */
Assert(fixed_part || MyBackendType); if (!fixed_part)
fixed_part = GetBackendTypeDesc(MyBackendType);
#ifndef PS_USE_NONE /* no ps display for stand-alone backend */ if (!IsUnderPostmaster) return;
/* no ps display if you didn't call save_ps_display_args() */ if (!save_argv) return;
#ifdef PS_USE_CLOBBER_ARGV /* If ps_buffer is a pointer, it might still be null */ if (!ps_buffer) return;
/* make extra argv slots point at end_of_area (a NUL) */ for (int i = 1; i < save_argc; i++)
save_argv[i] = ps_buffer + ps_buffer_size; #endif/* PS_USE_CLOBBER_ARGV */
/* first, check if we need to update the process title */ if (!update_ps_display_precheck()) return;
/* if there's already a suffix, overwrite it */ if (ps_buffer_nosuffix_len > 0)
ps_buffer_cur_len = ps_buffer_nosuffix_len; else
ps_buffer_nosuffix_len = ps_buffer_cur_len;
len = strlen(suffix);
/* check if we have enough space to append the suffix */ if (ps_buffer_cur_len + len + 1 >= ps_buffer_size)
{ /* not enough space. Check the buffer isn't full already */ if (ps_buffer_cur_len < ps_buffer_size - 1)
{ /* append a space before the suffix */
ps_buffer[ps_buffer_cur_len++] = ' ';
/* just add what we can and fill the ps_buffer */
memcpy(ps_buffer + ps_buffer_cur_len, suffix,
ps_buffer_size - ps_buffer_cur_len - 1);
ps_buffer[ps_buffer_size - 1] = '\0';
ps_buffer_cur_len = ps_buffer_size - 1;
}
} else
{
ps_buffer[ps_buffer_cur_len++] = ' ';
memcpy(ps_buffer + ps_buffer_cur_len, suffix, len + 1);
ps_buffer_cur_len = ps_buffer_cur_len + len;
}
Assert(strlen(ps_buffer) == ps_buffer_cur_len);
/* and set the new title */
flush_ps_display(); #endif/* not PS_USE_NONE */
}
/* *set_ps_display_remove_suffix *Removetheprocessdisplaysuffixaddedbyset_ps_display_suffix
*/ void
set_ps_display_remove_suffix(void)
{ #ifndef PS_USE_NONE /* first, check if we need to update the process title */ if (!update_ps_display_precheck()) return;
/* check we added a suffix */ if (ps_buffer_nosuffix_len == 0) return; /* no suffix */
/* remove the suffix from ps_buffer */
ps_buffer[ps_buffer_nosuffix_len] = '\0';
ps_buffer_cur_len = ps_buffer_nosuffix_len;
ps_buffer_nosuffix_len = 0;
Assert(ps_buffer_cur_len == strlen(ps_buffer));
/* and set the new title */
flush_ps_display(); #endif/* not PS_USE_NONE */
}
#ifdef PS_USE_CLOBBER_ARGV /* pad unused memory; need only clobber remainder of old status string */ if (last_status_len > ps_buffer_cur_len)
MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
last_status_len - ps_buffer_cur_len);
last_status_len = ps_buffer_cur_len; #endif/* PS_USE_CLOBBER_ARGV */
/* *Returnswhat'scurrentlyinthepsdisplay,incasesomeoneneeds *it.Notethatonlytheactivitypartisreturned.Onsomeplatforms *thestringwillnotbenull-terminated,soreturntheeffective *lengthinto*displen.
*/ constchar *
get_ps_display(int *displen)
{ #ifdef PS_USE_CLOBBER_ARGV /* If ps_buffer is a pointer, it might still be null */ if (!ps_buffer)
{
*displen = 0; return"";
} #endif