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__)))

Function Documentation

◆ find_my_exec()

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

Definition at line 127 of file exec.c.

References _, 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 do_kill(), ensureCleanShutdown(), find_other_exec(), find_other_exec_or_die(), getInstallationPaths(), getRestoreCommand(), InitStandaloneProcess(), main(), process_psqlrc(), set_pglocale_pgservice(), setup(), and setup_bin_paths().

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

◆ find_other_exec()

int find_other_exec ( const char *  argv0,
const char *  target,
const char *  versionstr,
char *  retpath 
)

Definition at line 322 of file exec.c.

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

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

324 {
325  char cmd[MAXPGPATH];
326  char line[MAXPGPATH];
327 
328  if (find_my_exec(argv0, retpath) < 0)
329  return -1;
330 
331  /* Trim off program name and keep just directory */
332  *last_dir_separator(retpath) = '\0';
333  canonicalize_path(retpath);
334 
335  /* Now append the other program's name */
336  snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
337  "/%s%s", target, EXE);
338 
339  if (validate_exec(retpath) != 0)
340  return -1;
341 
342  snprintf(cmd, sizeof(cmd), "\"%s\" -V", retpath);
343 
344  if (!pipe_read_line(cmd, line, sizeof(line)))
345  return -1;
346 
347  if (strcmp(line, versionstr) != 0)
348  return -2;
349 
350  return 0;
351 }
static char * argv0
Definition: pg_ctl.c:97
void canonicalize_path(char *path)
Definition: path.c:254
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:127
char * last_dir_separator(const char *filename)
Definition: path.c:138
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:358
#define EXE
Definition: port.h:141
#define snprintf
Definition: port.h:216
int validate_exec(const char *path)
Definition: exec.c:66

◆ pclose_check()

int pclose_check ( FILE *  stream)

Definition at line 395 of file exec.c.

References _, errcode(), log_error, pfree(), and wait_result_to_str().

Referenced by pipe_read_line().

396 {
397  int exitstatus;
398  char *reason;
399 
400  exitstatus = pclose(stream);
401 
402  if (exitstatus == 0)
403  return 0; /* all is well */
404 
405  if (exitstatus == -1)
406  {
407  /* pclose() itself failed, and hopefully set errno */
408  log_error(errcode(ERRCODE_SYSTEM_ERROR),
409  _("%s() failed: %m"), "pclose");
410  }
411  else
412  {
413  reason = wait_result_to_str(exitstatus);
414  log_error(errcode(ERRCODE_SYSTEM_ERROR),
415  "%s", reason);
416  pfree(reason);
417  }
418  return exitstatus;
419 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
void pfree(void *pointer)
Definition: mcxt.c:1169
#define log_error(errcodefn,...)
Definition: exec.c:41
#define _(x)
Definition: elog.c:89

◆ pipe_read_line()

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

Definition at line 358 of file exec.c.

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

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

359 {
360  FILE *pgver;
361 
362  /* flush output buffers in case popen does not... */
363  fflush(stdout);
364  fflush(stderr);
365 
366  errno = 0;
367  if ((pgver = popen(cmd, "r")) == NULL)
368  {
369  perror("popen failure");
370  return NULL;
371  }
372 
373  errno = 0;
374  if (fgets(line, maxsize, pgver) == NULL)
375  {
376  if (feof(pgver))
377  fprintf(stderr, "no data was returned by command \"%s\"\n", cmd);
378  else
379  perror("fgets failure");
380  pclose(pgver); /* no error checking */
381  return NULL;
382  }
383 
384  if (pclose_check(pgver))
385  return NULL;
386 
387  return line;
388 }
#define fprintf
Definition: port.h:220
int pclose_check(FILE *stream)
Definition: exec.c:395

◆ resolve_symlinks()

static int resolve_symlinks ( char *  path)
static

Definition at line 230 of file exec.c.

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

Referenced by find_my_exec().

231 {
232 #ifdef HAVE_READLINK
233  struct stat buf;
234  char orig_wd[MAXPGPATH],
235  link_buf[MAXPGPATH];
236  char *fname;
237 
238  /*
239  * To resolve a symlink properly, we have to chdir into its directory and
240  * then chdir to where the symlink points; otherwise we may fail to
241  * resolve relative links correctly (consider cases involving mount
242  * points, for example). After following the final symlink, we use
243  * getcwd() to figure out where the heck we're at.
244  *
245  * One might think we could skip all this if path doesn't point to a
246  * symlink to start with, but that's wrong. We also want to get rid of
247  * any directory symlinks that are present in the given path. We expect
248  * getcwd() to give us an accurate, symlink-free path.
249  */
250  if (!getcwd(orig_wd, MAXPGPATH))
251  {
253  _("could not identify current directory: %m"));
254  return -1;
255  }
256 
257  for (;;)
258  {
259  char *lsep;
260  int rllen;
261 
262  lsep = last_dir_separator(path);
263  if (lsep)
264  {
265  *lsep = '\0';
266  if (chdir(path) == -1)
267  {
269  _("could not change directory to \"%s\": %m"), path);
270  return -1;
271  }
272  fname = lsep + 1;
273  }
274  else
275  fname = path;
276 
277  if (lstat(fname, &buf) < 0 ||
278  !S_ISLNK(buf.st_mode))
279  break;
280 
281  errno = 0;
282  rllen = readlink(fname, link_buf, sizeof(link_buf));
283  if (rllen < 0 || rllen >= sizeof(link_buf))
284  {
286  _("could not read symbolic link \"%s\": %m"), fname);
287  return -1;
288  }
289  link_buf[rllen] = '\0';
290  strcpy(path, link_buf);
291  }
292 
293  /* must copy final component out of 'path' temporarily */
294  strlcpy(link_buf, fname, sizeof(link_buf));
295 
296  if (!getcwd(path, MAXPGPATH))
297  {
299  _("could not identify current directory: %m"));
300  return -1;
301  }
302  join_path_components(path, path, link_buf);
303  canonicalize_path(path);
304 
305  if (chdir(orig_wd) == -1)
306  {
308  _("could not change directory to \"%s\": %m"), orig_wd);
309  return -1;
310  }
311 #endif /* HAVE_READLINK */
312 
313  return 0;
314 }
void canonicalize_path(char *path)
Definition: path.c:254
#define log_error(errcodefn,...)
Definition: exec.c:41
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
#define readlink(path, buf, size)
Definition: win32_port.h:228
int errcode_for_file_access(void)
Definition: elog.c:721
char * last_dir_separator(const char *filename)
Definition: path.c:138
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:218
#define lstat(path, sb)
Definition: win32_port.h:276
#define _(x)
Definition: elog.c:89

