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

Function Documentation

◆ find_my_exec()

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

Definition at line 135 of file exec.c.

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

332 {
333  char cmd[MAXPGPATH];
334  char line[MAXPGPATH];
335 
336  if (find_my_exec(argv0, retpath) < 0)
337  return -1;
338 
339  /* Trim off program name and keep just directory */
340  *last_dir_separator(retpath) = '\0';
341  canonicalize_path(retpath);
342 
343  /* Now append the other program's name */
344  snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
345  "/%s%s", target, EXE);
346 
347  if (validate_exec(retpath) != 0)
348  return -1;
349 
350  snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
351 
352  if (!pipe_read_line(cmd, line, sizeof(line)))
353  return -1;
354 
355  if (strcmp(line, versionstr) != 0)
356  return -2;
357 
358  return 0;
359 }
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:135
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:366
char * last_dir_separator(const char *filename)
Definition: path.c:138
#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 403 of file exec.c.

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

367 {
368  FILE *pgver;
369 
370  /* flush output buffers in case popen does not... */
371  fflush(stdout);
372  fflush(stderr);
373 
374  errno = 0;
375  if ((pgver = popen(cmd, "r")) == NULL)
376  {
377  perror("popen failure");
378  return NULL;
379  }
380 
381  errno = 0;
382  if (fgets(line, maxsize, pgver) == NULL)
383  {
384  if (feof(pgver))
385  fprintf(stderr, "no data was returned by command \"%s\"\n", cmd);
386  else
387  perror("fgets failure");
388  pclose(pgver); /* no error checking */
389  return NULL;
390  }
391 
392  if (pclose_check(pgver))
393  return NULL;
394 
395  return line;
396 }
int pclose_check(FILE *stream)
Definition: exec.c:403
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 238 of file exec.c.

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

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

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