Skip to content

Commit ede5072

Browse files
committed
Change ps_status.c to explicitly track the current logical length of ps_buffer.
This saves cycles in get_ps_display() on many popular platforms, and more importantly ensures that get_ps_display() will correctly return an empty string if init_ps_display() hasn't been called yet. Per trouble report from Ray Stell, in which log_line_prefix %i produced junk early in backend startup. Back-patch to 8.0. 7.4 doesn't have %i and its version of get_ps_display() makes no pretense of avoiding pad junk anyhow.
1 parent 2175d4f commit ede5072

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

src/backend/utils/misc/ps_status.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* to contain some useful information. Mechanism differs wildly across
66
* platforms.
77
*
8-
* $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.40 2010/01/02 16:57:58 momjian Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.41 2010/05/27 19:19:38 tgl Exp $
99
*
1010
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
1111
* various details abducted from various places
@@ -51,7 +51,7 @@ bool update_process_title = true;
5151
* (some other BSD systems)
5252
* PS_USE_CLOBBER_ARGV
5353
* write over the argv and environment area
54-
* (most SysV-like systems)
54+
* (Linux and most SysV-like systems)
5555
* PS_USE_WIN32
5656
* push the string out as the name of a Windows event
5757
* PS_USE_NONE
@@ -84,7 +84,7 @@ bool update_process_title = true;
8484

8585

8686
#ifndef PS_USE_CLOBBER_ARGV
87-
/* all but one options need a buffer to write their ps line in */
87+
/* all but one option need a buffer to write their ps line in */
8888
#define PS_BUFFER_SIZE 256
8989
static char ps_buffer[PS_BUFFER_SIZE];
9090
static const size_t ps_buffer_size = PS_BUFFER_SIZE;
@@ -94,6 +94,8 @@ static size_t ps_buffer_size; /* space determined at run time */
9494
static size_t last_status_len; /* use to minimize length of clobber */
9595
#endif /* PS_USE_CLOBBER_ARGV */
9696

97+
static size_t ps_buffer_cur_len; /* nominal strlen(ps_buffer) */
98+
9799
static size_t ps_buffer_fixed_size; /* size of the constant prefix */
98100

99101
/* save the original argv[] location here */
@@ -226,6 +228,7 @@ init_ps_display(const char *username, const char *dbname,
226228
/* no ps display if you didn't call save_ps_display_args() */
227229
if (!save_argv)
228230
return;
231+
229232
#ifdef PS_USE_CLOBBER_ARGV
230233
/* If ps_buffer is a pointer, it might still be null */
231234
if (!ps_buffer)
@@ -270,7 +273,7 @@ init_ps_display(const char *username, const char *dbname,
270273
username, dbname, host_info);
271274
#endif
272275

273-
ps_buffer_fixed_size = strlen(ps_buffer);
276+
ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
274277

275278
set_ps_display(initial_str, true);
276279
#endif /* not PS_USE_NONE */
@@ -285,11 +288,11 @@ init_ps_display(const char *username, const char *dbname,
285288
void
286289
set_ps_display(const char *activity, bool force)
287290
{
288-
291+
#ifndef PS_USE_NONE
292+
/* update_process_title=off disables updates, unless force = true */
289293
if (!force && !update_process_title)
290294
return;
291295

292-
#ifndef PS_USE_NONE
293296
/* no ps display for stand-alone backend */
294297
if (!IsUnderPostmaster)
295298
return;
@@ -303,6 +306,7 @@ set_ps_display(const char *activity, bool force)
303306
/* Update ps_buffer to contain both fixed part and activity */
304307
strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
305308
ps_buffer_size - ps_buffer_fixed_size);
309+
ps_buffer_cur_len = strlen(ps_buffer);
306310

307311
/* Transmit new setting to kernel, if necessary */
308312

@@ -315,7 +319,7 @@ set_ps_display(const char *activity, bool force)
315319
union pstun pst;
316320

317321
pst.pst_command = ps_buffer;
318-
pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
322+
pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
319323
}
320324
#endif /* PS_USE_PSTAT */
321325

@@ -325,16 +329,11 @@ set_ps_display(const char *activity, bool force)
325329
#endif /* PS_USE_PS_STRINGS */
326330

327331
#ifdef PS_USE_CLOBBER_ARGV
328-
{
329-
int buflen;
330-
331-
/* pad unused memory */
332-
buflen = strlen(ps_buffer);
333-
/* clobber remainder of old status string */
334-
if (last_status_len > buflen)
335-
MemSet(ps_buffer + buflen, PS_PADDING, last_status_len - buflen);
336-
last_status_len = buflen;
337-
}
332+
/* pad unused memory; need only clobber remainder of old status string */
333+
if (last_status_len > ps_buffer_cur_len)
334+
MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
335+
last_status_len - ps_buffer_cur_len);
336+
last_status_len = ps_buffer_cur_len;
338337
#endif /* PS_USE_CLOBBER_ARGV */
339338

340339
#ifdef PS_USE_WIN32
@@ -369,24 +368,15 @@ const char *
369368
get_ps_display(int *displen)
370369
{
371370
#ifdef PS_USE_CLOBBER_ARGV
372-
size_t offset;
373-
374371
/* If ps_buffer is a pointer, it might still be null */
375372
if (!ps_buffer)
376373
{
377374
*displen = 0;
378375
return "";
379376
}
380-
381-
/* Remove any trailing spaces to offset the effect of PS_PADDING */
382-
offset = ps_buffer_size;
383-
while (offset > ps_buffer_fixed_size && ps_buffer[offset - 1] == PS_PADDING)
384-
offset--;
385-
386-
*displen = offset - ps_buffer_fixed_size;
387-
#else
388-
*displen = strlen(ps_buffer + ps_buffer_fixed_size);
389377
#endif
390378

379+
*displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
380+
391381
return ps_buffer + ps_buffer_fixed_size;
392382
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy