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 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, char *line, int maxsize)
 
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 63 of file exec.c.

Function Documentation

◆ find_my_exec()

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

Definition at line 158 of file exec.c.

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

329 {
330  char cmd[MAXPGPATH];
331  char line[MAXPGPATH];
332 
333  if (find_my_exec(argv0, retpath) < 0)
334  return -1;
335 
336  /* Trim off program name and keep just directory */
337  *last_dir_separator(retpath) = '\0';
338  canonicalize_path(retpath);
339 
340  /* Now append the other program's name */
341  snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
342  "/%s%s", target, EXE);
343 
344  if (validate_exec(retpath) != 0)
345  return -1;
346 
347  snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
348 
349  if (!pipe_read_line(cmd, line, sizeof(line)))
350  return -1;
351 
352  if (strcmp(line, versionstr) != 0)
353  return -2;
354 
355  return 0;
356 }
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:158
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:363
char * last_dir_separator(const char *filename)
Definition: path.c:139
#define snprintf
Definition: port.h:238
#define EXE
Definition: port.h:154

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

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

◆ normalize_exec_path()

static int normalize_exec_path ( char *  path)
static

Definition at line 239 of file exec.c.

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

399 {
400  int exitstatus;
401  char *reason;
402 
403  exitstatus = pclose(stream);
404 
405  if (exitstatus == 0)
406  return 0; /* all is well */
407 
408  if (exitstatus == -1)
409  {
410  /* pclose() itself failed, and hopefully set errno */
411  log_error(errcode(ERRCODE_SYSTEM_ERROR),
412  _("%s() failed: %m"), "pclose");
413  }
414  else
415  {
416  reason = wait_result_to_str(exitstatus);
417  log_error(errcode(ERRCODE_SYSTEM_ERROR),
418  "%s", reason);
419  pfree(reason);
420  }
421  return exitstatus;
422 }
void pfree(void *pointer)
Definition: mcxt.c:1456
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 280 of file exec.c.

281 {
282  char *path;
283 
284 #ifndef WIN32
285  path = realpath(fname, NULL);
286  if (path == NULL && errno == EINVAL)
287  {
288  /*
289  * Cope with old-POSIX systems that require a user-provided buffer.
290  * Assume MAXPGPATH is enough room on all such systems.
291  */
292  char *buf = malloc(MAXPGPATH);
293 
294  if (buf == NULL)
295  return NULL; /* assume errno is set */
296  path = realpath(fname, buf);
297  if (path == NULL) /* don't leak memory */
298  {
299  int save_errno = errno;
300 
301  free(buf);
302  errno = save_errno;
303  }
304  }
305 #else /* WIN32 */
306 
307  /*
308  * Microsoft is resolutely non-POSIX, but _fullpath() does the same thing.
309  * The documentation claims it reports errors by setting errno, which is a
310  * bit surprising for Microsoft, but we'll believe that until it's proven
311  * wrong. Clear errno first, though, so we can at least tell if a failure
312  * occurs and doesn't set it.
313  */
314  errno = 0;
315  path = _fullpath(NULL, fname, 0);
316 #endif
317 
318  return path;
319 }
#define malloc(a)
Definition: header.h:50
static char * buf
Definition: pg_test_fsync.c:73

References buf, free, malloc, and MAXPGPATH.

Referenced by normalize_exec_path().

◆ pipe_read_line()

char* pipe_read_line ( char *  cmd,
char *  line,
int  maxsize 
)

Definition at line 363 of file exec.c.

364 {
365  FILE *pgver;
366 
367  fflush(NULL);
368 
369  errno = 0;
370  if ((pgver = popen(cmd, "r")) == NULL)
371  {
372  perror("popen failure");
373  return NULL;
374  }
375 
376  errno = 0;
377  if (fgets(line, maxsize, pgver) == NULL)
378  {
379  if (feof(pgver))
380  fprintf(stderr, "no data was returned by command \"%s\"\n", cmd);
381  else
382  perror("fgets failure");
383  pclose(pgver); /* no error checking */
384  return NULL;
385  }
386 
387  if (pclose_check(pgver))
388  return NULL;
389 
390  return line;
391 }
int pclose_check(FILE *stream)
Definition: exec.c:398
static void const char fflush(stdout)
#define fprintf
Definition: port.h:242

References fflush(), fprintf, and pclose_check().

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 436 of file exec.c.

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

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 86 of file exec.c.

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

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().