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

141 {
142  char cwd[MAXPGPATH],
143  test_path[MAXPGPATH];
144  char *path;
145 
146  if (!getcwd(cwd, MAXPGPATH))
147  {
149  _("could not identify current directory: %m"));
150  return -1;
151  }
152 
153  /*
154  * If argv0 contains a separator, then PATH wasn't used.
155  */
156  if (first_dir_separator(argv0) != NULL)
157  {
158  if (is_absolute_path(argv0))
159  strlcpy(retpath, argv0, MAXPGPATH);
160  else
161  join_path_components(retpath, cwd, argv0);
162  canonicalize_path(retpath);
163 
164  if (validate_exec(retpath) == 0)
165  return resolve_symlinks(retpath);
166 
167  log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
168  _("invalid binary \"%s\""), retpath);
169  return -1;
170  }
171 
172 #ifdef WIN32
173  /* Win32 checks the current directory first for names without slashes */
174  join_path_components(retpath, cwd, argv0);
175  if (validate_exec(retpath) == 0)
176  return resolve_symlinks(retpath);
177 #endif
178 
179  /*
180  * Since no explicit path was supplied, the user must have been relying on
181  * PATH. We'll search the same PATH.
182  */
183  if ((path = getenv("PATH")) && *path)
184  {
185  char *startp = NULL,
186  *endp = NULL;
187 
188  do
189  {
190  if (!startp)
191  startp = path;
192  else
193  startp = endp + 1;
194 
195  endp = first_path_var_separator(startp);
196  if (!endp)
197  endp = startp + strlen(startp); /* point to end */
198 
199  strlcpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
200 
201  if (is_absolute_path(test_path))
202  join_path_components(retpath, test_path, argv0);
203  else
204  {
205  join_path_components(retpath, cwd, test_path);
206  join_path_components(retpath, retpath, argv0);
207  }
208  canonicalize_path(retpath);
209 
210  switch (validate_exec(retpath))
211  {
212  case 0: /* found ok */
213  return resolve_symlinks(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\""),
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:986
#define log_error(errcodefn,...)
Definition: exec.c:54
int validate_exec(const char *path)
Definition: exec.c:79
static int resolve_symlinks(char *path)
Definition: exec.c:243
int errcode_for_file_access(void)
Definition: elog.c:716
int errcode(int sqlerrcode)
Definition: elog.c:693
#define _(x)
Definition: elog.c:89
#define MAXPGPATH
static char * argv0
Definition: pg_ctl.c:97
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:89
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 335 of file exec.c.

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

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

409 {
410  int exitstatus;
411  char *reason;
412 
413  exitstatus = pclose(stream);
414 
415  if (exitstatus == 0)
416  return 0; /* all is well */
417 
418  if (exitstatus == -1)
419  {
420  /* pclose() itself failed, and hopefully set errno */
421  log_error(errcode(ERRCODE_SYSTEM_ERROR),
422  _("%s() failed: %m"), "pclose");
423  }
424  else
425  {
426  reason = wait_result_to_str(exitstatus);
427  log_error(errcode(ERRCODE_SYSTEM_ERROR),
428  "%s", reason);
429  pfree(reason);
430  }
431  return exitstatus;
432 }
void pfree(void *pointer)
Definition: mcxt.c:1175
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32

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

372 {
373  FILE *pgver;
374 
375  /* flush output buffers in case popen does not... */
376  fflush(stdout);
377  fflush(stderr);
378 
379  errno = 0;
380  if ((pgver = popen(cmd, "r")) == NULL)
381  {
382  perror("popen failure");
383  return NULL;
384  }
385 
386  errno = 0;
387  if (fgets(line, maxsize, pgver) == NULL)
388  {
389  if (feof(pgver))
390  fprintf(stderr, "no data was returned by command \"%s\"\n", cmd);
391  else
392  perror("fgets failure");
393  pclose(pgver); /* no error checking */
394  return NULL;
395  }
396 
397  if (pclose_check(pgver))
398  return NULL;
399 
400  return line;
401 }
int pclose_check(FILE *stream)
Definition: exec.c:408
static void const char fflush(stdout)
#define fprintf
Definition: port.h:229

References fflush(), fprintf, pclose_check(), and generate_unaccent_rules::stdout.

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

◆ resolve_symlinks()

static int resolve_symlinks ( char *  path)
static

Definition at line 243 of file exec.c.

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

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

Referenced by find_my_exec().

◆ set_pglocale_pgservice()

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

Definition at line 446 of file exec.c.

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

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

80 {
81  struct stat buf;
82  int is_r;
83  int is_x;
84 
85 #ifdef WIN32
86  char path_exe[MAXPGPATH + sizeof(".exe") - 1];
87 
88  /* Win32 requires a .exe suffix for stat() */
89  if (strlen(path) >= strlen(".exe") &&
90  pg_strcasecmp(path + strlen(path) - strlen(".exe"), ".exe") != 0)
91  {
92  strlcpy(path_exe, path, sizeof(path_exe) - 4);
93  strcat(path_exe, ".exe");
94  path = path_exe;
95  }
96 #endif
97 
98  /*
99  * Ensure that the file exists and is a regular file.
100  *
101  * XXX if you have a broken system where stat() looks at the symlink
102  * instead of the underlying file, you lose.
103  */
104  if (stat(path, &buf) < 0)
105  return -1;
106 
107  if (!S_ISREG(buf.st_mode))
108  return -1;
109 
110  /*
111  * Ensure that the file is both executable and readable (required for
112  * dynamic loading).
113  */
114 #ifndef WIN32
115  is_r = (access(path, R_OK) == 0);
116  is_x = (access(path, X_OK) == 0);
117 #else
118  is_r = buf.st_mode & S_IRUSR;
119  is_x = buf.st_mode & S_IXUSR;
120 #endif
121  return is_x ? (is_r ? 0 : -2) : -1;
122 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define stat
Definition: win32_port.h:283
#define S_IRUSR
Definition: win32_port.h:288
#define S_ISREG(m)
Definition: win32_port.h:327
#define S_IXUSR
Definition: win32_port.h:294

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

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