Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abfb417

Browse files
committed
Make the backend grok relative paths for the data directory by converting
it to an absolute path.
1 parent 7bea44f commit abfb417

File tree

5 files changed

+111
-42
lines changed

5 files changed

+111
-42
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.95 2000/10/24 09:56:09 vadim Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.96 2000/11/04 12:43:23 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
15+
#include "postgres.h"
16+
1517
#include <unistd.h>
1618
#include <time.h>
1719
#include <signal.h>
1820
#include <setjmp.h>
1921

2022
#define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */
2123

22-
#include "postgres.h"
2324
#ifdef HAVE_GETOPT_H
2425
#include <getopt.h>
2526
#endif
@@ -220,6 +221,7 @@ BootstrapMain(int argc, char *argv[])
220221
char *dbName;
221222
int flag;
222223
bool xloginit = false;
224+
char *potential_DataDir = NULL;
223225

224226
extern int optind;
225227
extern char *optarg;
@@ -255,15 +257,15 @@ BootstrapMain(int argc, char *argv[])
255257
if (!IsUnderPostmaster)
256258
{
257259
ResetAllOptions();
258-
DataDir = getenv("PGDATA"); /* Null if no PGDATA variable */
260+
potential_DataDir = getenv("PGDATA"); /* Null if no PGDATA variable */
259261
}
260262

