Skip to content

Commit 9aca512

Browse files
committed
Make sure -D is an absolute path when starting server on Windows.
This is needed because Windows services may get started with a different current directory than where pg_ctl is executed. We want relative -D paths to be interpreted relative to pg_ctl's CWD, similarly to what happens on other platforms. In support of this, move the backend's make_absolute_path() function into src/port/path.c (where it probably should have been long since) and get rid of the rather inferior version in pg_regress. Kumar Rajeev Rastogi, reviewed by MauMau
1 parent 8120c74 commit 9aca512

File tree

6 files changed

+124
-101
lines changed

6 files changed

+124
-101
lines changed

src/backend/utils/init/miscinit.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -117,77 +117,6 @@ ChangeToDataDir(void)
117117
DataDir)));
118118
}
119119

120-
/*
121-
* If the given pathname isn't already absolute, make it so, interpreting
122-
* it relative to the current working directory.
123-
*
124-
* Also canonicalizes the path. The result is always a malloc'd copy.
125-
*
126-
* Note: interpretation of relative-path arguments during postmaster startup
127-
* should happen before doing ChangeToDataDir(), else the user will probably
128-
* not like the results.
129-
*/
130-
char *
131-
make_absolute_path(const char *path)
132-
{
133-
char *new;
134-
135-
/* Returning null for null input is convenient for some callers */
136-
if (path == NULL)
137-
return NULL;
138-
139-
if (!is_absolute_path(path))
140-
{
141-
char *buf;
142-
size_t buflen;
143-
144-
buflen = MAXPGPATH;
145-
for (;;)
146-
{
147-
buf = malloc(buflen);
148-
if (!buf)
149-
ereport(FATAL,
150-
(errcode(ERRCODE_OUT_OF_MEMORY),
151-
errmsg("out of memory")));
152-
153-
if (getcwd(buf, buflen))
154-
break;
155-
else if (errno == ERANGE)
156-
{
157-
free(buf);
158-
buflen *= 2;
159-
continue;
160-
}
161-
else
162-
{
163-
free(buf);
164-
elog(FATAL, "could not get current working directory: %m");
165-
}
166-
}
167-
168-
new = malloc(strlen(buf) + strlen(path) + 2);
169-
if (!new)
170-
ereport(FATAL,
171-
(errcode(ERRCODE_OUT_OF_MEMORY),
172-
errmsg("out of memory")));
173-
sprintf(new, "%s/%s", buf, path);
174-
free(buf);
175-
}
176-
else
177-
{
178-
new = strdup(path);
179-
if (!new)
180-
ereport(FATAL,
181-
(errcode(ERRCODE_OUT_OF_MEMORY),
182-
errmsg("out of memory")));
183-
}
184-
185-
/* Make sure punctuation is canonical, too */
186-
canonicalize_path(new);
187-
188-
return new;
189-
}
190-
191120

192121
/* ----------------------------------------------------------------
193122
* User ID state

src/bin/pg_ctl/pg_ctl.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,19 @@ pgwin32_CommandLine(bool registration)
13871387
register_servicename);
13881388

13891389
if (pg_config)
1390-
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_config);
1390+
{
1391+
/* We need the -D path to be absolute */
1392+
char *dataDir;
1393+
1394+
if ((dataDir = make_absolute_path(pg_config)) == NULL)
1395+
{
1396+
/* make_absolute_path already reported the error */
1397+
exit(1);
1398+
}
1399+
make_native_path(dataDir);
1400+
appendPQExpBuffer(cmdLine, " -D \"%s\"", dataDir);
1401+
free(dataDir);
1402+
}
13911403

13921404
if (registration && do_wait)
13931405
appendPQExpBuffer(cmdLine, " -w");

src/include/miscadmin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
296296

297297
extern void SetDataDir(const char *dir);
298298
extern void ChangeToDataDir(void);
299-
extern char *make_absolute_path(const char *path);
300299

301300
/* in utils/misc/superuser.c */
302301
extern bool superuser(void); /* current user is superuser */

src/include/port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern void make_native_path(char *path);
4545
extern bool path_contains_parent_reference(const char *path);
4646
extern bool path_is_relative_and_below_cwd(const char *path);
4747
extern bool path_is_prefix_of_path(const char *path1, const char *path2);
48+
extern char *make_absolute_path(const char *path);
4849
extern const char *get_progname(const char *argv0);
4950
extern void get_share_path(const char *my_exec_path, char *ret_path);
5051
extern void get_etc_path(const char *my_exec_path, char *ret_path);

