Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94daee3

Browse files
committed
Further cleanup of ps_status setup code. On platforms where the
environment strings need to be moved around, do so when called frominitial startup (main.c), not in init_ps_status. This eliminates theformer risk of invalidating saved environment-string pointers, sinceno code has yet had a chance to grab any such pointers when main.cis running.
1 parenta19f260 commit94daee3

File tree

4 files changed

+95
-88
lines changed

4 files changed

+95
-88
lines changed

‎src/backend/main/main.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.46 2001/10/21 03:25:35 tgl Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.47 2001/10/22 19:41:38 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -99,6 +99,32 @@ main(int argc, char *argv[])
9999
* best to minimize these.
100100
*/
101101

102+
/*
103+
* Remember the physical location of the initially given argv[] array,
104+
* since on some platforms that storage must be overwritten in order
105+
* to set the process title for ps. Then make a copy of the argv[]
106+
* array for subsequent use, so that argument parsing doesn't get
107+
* affected if init_ps_display overwrites the original argv[].
108+
*
109+
* (NB: do NOT think to remove the copying of argv[], even though
110+
* postmaster.c finishes looking at argv[] long before we ever consider
111+
* changing the ps display. On some platforms, getopt() keeps pointers
112+
* into the argv array, and will get horribly confused when it is
113+
* re-called to analyze a subprocess' argument string if the argv storage
114+
* has been clobbered meanwhile.)
115+
*
116+
* On some platforms, save_ps_display_args moves the environment strings
117+
* to make extra room. Therefore this should be done as early as
118+
* possible during startup, to avoid entanglements with code that might
119+
* save a getenv() result pointer.
120+
*/
121+
save_ps_display_args(argc,argv);
122+
123+
new_argv= (char**)malloc((argc+1)*sizeof(char*));
124+
for (i=0;i<argc;i++)
125+
new_argv[i]=strdup(argv[i]);
126+
new_argv[argc]=NULL;
127+
102128
/* Initialize NLS settings so we can give localized error messages */
103129
#ifdefENABLE_NLS
104130
#ifdefLC_MESSAGES
@@ -168,27 +194,6 @@ main(int argc, char *argv[])
168194
setlocale(LC_MONETARY,"");
169195
#endif
170196

171-
/*
172-
* Remember the physical location of the initially given argv[] array,
173-
* since on some platforms that storage must be overwritten in order
174-
* to set the process title for ps. Then make a copy of the argv[]
175-
* array for subsequent use, so that argument parsing doesn't get
176-
* affected if init_ps_display overwrites the original argv[].
177-
*
178-
* (NB: do NOT think to remove this copying, even though postmaster.c
179-
* finishes looking at argv[] long before we ever consider changing
180-
* the ps display. On some platforms, getopt(3) keeps pointers into
181-
* the argv array, and will get horribly confused when it is re-called
182-
* to analyze a subprocess' argument string if the argv storage has
183-
* been clobbered meanwhile.)
184-
*/
185-
save_ps_display_args(argc,argv);
186-
187-
new_argv= (char**)malloc((argc+1)*sizeof(char*));
188-
for (i=0;i<argc;i++)
189-
new_argv[i]=strdup(argv[i]);
190-
new_argv[argc]=NULL;
191-
192197
/*
193198
* Now dispatch to one of PostmasterMain, PostgresMain, or
194199
* BootstrapMain depending on the program name (and possibly first

‎src/backend/postmaster/pgstat.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*Copyright (c) 2001, PostgreSQL Global Development Group
1818
*
19-
*$Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.12 2001/10/21 03:25:35 tgl Exp $
19+
*$Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.13 2001/10/22 19:41:38 tgl Exp $
2020
* ----------
2121
*/
2222
#include"postgres.h"
@@ -1185,9 +1185,6 @@ pgstat_main(void)
11851185

11861186
/*
11871187
* Identify myself via ps
1188-
*
1189-
* WARNING: On some platforms the environment will be moved around to
1190-
* make room for the ps display string.
11911188
*/
11921189
init_ps_display("stats collector process","","");
11931190
set_ps_display("");
@@ -1470,9 +1467,6 @@ pgstat_recvbuffer(void)
14701467

14711468
/*
14721469
* Identify myself via ps
1473-
*
1474-
* WARNING: On some platforms the environment will be moved around to
1475-
* make room for the ps display string.
14761470
*/
14771471
init_ps_display("stats buffer process","","");
14781472
set_ps_display("");

‎src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.250 2001/10/21 03:25:35 tgl Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.251 2001/10/22 19:41:38 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -2090,12 +2090,7 @@ DoBackend(Port *port)
20902090
}
20912091

20922092
/*
2093-
* Set process parameters for ps
2094-
*
2095-
* WARNING: On some platforms the environment will be moved around to
2096-
* make room for the ps display string. So any references to
2097-
* optarg or getenv() from above will be invalid after this call.
2098-
* Better use strdup or something similar.
2093+
* Set process parameters for ps display.
20992094
*/
21002095
init_ps_display(port->user,port->database,remote_host);
21012096
set_ps_display("authentication");
@@ -2443,9 +2438,6 @@ SSDataBase(int xlop)
24432438

