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

Commit780cba9

Browse files
committed
Move responsibility for copying argv[] array into ps_status.c, where it
logically belongs. Arrange to update the _NSGetArgv() copy of the argvpointer on Darwin. (It seems likely that other NeXT-derived platformsalso have an _NSGetArgv() problem, but until we have some reports I'lljust make this #ifdef __darwin__.)
1 parent45f5eba commit780cba9

File tree

3 files changed

+81
-53
lines changed

3 files changed

+81
-53
lines changed

‎src/backend/main/main.c

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/main/main.c,v 1.73 2004/02/02 00:11:31 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/main/main.c,v 1.74 2004/02/22 21:26:55 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -49,8 +49,6 @@
4949
int
5050
main(intargc,char*argv[])
5151
{
52-
char**new_argv;
53-
inti;
5452
intlen;
5553
structpasswd*pw;
5654
char*pw_name_persist;
@@ -116,30 +114,18 @@ main(int argc, char *argv[])
116114
*/
117115

118116
/*
119-
* Remember the physical location of the initially given argv[] array,
120-
*since on some platforms that storage must be overwritten in order
121-
* to set the process title for ps. Then make a copy of the argv[]
122-
*array for subsequent use, so that argument parsing doesn't get
123-
*affected if init_ps_display overwritestheoriginalargv[].
117+
* Remember the physical location of the initially given argv[] array
118+
*for possible use by ps display. On some platforms, the argv[]
119+
*storage must be overwritten in orderto set the process title for ps.
120+
*In such cases save_ps_display_args makes and returns a new copy of
121+
* the argv[] array.
124122
*
125-
* (NB: do NOT think to remove the copying of argv[], even though
126-
* postmaster.c finishes looking at argv[] long before we ever
127-
* consider changing the ps display. On some platforms, getopt()
128-
* keeps pointers into the argv array, and will get horribly confused
129-
* when it is re-called to analyze a subprocess' argument string if
130-
* the argv storage has been clobbered meanwhile.)
131-
*
132-
* On some platforms, save_ps_display_args moves the environment strings
133-
* to make extra room.Therefore this should be done as early as
134-
* possible during startup, to avoid entanglements with code that
135-
* might save a getenv() result pointer.
123+
* save_ps_display_args may also move the environment strings to make
124+
* extra room. Therefore this should be done as early as possible during
125+
* startup, to avoid entanglements with code that might save a getenv()
126+
* result pointer.
136127
*/
137-
save_ps_display_args(argc,argv);
138-
139-
new_argv= (char**)malloc((argc+1)*sizeof(char*));
140-
for (i=0;i<argc;i++)
141-
new_argv[i]=strdup(argv[i]);
142-
new_argv[argc]=NULL;
128+
argv=save_ps_display_args(argc,argv);
143129

144130
/*
145131
* Set up locale information from environment.Note that LC_CTYPE and
@@ -225,53 +211,53 @@ main(int argc, char *argv[])
225211
* depending on the program name (and possibly first argument) we
226212
* were called with. The lack of consistency here is historical.
227213
*/
228-
len=strlen(new_argv[0]);
214+
len=strlen(argv[0]);
229215

230-
if ((len >=10&&strcmp(new_argv[0]+len-10,"postmaster")==0)
216+
if ((len >=10&&strcmp(argv[0]+len-10,"postmaster")==0)
231217
#ifdefWIN32
232-
|| (len >=14&&strcmp(new_argv[0]+len-14,"postmaster.exe")==0)
218+
|| (len >=14&&strcmp(argv[0]+len-14,"postmaster.exe")==0)
233219
#endif
234220
)
235221
{
236222
/* Called as "postmaster" */
237-
exit(PostmasterMain(argc,new_argv));
223+
exit(PostmasterMain(argc,argv));
238224
}
239225

240226
/*
241227
* If the first argument is "-boot", then invoke bootstrap mode. Note
242228
* we remove "-boot" from the arguments passed on to BootstrapMain.
243229
*/
244-
if (argc>1&&strcmp(new_argv[1],"-boot")==0)
245-
exit(BootstrapMain(argc-1,new_argv+1));
230+
if (argc>1&&strcmp(argv[1],"-boot")==0)
231+
exit(BootstrapMain(argc-1,argv+1));
246232

247233
#ifdefEXEC_BACKEND
248234
/*
249235
* If the first argument is "-forkexec", then invoke SubPostmasterMain. Note
250236
* we remove "-forkexec" from the arguments passed on to SubPostmasterMain.
251237
*/
252-
if (argc>1&&strcmp(new_argv[1],"-forkexec")==0)
238+
if (argc>1&&strcmp(argv[1],"-forkexec")==0)
253239
{
254-
SubPostmasterMain(argc-2,new_argv+2);
240+
SubPostmasterMain(argc-2,argv+2);
255241
exit(0);
256242
}
257243

258244
/*
259245
* If the first argument is "-statBuf", then invoke pgstat_main. Note
260246
* we remove "-statBuf" from the arguments passed on to pgstat_main.
261247
*/
262-
if (argc>1&&strcmp(new_argv[1],"-statBuf")==0)
248+
if (argc>1&&strcmp(argv[1],"-statBuf")==0)
263249
{
264-
pgstat_main(argc-2,new_argv+2);
250+
pgstat_main(argc-2,argv+2);
265251
exit(0);
266252
}
267253

268254
/*
269255
* If the first argument is "-statCol", then invoke pgstat_mainChild. Note
270256
* we remove "-statCol" from the arguments passed on to pgstat_mainChild.
271257
*/
272-
if (argc>1&&strcmp(new_argv[1],"-statCol")==0)
258+
if (argc>1&&strcmp(argv[1],"-statCol")==0)
273259
{
274-
pgstat_mainChild(argc-2,new_argv+2);
260+
pgstat_mainChild(argc-2,argv+2);
275261
exit(0);
276262
}
277263
#endif
@@ -280,7 +266,7 @@ main(int argc, char *argv[])
280266
* If the first argument is "--describe-config", then invoke runtime
281267
* configuration option display mode.
282268
*/
283-
if (argc>1&&strcmp(new_argv[1],"--describe-config")==0)
269+
if (argc>1&&strcmp(argv[1],"--describe-config")==0)
284270
exit(GucInfoMain());
285271

286272
/*
@@ -293,7 +279,7 @@ main(int argc, char *argv[])
293279
if (pw==NULL)
294280
{
295281
fprintf(stderr,gettext("%s: invalid effective UID: %d\n"),
296-
new_argv[0], (int)geteuid());
282+
argv[0], (int)geteuid());
297283
exit(1);
298284
}
299285
/* Allocate new memory because later getpwuid() calls can overwrite it */
@@ -306,11 +292,11 @@ main(int argc, char *argv[])
306292
if (!GetUserName(pw_name_persist,&namesize))
307293
{
308294
fprintf(stderr,gettext("%s: could not determine user name (GetUserName failed)\n"),
309-
new_argv[0]);
295+
argv[0]);
310296
exit(1);
311297
}
312298
}
313299
#endif
314300

315-
exit(PostgresMain(argc,new_argv,pw_name_persist));
301+
exit(PostgresMain(argc,argv,pw_name_persist));
316302
}

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

