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 log_error(errcodefn, ...)    ereport(LOG, (errcodefn, errmsg_internal(__VA_ARGS__)))
 

Functions

static int resolve_symlinks (char *path)
 
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

◆ log_error

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

Definition at line 54 of file exec.c.

Function Documentation

◆ find_my_exec()

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

Definition at line 152 of file exec.c.

153 {
154  char cwd[MAXPGPATH],
155  test_path[MAXPGPATH];
156  char *path;
157 
158  if (!getcwd(cwd, MAXPGPATH))
159  {
161  _("could not identify current directory: %m"));
162  return -1;
163  }
164 
165  /*
166  * If argv0 contains a separator, then PATH wasn't used.
167  */
168  if (first_dir_separator(argv0) != NULL)
169  {
170  if (is_absolute_path(argv0))
171  strlcpy(retpath, argv0, MAXPGPATH);
172  else
173  join_path_components(retpath, cwd, argv0);
174  canonicalize_path(retpath);
175 
176  if (validate_exec(retpath) == 0)
177  return resolve_symlinks(retpath);
178 
179  log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
180  _("invalid binary \"%s\": %m"), retpath);
181  return -1;
182  }
183 
184 #ifdef WIN32
185  /* Win32 checks the current directory first for names without slashes */
186  join_path_components(retpath, cwd, argv0);
187  if (validate_exec(retpath) == 0)
188  return resolve_symlinks(retpath);
189 #endif
190 
191  /*
192  * Since no explicit path was supplied, the user must have been relying on
193  * PATH. We'll search the same PATH.
194  */
195  if ((path = getenv("PATH")) && *path)
196  {
197  char *startp = NULL,
198  *endp = NULL;
199 
200  do
201  {
202  if (!startp)
203  startp = path;
204  else
205  startp = endp + 1;
206 
207  endp = first_path_var_separator(startp);
208  if (!endp)
209  endp = startp + strlen(startp); /* point to end */
210 
211  strlcpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
212 
213  if (is_absolute_path(test_path))
214  join_path_components(retpath, test_path, argv0);
215  else
216  {
217  join_path_components(retpath, cwd, test_path);
218  join_path_components(retpath, retpath, argv0);
219  }
220  canonicalize_path(retpath);
221 
222  switch (validate_exec(retpath))
223  {
224  case 0: /* found ok */
225  return resolve_symlinks(retpath);
226  case -1: /* wasn't even a candidate, keep looking */
227  break;
228  case -2: /* found but disqualified */
229  log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
230  _("could not read binary \"%s\": %m"),
231  retpath);
232  break;
233  }
234  } while (*endp);
235  }
236 
237  log_error(errcode(ERRCODE_UNDEFINED_FILE),
238  _("could not find a \"%s\" to execute"), argv0);
239  return -1;
240 }
#define Min(x, y)
Definition: c.h:988
#define log_error(errcodefn,...)
Definition: exec.c:54
int validate_exec(const char *path)
Definition: exec.c:80
static int resolve_symlinks(char *path)
Definition: exec.c:259
int errcode_for_file_access(void)
Definition: elog.c:881
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
#define is_absolute_path(filename)
Definition: port.h:103
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(), errcode_for_file_access(), first_dir_separator(), first_path_var_separator(), is_absolute_path, join_path_components(), log_error, MAXPGPATH, Min, resolve_symlinks(), 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 351 of file exec.c.

353 {
354  char cmd[MAXPGPATH];
355  char line[MAXPGPATH];
356 
357  if (find_my_exec(argv0, retpath) < 0)
358  return -1;
359 
360  /* Trim off program name and keep just directory */
361  *last_dir_separator(retpath) = '\0';
362  canonicalize_path(retpath);
363 
364  /* Now append the other program's name */
365  snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
366  "/%s%s", target, EXE);
367 
368  if (validate_exec(retpath) != 0)
369  return -1;
370 
371  snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
372 
373  if (!pipe_read_line(cmd, line, sizeof(line)))
374  return -1;
375 
376  if (strcmp(line, versionstr) != 0)
377  return -2;
378 
379  return 0;
380 }
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:152
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:387
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().

◆ pclose_check()

int pclose_check ( FILE *  stream)

Definition at line 422 of file exec.c.

423 {
424  int exitstatus;
425  char *reason;
426 
427  exitstatus = pclose(stream);
428 
429  if (exitstatus == 0)
430  return 0; /* all is well */
431 
432  if (exitstatus == -1)
433  {
434  /* pclose() itself failed, and hopefully set errno */
435  log_error(errcode(ERRCODE_SYSTEM_ERROR),
436  _("%s() failed: %m"), "pclose");
437  }
438  else
439  {
440  reason = wait_result_to_str(exitstatus);
441  log_error(errcode(ERRCODE_SYSTEM_ERROR),
442  "%s", reason);
443  pfree(reason);
444  }
445  return exitstatus;
446 }
void pfree(void *pointer)
Definition: mcxt.c:1436
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().

◆ pipe_read_line()

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

Definition at line 387 of file exec.c.