◆ set_pglocale_pgservice()

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

Definition at line 433 of file exec.c.

References _, AddUserToTokenDacl(), cleanup(), errcode(), find_my_exec(), get_etc_path(), get_locale_path(), i, log_error, MAXPGPATH, my_exec_path, PG_TEXTDOMAIN, setenv, and setlocale.

Referenced by main(), and regression_main().

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

◆ validate_exec()

int validate_exec ( const char *  path)

Definition at line 66 of file exec.c.

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

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

67 {
68  struct stat buf;
69  int is_r;
70  int is_x;
71 
72 #ifdef WIN32
73  char path_exe[MAXPGPATH + sizeof(".exe") - 1];
74 
75  /* Win32 requires a .exe suffix for stat() */
76  if (strlen(path) >= strlen(".exe") &&
77  pg_strcasecmp(path + strlen(path) - strlen(".exe"), ".exe") != 0)
78  {
79  strlcpy(path_exe, path, sizeof(path_exe) - 4);
80  strcat(path_exe, ".exe");
81  path = path_exe;
82  }
83 #endif
84 
85  /*
86  * Ensure that the file exists and is a regular file.
87  *
88  * XXX if you have a broken system where stat() looks at the symlink
89  * instead of the underlying file, you lose.
90  */
91  if (stat(path, &buf) < 0)
92  return -1;
93 
94  if (!S_ISREG(buf.st_mode))
95  return -1;
96 
97  /*
98  * Ensure that the file is both executable and readable (required for
99  * dynamic loading).
100  */
101 #ifndef WIN32
102  is_r = (access(path, R_OK) == 0);
103  is_x = (access(path, X_OK) == 0);
104 #else
105  is_r = buf.st_mode & S_IRUSR;
106  is_x = buf.st_mode & S_IXUSR;
107 #endif
108  return is_x ? (is_r ? 0 : -2) : -1;
109 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define S_IXUSR
Definition: win32_port.h:286
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
#define S_ISREG(m)
Definition: win32_port.h:319
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define S_IRUSR
Definition: win32_port.h:280
#define stat
Definition: win32_port.h:275