PostgreSQL Source Code git master
Macros | Functions | Variables
ps_status.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "miscadmin.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
Include dependency graph for ps_status.c:

Go to the source code of this file.

Macros

#define  PS_USE_NONE
 
#define  PS_PADDING   ' '
 

Functions

char **  save_ps_display_args (int argc, char **argv)
 
void  init_ps_display (const char *fixed_part)
 
void  set_ps_display_suffix (const char *suffix)
 
 
void  set_ps_display_with_len (const char *activity, size_t len)
 
const char *  get_ps_display (int *displen)
 

Variables

char **  environ
 
 
static int  save_argc
 
static char **  save_argv
 

Macro Definition Documentation

PS_PADDING

#define PS_PADDING   ' '

Definition at line 68 of file ps_status.c.

PS_USE_NONE

#define PS_USE_NONE

Definition at line 60 of file ps_status.c.

Function Documentation

get_ps_display()

const char * get_ps_display ( int *  displen )

Definition at line 548 of file ps_status.c.

549{
550#ifdef PS_USE_CLOBBER_ARGV
551 /* If ps_buffer is a pointer, it might still be null */
552 if (!ps_buffer)
553 {
554 *displen = 0;
555 return "";
556 }
557#endif
558
559#ifndef PS_USE_NONE
560 *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
561
562 return ps_buffer + ps_buffer_fixed_size;
563#else
564 *displen = 0;
565 return "";
566#endif
567}

Referenced by log_status_format(), write_csvlog(), and write_jsonlog().

init_ps_display()

void init_ps_display ( const char *  fixed_part )

Definition at line 285 of file ps_status.c.

286{
287#ifndef PS_USE_NONE
288 bool save_update_process_title;
289#endif
290
291 Assert(fixed_part || MyBackendType);
292 if (!fixed_part)
293 fixed_part = GetBackendTypeDesc(MyBackendType);
294
295#ifndef PS_USE_NONE
296 /* no ps display for stand-alone backend */
298 return;
299
300 /* no ps display if you didn't call save_ps_display_args() */
301 if (!save_argv)
302 return;
303
304#ifdef PS_USE_CLOBBER_ARGV
305 /* If ps_buffer is a pointer, it might still be null */
306 if (!ps_buffer)
307 return;
308
309 /* make extra argv slots point at end_of_area (a NUL) */
310 for (int i = 1; i < save_argc; i++)
311 save_argv[i] = ps_buffer + ps_buffer_size;
312#endif /* PS_USE_CLOBBER_ARGV */
313
314 /*
315 * Make fixed prefix of ps display.
316 */
317
318#if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
319
320 /*
321 * apparently setproctitle() already adds a `progname:' prefix to the ps
322 * line
323 */
324#define PROGRAM_NAME_PREFIX ""
325#else
326#define PROGRAM_NAME_PREFIX "postgres: "
327#endif
328
329 if (*cluster_name == '0円')
330 {
331 snprintf(ps_buffer, ps_buffer_size,
332 PROGRAM_NAME_PREFIX "%s ",
333 fixed_part);
334 }
335 else
336 {
337 snprintf(ps_buffer, ps_buffer_size,
338 PROGRAM_NAME_PREFIX "%s: %s ",
339 cluster_name, fixed_part);
340 }
341
342 ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
343
344 /*
345 * On the first run, force the update.
346 */
347 save_update_process_title = update_process_title;
349 set_ps_display("");
350 update_process_title = save_update_process_title;
351#endif /* not PS_USE_NONE */
352}
bool IsUnderPostmaster
Definition: globals.c:120
char * cluster_name
Definition: guc_tables.c:555
Assert(PointerIsAligned(start, uint64))
i
int i
Definition: isn.c:77
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:263
BackendType MyBackendType
Definition: miscinit.c:64
#define snprintf
Definition: port.h:239
static int save_argc
Definition: ps_status.c:100
bool update_process_title
Definition: ps_status.c:31
static char ** save_argv
Definition: ps_status.c:101
static void set_ps_display(const char *activity)
Definition: ps_status.h:40