24442439
/*
24452440
* Identify myself via ps
2446-
*
2447-
* WARNING: On some platforms the environment will be moved around to
2448-
* make room for the ps display string.
24492441
*/
24502442
switch (xlop)
24512443
{

‎src/backend/utils/misc/ps_status.c

Lines changed: 65 additions & 49 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-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.6 2001/10/21 03:25:35 tgl Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.7 2001/10/22 19:41:38 tgl Exp $
99
*
1010
* Copyright 2000 by PostgreSQL Global Development Group
1111
* various details abducted from various places
@@ -95,45 +95,22 @@ static char **save_argv;
9595

9696
/*
9797
* Call this early in startup to save the original argc/argv values.
98+
*
9899
* argv[] will not be overwritten by this routine, but may be overwritten
99-
* during init_ps_display.
100+
* during init_ps_display. Also, the physical location of the environment
101+
* strings may be moved, so this should be called before any code that
102+
* might try to hang onto a getenv() result.
100103
*/
101104
void
102105
save_ps_display_args(intargc,char*argv[])
103106
{
104107
save_argc=argc;
105108
save_argv=argv;
106-
}
107-
108-
/*
109-
* Call this once during subprocess startup to set the identification
110-
* values. At this point, the original argv[] array may be overwritten.
111-
*/
112-
void
113-
init_ps_display(constchar*username,constchar*dbname,
114-
constchar*host_info)
115-
{
116-
#ifndefPS_USE_NONE
117-
Assert(username);
118-
Assert(dbname);
119-
120-
/* no ps display for stand-alone backend */
121-
if (!IsUnderPostmaster)
122-
return;
123-
124-
/* no ps display if you didn't call save_ps_display_args() */
125-
if (!save_argv)
126-
return;
127-
128-
#ifdefPS_USE_CHANGE_ARGV
129-
save_argv[0]=ps_buffer;
130-
save_argv[1]=NULL;
131-
#endif/* PS_USE_CHANGE_ARGV */
132109

133110
#ifdefPS_USE_CLOBBER_ARGV
134-
135111
/*
136-
* If we're going to overwrite the argv area, count the space.
112+
* If we're going to overwrite the argv area, count the available
113+
* space. Also move the environment to make additional room.
137114
*/
138115
{
139116
char*end_of_area=NULL;
@@ -143,48 +120,87 @@ init_ps_display(const char *username, const char *dbname,
143120
/*
144121
* check for contiguous argv strings
145122
*/
146-
for (i=0;i<save_argc;i++)
147-
if (i==0||end_of_area+1==save_argv[i])
148-
end_of_area=save_argv[i]+strlen(save_argv[i]);
149-
150-
/*
151-
* check for contiguous environ strings following argv
152-
*/
153-
for (i=0;end_of_area!=NULL&&environ[i]!=NULL;i++)
154-
if (end_of_area+1==environ[i])
155-
end_of_area=environ[i]+strlen(environ[i]);
123+
for (i=0;i<argc;i++)
124+
{
125+
if (i==0||end_of_area+1==argv[i])
126+
end_of_area=argv[i]+strlen(argv[i]);
127+
}
156128

157-
if (end_of_area==NULL)
129+
if (end_of_area==NULL)/* probably can't happen? */
158130
{
159131
ps_buffer=NULL;
160132
ps_buffer_size=0;
161133
return;
162134
}
163-
else
135+
136+
/*
137+
* check for contiguous environ strings following argv
138+
*/
139+
for (i=0;environ[i]!=NULL;i++)
164140
{
165-
ps_buffer=save_argv[0];
166-
ps_buffer_size=end_of_area-save_argv[0]-1;
141+
if (end_of_area+1==environ[i])
142+
end_of_area=environ[i]+strlen(environ[i]);
167143
}
168-
save_argv[1]=NULL;
144+
145+
ps_buffer=argv[0];
146+
ps_buffer_size=end_of_area-argv[0]-1;
169147

170148
/*
171149
* move the environment out of the way
172150
*/
173-
for (i=0;environ[i]!=NULL;i++)
174-
;
175151
new_environ=malloc(sizeof(char*)* (i+1));
176152
for (i=0;environ[i]!=NULL;i++)
177153
new_environ[i]=strdup(environ[i]);
178154
new_environ[i]=NULL;
179155
environ=new_environ;
180156
}
181157
#endif/* PS_USE_CLOBBER_ARGV */
158+
}
159+
160+
/*
161+
* Call this once during subprocess startup to set the identification
162+
* values. At this point, the original argv[] array may be overwritten.
163+
*/
164+
void
165+
init_ps_display(constchar*username,constchar*dbname,
166+
constchar*host_info)
167+
{
168+
Assert(username);
169+
Assert(dbname);
170+
Assert(host_info);
171+
172+
#ifndefPS_USE_NONE
173+
/* no ps display for stand-alone backend */
174+
if (!IsUnderPostmaster)
175+
return;
176+
177+
/* no ps display if you didn't call save_ps_display_args() */
178+
if (!save_argv)
179+
return;
180+
#ifdefPS_USE_CLOBBER_ARGV
181+
/* If ps_buffer is a pointer, it might still be null */
182+
if (!ps_buffer)
183+
return;
184+
#endif
185+
186+
/*
187+
* Overwrite argv[] to point at appropriate space, if needed
188+
*/
189+
190+
#ifdefPS_USE_CHANGE_ARGV
191+
save_argv[0]=ps_buffer;
192+
save_argv[1]=NULL;
193+
#endif/* PS_USE_CHANGE_ARGV */
194+
195+
#ifdefPS_USE_CLOBBER_ARGV
196+
save_argv[1]=NULL;
197+
#endif/* PS_USE_CLOBBER_ARGV */
182198

183199
/*
184-
* Make fixed prefix
200+
* Make fixed prefix of ps display.
185201
*/
186-
#ifdefPS_USE_SETPROCTITLE
187202

203+
#ifdefPS_USE_SETPROCTITLE
188204
/*
189205
* apparently setproctitle() already adds a `progname:' prefix to the
190206
* ps line

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp