PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
exec.c File Reference
#include "postgres.h"
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common/string.h"
Include dependency graph for exec.c:

Go to the source code of this file.

Macros

#define _DARWIN_BETTER_REALPATH
 
#define log_error(errcodefn, ...)    ereport(LOG, (errcodefn, errmsg_internal(__VA_ARGS__)))
 

Functions

static int normalize_exec_path (char *path)
 
static char * pg_realpath (const char *fname)
 
int validate_exec (const char *path)
 
int find_my_exec (const char *argv0, char *retpath)
 
int find_other_exec (const char *argv0, const char *target, const char *versionstr, char *retpath)
 
char * pipe_read_line (char *cmd)
 
int pclose_check (FILE *stream)
 
void set_pglocale_pgservice (const char *argv0, const char *app)
 

Macro Definition Documentation

◆ _DARWIN_BETTER_REALPATH

#define _DARWIN_BETTER_REALPATH

Definition at line 24 of file exec.c.

◆ log_error

#define log_error (   errcodefn,
  ... 
)     ereport(LOG, (errcodefn, errmsg_internal(__VA_ARGS__)))

Definition at line 66 of file exec.c.

Function Documentation

◆ find_my_exec()

int find_my_exec ( const char *  argv0,
char *  retpath 
)

Definition at line 161 of file exec.c.

162{
163 char *path;
164
165 /*
166 * If argv0 contains a separator, then PATH wasn't used.
167 */
168 strlcpy(retpath, argv0, MAXPGPATH);
169 if (first_dir_separator(retpath) != NULL)
170 {
171 if (validate_exec(retpath) == 0)
172 return normalize_exec_path(retpath);
173
174 log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
175 _("invalid binary \"%s\": %m"), retpath);
176 return -1;
177 }
178
179#ifdef WIN32
180 /* Win32 checks the current directory first for names without slashes */
181 if (validate_exec(retpath) == 0)
182 return normalize_exec_path(retpath);
183#endif
184
185 /*
186 * Since no explicit path was supplied, the user must have been relying on
187 * PATH. We'll search the same PATH.
188 */
189 if ((path = getenv("PATH")) && *path)
190 {
191 char *startp = NULL,
192 *endp = NULL;
193
194 do
195 {
196 if (!startp)
197 startp = path;
198 else
199 startp = endp + 1;
200
201 endp = first_path_var_separator(startp);
202 if (!endp)
203 endp = startp + strlen(startp); /* point to end */
204
205 strlcpy(retpath, startp, Min(endp - startp + 1, MAXPGPATH));
206
207 join_path_components(retpath, retpath, argv0);
208 canonicalize_path(retpath);
209
210 switch (validate_exec(retpath))
211 {
212 case 0: /* found ok */
213 return normalize_exec_path(retpath);
214 case -1: /* wasn't even a candidate, keep looking */
215 break;
216 case -2: /* found but disqualified */
217 log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
218 _("could not read binary \"%s\": %m"),
219 retpath);
220 break;
221 }
222 } while (*endp);
223 }
224
225 log_error(errcode(ERRCODE_UNDEFINED_FILE),
226 _("could not find a \"%s\" to execute"), argv0);
227 return -1;
228}
#define Min(x, y)
Definition: c.h:1007
#define log_error(errcodefn,...)
Definition: exec.c:66
int validate_exec(const char *path)
Definition: exec.c:89
static int normalize_exec_path(char *path)
Definition: exec.c:242
int errcode(int sqlerrcode)
Definition: elog.c:863
#define _(x)
Definition: elog.c:91
#define MAXPGPATH
static char * argv0
Definition: pg_ctl.c:94
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:286
char * first_path_var_separator(const char *pathlist)
Definition: path.c:127
void canonicalize_path(char *path)
Definition: path.c:337
char * first_dir_separator(const char *filename)
Definition: path.c:110
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

References _, argv0, canonicalize_path(), errcode(), first_dir_separator(), first_path_var_separator(), join_path_components(), log_error, MAXPGPATH, Min, normalize_exec_path(), strlcpy(), and validate_exec().

Referenced by ensureCleanShutdown(), find_other_exec(), find_other_exec_or_die(), get_exec_path(), getInstallationPaths(), getRestoreCommand(), InitStandaloneProcess(), main(), process_psqlrc(), set_pglocale_pgservice(), setup(), and setup_bin_paths().

◆ find_other_exec()

int find_other_exec ( const char *  argv0,
const char *  target,
const char *  versionstr,
char *  retpath 
)

Definition at line 311 of file exec.c.