388 {
389  FILE *pgver;
390 
391  fflush(NULL);
392 
393  errno = 0;
394  if ((pgver = popen(cmd, "r")) == NULL)
395  {
396  perror("popen failure");
397  return NULL;
398  }
399 
400  errno = 0;
401  if (fgets(line, maxsize, pgver) == NULL)
402  {
403  if (feof(pgver))
404  fprintf(stderr, "no data was returned by command \"%s\"\n", cmd);
405  else
406  perror("fgets failure");
407  pclose(pgver); /* no error checking */
408  return NULL;
409  }
410 
411  if (pclose_check(pgver))
412  return NULL;
413 
414  return line;
415 }
int pclose_check(FILE *stream)
Definition: exec.c:422
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().

◆ resolve_symlinks()

static int resolve_symlinks ( char *  path)
static

Definition at line 259 of file exec.c.

260 {
261 #ifdef HAVE_READLINK
262  struct stat buf;
263  char orig_wd[MAXPGPATH],
264  link_buf[MAXPGPATH];
265  char *fname;
266 
267  /*
268  * To resolve a symlink properly, we have to chdir into its directory and
269  * then chdir to where the symlink points; otherwise we may fail to
270  * resolve relative links correctly (consider cases involving mount
271  * points, for example). After following the final symlink, we use
272  * getcwd() to figure out where the heck we're at.
273  *
274  * One might think we could skip all this if path doesn't point to a
275  * symlink to start with, but that's wrong. We also want to get rid of
276  * any directory symlinks that are present in the given path. We expect
277  * getcwd() to give us an accurate, symlink-free path.
278  */
279  if (!getcwd(orig_wd, MAXPGPATH))
280  {
282  _("could not identify current directory: %m"));
283  return -1;
284  }
285 
286  for (;;)
287  {
288  char *lsep;
289  int rllen;
290 
291  lsep = last_dir_separator(path);
292  if (lsep)
293  {
294  *lsep = '\0';
295  if (chdir(path) == -1)
296  {
298  _("could not change directory to \"%s\": %m"), path);
299  return -1;
300  }
301  fname = lsep + 1;
302  }
303  else
304  fname = path;
305 
306  if (lstat(fname, &buf) < 0 ||
307  !S_ISLNK(buf.st_mode))
308  break;
309 
310  errno = 0;
311  rllen = readlink(fname, link_buf, sizeof(link_buf));
312  if (rllen < 0 || rllen >= sizeof(link_buf))
313  {
315  _("could not read symbolic link \"%s\": %m"), fname);
316  return -1;
317  }
318  link_buf[rllen] = '\0';
319  strcpy(path, link_buf);
320  }
321 
322  /* must copy final component out of 'path' temporarily */
323  strlcpy(link_buf, fname, sizeof(link_buf));
324 
325  if (!getcwd(path, MAXPGPATH))
326  {
328  _("could not identify current directory: %m"));
329  return -1;
330  }
331  join_path_components(path, path, link_buf);
332  canonicalize_path(path);
333 
334  if (chdir(orig_wd) == -1)
335  {
337  _("could not change directory to \"%s\": %m"), orig_wd);
338  return -1;
339  }
340 #endif /* HAVE_READLINK */
341 
342  return 0;
343 }
static char * buf
Definition: pg_test_fsync.c:67
#define lstat(path, sb)
Definition: win32_port.h:287
#define S_ISLNK(m)
Definition: win32_port.h:346
#define readlink(path, buf, size)
Definition: win32_port.h:238

References _, buf, canonicalize_path(), errcode_for_file_access(), join_path_components(), last_dir_separator(), log_error, lstat, MAXPGPATH, readlink, S_ISLNK, and strlcpy().

Referenced by find_my_exec().

◆ set_pglocale_pgservice()

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

Definition at line 460 of file exec.c.

461 {
462  char path[MAXPGPATH];
463  char my_exec_path[MAXPGPATH];
464 
465  /* don't set LC_ALL in the backend */
466  if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
467  {
468  setlocale(LC_ALL, "");
469 
470  /*
471  * One could make a case for reproducing here PostmasterMain()'s test
472  * for whether the process is multithreaded. Unlike the postmaster,
473  * no frontend program calls sigprocmask() or otherwise provides for
474  * mutual exclusion between signal handlers. While frontends using
475  * fork(), if multithreaded, are formally exposed to undefined
476  * behavior, we have not witnessed a concrete bug. Therefore,
477  * complaining about multithreading here may be mere pedantry.
478  */
479  }
480 
481  if (find_my_exec(argv0, my_exec_path) < 0)
482  return;
483 
484 #ifdef ENABLE_NLS
486  bindtextdomain(app, path);
487  textdomain(app);
488  /* set for libpq to use, but don't override existing setting */
489  setenv("PGLOCALEDIR", path, 0);
490 #endif
491 
492  if (getenv("PGSYSCONFDIR") == NULL)
493  {
494  get_etc_path(my_exec_path, path);
495  /* set for libpq to use */
496  setenv("PGSYSCONFDIR", path, 0);
497  }
498 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1204
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:534
#define setlocale(a, b)
Definition: win32_port.h:464

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

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

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