References Assert(), cluster_name, GetBackendTypeDesc(), i, IsUnderPostmaster, MyBackendType, save_argc, save_argv, set_ps_display(), snprintf, and update_process_title.

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMainCommon(), BackendInitialize(), BackgroundWorkerMain(), ReplSlotSyncWorkerMain(), and SysLoggerMain().

save_ps_display_args()

char ** save_ps_display_args ( int  argc,
char **  argv 
)

Definition at line 130 of file ps_status.c.

131{
132 save_argc = argc;
133 save_argv = argv;
134
135#if defined(PS_USE_CLOBBER_ARGV)
136
137 /*
138 * If we're going to overwrite the argv area, count the available space.
139 * Also move the environment strings to make additional room.
140 */
141 {
142 char *end_of_area = NULL;
143 char **new_environ;
144 int i;
145
146 /*
147 * check for contiguous argv strings
148 */
149 for (i = 0; i < argc; i++)
150 {
151 if (i == 0 || end_of_area + 1 == argv[i])
152 end_of_area = argv[i] + strlen(argv[i]);
153 }
154
155 if (end_of_area == NULL) /* probably can't happen? */
156 {
157 ps_buffer = NULL;
158 ps_buffer_size = 0;
159 return argv;
160 }
161
162 /*
163 * check for contiguous environ strings following argv
164 */
165 for (i = 0; environ[i] != NULL; i++)
166 {
167 if (end_of_area + 1 == environ[i])
168 {
169 /*
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.
177 */
178#if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__))
179 if (strncmp(environ[i], "LD_LIBRARY_PATH=", 16) == 0)
180 {
181 /*
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.
186 */
187 end_of_area = environ[i] + 15;
188 }
189 else
190#endif
191 {
192 end_of_area = environ[i] + strlen(environ[i]);
193 }
194 }
195 }
196
197 ps_buffer = argv[0];
198 last_status_len = ps_buffer_size = end_of_area - argv[0];
199
200 /*
201 * move the environment out of the way
202 */
203 new_environ = (char **) malloc((i + 1) * sizeof(char *));
204 if (!new_environ)
205 {
206 write_stderr("out of memory\n");
207 exit(1);
208 }
209 for (i = 0; environ[i] != NULL; i++)
210 {
211 new_environ[i] = strdup(environ[i]);
212 if (!new_environ[i])
213 {
214 write_stderr("out of memory\n");
215 exit(1);
216 }
217 }
218 new_environ[i] = NULL;
219 environ = new_environ;
220
221 /* See notes about Valgrind above. */
222#ifdef USE_VALGRIND
223 ps_status_new_environ = new_environ;
224#endif
225 }
226
227 /*
228 * If we're going to change the original argv[] then make a copy for
229 * argument parsing purposes.
230 *
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
237 * on argv[].
238 */
239 {
240 char **new_argv;
241 int i;
242
243 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
244 if (!new_argv)
245 {
246 write_stderr("out of memory\n");
247 exit(1);
248 }
249 for (i = 0; i < argc; i++)
250 {
251 new_argv[i] = strdup(argv[i]);
252 if (!new_argv[i])
253 {
254 write_stderr("out of memory\n");
255 exit(1);
256 }
257 }
258 new_argv[argc] = NULL;
259
260#if defined(__darwin__)
261
262 /*
263 * macOS has a static copy of the argv pointer, which we may fix like
264 * so:
265 */
266 *_NSGetArgv() = new_argv;
267#endif
268
269 argv = new_argv;
270 }
271#endif /* PS_USE_CLOBBER_ARGV */
272
273 return argv;
274}
#define write_stderr(str)
Definition: parallel.c:186
#define malloc(a)
Definition: header.h:50
char ** environ

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

Referenced by main().

set_ps_display_remove_suffix()

void set_ps_display_remove_suffix ( void  )