313{
314 char cmd[MAXPGPATH];
315 char *line;
316
317 if (find_my_exec(argv0, retpath) < 0)
318 return -1;
319
320 /* Trim off program name and keep just directory */
321 *last_dir_separator(retpath) = '\0';
322 canonicalize_path(retpath);
323
324 /* Now append the other program's name */
325 snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
326 "/%s%s", target, EXE);
327
328 if (validate_exec(retpath) != 0)
329 return -1;
330
331 snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
332
333 if ((line = pipe_read_line(cmd)) == NULL)
334 return -1;
335
336 if (strcmp(line, versionstr) != 0)
337 {
338 pfree(line);
339 return -2;
340 }
341
342 pfree(line);
343 return 0;
344}
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:161
char * pipe_read_line(char *cmd)
Definition: exec.c:353
void pfree(void *pointer)
Definition: mcxt.c:1594
char * last_dir_separator(const char *filename)
Definition: path.c:145
#define snprintf
Definition: port.h:239
#define EXE
Definition: port.h:155

References argv0, canonicalize_path(), EXE, find_my_exec(), last_dir_separator(), MAXPGPATH, pfree(), pipe_read_line(), snprintf, and validate_exec().

Referenced by ensureCleanShutdown(), find_other_exec_or_die(), get_exec_path(), getInstallationPaths(), getRestoreCommand(), isolation_start_test(), main(), and setup_bin_paths().

◆ normalize_exec_path()

static int normalize_exec_path ( char *  path)
static

Definition at line 242 of file exec.c.

243{
244 /*
245 * We used to do a lot of work ourselves here, but now we just let
246 * realpath(3) do all the heavy lifting.
247 */
248 char *abspath = pg_realpath(path);
249
250 if (abspath == NULL)
251 {
253 _("could not resolve path \"%s\" to absolute form: %m"),
254 path);
255 return -1;
256 }
257 strlcpy(path, abspath, MAXPGPATH);
258 free(abspath);
259
260#ifdef WIN32
261 /* On Windows, be sure to convert '\' to '/' */
262 canonicalize_path(path);
263#endif
264
265 return 0;
266}
static char * pg_realpath(const char *fname)
Definition: exec.c:283
int errcode_for_file_access(void)
Definition: elog.c:886
#define free(a)
Definition: header.h:65

References _, canonicalize_path(), errcode_for_file_access(), free, log_error, MAXPGPATH, pg_realpath(), and strlcpy().

Referenced by find_my_exec().

◆ pclose_check()

int pclose_check ( FILE *  stream)

Definition at line 392 of file exec.c.

393{
394 int exitstatus;
395 char *reason;
396
397 exitstatus = pclose(stream);
398
399 if (exitstatus == 0)
400 return 0; /* all is well */
401
402 if (exitstatus == -1)
403 {
404 /* pclose() itself failed, and hopefully set errno */
405 log_error(errcode(ERRCODE_SYSTEM_ERROR),
406 _("%s() failed: %m"), "pclose");
407 }
408 else
409 {
410 reason = wait_result_to_str(exitstatus);
411 log_error(errcode(ERRCODE_SYSTEM_ERROR),
412 "%s", reason);
413 pfree(reason);
414 }
415 return exitstatus;
416}
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:33

References _, errcode(), log_error, pfree(), and wait_result_to_str().

Referenced by pipe_read_line().

◆ pg_realpath()

static char * pg_realpath ( const char *  fname)
static

Definition at line 283 of file exec.c.

284{
285 char *path;
286
287#ifndef WIN32
288 path = realpath(fname, NULL);
289#else /* WIN32 */
290
291 /*
292 * Microsoft is resolutely non-POSIX, but _fullpath() does the same thing.
293 * The documentation claims it reports errors by setting errno, which is a
294 * bit surprising for Microsoft, but we'll believe that until it's proven
295 * wrong. Clear errno first, though, so we can at least tell if a failure
296 * occurs and doesn't set it.
297 */
298 errno = 0;
299 path = _fullpath(NULL, fname, 0);
300#endif
301
302 return path;
303}

Referenced by normalize_exec_path().

◆ pipe_read_line()

char * pipe_read_line ( char *  cmd)

Definition at line 353 of file exec.c.

354{
355 FILE *pipe_cmd;
356 char *line;
357
358 fflush(NULL);
359
360 errno = 0;
361 if ((pipe_cmd = popen(cmd, "r")) == NULL)
362 {
363 log_error(errcode(ERRCODE_SYSTEM_ERROR),
364 _("could not execute command \"%s\": %m"), cmd);
365 return NULL;
366 }
367
368 /* Make sure popen() didn't change errno */
369 errno = 0;
370 line = pg_get_line(pipe_cmd, NULL);
371
372 if (line == NULL)
373 {
374 if (ferror(pipe_cmd))
376 _("could not read from command \"%s\": %m"), cmd);
377 else
378 log_error(errcode(ERRCODE_NO_DATA),
379 _("no data was returned by command \"%s\""), cmd);
380 }
381
382 (void) pclose_check(pipe_cmd);
383
384 return line;
385}
int pclose_check(FILE *stream)
Definition: exec.c:392
char * pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:59

