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 "common/string.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)
 
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 65 of file exec.c.

Function Documentation

◆ find_my_exec()

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

Definition at line 160 of file exec.c.

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

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

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

◆ normalize_exec_path()

static int normalize_exec_path ( char *  path)
static

Definition at line 241 of file exec.c.

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

411 {
412  int exitstatus;
413  char *reason;
414 
415  exitstatus = pclose(stream);
416 
417  if (exitstatus == 0)
418  return 0; /* all is well */
419 
420  if (exitstatus == -1)
421  {
422  /* pclose() itself failed, and hopefully set errno */
423  log_error(errcode(ERRCODE_SYSTEM_ERROR),
424  _("%s() failed: %m"), "pclose");
425  }
426  else
427  {
428  reason = wait_result_to_str(exitstatus);
429  log_error(errcode(ERRCODE_SYSTEM_ERROR),
430  "%s", reason);
431  pfree(reason);
432  }
433  return exitstatus;
434 }
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 282 of file exec.c.

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

Definition at line 371 of file exec.c.

372 {
373  FILE *pipe_cmd;
374  char *line;
375 
376  fflush(NULL);
377 
378  errno = 0;
379  if ((pipe_cmd = popen(cmd, "r")) == NULL)
380  {
381  log_error(errcode(ERRCODE_SYSTEM_ERROR),
382  _("could not execute command \"%s\": %m"), cmd);
383  return NULL;
384  }
385 
386  /* Make sure popen() didn't change errno */
387  errno = 0;
388  line = pg_get_line(pipe_cmd, NULL);
389 
390  if (line == NULL)
391  {
392  if (ferror(pipe_cmd))
394  _("could not read from command \"%s\": %m"), cmd);
395  else
396  log_error(errcode(ERRCODE_NO_DATA),
397  _("no data was returned by command \"%s\""), cmd);
398  }
399 
400  (void) pclose_check(pipe_cmd);
401 
402  return line;
403 }
int pclose_check(FILE *stream)
Definition: exec.c:410
static void const char fflush(stdout)
char * pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:59

References _, errcode(), errcode_for_file_access(), fflush(), log_error, pclose_check(), and pg_get_line().

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

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

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