Definition at line 439 of file ps_status.c.

440{
441#ifndef PS_USE_NONE
442 /* first, check if we need to update the process title */
443 if (!update_ps_display_precheck())
444 return;
445
446 /* check we added a suffix */
447 if (ps_buffer_nosuffix_len == 0)
448 return; /* no suffix */
449
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;
454
455 Assert(ps_buffer_cur_len == strlen(ps_buffer));
456
457 /* and set the new title */
458 flush_ps_display();
459#endif /* not PS_USE_NONE */
460}

References Assert().

Referenced by LockBufferForCleanup(), ResolveRecoveryConflictWithVirtualXIDs(), SyncRepWaitForLSN(), and WaitOnLock().

set_ps_display_suffix()

void set_ps_display_suffix ( const char *  suffix )

Definition at line 387 of file ps_status.c.

388{
389#ifndef PS_USE_NONE
390 size_t len;
391
392 /* first, check if we need to update the process title */
393 if (!update_ps_display_precheck())
394 return;
395
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;
399 else
400 ps_buffer_nosuffix_len = ps_buffer_cur_len;
401
402 len = strlen(suffix);
403
404 /* check if we have enough space to append the suffix */
405 if (ps_buffer_cur_len + len + 1 >= ps_buffer_size)
406 {
407 /* not enough space. Check the buffer isn't full already */
408 if (ps_buffer_cur_len < ps_buffer_size - 1)
409 {
410 /* append a space before the suffix */
411 ps_buffer[ps_buffer_cur_len++] = ' ';
412
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;
418 }
419 }
420 else
421 {
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;
425 }
426
427 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
428
429 /* and set the new title */
430 flush_ps_display();
431#endif /* not PS_USE_NONE */
432}
const void size_t len

References Assert(), and len.

Referenced by LockBufferForCleanup(), ResolveRecoveryConflictWithVirtualXIDs(), SyncRepWaitForLSN(), and WaitOnLock().

set_ps_display_with_len()

void set_ps_display_with_len ( const char *  activity,
size_t  len 
)

Definition at line 469 of file ps_status.c.

470{
471 Assert(strlen(activity) == len);
472
473#ifndef PS_USE_NONE
474 /* first, check if we need to update the process title */
475 if (!update_ps_display_precheck())
476 return;
477
478 /* wipe out any suffix when the title is completely changed */
479 ps_buffer_nosuffix_len = 0;
480
481 /* Update ps_buffer to contain both fixed part and activity */
482 if (ps_buffer_fixed_size + len >= ps_buffer_size)
483 {
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;
489 }
490 else
491 {
492 memcpy(ps_buffer + ps_buffer_fixed_size, activity, len + 1);
493 ps_buffer_cur_len = ps_buffer_fixed_size + len;
494 }
495 Assert(strlen(ps_buffer) == ps_buffer_cur_len);
496
497 /* Transmit new setting to kernel, if necessary */
498 flush_ps_display();
499#endif /* not PS_USE_NONE */
500}

References Assert(), and len.

Referenced by exec_execute_message(), exec_simple_query(), and set_ps_display().

Variable Documentation

environ

char** environ
extern

Referenced by get_environ(), PostmasterMain(), and save_ps_display_args().

save_argc

int save_argc
static

Definition at line 100 of file ps_status.c.

Referenced by init_ps_display(), and save_ps_display_args().

save_argv

char** save_argv
static

Definition at line 101 of file ps_status.c.

Referenced by init_ps_display(), and save_ps_display_args().

update_process_title

bool update_process_title = DEFAULT_UPDATE_PROCESS_TITLE

Definition at line 31 of file ps_status.c.

Referenced by init_ps_display(), ResolveRecoveryConflictWithVirtualXIDs(), SendBaseBackup(), SyncRepWaitForLSN(), WalRcvWaitForStartPosition(), XLogSendPhysical(), and XLogWalRcvFlush().

AltStyle によって変換されたページ (->オリジナル) /