References _, errcode(), errcode_for_file_access(), log_error, pclose_check(), and pg_get_line().

Referenced by check_exec(), find_other_exec(), and getRestoreCommand().

◆ set_pglocale_pgservice()

void set_pglocale_pgservice ( const char *  argv0,
const char *  app 
)

Definition at line 430 of file exec.c.

431{
432 char path[MAXPGPATH];
434
435 /* don't set LC_ALL in the backend */
436 if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
437 {
438 setlocale(LC_ALL, "");
439
440 /*
441 * One could make a case for reproducing here PostmasterMain()'s test
442 * for whether the process is multithreaded. Unlike the postmaster,
443 * no frontend program calls sigprocmask() or otherwise provides for
444 * mutual exclusion between signal handlers. While frontends using
445 * fork(), if multithreaded, are formally exposed to undefined
446 * behavior, we have not witnessed a concrete bug. Therefore,
447 * complaining about multithreading here may be mere pedantry.
448 */
449 }
450
452 return;
453
454#ifdef ENABLE_NLS
456 bindtextdomain(app, path);
457 textdomain(app);
458 /* set for libpq to use, but don't override existing setting */
459 setenv("PGLOCALEDIR", path, 0);
460#endif
461
462 if (getenv("PGSYSCONFDIR") == NULL)
463 {
465 /* set for libpq to use */
466 setenv("PGSYSCONFDIR", path, 0);
467 }
468}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1217
char my_exec_path[MAXPGPATH]
Definition: globals.c:81
void get_locale_path(const char *my_exec_path, char *ret_path)
Definition: path.c:965
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:911
#define setenv(x, y, z)
Definition: win32_port.h:545
#define setlocale(a, b)
Definition: win32_port.h:475

References argv0, find_my_exec(), get_etc_path(), get_locale_path(), MAXPGPATH, my_exec_path, PG_TEXTDOMAIN, setenv, and setlocale.

Referenced by main(), and regression_main().

◆ validate_exec()

int validate_exec ( const char *  path)

Definition at line 89 of file exec.c.

90{
91 struct stat buf;
92 int is_r;
93 int is_x;
94
95#ifdef WIN32
96 char path_exe[MAXPGPATH + sizeof(".exe") - 1];
97
98 /* Win32 requires a .exe suffix for stat() */
99 if (strlen(path) < strlen(".exe") ||
100 pg_strcasecmp(path + strlen(path) - strlen(".exe"), ".exe") != 0)
101 {
102 strlcpy(path_exe, path, sizeof(path_exe) - 4);
103 strcat(path_exe, ".exe");
104 path = path_exe;
105 }
106#endif
107
108 /*
109 * Ensure that the file exists and is a regular file.
110 *
111 * XXX if you have a broken system where stat() looks at the symlink
112 * instead of the underlying file, you lose.
113 */
114 if (stat(path, &buf) < 0)
115 return -1;
116
117 if (!S_ISREG(buf.st_mode))
118 {
119 /*
120 * POSIX offers no errno code that's simply "not a regular file". If
121 * it's a directory we can use EISDIR. Otherwise, it's most likely a
122 * device special file, and EPERM (Operation not permitted) isn't too
123 * horribly off base.
124 */
125 errno = S_ISDIR(buf.st_mode) ? EISDIR : EPERM;
126 return -1;
127 }
128
129 /*
130 * Ensure that the file is both executable and readable (required for
131 * dynamic loading).
132 */
133#ifndef WIN32
134 is_r = (access(path, R_OK) == 0);
135 is_x = (access(path, X_OK) == 0);
136 /* access() will set errno if it returns -1 */
137#else
138 is_r = buf.st_mode & S_IRUSR;
139 is_x = buf.st_mode & S_IXUSR;
140 errno = EACCES; /* appropriate thing if we return nonzero */
141#endif
142 return is_x ? (is_r ? 0 : -2) : -1;
143}
static char * buf
Definition: pg_test_fsync.c:72
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
short access
Definition: preproc-type.c:36
#define stat
Definition: win32_port.h:274
#define S_ISDIR(m)
Definition: win32_port.h:315
#define S_IRUSR
Definition: win32_port.h:279
#define S_ISREG(m)
Definition: win32_port.h:318
#define S_IXUSR
Definition: win32_port.h:285

References buf, MAXPGPATH, pg_strcasecmp(), S_IRUSR, S_ISDIR, S_ISREG, S_IXUSR, stat, and strlcpy().

Referenced by check_exec(), find_my_exec(), and find_other_exec().