Skip to content

Commit ceceeff

Browse files
committed
Clean up find_my_exec to work cleaner.
Add Win32 code to look in the current directory before the path. Add code so memory is allocated using palloc in backend object files.
1 parent 868404b commit ceceeff

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

src/port/exec.c

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/port/exec.c,v 1.10 2004/05/19 17:15:21 momjian Exp $
10+
* $PostgreSQL: pgsql/src/port/exec.c,v 1.11 2004/05/20 15:35:41 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414

1515
#ifndef FRONTEND
1616
#include "postgres.h"
17+
#define malloc(l) palloc(l)
18+
#define free(p) pfree(p)
19+
#define strdup(p) pstrdup(p)
1720
#else
1821
#include "postgres_fe.h"
1922
#endif
@@ -178,12 +181,15 @@ validate_exec(char *path)
178181
int
179182
find_my_exec(const char *argv0, char *full_path)
180183
{
181-
char buf[MAXPGPATH + 2];
184+
char cwd[MAXPGPATH];
182185
char *p;
183186
char *path,
184187
*startp,
185188
*endp;
186189

190+
if (!getcwd(cwd, MAXPGPATH))
191+
cwd[0] = '\0';
192+
187193
/*
188194
* First try: use the binary that's located in the
189195
* same directory if it was invoked with an explicit path.
@@ -195,31 +201,42 @@ find_my_exec(const char *argv0, char *full_path)
195201
* it).
196202
*/
197203
/* Does argv0 have a separator? */
198-
if (argv0 && (p = last_path_separator(argv0)))
204+
if ((p = last_path_separator(argv0)))
199205
{
200206
if (*++p == '\0')
201207
{
202208
log_error("argv[0] ends with a path separator \"%s\"", argv0);
203209
return -1;
204210
}
205-
if (is_absolute_path(argv0) || !getcwd(buf, MAXPGPATH))
206-
buf[0] = '\0';
207-
else /* path is not absolute and getcwd worked */
208-
strcat(buf, "/");
209-
strcat(buf, argv0);
210-
if (validate_exec(buf) == 0)
211+
212+
if (is_absolute_path(argv0))
213+
StrNCpy(full_path, argv0, MAXPGPATH);
214+
else
215+
snprintf(full_path, MAXPGPATH, "%s/%s", cwd, argv0);
216+
217+
canonicalize_path(full_path);
218+
if (validate_exec(full_path) == 0)
211219
{
212-
strncpy(full_path, buf, MAXPGPATH);
213220
win32_make_absolute(full_path);
214221
return 0;
215222
}
216223
else
217224
{
218-
log_error("invalid binary \"%s\"", buf);
225+
log_error("invalid binary \"%s\"", full_path);
219226
return -1;
220227
}
221228
}
222229

230+
#ifdef WIN32
231+
/* Win32 checks the current directory first for names without slashes */
232+
if (validate_exec(argv0) == 0)
233+
{
234+
snprintf(full_path, MAXPGPATH, "%s/%s", cwd, argv0);
235+
win32_make_absolute(full_path);
236+
return 0;
237+
}
238+
#endif
239+
223240
/*
224241
* Second try: since no explicit path was supplied, the user must have
225242
* been relying on PATH. We'll use the same PATH.
@@ -235,24 +252,23 @@ find_my_exec(const char *argv0, char *full_path)
235252
continue;
236253
if (endp)
237254
*endp = '\0';
238-
if (is_absolute_path(startp) || !getcwd(buf, MAXPGPATH))
239-
buf[0] = '\0';
240-
else /* path is not absolute and getcwd worked */
241-
strcat(buf, "/");
242-
strcat(buf, startp);
243-
strcat(buf, "/");
244-
strcat(buf, argv0);
245-
switch (validate_exec(buf))
255+
256+
if (is_absolute_path(startp))
257+
snprintf(full_path, MAXPGPATH, "%s/%s", startp, argv0);
258+
else
259+
snprintf(full_path, MAXPGPATH, "%s/%s/%s", cwd, startp, argv0);
260+
261+
canonicalize_path(full_path);
262+
switch (validate_exec(full_path))
246263
{
247264
case 0: /* found ok */
248-
strncpy(full_path, buf, MAXPGPATH);
249265
win32_make_absolute(full_path);
250266
free(path);
251267
return 0;
252268
case -1: /* wasn't even a candidate, keep looking */
253269
break;
254270
case -2: /* found but disqualified */
255-
log_error("could not read binary \"%s\"", buf);
271+
log_error("could not read binary \"%s\"", full_path);
256272
free(path);
257273
return -1;
258274
}
@@ -270,9 +286,8 @@ find_my_exec(const char *argv0, char *full_path)
270286
* Win32 has a native way to find the executable name, but the above
271287
* method works too.
272288
*/
273-
if (GetModuleFileName(NULL,basename,MAXPGPATH) == 0)
274-
ereport(FATAL,
275-
(errmsg("GetModuleFileName failed (%i)",(int)GetLastError())));
289+
if (GetModuleFileName(NULL,full_path,MAXPGPATH) == 0)
290+
log_error("GetModuleFileName failed (%i)",(int)GetLastError());
276291
#endif
277292
}
278293

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