261263
while ((flag = getopt(argc, argv, "D:dCQxpB:F")) != EOF)
262264
{
263265
switch (flag)
264266
{
265267
case 'D':
266-
DataDir = optarg;
268+
potential_DataDir = optarg;
267269
break;
268270
case 'd':
269271
DebugMode = true; /* print out debugging info while
@@ -301,15 +303,20 @@ BootstrapMain(int argc, char *argv[])
301303
SetProcessingMode(BootstrapProcessing);
302304
IgnoreSystemIndexes(true);
303305

304-
if (!DataDir)
306+
if (!IsUnderPostmaster)
305307
{
306-
fprintf(stderr, "%s does not know where to find the database system "
307-
"data. You must specify the directory that contains the "
308-
"database system either by specifying the -D invocation "
309-
"option or by setting the PGDATA environment variable.\n\n",
310-
argv[0]);
311-
proc_exit(1);
308+
if (!potential_DataDir)
309+
{
310+
fprintf(stderr, "%s does not know where to find the database system "
311+
"data. You must specify the directory that contains the "
312+
"database system either by specifying the -D invocation "
313+
"option or by setting the PGDATA environment variable.\n\n",
314+
argv[0]);
315+
proc_exit(1);
316+
}
317+
SetDataDir(potential_DataDir);
312318
}
319+
Assert(DataDir);
313320

314321
if (dbName == NULL)
315322
{

src/backend/postmaster/postmaster.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.177 2000/11/01 21:14:02 petere Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.178 2000/11/04 12:43:23 petere Exp $
1515
*
1616
* NOTES
1717
*
@@ -268,12 +268,12 @@ extern void GetCharSetByHost(char *, int, char *);
268268

269269

270270
static void
271-
checkDataDir(const char *DataDir)
271+
checkDataDir(const char *checkdir)
272272
{
273273
char path[MAXPGPATH];
274274
FILE *fp;
275275

276-
if (DataDir == NULL)
276+
if (checkdir == NULL)
277277
{
278278
fprintf(stderr, "%s does not know where to find the database system "
279279
"data. You must specify the directory that contains the "
@@ -285,10 +285,10 @@ checkDataDir(const char *DataDir)
285285

286286
#ifdef OLD_FILE_NAMING
287287
snprintf(path, sizeof(path), "%s%cbase%ctemplate1%cpg_class",
288-
DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR);
288+
checkdir, SEP_CHAR, SEP_CHAR, SEP_CHAR);
289289
#else
290290
snprintf(path, sizeof(path), "%s%cbase%c%u%c%u",
291-
DataDir, SEP_CHAR, SEP_CHAR,
291+
checkdir, SEP_CHAR, SEP_CHAR,
292292
TemplateDbOid, SEP_CHAR, RelOid_pg_class);
293293
#endif
294294

@@ -298,13 +298,13 @@ checkDataDir(const char *DataDir)
298298
fprintf(stderr, "%s does not find the database system."
299299
"\n\tExpected to find it in the PGDATA directory \"%s\","
300300
"\n\tbut unable to open file \"%s\": %s\n\n",
301-
progname, DataDir, path, strerror(errno));
301+
progname, checkdir, path, strerror(errno));
302302
exit(2);
303303
}
304304

305305
FreeFile(fp);
306306

307-
ValidatePgVersion(DataDir);
307+
ValidatePgVersion(checkdir);
308308
}
309309

310310

@@ -314,6 +314,7 @@ PostmasterMain(int argc, char *argv[])
314314
int opt;
315315
int status;
316316
char original_extraoptions[MAXPGPATH];
317+
char *potential_DataDir = NULL;
317318

318319
IsUnderPostmaster = true; /* so that backends know this */
319320

@@ -353,8 +354,7 @@ PostmasterMain(int argc, char *argv[])
353354
/*
354355
* Options setup
355356
*/
356-
if (getenv("PGDATA"))
357-
DataDir = strdup(getenv("PGDATA")); /* default value */
357+
potential_DataDir = getenv("PGDATA"); /* default value */
358358

359359
ResetAllOptions();
360360

@@ -377,9 +377,7 @@ PostmasterMain(int argc, char *argv[])
377377
switch(opt)
378378
{
379379
case 'D':
380-
if (DataDir)
381-
free(DataDir);
382-
DataDir = strdup(optarg);
380+
potential_DataDir = optarg;
383381
break;
384382

385383
case '-':
@@ -415,12 +413,14 @@ PostmasterMain(int argc, char *argv[])
415413
}
416414
}
417415

418-
optind = 1; /* start over */
419-
checkDataDir(DataDir); /* issues error messages */
416+
checkDataDir(potential_DataDir); /* issues error messages */
417+
SetDataDir(potential_DataDir);
420418

421419
ProcessConfigFile(PGC_POSTMASTER);
422420

423421
IgnoreSystemIndexes(false);
422+
423+
optind = 1; /* start over */
424424
while ((opt = getopt(argc, argv, "A:a:B:b:D:d:Film:MN:no:p:Ss-:?")) != EOF)
425425
{
426426
switch (opt)

src/backend/tcop/postgres.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.184 2000/10/28 18:27:56 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.185 2000/11/04 12:43:24 petere Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -29,7 +29,7 @@
2929
#include <errno.h>
3030
#if HAVE_SYS_SELECT_H
3131
#include <sys/select.h>
32-
#endif /* aix */
32+
#endif
3333
#include <netinet/in.h>
3434
#include <arpa/inet.h>
3535
#include <netdb.h>
@@ -1058,6 +1058,8 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
10581058
char *remote_host;
10591059
unsigned short remote_port;
10601060

1061+
char *potential_DataDir = NULL;
1062+
10611063
extern int optind;
10621064
extern char *optarg;
10631065
extern int DebugLvl;
@@ -1082,8 +1084,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
10821084
if (!IsUnderPostmaster)
10831085
{
10841086
ResetAllOptions();
1085-
if (getenv("PGDATA"))
1086-
DataDir = strdup(getenv("PGDATA"));
1087+
potential_DataDir = getenv("PGDATA");
10871088
}
10881089
StatFp = stderr;
10891090

@@ -1142,9 +1143,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
11421143
case 'D': /* PGDATA directory */
11431144
if (secure)
11441145
{
1145-
if (DataDir)
1146-
free(DataDir);
1147-
DataDir = strdup(optarg);
1146+
potential_DataDir = optarg;
11481147
}
11491148
break;
11501149

@@ -1429,15 +1428,20 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
14291428
Show_query_stats = false;
14301429
}
14311430

1432-
if (!DataDir)
1431+
if (!IsUnderPostmaster)
14331432
{
1434-
fprintf(stderr, "%s does not know where to find the database system "
1435-
"data. You must specify the directory that contains the "
1436-
"database system either by specifying the -D invocation "
1437-
"option or by setting the PGDATA environment variable.\n\n",
1438-
argv[0]);
1439-
proc_exit(1);
1433+
if (!potential_DataDir)
1434+
{
1435+
fprintf(stderr, "%s does not know where to find the database system "
1436+
"data. You must specify the directory that contains the "
1437+
"database system either by specifying the -D invocation "
1438+
"option or by setting the PGDATA environment variable.\n\n",
1439+
argv[0]);
1440+
proc_exit(1);
1441+
}
1442+
SetDataDir(potential_DataDir);
14401443
}
1444+
Assert(DataDir);
14411445

14421446
/*
14431447
* 1. Set BlockSig and UnBlockSig masks. 2. Set up signal handlers. 3.
@@ -1631,7 +1635,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
16311635
if (!IsUnderPostmaster)
16321636
{
16331637
puts("\nPOSTGRES backend interactive interface ");
1634-
puts("$Revision: 1.184 $ $Date: 2000/10/28 18:27:56 $\n");
1638+
puts("$Revision: 1.185 $ $Date: 2000/11/04 12:43:24 $\n");
16351639
}
16361640

16371641
/*

src/backend/utils/init/miscinit.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.55 2000/09/19 18:17:57 petere Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.56 2000/11/04 12:43:24 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -366,6 +366,62 @@ GetUserName(Oid userid)
366366

367367

368368

369+
/*-------------------------------------------------------------------------
370+
* Set data directory, but make sure it's an absolute path. Use this,
371+
* never set DataDir directly.
372+
*-------------------------------------------------------------------------
373+
*/
374+
void
375+
SetDataDir(const char *dir)
376+
{
377+
char *new;
378+
379+
AssertArg(dir);
380+
if (DataDir)
381+
free(DataDir);
382+
383+
if (dir[0] != '/')
384+
{
385+
char *buf;
386+
size_t buflen;
387+
388+
buflen = MAXPGPATH;
389+
for (;;)
390+
{
391+
buf = malloc(buflen);
392+
if (!buf)
393+
elog(FATAL, "out of memory");
394+
395+
if (getcwd(buf, buflen))
396+
break;
397+
else if (errno == ERANGE)
398+
{
399+
free(buf);
400+
buflen *= 2;
401+
continue;
402+
}
403+
else
404+
{
405+
free(buf);
406+
elog(FATAL, "cannot get current working directory: %m");
407+
}
408+
}
409+
410+
new = malloc(strlen(buf) + 1 + strlen(dir) + 1);
411+
sprintf(new, "%s/%s", buf, dir);
412+
}
413+
else
414+
{
415+
new = strdup(dir);
416+
}
417+
418+
if (!new)
419+
elog(FATAL, "out of memory");
420+
DataDir = new;
421+
}
422+
423+
424+
369425
/*-------------------------------------------------------------------------
370426
*
371427
* postmaster pid file stuffs. $DATADIR/postmaster.pid is created when:

src/include/miscadmin.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: miscadmin.h,v 1.68 2000/10/08 09:25:38 ishii Exp $
15+
* $Id: miscadmin.h,v 1.69 2000/11/04 12:43:24 petere Exp $
1616
*
1717
* NOTES
1818
* some of the information in this file will be moved to
@@ -137,6 +137,8 @@ extern Oid GetSessionUserId(void);
137137
extern void SetSessionUserId(Oid userid);
138138
extern void SetSessionUserIdFromUserName(const char *username);
139139

140+
extern void SetDataDir(const char *dir);
141+
140142
extern int FindExec(char *full_path, const char *argv0, const char *binary_name);
141143
extern int CheckPathAccess(char *path, char *name, int open_mode);
142144

0 commit comments

Comments
 (0)