src/port/path.c

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
*-------------------------------------------------------------------------
1414
*/
1515

16-
#include "c.h"
16+
#ifndef FRONTEND
17+
#include "postgres.h"
18+
#else
19+
#include "postgres_fe.h"
20+
#endif
1721

1822
#include <ctype.h>
1923
#include <sys/stat.h>
@@ -549,6 +553,111 @@ make_relative_path(char *ret_path, const char *target_path,
549553
}
550554

551555

556+
/*
557+
* make_absolute_path
558+
*
559+
* If the given pathname isn't already absolute, make it so, interpreting
560+
* it relative to the current working directory.
561+
*
562+
* Also canonicalizes the path. The result is always a malloc'd copy.
563+
*
564+
* In backend, failure cases result in ereport(ERROR); in frontend,
565+
* we write a complaint on stderr and return NULL.
566+
*
567+
* Note: interpretation of relative-path arguments during postmaster startup
568+
* should happen before doing ChangeToDataDir(), else the user will probably
569+
* not like the results.
570+
*/
571+
char *
572+
make_absolute_path(const char *path)
573+
{
574+
char *new;
575+
576+
/* Returning null for null input is convenient for some callers */
577+
if (path == NULL)
578+
return NULL;
579+
580+
if (!is_absolute_path(path))
581+
{
582+
char *buf;
583+
size_t buflen;
584+
585+
buflen = MAXPGPATH;
586+
for (;;)
587+
{
588+
buf = malloc(buflen);
589+
if (!buf)
590+
{
591+
#ifndef FRONTEND
592+
ereport(ERROR,
593+
(errcode(ERRCODE_OUT_OF_MEMORY),
594+
errmsg("out of memory")));
595+
#else
596+
fprintf(stderr, _("out of memory\n"));
597+
return NULL;
598+
#endif
599+
}
600+
601+
if (getcwd(buf, buflen))
602+
break;
603+
else if (errno == ERANGE)
604+
{
605+
free(buf);
606+
buflen *= 2;
607+
continue;
608+
}
609+
else
610+
{
611+
free(buf);
612+
#ifndef FRONTEND
613+
elog(ERROR, "could not get current working directory: %m");
614+
#else
615+
fprintf(stderr, _("could not get current working directory: %s\n"),
616+
strerror(errno));
617+
return NULL;
618+
#endif
619+
}
620+
}
621+
622+
new = malloc(strlen(buf) + strlen(path) + 2);
623+
if (!new)
624+
{
625+
free(buf);
626+
#ifndef FRONTEND
627+
ereport(ERROR,
628+
(errcode(ERRCODE_OUT_OF_MEMORY),
629+
errmsg("out of memory")));
630+
#else
631+
fprintf(stderr, _("out of memory\n"));
632+
return NULL;
633+
#endif
634+
}
635+
sprintf(new, "%s/%s", buf, path);
636+
free(buf);
637+
}
638+
else
639+
{
640+
new = strdup(path);
641+
if (!new)
642+
{
643+
#ifndef FRONTEND
644+
ereport(ERROR,
645+
(errcode(ERRCODE_OUT_OF_MEMORY),
646+
errmsg("out of memory")));
647+
#else
648+
fprintf(stderr, _("out of memory\n"));
649+
return NULL;
650+
#endif
651+
}
652+
}
653+
654+
/* Make sure punctuation is canonical, too */
655+
canonicalize_path(new);
656+
657+
return new;
658+
}
659+
660+
552661
/*
553662
* get_share_path
554663
*/

src/test/regress/pg_regress.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,33 +1855,6 @@ create_role(const char *rolename, const _stringlist * granted_dbs)
18551855
}
18561856
}
18571857

1858-
static char *
1859-
make_absolute_path(const char *in)
1860-
{
1861-
char *result;
1862-
1863-
if (is_absolute_path(in))
1864-
result = strdup(in);
1865-
else
1866-
{
1867-
static char cwdbuf[MAXPGPATH];
1868-
1869-
if (!cwdbuf[0])
1870-
{
1871-
if (!getcwd(cwdbuf, sizeof(cwdbuf)))
1872-
{
1873-
fprintf(stderr, _("could not get current working directory: %s\n"), strerror(errno));
1874-
exit(2);
1875-
}
1876-
}
1877-
1878-
result = psprintf("%s/%s", cwdbuf, in);
1879-
}
1880-
1881-
canonicalize_path(result);
1882-
return result;
1883-
}
1884-
18851858
static void
18861859
help(void)
18871860
{

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