Lines changed: 52 additions & 10 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.16 2003/11/29 19:52:04 pgsql Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.17 2004/02/22 21:26:55 tgl Exp $
99
*
1010
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
1111
* various details abducted from various places
@@ -22,6 +22,9 @@
2222
#include<machine/vmparam.h>/* for old BSD */
2323
#include<sys/exec.h>
2424
#endif
25+
#if defined(__darwin__)
26+
#include<crt_externs.h>
27+
#endif
2528

2629
#include"miscadmin.h"
2730
#include"utils/ps_status.h"
@@ -94,19 +97,21 @@ static char **save_argv;
9497

9598
/*
9699
* Call this early in startup to save the original argc/argv values.
100+
* If needed, we make a copy of the original argv[] array to preserve it
101+
* from being clobbered by subsequent ps_display actions.
97102
*
98-
* argv[] will not be overwritten by this routine, but may be overwritten
99-
* during init_ps_display.Also, the physical location of the environment
100-
* strings may be moved, so this should be called before any code that
101-
* might try to hang onto a getenv() result.
103+
*(The originalargv[] will not be overwritten by this routine, but may be
104+
*overwrittenduring init_ps_display.Also, the physical location of the
105+
*environmentstrings may be moved, so this should be called before any code
106+
*thatmight try to hang onto a getenv() result.)
102107
*/
103-
void
104-
save_ps_display_args(intargc,char*argv[])
108+
char**
109+
save_ps_display_args(intargc,char**argv)
105110
{
106111
save_argc=argc;
107112
save_argv=argv;
108113

109-
#ifdefPS_USE_CLOBBER_ARGV
114+
#if defined(PS_USE_CLOBBER_ARGV)
110115

111116
/*
112117
* If we're going to overwrite the argv area, count the available
@@ -130,7 +135,7 @@ save_ps_display_args(int argc, char *argv[])
130135
{
131136
ps_buffer=NULL;
132137
ps_buffer_size=0;
133-
return;
138+
returnargv;
134139
}
135140

136141
/*
@@ -148,13 +153,50 @@ save_ps_display_args(int argc, char *argv[])
148153
/*
149154
* move the environment out of the way
150155
*/
151-
new_environ=malloc(sizeof(char*)* (i+1));
156+
new_environ=(char**)malloc((i+1)*sizeof(char*));
152157
for (i=0;environ[i]!=NULL;i++)
153158
new_environ[i]=strdup(environ[i]);
154159
new_environ[i]=NULL;
155160
environ=new_environ;
156161
}
157162
#endif/* PS_USE_CLOBBER_ARGV */
163+
164+
#if defined(PS_USE_CHANGE_ARGV)|| defined(PS_USE_CLOBBER_ARGV)
165+
166+
/*
167+
* If we're going to change the original argv[] then make a copy for
168+
* argument parsing purposes.
169+
*
170+
* (NB: do NOT think to remove the copying of argv[], even though
171+
* postmaster.c finishes looking at argv[] long before we ever
172+
* consider changing the ps display. On some platforms, getopt()
173+
* keeps pointers into the argv array, and will get horribly confused
174+
* when it is re-called to analyze a subprocess' argument string if
175+
* the argv storage has been clobbered meanwhile. Other platforms
176+
* have other dependencies on argv[].
177+
*/
178+
{
179+
char**new_argv;
180+
inti;
181+
182+
new_argv= (char**)malloc((argc+1)*sizeof(char*));
183+
for (i=0;i<argc;i++)
184+
new_argv[i]=strdup(argv[i]);
185+
new_argv[argc]=NULL;
186+
187+
#if defined(__darwin__)
188+
/*
189+
* Darwin (and perhaps other NeXT-derived platforms?) has a static
190+
* copy of the argv pointer, which we may fix like so:
191+
*/
192+
*_NSGetArgv()=new_argv;
193+
#endif
194+
195+
argv=new_argv;
196+
}
197+
#endif/* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
198+
199+
returnargv;
158200
}
159201

160202
/*

‎src/include/utils/ps_status.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
*
55
* Declarations for backend/utils/misc/ps_status.c
66
*
7-
* $PostgreSQL: pgsql/src/include/utils/ps_status.h,v 1.24 2003/11/29 22:41:16 pgsql Exp $
7+
* $PostgreSQL: pgsql/src/include/utils/ps_status.h,v 1.25 2004/02/22 21:26:54 tgl Exp $
88
*
99
*-------------------------------------------------------------------------
1010
*/
1111

1212
#ifndefPS_STATUS_H
1313
#definePS_STATUS_H
1414

15-
externvoidsave_ps_display_args(intargc,char*argv[]);
15+
externchar**save_ps_display_args(intargc,char**argv);
1616

1717
externvoidinit_ps_display(constchar*username,constchar*dbname,
1818
constchar*host_info);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp