LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - exec.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 93 125 74.4 %
Date: 2021-12-09 04:09:06 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  exec.c
       3             :  *
       4             :  *  execution functions
       5             :  *
       6             :  *  Copyright (c) 2010-2021, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/exec.c
       8             :  */
       9             : 
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include <fcntl.h>
      13             : 
      14             : #include "common/string.h"
      15             : #include "pg_upgrade.h"
      16             : 
      17             : static void check_data_dir(ClusterInfo *cluster);
      18             : static void check_bin_dir(ClusterInfo *cluster, bool check_versions);
      19             : static void get_bin_version(ClusterInfo *cluster);
      20             : static void check_exec(const char *dir, const char *program, bool check_version);
      21             : 
      22             : #ifdef WIN32
      23             : static int  win32_check_directory_write_permissions(void);
      24             : #endif
      25             : 
      26             : 
      27             : /*
      28             :  * get_bin_version
      29             :  *
      30             :  *  Fetch major version of binaries for cluster.
      31             :  */
      32             : static void
      33           4 : get_bin_version(ClusterInfo *cluster)
      34             : {
      35             :     char        cmd[MAXPGPATH],
      36             :                 cmd_output[MAX_STRING];
      37             :     FILE       *output;
      38           4 :     int         v1 = 0,
      39           4 :                 v2 = 0;
      40             : 
      41           4 :     snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
      42             : 
      43           8 :     if ((output = popen(cmd, "r")) == NULL ||
      44           4 :         fgets(cmd_output, sizeof(cmd_output), output) == NULL)
      45           0 :         pg_fatal("could not get pg_ctl version data using %s: %s\n",
      46           0 :                  cmd, strerror(errno));
      47             : 
      48           4 :     pclose(output);
      49             : 
      50           4 :     if (sscanf(cmd_output, "%*s %*s %d.%d", &v1, &v2) < 1)
      51           0 :         pg_fatal("could not get pg_ctl version output from %s\n", cmd);
      52             : 
      53           4 :     if (v1 < 10)
      54             :     {
      55             :         /* old style, e.g. 9.6.1 */
      56           0 :         cluster->bin_version = v1 * 10000 + v2 * 100;
      57             :     }
      58             :     else
      59             :     {
      60             :         /* new style, e.g. 10.1 */
      61           4 :         cluster->bin_version = v1 * 10000;
      62             :     }
      63           4 : }
      64             : 
      65             : 
      66             : /*
      67             :  * exec_prog()
      68             :  *      Execute an external program with stdout/stderr redirected, and report
      69             :  *      errors
      70             :  *
      71             :  * Formats a command from the given argument list, logs it to the log file,
      72             :  * and attempts to execute that command.  If the command executes
      73             :  * successfully, exec_prog() returns true.
      74             :  *
      75             :  * If the command fails, an error message is optionally written to the specified
      76             :  * log_file, and the program optionally exits.
      77             :  *
      78             :  * The code requires it be called first from the primary thread on Windows.
      79             :  */
      80             : bool
      81          70 : exec_prog(const char *log_file, const char *opt_log_file,
      82             :           bool report_error, bool exit_on_error, const char *fmt,...)
      83             : {
      84          70 :     int         result = 0;
      85             :     int         written;
      86             : 
      87             : #define MAXCMDLEN (2 * MAXPGPATH)
      88             :     char        cmd[MAXCMDLEN];
      89             :     FILE       *log;
      90             :     va_list     ap;
      91             : 
      92             : #ifdef WIN32
      93             :     static DWORD mainThreadId = 0;
      94             : 
      95             :     /* We assume we are called from the primary thread first */
      96             :     if (mainThreadId == 0)
      97             :         mainThreadId = GetCurrentThreadId();
      98             : #endif
      99             : 
     100          70 :     written = 0;
     101          70 :     va_start(ap, fmt);
     102          70 :     written += vsnprintf(cmd + written, MAXCMDLEN - written, fmt, ap);
     103          70 :     va_end(ap);
     104          70 :     if (written >= MAXCMDLEN)
     105           0 :         pg_fatal("command too long\n");
     106          70 :     written += snprintf(cmd + written, MAXCMDLEN - written,
     107             :                         " >> \"%s\" 2>&1", log_file);
     108          70 :     if (written >= MAXCMDLEN)
     109           0 :         pg_fatal("command too long\n");
     110             : 
     111          70 :     pg_log(PG_VERBOSE, "%s\n", cmd);
     112             : 
     113             : #ifdef WIN32
     114             : 
     115             :     /*
     116             :      * For some reason, Windows issues a file-in-use error if we write data to
     117             :      * the log file from a non-primary thread just before we create a
     118             :      * subprocess that also writes to the same log file.  One fix is to sleep
     119             :      * for 100ms.  A cleaner fix is to write to the log file _after_ the
     120             :      * subprocess has completed, so we do this only when writing from a
     121             :      * non-primary thread.  fflush(), running system() twice, and pre-creating
     122             :      * the file do not see to help.
     123             :      */
     124             :     if (mainThreadId != GetCurrentThreadId())
     125             :         result = system(cmd);
     126             : #endif
     127             : 
     128          70 :     log = fopen(log_file, "a");
     129             : 
     130             : #ifdef WIN32
     131             :     {
     132             :         /*
     133             :          * "pg_ctl -w stop" might have reported that the server has stopped
     134             :          * because the postmaster.pid file has been removed, but "pg_ctl -w
     135             :          * start" might still be in the process of closing and might still be
     136             :          * holding its stdout and -l log file descriptors open.  Therefore,
     137             :          * try to open the log file a few more times.
     138             :          */
     139             :         int         iter;
     140             : 
     141             :         for (iter = 0; iter < 4 && log == NULL; iter++)
     142             :         {
     143             :             pg_usleep(1000000); /* 1 sec */
     144             :             log = fopen(log_file, "a");
     145             :         }
     146             :     }
     147             : #endif
     148             : 
     149          70 :     if (log == NULL)
     150           0 :         pg_fatal("could not open log file \"%s\": %m\n", log_file);
     151             : 
     152             : #ifdef WIN32
     153             :     /* Are we printing "command:" before its output? */
     154             :     if (mainThreadId == GetCurrentThreadId())
     155             :         fprintf(log, "\n\n");
     156             : #endif
     157          70 :     fprintf(log, "command: %s\n", cmd);
     158             : #ifdef WIN32
     159             :     /* Are we printing "command:" after its output? */
     160             :     if (mainThreadId != GetCurrentThreadId())
     161             :         fprintf(log, "\n\n");
     162             : #endif
     163             : 
     164             :     /*
     165             :      * In Windows, we must close the log file at this point so the file is not
     166             :      * open while the command is running, or we get a share violation.
     167             :      */
     168          70 :     fclose(log);
     169             : 
     170             : #ifdef WIN32
     171             :     /* see comment above */
     172             :     if (mainThreadId == GetCurrentThreadId())
     173             : #endif
     174          70 :         result = system(cmd);
     175             : 
     176          70 :     if (result != 0 && report_error)
     177             :     {
     178             :         /* we might be in on a progress status line, so go to the next line */
     179           0 :         report_status(PG_REPORT, "\n*failure*");
     180           0 :         fflush(stdout);
     181             : 
     182           0 :         pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
     183           0 :         if (opt_log_file)
     184           0 :             pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
     185             :                    "Consult the last few lines of \"%s\" or \"%s\" for\n"
     186             :                    "the probable cause of the failure.\n",
     187             :                    log_file, opt_log_file);
     188             :         else
     189           0 :             pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
     190             :                    "Consult the last few lines of \"%s\" for\n"
     191             :                    "the probable cause of the failure.\n",
     192             :                    log_file);
     193             :     }
     194             : 
     195             : #ifndef WIN32
     196             : 
     197             :     /*
     198             :      * We can't do this on Windows because it will keep the "pg_ctl start"
     199             :      * output filename open until the server stops, so we do the \n\n above on
     200             :      * that platform.  We use a unique filename for "pg_ctl start" that is
     201             :      * never reused while the server is running, so it works fine.  We could
     202             :      * log these commands to a third file, but that just adds complexity.
     203             :      */
     204          70 :     if ((log = fopen(log_file, "a")) == NULL)
     205           0 :         pg_fatal("could not write to log file \"%s\": %m\n", log_file);
     206          70 :     fprintf(log, "\n\n");
     207          70 :     fclose(log);
     208             : #endif
     209             : 
     210          70 :     return result == 0;
     211             : }
     212             : 
     213             : 
     214             : /*
     215             :  * pid_lock_file_exists()
     216             :  *
     217             :  * Checks whether the postmaster.pid file exists.
     218             :  */
     219             : bool
     220           4 : pid_lock_file_exists(const char *datadir)
     221             : {
     222             :     char        path[MAXPGPATH];
     223             :     int         fd;
     224             : 
     225           4 :     snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
     226             : 
     227           4 :     if ((fd = open(path, O_RDONLY, 0)) < 0)
     228             :     {
     229             :         /* ENOTDIR means we will throw a more useful error later */
     230           4 :         if (errno != ENOENT && errno != ENOTDIR)
     231           0 :             pg_fatal("could not open file \"%s\" for reading: %s\n",
     232           0 :                      path, strerror(errno));
     233             : 
     234           4 :         return false;
     235             :     }
     236             : 
     237           0 :     close(fd);
     238           0 :     return true;
     239             : }
     240             : 
     241             : 
     242             : /*
     243             :  * verify_directories()
     244             :  *
     245             :  * does all the hectic work of verifying directories and executables
     246             :  * of old and new server.
     247             :  *
     248             :  * NOTE: May update the values of all parameters
     249             :  */
     250             : void
     251           2 : verify_directories(void)
     252             : {
     253             : #ifndef WIN32
     254           2 :     if (access(".", R_OK | W_OK | X_OK) != 0)
     255             : #else
     256             :     if (win32_check_directory_write_permissions() != 0)
     257             : #endif
     258           0 :         pg_fatal("You must have read and write access in the current directory.\n");
     259             : 
     260           2 :     check_bin_dir(&old_cluster, false);
     261           2 :     check_data_dir(&old_cluster);
     262           2 :     check_bin_dir(&new_cluster, true);
     263           2 :     check_data_dir(&new_cluster);
     264           2 : }
     265             : 
     266             : 
     267             : #ifdef WIN32
     268             : /*
     269             :  * win32_check_directory_write_permissions()
     270             :  *
     271             :  *  access() on WIN32 can't check directory permissions, so we have to
     272             :  *  optionally create, then delete a file to check.
     273             :  *      http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
     274             :  */
     275             : static int
     276             : win32_check_directory_write_permissions(void)
     277             : {
     278             :     int         fd;
     279             : 
     280             :     /*
     281             :      * We open a file we would normally create anyway.  We do this even in
     282             :      * 'check' mode, which isn't ideal, but this is the best we can do.
     283             :      */
     284             :     if ((fd = open(GLOBALS_DUMP_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
     285             :         return -1;
     286             :     close(fd);
     287             : 
     288             :     return unlink(GLOBALS_DUMP_FILE);
     289             : }
     290             : #endif
     291             : 
     292             : 
     293             : /*
     294             :  * check_single_dir()
     295             :  *
     296             :  *  Check for the presence of a single directory in PGDATA, and fail if
     297             :  * is it missing or not accessible.
     298             :  */
     299             : static void
     300          36 : check_single_dir(const char *pg_data, const char *subdir)
     301             : {
     302             :     struct stat statBuf;
     303             :     char        subDirName[MAXPGPATH];
     304             : 
     305          36 :     snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
     306             :     /* Win32 can't stat() a directory with a trailing slash. */
     307          36 :              *subdir ? "/" : "",
     308             :              subdir);
     309             : 
     310          36 :     if (stat(subDirName, &statBuf) != 0)
     311           0 :         report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
     312           0 :                       subDirName, strerror(errno));
     313          36 :     else if (!S_ISDIR(statBuf.st_mode))
     314           0 :         report_status(PG_FATAL, "\"%s\" is not a directory\n",
     315             :                       subDirName);
     316          36 : }
     317             : 
     318             : 
     319             : /*
     320             :  * check_data_dir()
     321             :  *
     322             :  *  This function validates the given cluster directory - we search for a
     323             :  *  small set of subdirectories that we expect to find in a valid $PGDATA
     324             :  *  directory.  If any of the subdirectories are missing (or secured against
     325             :  *  us) we display an error message and exit()
     326             :  *
     327             :  */
     328             : static void
     329           4 : check_data_dir(ClusterInfo *cluster)
     330             : {
     331           4 :     const char *pg_data = cluster->pgdata;
     332             : 
     333             :     /* get the cluster version */
     334           4 :     cluster->major_version = get_major_server_version(cluster);
     335             : 
     336           4 :     check_single_dir(pg_data, "");
     337           4 :     check_single_dir(pg_data, "base");
     338           4 :     check_single_dir(pg_data, "global");
     339           4 :     check_single_dir(pg_data, "pg_multixact");
     340           4 :     check_single_dir(pg_data, "pg_subtrans");
     341           4 :     check_single_dir(pg_data, "pg_tblspc");
     342           4 :     check_single_dir(pg_data, "pg_twophase");
     343             : 
     344             :     /* pg_xlog has been renamed to pg_wal in v10 */
     345           4 :     if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
     346           0 :         check_single_dir(pg_data, "pg_xlog");
     347             :     else
     348           4 :         check_single_dir(pg_data, "pg_wal");
     349             : 
     350             :     /* pg_clog has been renamed to pg_xact in v10 */
     351           4 :     if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
     352           0 :         check_single_dir(pg_data, "pg_clog");
     353             :     else
     354           4 :         check_single_dir(pg_data, "pg_xact");
     355           4 : }
     356             : 
     357             : 
     358             : /*
     359             :  * check_bin_dir()
     360             :  *
     361             :  *  This function searches for the executables that we expect to find
     362             :  *  in the binaries directory.  If we find that a required executable
     363             :  *  is missing (or secured against us), we display an error message and
     364             :  *  exit().
     365             :  *
     366             :  *  If check_versions is true, then the versions of the binaries are checked
     367             :  *  against the version of this pg_upgrade.  This is for checking the target
     368             :  *  bindir.
     369             :  */
     370             : static void
     371           4 : check_bin_dir(ClusterInfo *cluster, bool check_versions)
     372             : {
     373             :     struct stat statBuf;
     374             : 
     375             :     /* check bindir */
     376           4 :     if (stat(cluster->bindir, &statBuf) != 0)
     377           0 :         report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
     378           0 :                       cluster->bindir, strerror(errno));
     379           4 :     else if (!S_ISDIR(statBuf.st_mode))
     380           0 :         report_status(PG_FATAL, "\"%s\" is not a directory\n",
     381             :                       cluster->bindir);
     382             : 
     383           4 :     check_exec(cluster->bindir, "postgres", check_versions);
     384           4 :     check_exec(cluster->bindir, "pg_controldata", check_versions);
     385           4 :     check_exec(cluster->bindir, "pg_ctl", check_versions);
     386             : 
     387             :     /*
     388             :      * Fetch the binary version after checking for the existence of pg_ctl.
     389             :      * This way we report a useful error if the pg_ctl binary used for version
     390             :      * fetching is missing/broken.
     391             :      */
     392           4 :     get_bin_version(cluster);
     393             : 
     394             :     /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
     395           4 :     if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
     396           0 :         check_exec(cluster->bindir, "pg_resetxlog", check_versions);
     397             :     else
     398           4 :         check_exec(cluster->bindir, "pg_resetwal", check_versions);
     399             : 
     400           4 :     if (cluster == &new_cluster)
     401             :     {
     402             :         /*
     403             :          * These binaries are only needed for the target version. pg_dump and
     404             :          * pg_dumpall are used to dump the old cluster, but must be of the
     405             :          * target version.
     406             :          */
     407           2 :         check_exec(cluster->bindir, "initdb", check_versions);
     408           2 :         check_exec(cluster->bindir, "pg_dump", check_versions);
     409           2 :         check_exec(cluster->bindir, "pg_dumpall", check_versions);
     410           2 :         check_exec(cluster->bindir, "pg_restore", check_versions);
     411           2 :         check_exec(cluster->bindir, "psql", check_versions);
     412           2 :         check_exec(cluster->bindir, "vacuumdb", check_versions);
     413             :     }
     414           4 : }
     415             : 
     416             : static void
     417          28 : check_exec(const char *dir, const char *program, bool check_version)
     418             : {
     419             :     char        path[MAXPGPATH];
     420             :     char        line[MAXPGPATH];
     421             :     char        cmd[MAXPGPATH];
     422             :     char        versionstr[128];
     423             :     int         ret;
     424             : 
     425          28 :     snprintf(path, sizeof(path), "%s/%s", dir, program);
     426             : 
     427          28 :     ret = validate_exec(path);
     428             : 
     429          28 :     if (ret == -1)
     430           0 :         pg_fatal("check for \"%s\" failed: not a regular file\n",
     431             :                  path);
     432          28 :     else if (ret == -2)
     433           0 :         pg_fatal("check for \"%s\" failed: cannot execute (permission denied)\n",
     434             :                  path);
     435             : 
     436          28 :     snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
     437             : 
     438          28 :     if (!pipe_read_line(cmd, line, sizeof(line)))
     439           0 :         pg_fatal("check for \"%s\" failed: cannot execute\n",
     440             :                  path);
     441             : 
     442          28 :     if (check_version)
     443             :     {
     444          20 :         pg_strip_crlf(line);
     445             : 
     446          20 :         snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
     447             : 
     448          20 :         if (strcmp(line, versionstr) != 0)
     449           0 :             pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"\n",
     450             :                      path, line, versionstr);
     451             :     }
     452          28 : }

Generated by: LCOV version 1.14