PostgreSQL Source Code git master
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 65 of file exec.c.

Function Documentation

◆ find_my_exec()

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

Definition at line 160 of file exec.c.

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

312{
313 char cmd[MAXPGPATH];
314 char *line;
315
316 if (find_my_exec(argv0, retpath) < 0)
317 return -1;
318
319 /* Trim off program name and keep just directory */
320 *last_dir_separator(retpath) = '\0';
321 canonicalize_path(retpath);
322
323 /* Now append the other program's name */
324 snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
325 "/%s%s", target, EXE);
326
327 if (validate_exec(retpath) != 0)
328 return -1;
329
330 snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
331
332 if ((line = pipe_read_line(cmd)) == NULL)
333 return -1;
334
335 if (strcmp(line, versionstr) != 0)
336 {
337 pfree(line);
338 return -2;
339 }
340
341 pfree(line);
342 return 0;
343}
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
char * pipe_read_line(char *cmd)
Definition: exec.c:352
void pfree(void *pointer)
Definition: mcxt.c:1521
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 241 of file exec.c.

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

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

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

Referenced by normalize_exec_path().

◆ pipe_read_line()

char * pipe_read_line ( char *  cmd)

Definition at line 352 of file exec.c.

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

References _, errcode(), errcode_for_file_access(), fflush(), 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 429 of file exec.c.

430{
431 char path[MAXPGPATH];
433
434 /* don't set LC_ALL in the backend */
435 if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
436 {
437 setlocale(LC_ALL, "");
438
439 /*
440 * One could make a case for reproducing here PostmasterMain()'s test
441 * for whether the process is multithreaded. Unlike the postmaster,
442 * no frontend program calls sigprocmask() or otherwise provides for
443 * mutual exclusion between signal handlers. While frontends using
444 * fork(), if multithreaded, are formally exposed to undefined
445 * behavior, we have not witnessed a concrete bug. Therefore,
446 * complaining about multithreading here may be mere pedantry.
447 */
448 }
449
451 return;
452
453#ifdef ENABLE_NLS
455 bindtextdomain(app, path);
456 textdomain(app);
457 /* set for libpq to use, but don't override existing setting */
458 setenv("PGLOCALEDIR", path, 0);
459#endif
460
461 if (getenv("PGSYSCONFDIR") == NULL)
462 {
464 /* set for libpq to use */
465 setenv("PGSYSCONFDIR", path, 0);
466 }
467}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1171
char my_exec_path[MAXPGPATH]
Definition: globals.c:80
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 88 of file exec.c.

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