PostgreSQL Source Code  git master
pg_regress.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include "common/logging.h"
#include "common/restricted_token.h"
#include "common/string.h"
#include "common/username.h"
#include "getopt_long.h"
#include "lib/stringinfo.h"
#include "libpq/pqcomm.h"
#include "pg_config_paths.h"
#include "pg_regress.h"
#include "portability/instr_time.h"
Include dependency graph for pg_regress.c:

Go to the source code of this file.

Data Structures

struct  _resultmap
 

Macros

#define MAX_PARALLEL_TESTS   100
 
#define ULONGPID(x)   (unsigned long) (x)
 

Typedefs

typedef struct _resultmap _resultmap
 

Functions

static bool directory_exists (const char *dir)
 
static void make_directory (const char *dir)
 
static void header (const char *fmt,...) pg_attribute_printf(1
 
static void static void status (const char *fmt,...) pg_attribute_printf(1
 
static void static void static void psql_command (const char *database, const char *query,...) pg_attribute_printf(2
 
static void static void static void void add_stringlist_item (_stringlist **listhead, const char *str)
 
static void free_stringlist (_stringlist **listhead)
 
static void split_to_stringlist (const char *s, const char *delim, _stringlist **listhead)
 
static void status_end (void)
 
static void stop_postmaster (void)
 
static bool string_matches_pattern (const char *str, const char *pattern)
 
void replace_string (StringInfo string, const char *replace, const char *replacement)
 
static void convert_sourcefiles_in (const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
 
static void convert_sourcefiles (void)
 
static void load_resultmap (void)
 
static const char * get_expectfile (const char *testname, const char *file)
 
static void doputenv (const char *var, const char *val)
 
static void initialize_environment (void)
 
PID_TYPE spawn_process (const char *cmdline)
 
static long file_size (const char *file)
 
static int file_line_count (const char *file)
 
bool file_exists (const char *file)
 
static char * get_alternative_expectfile (const char *expectfile, int i)
 
static int run_diff (const char *cmd, const char *filename)
 
static bool results_differ (const char *testname, const char *resultsfile, const char *default_expectfile)
 
static void wait_for_tests (PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
 
static void log_child_failure (int exitstatus)
 
static void run_schedule (const char *schedule, test_function tfunc)
 
static void run_single_test (const char *test, test_function tfunc)
 
static void open_result_files (void)
 
static void drop_database_if_exists (const char *dbname)
 
static void create_database (const char *dbname)
 
static void drop_role_if_exists (const char *rolename)
 
static void create_role (const char *rolename, const _stringlist *granted_dbs)
 
static void help (void)
 
int regression_main (int argc, char *argv[], init_function ifunc, test_function tfunc)
 

Variables

char * host_platform = HOST_TUPLE
 
static char * shellprog = SHELLPROG
 
const char * basic_diff_opts = ""
 
const char * pretty_diff_opts = "-U3"
 
_stringlistdblist = NULL
 
bool debug = false
 
char * inputdir = "."
 
char * outputdir = "."
 
char * bindir = PGBINDIR
 
char * launcher = NULL
 
static _stringlistloadextension = NULL
 
static int max_connections = 0
 
static int max_concurrent_tests = 0
 
static char * encoding = NULL
 
static _stringlistschedulelist = NULL
 
static _stringlistextra_tests = NULL
 
static char * temp_instance = NULL
 
static _stringlisttemp_configs = NULL
 
static bool nolocale = false
 
static bool use_existing = false
 
static char * hostname = NULL
 
static int port = -1
 
static bool port_specified_by_user = false
 
static char * dlpath = PKGLIBDIR
 
static char * user = NULL
 
static _stringlistextraroles = NULL
 
static char * config_auth_datadir = NULL
 
static const char * progname
 
static char * logfilename
 
static FILE * logfile
 
static char * difffilename
 
static const char * sockdir
 
static _resultmapresultmap = NULL
 
static PID_TYPE postmaster_pid = INVALID_PID
 
static bool postmaster_running = false
 
static int success_count = 0
 
static int fail_count = 0
 
static int fail_ignore_count = 0
 

Macro Definition Documentation

◆ MAX_PARALLEL_TESTS

#define MAX_PARALLEL_TESTS   100

Referenced by run_schedule().

◆ ULONGPID

#define ULONGPID (   x)    (unsigned long) (x)

Referenced by regression_main().

Typedef Documentation

◆ _resultmap

typedef struct _resultmap _resultmap

Function Documentation

◆ add_stringlist_item()

static void static void static void void add_stringlist_item ( _stringlist **  listhead,
const char *  str 
)

Definition at line 157 of file pg_regress.c.

References _stringlist::next, pg_malloc(), pg_strdup(), and _stringlist::str.

Referenced by ecpg_start_test(), isolation_init(), isolation_start_test(), psql_init(), psql_start_test(), regression_main(), run_schedule(), and split_to_stringlist().

158 {
159  _stringlist *newentry = pg_malloc(sizeof(_stringlist));
160  _stringlist *oldentry;
161 
162  newentry->str = pg_strdup(str);
163  newentry->next = NULL;
164  if (*listhead == NULL)
165  *listhead = newentry;
166  else
167  {
168  for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
169  /* skip */ ;
170  oldentry->next = newentry;
171  }
172 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * str
Definition: pg_regress.h:26
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct _stringlist * next
Definition: pg_regress.h:27

◆ convert_sourcefiles()

static void convert_sourcefiles ( void  )
static

Definition at line 600 of file pg_regress.c.

References convert_sourcefiles_in(), and outputdir.

Referenced by initialize_environment().

601 {
602  convert_sourcefiles_in("input", outputdir, "sql", "sql");
603  convert_sourcefiles_in("output", outputdir, "expected", "out");
604 }
static void convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
Definition: pg_regress.c:476
char * outputdir
Definition: pg_regress.c:78

◆ convert_sourcefiles_in()

static void convert_sourcefiles_in ( const char *  source_subdir,
const char *  dest_dir,
const char *  dest_subdir,
const char *  suffix 
)
static

Definition at line 476 of file pg_regress.c.

References _, StringInfoData::data, directory_exists(), dlpath, fprintf, infile(), initStringInfo(), inputdir, make_directory(), MAXPGPATH, name, outfile, outputdir, pfree(), pg_get_line_buf(), pgfnames(), pgfnames_cleanup(), progname, replace_string(), rmtree(), snprintf, and strerror.

Referenced by convert_sourcefiles().

477 {
478  char testtablespace[MAXPGPATH];
479  char indir[MAXPGPATH];
480  char outdir_sub[MAXPGPATH];
481  char **name;
482  char **names;
483  int count = 0;
484 
485  snprintf(indir, MAXPGPATH, "%s/%s", inputdir, source_subdir);
486 
487  /* Check that indir actually exists and is a directory */
488  if (!directory_exists(indir))
489  {
490  /*
491  * No warning, to avoid noise in tests that do not have these
492  * directories; for example, ecpg, contrib and src/pl.
493  */
494  return;
495  }
496 
497  names = pgfnames(indir);
498  if (!names)
499  /* Error logged in pgfnames */
500  exit(2);
501 
502  /* Create the "dest" subdirectory if not present */
503  snprintf(outdir_sub, MAXPGPATH, "%s/%s", dest_dir, dest_subdir);
504  if (!directory_exists(outdir_sub))
505  make_directory(outdir_sub);
506 
507  snprintf(testtablespace, MAXPGPATH, "%s/testtablespace", outputdir);
508 
509 #ifdef WIN32
510 
511  /*
512  * On Windows only, clean out the test tablespace dir, or create it if it
513  * doesn't exist so as it is possible to run the regression tests as a
514  * Windows administrative user account with the restricted token obtained
515  * when starting pg_regress. On other platforms we expect the Makefile to
516  * take care of that.
517  */
518  if (directory_exists(testtablespace))
519  if (!rmtree(testtablespace, true))
520  {
521  fprintf(stderr, _("\n%s: could not remove test tablespace \"%s\"\n"),
522  progname, testtablespace);
523  exit(2);
524  }
525  make_directory(testtablespace);
526 #endif
527 
528  /* finally loop on each file and do the replacement */
529  for (name = names; *name; name++)
530  {
531  char srcfile[MAXPGPATH];
532  char destfile[MAXPGPATH];
533  char prefix[MAXPGPATH];
534  FILE *infile,
535  *outfile;
536  StringInfoData line;
537 
538  /* reject filenames not finishing in ".source" */
539  if (strlen(*name) < 8)
540  continue;
541  if (strcmp(*name + strlen(*name) - 7, ".source") != 0)
542  continue;
543 
544  count++;
545 
546  /* build the full actual paths to open */
547  snprintf(prefix, strlen(*name) - 6, "%s", *name);
548  snprintf(srcfile, MAXPGPATH, "%s/%s", indir, *name);
549  snprintf(destfile, MAXPGPATH, "%s/%s/%s.%s", dest_dir, dest_subdir,
550  prefix, suffix);
551 
552  infile = fopen(srcfile, "r");
553  if (!infile)
554  {
555  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
556  progname, srcfile, strerror(errno));
557  exit(2);
558  }
559  outfile = fopen(destfile, "w");
560  if (!outfile)
561  {
562  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
563  progname, destfile, strerror(errno));
564  exit(2);
565  }
566 
567  initStringInfo(&line);
568 
569  while (pg_get_line_buf(infile, &line))
570  {
571  replace_string(&line, "@abs_srcdir@", inputdir);
572  replace_string(&line, "@abs_builddir@", outputdir);
573  replace_string(&line, "@testtablespace@", testtablespace);
574  replace_string(&line, "@libdir@", dlpath);
575  replace_string(&line, "@DLSUFFIX@", DLSUFFIX);
576  fputs(line.data, outfile);
577  }
578 
579  pfree(line.data);
580  fclose(infile);
581  fclose(outfile);
582  }
583 
584  /*
585  * If we didn't process any files, complain because it probably means
586  * somebody neglected to pass the needed --inputdir argument.
587  */
588  if (count <= 0)
589  {
590  fprintf(stderr, _("%s: no *.source files found in \"%s\"\n"),
591  progname, indir);
592  exit(2);
593  }
594 
595  pgfnames_cleanup(names);
596 }
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:88
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1290
char ** pgfnames(const char *path)
Definition: pgfnames.c:37
static const char * progname
Definition: pg_regress.c:100
static void make_directory(const char *dir)
Definition: pg_regress.c:1303
#define fprintf
Definition: port.h:219
void pfree(void *pointer)
Definition: mcxt.c:1057
#define MAXPGPATH
static char * dlpath
Definition: pg_regress.c:94
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
char * outputdir
Definition: pg_regress.c:78
char * inputdir
Definition: pg_regress.c:77
static void infile(const char *filename)
Definition: zic.c:1231
void pgfnames_cleanup(char **filenames)
Definition: pgfnames.c:86
#define strerror
Definition: port.h:228
const char * name
Definition: encode.c:561
void replace_string(StringInfo string, const char *replace, const char *replacement)
Definition: pg_regress.c:447
static char * outfile
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ create_database()

static void create_database ( const char *  dbname)
static

Definition at line 1993 of file pg_regress.c.

References _, encoding, header(), _stringlist::next, nolocale, psql_command(), and _stringlist::str.

Referenced by regression_main().

1994 {
1995  _stringlist *sl;
1996 
1997  /*
1998  * We use template0 so that any installation-local cruft in template1 will
1999  * not mess up the tests.
2000  */
2001  header(_("creating database \"%s\""), dbname);
2002  if (encoding)
2003  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
2004  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
2005  else
2006  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
2007  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
2009  "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
2010  "ALTER DATABASE \"%s\" SET lc_monetary TO 'C';"
2011  "ALTER DATABASE \"%s\" SET lc_numeric TO 'C';"
2012  "ALTER DATABASE \"%s\" SET lc_time TO 'C';"
2013  "ALTER DATABASE \"%s\" SET bytea_output TO 'hex';"
2014  "ALTER DATABASE \"%s\" SET timezone_abbreviations TO 'Default';",
2016 
2017  /*
2018  * Install any requested extensions. We use CREATE IF NOT EXISTS so that
2019  * this will work whether or not the extension is preinstalled.
2020  */
2021  for (sl = loadextension; sl != NULL; sl = sl->next)
2022  {
2023  header(_("installing %s"), sl->str);
2024  psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
2025  }
2026 }
char * str
Definition: pg_regress.h:26
static char * encoding
Definition: pg_regress.c:84
static _stringlist * loadextension
Definition: pg_regress.c:81
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1123
char * dbname
Definition: streamutil.c:51
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
struct _stringlist * next
Definition: pg_regress.h:27
static bool nolocale
Definition: pg_regress.c:89
#define _(x)
Definition: elog.c:88

◆ create_role()

static void create_role ( const char *  rolename,
const _stringlist granted_dbs 
)
static

Definition at line 2036 of file pg_regress.c.

References _, header(), _stringlist::next, psql_command(), and _stringlist::str.

Referenced by regression_main().

2037 {
2038  header(_("creating role \"%s\""), rolename);
2039  psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
2040  for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
2041  {
2042  psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
2043  granted_dbs->str, rolename);
2044  }
2045 }
char * str
Definition: pg_regress.h:26
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1123
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
struct _stringlist * next
Definition: pg_regress.h:27
#define _(x)
Definition: elog.c:88

◆ directory_exists()

static bool directory_exists ( const char *  dir)
static

Definition at line 1290 of file pg_regress.c.

References S_ISDIR, stat::st_mode, and stat.

Referenced by convert_sourcefiles_in(), open_result_files(), and regression_main().

1291 {
1292  struct stat st;
1293 
1294  if (stat(dir, &st) != 0)
1295  return false;
1296  if (S_ISDIR(st.st_mode))
1297  return true;
1298  return false;
1299 }
#define S_ISDIR(m)
Definition: win32_port.h:316
#define stat
Definition: win32_port.h:275

◆ doputenv()

static void doputenv ( const char *  var,
const char *  val 
)
static

Definition at line 731 of file pg_regress.c.

References psprintf(), and putenv.

Referenced by initialize_environment(), and regression_main().

732 {
733  char *s;
734 
735  s = psprintf("%s=%s", var, val);
736  putenv(s);
737 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define putenv(x)
Definition: win32_port.h:502
long val
Definition: informix.c:664

◆ drop_database_if_exists()

static void drop_database_if_exists ( const char *  dbname)
static

Definition at line 1986 of file pg_regress.c.

References _, header(), and psql_command().

Referenced by regression_main().

1987 {
1988  header(_("dropping database \"%s\""), dbname);
1989  psql_command("postgres", "DROP DATABASE IF EXISTS \"%s\"", dbname);
1990 }
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1123
char * dbname
Definition: streamutil.c:51
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
#define _(x)
Definition: elog.c:88

◆ drop_role_if_exists()

static void drop_role_if_exists ( const char *  rolename)
static

Definition at line 2029 of file pg_regress.c.

References _, header(), and psql_command().

Referenced by regression_main().

2030 {
2031  header(_("dropping role \"%s\""), rolename);
2032  psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
2033 }
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1123
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
#define _(x)
Definition: elog.c:88

◆ file_exists()

bool file_exists ( const char *  file)

Definition at line 1279 of file pg_regress.c.

Referenced by results_differ().

1280 {
1281  FILE *f = fopen(file, "r");
1282 
1283  if (!f)
1284  return false;
1285  fclose(f);
1286  return true;
1287 }

◆ file_line_count()

static int file_line_count ( const char *  file)
static

Definition at line 1257 of file pg_regress.c.

References _, fprintf, progname, and strerror.

Referenced by results_differ().

1258 {
1259  int c;
1260  int l = 0;
1261  FILE *f = fopen(file, "r");
1262 
1263  if (!f)
1264  {
1265  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1266  progname, file, strerror(errno));
1267  return -1;
1268  }
1269  while ((c = fgetc(f)) != EOF)
1270  {
1271  if (c == '\n')
1272  l++;
1273  }
1274  fclose(f);
1275  return l;
1276 }
static const char * progname
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:219
char * c
#define strerror
Definition: port.h:228
#define _(x)
Definition: elog.c:88

◆ file_size()

static long file_size ( const char *  file)
static

Definition at line 1236 of file pg_regress.c.

References _, fprintf, progname, and strerror.

Referenced by regression_main(), and run_diff().

1237 {
1238  long r;
1239  FILE *f = fopen(file, "r");
1240 
1241  if (!f)
1242  {
1243  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1244  progname, file, strerror(errno));
1245  return -1;
1246  }
1247  fseek(f, 0, SEEK_END);
1248  r = ftell(f);
1249  fclose(f);
1250  return r;
1251 }
static const char * progname
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:219
#define strerror
Definition: port.h:228
#define _(x)
Definition: elog.c:88

◆ free_stringlist()

static void free_stringlist ( _stringlist **  listhead)
static

Definition at line 178 of file pg_regress.c.

References free.

Referenced by regression_main(), and run_schedule().

179 {
180  if (listhead == NULL || *listhead == NULL)
181  return;
182  if ((*listhead)->next != NULL)
183  free_stringlist(&((*listhead)->next));
184  free((*listhead)->str);
185  free(*listhead);
186  *listhead = NULL;
187 }
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:178
#define free(a)
Definition: header.h:65

◆ get_alternative_expectfile()

static char* get_alternative_expectfile ( const char *  expectfile,
int  i 
)
static

Definition at line 1317 of file pg_regress.c.

References free, malloc, and snprintf.

Referenced by results_differ().

1318 {
1319  char *last_dot;
1320  int ssize = strlen(expectfile) + 2 + 1;
1321  char *tmp;
1322  char *s;
1323 
1324  if (!(tmp = (char *) malloc(ssize)))
1325  return NULL;
1326 
1327  if (!(s = (char *) malloc(ssize)))
1328  {
1329  free(tmp);
1330  return NULL;
1331  }
1332 
1333  strcpy(tmp, expectfile);
1334  last_dot = strrchr(tmp, '.');
1335  if (!last_dot)
1336  {
1337  free(tmp);
1338  free(s);
1339  return NULL;
1340  }
1341  *last_dot = '\0';
1342  snprintf(s, ssize, "%s_%d.%s", tmp, i, last_dot + 1);
1343  free(tmp);
1344  return s;
1345 }
#define malloc(a)
Definition: header.h:50
#define free(a)
Definition: header.h:65
int i
#define snprintf
Definition: port.h:215

◆ get_expectfile()

static const char* get_expectfile ( const char *  testname,
const char *  file 
)
static

Definition at line 702 of file pg_regress.c.

References _resultmap::next, _resultmap::resultfile, _resultmap::test, and _resultmap::type.

Referenced by results_differ().

703 {
704  char *file_type;
705  _resultmap *rm;
706 
707  /*
708  * Determine the file type from the file name. This is just what is
709  * following the last dot in the file name.
710  */
711  if (!file || !(file_type = strrchr(file, '.')))
712  return NULL;
713 
714  file_type++;
715 
716  for (rm = resultmap; rm != NULL; rm = rm->next)
717  {
718  if (strcmp(testname, rm->test) == 0 && strcmp(file_type, rm->type) == 0)
719  {
720  return rm->resultfile;
721  }
722  }
723 
724  return NULL;
725 }
struct _resultmap * next
Definition: pg_regress.c:49
static _resultmap * resultmap
Definition: pg_regress.c:111
char * test
Definition: pg_regress.c:46
char * type
Definition: pg_regress.c:47
char * resultfile
Definition: pg_regress.c:48

◆ header()

static void header ( const char *  fmt,
  ... 
)
static

◆ help()

static void help ( void  )
static

Definition at line 2048 of file pg_regress.c.

References _, printf, and progname.

Referenced by regression_main().

2049 {
2050  printf(_("PostgreSQL regression test driver\n"));
2051  printf(_("\n"));
2052  printf(_("Usage:\n %s [OPTION]... [EXTRA-TEST]...\n"), progname);
2053  printf(_("\n"));
2054  printf(_("Options:\n"));
2055  printf(_(" --bindir=BINPATH use BINPATH for programs that are run;\n"));
2056  printf(_(" if empty, use PATH from the environment\n"));
2057  printf(_(" --config-auth=DATADIR update authentication settings for DATADIR\n"));
2058  printf(_(" --create-role=ROLE create the specified role before testing\n"));
2059  printf(_(" --dbname=DB use database DB (default \"regression\")\n"));
2060  printf(_(" --debug turn on debug mode in programs that are run\n"));
2061  printf(_(" --dlpath=DIR look for dynamic libraries in DIR\n"));
2062  printf(_(" --encoding=ENCODING use ENCODING as the encoding\n"));
2063  printf(_(" -h, --help show this help, then exit\n"));
2064  printf(_(" --inputdir=DIR take input files from DIR (default \".\")\n"));
2065  printf(_(" --launcher=CMD use CMD as launcher of psql\n"));
2066  printf(_(" --load-extension=EXT load the named extension before running the\n"));
2067  printf(_(" tests; can appear multiple times\n"));
2068  printf(_(" --max-connections=N maximum number of concurrent connections\n"));
2069  printf(_(" (default is 0, meaning unlimited)\n"));
2070  printf(_(" --max-concurrent-tests=N maximum number of concurrent tests in schedule\n"));
2071  printf(_(" (default is 0, meaning unlimited)\n"));
2072  printf(_(" --outputdir=DIR place output files in DIR (default \".\")\n"));
2073  printf(_(" --schedule=FILE use test ordering schedule from FILE\n"));
2074  printf(_(" (can be used multiple times to concatenate)\n"));
2075  printf(_(" --temp-instance=DIR create a temporary instance in DIR\n"));
2076  printf(_(" --use-existing use an existing installation\n"));
2077  printf(_(" -V, --version output version information, then exit\n"));
2078  printf(_("\n"));
2079  printf(_("Options for \"temp-instance\" mode:\n"));
2080  printf(_(" --no-locale use C locale\n"));
2081  printf(_(" --port=PORT start postmaster on PORT\n"));
2082  printf(_(" --temp-config=FILE append contents of FILE to temporary config\n"));
2083  printf(_("\n"));
2084  printf(_("Options for using an existing installation:\n"));
2085  printf(_(" --host=HOST use postmaster running on HOST\n"));
2086  printf(_(" --port=PORT use postmaster running at PORT\n"));
2087  printf(_(" --user=USER connect as USER\n"));
2088  printf(_("\n"));
2089  printf(_("The exit status is 0 if all tests passed, 1 if some tests failed, and 2\n"));
2090  printf(_("if the tests could not be run for some reason.\n"));
2091  printf(_("\n"));
2092  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2093  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2094 }
static const char * progname
Definition: pg_regress.c:100
#define printf(...)
Definition: port.h:221
#define _(x)
Definition: elog.c:88

◆ initialize_environment()

static void initialize_environment ( void  )
static

Definition at line 743 of file pg_regress.c.

References _, addrinfo::ai_addr, addrinfo::ai_addrlen, addrinfo::ai_canonname, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_NUMERICHOST, addrinfo::ai_protocol, addrinfo::ai_socktype, Assert, convert_sourcefiles(), doputenv(), encoding, fprintf, free, get_user_name(), getaddrinfo, hostname, load_resultmap(), MAXPGPATH, _stringlist::next, nolocale, pg_malloc(), pghost, pgport, port, printf, progname, psprintf(), putenv, realloc, snprintf, sockdir, sprintf, _stringlist::str, strerror, temp_instance, unsetenv, and user.

Referenced by regression_main().

744 {
745  /*
746  * Set default application_name. (The test_function may choose to
747  * override this, but if it doesn't, we have something useful in place.)
748  */
749  putenv("PGAPPNAME=pg_regress");
750 
751  if (nolocale)
752  {
753  /*
754  * Clear out any non-C locale settings
755  */
756  unsetenv("LC_COLLATE");
757  unsetenv("LC_CTYPE");
758  unsetenv("LC_MONETARY");
759  unsetenv("LC_NUMERIC");
760  unsetenv("LC_TIME");
761  unsetenv("LANG");
762 
763  /*
764  * Most platforms have adopted the POSIX locale as their
765  * implementation-defined default locale. Exceptions include native
766  * Windows, macOS with --enable-nls, and Cygwin with --enable-nls.
767  * (Use of --enable-nls matters because libintl replaces setlocale().)
768  * Also, PostgreSQL does not support macOS with locale environment
769  * variables unset; see PostmasterMain().
770  */
771 #if defined(WIN32) || defined(__CYGWIN__) || defined(__darwin__)
772  putenv("LANG=C");
773 #endif
774  }
775 
776  /*
777  * Set translation-related settings to English; otherwise psql will
778  * produce translated messages and produce diffs. (XXX If we ever support
779  * translation of pg_regress, this needs to be moved elsewhere, where psql
780  * is actually called.)
781  */
782  unsetenv("LANGUAGE");
783  unsetenv("LC_ALL");
784  putenv("LC_MESSAGES=C");
785 
786  /*
787  * Set encoding as requested
788  */
789  if (encoding)
790  doputenv("PGCLIENTENCODING", encoding);
791  else
792  unsetenv("PGCLIENTENCODING");
793 
794  /*
795  * Set timezone and datestyle for datetime-related tests
796  */
797  putenv("PGTZ=PST8PDT");
798  putenv("PGDATESTYLE=Postgres, MDY");
799 
800  /*
801  * Likewise set intervalstyle to ensure consistent results. This is a bit
802  * more painful because we must use PGOPTIONS, and we want to preserve the
803  * user's ability to set other variables through that.
804  */
805  {
806  const char *my_pgoptions = "-c intervalstyle=postgres_verbose";
807  const char *old_pgoptions = getenv("PGOPTIONS");
808  char *new_pgoptions;
809 
810  if (!old_pgoptions)
811  old_pgoptions = "";
812  new_pgoptions = psprintf("PGOPTIONS=%s %s",
813  old_pgoptions, my_pgoptions);
814  putenv(new_pgoptions);
815  }
816 
817  if (temp_instance)
818  {
819  /*
820  * Clear out any environment vars that might cause psql to connect to
821  * the wrong postmaster, or otherwise behave in nondefault ways. (Note
822  * we also use psql's -X switch consistently, so that ~/.psqlrc files
823  * won't mess things up.) Also, set PGPORT to the temp port, and set
824  * PGHOST depending on whether we are using TCP or Unix sockets.
825  */
826  unsetenv("PGDATABASE");
827  unsetenv("PGUSER");
828  unsetenv("PGSERVICE");
829  unsetenv("PGSSLMODE");
830  unsetenv("PGREQUIRESSL");
831  unsetenv("PGCONNECT_TIMEOUT");
832  unsetenv("PGDATA");
833 #ifdef HAVE_UNIX_SOCKETS
834  if (hostname != NULL)
835  doputenv("PGHOST", hostname);
836  else
837  {
838  sockdir = getenv("PG_REGRESS_SOCK_DIR");
839  if (!sockdir)
840  sockdir = make_temp_sockdir();
841  doputenv("PGHOST", sockdir);
842  }
843 #else
844  Assert(hostname != NULL);
845  doputenv("PGHOST", hostname);
846 #endif
847  unsetenv("PGHOSTADDR");
848  if (port != -1)
849  {
850  char s[16];
851 
852  sprintf(s, "%d", port);
853  doputenv("PGPORT", s);
854  }
855  }
856  else
857  {
858  const char *pghost;
859  const char *pgport;
860 
861  /*
862  * When testing an existing install, we honor existing environment
863  * variables, except if they're overridden by command line options.
864  */
865  if (hostname != NULL)
866  {
867  doputenv("PGHOST", hostname);
868  unsetenv("PGHOSTADDR");
869  }
870  if (port != -1)
871  {
872  char s[16];
873 
874  sprintf(s, "%d", port);
875  doputenv("PGPORT", s);
876  }
877  if (user != NULL)
878  doputenv("PGUSER", user);
879 
880  /*
881  * However, we *don't* honor PGDATABASE, since we certainly don't wish
882  * to connect to whatever database the user might like as default.
883  * (Most tests override PGDATABASE anyway, but there are some ECPG
884  * test cases that don't.)
885  */
886  unsetenv("PGDATABASE");
887 
888  /*
889  * Report what we're connecting to
890  */
891  pghost = getenv("PGHOST");
892  pgport = getenv("PGPORT");
893 #ifndef HAVE_UNIX_SOCKETS
894  if (!pghost)
895  pghost = "localhost";
896 #endif
897 
898  if (pghost && pgport)
899  printf(_("(using postmaster on %s, port %s)\n"), pghost, pgport);
900  if (pghost && !pgport)
901  printf(_("(using postmaster on %s, default port)\n"), pghost);
902  if (!pghost && pgport)
903  printf(_("(using postmaster on Unix socket, port %s)\n"), pgport);
904  if (!pghost && !pgport)
905  printf(_("(using postmaster on Unix socket, default port)\n"));
906  }
907 
909  load_resultmap();
910 }
static const char * sockdir
Definition: pg_regress.c:104
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * encoding
Definition: pg_regress.c:84
#define printf(...)
Definition: port.h:221
static char * temp_instance
Definition: pg_regress.c:87
static void convert_sourcefiles(void)
Definition: pg_regress.c:600
#define sprintf
Definition: port.h:217
static int port
Definition: pg_regress.c:92
char * pghost
Definition: pgbench.c:243
#define Assert(condition)
Definition: c.h:746
static void load_resultmap(void)
Definition: pg_regress.c:620
#define putenv(x)
Definition: win32_port.h:502
static void doputenv(const char *var, const char *val)
Definition: pg_regress.c:731
static char * user
Definition: pg_regress.c:95
static bool nolocale
Definition: pg_regress.c:89
static char * hostname
Definition: pg_regress.c:91
#define _(x)
Definition: elog.c:88
char * pgport
Definition: pgbench.c:244
#define unsetenv(x)
Definition: win32_port.h:503

◆ load_resultmap()

static void load_resultmap ( void  )
static

Definition at line 620 of file pg_regress.c.

References _, buf, fprintf, host_platform, i, inputdir, MAXPGPATH, _resultmap::next, pg_malloc(), pg_strdup(), progname, _resultmap::resultfile, resultmap, snprintf, strerror, string_matches_pattern(), _resultmap::test, and _resultmap::type.

Referenced by initialize_environment().

621 {
622  char buf[MAXPGPATH];
623  FILE *f;
624 
625  /* scan the file ... */
626  snprintf(buf, sizeof(buf), "%s/resultmap", inputdir);
627  f = fopen(buf, "r");
628  if (!f)
629  {
630  /* OK if it doesn't exist, else complain */
631  if (errno == ENOENT)
632  return;
633  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
634  progname, buf, strerror(errno));
635  exit(2);
636  }
637 
638  while (fgets(buf, sizeof(buf), f))
639  {
640  char *platform;
641  char *file_type;
642  char *expected;
643  int i;
644 
645  /* strip trailing whitespace, especially the newline */
646  i = strlen(buf);
647  while (i > 0 && isspace((unsigned char) buf[i - 1]))
648  buf[--i] = '\0';
649 
650  /* parse out the line fields */
651  file_type = strchr(buf, ':');
652  if (!file_type)
653  {
654  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
655  buf);
656  exit(2);
657  }
658  *file_type++ = '\0';
659 
660  platform = strchr(file_type, ':');
661  if (!platform)
662  {
663  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
664  buf);
665  exit(2);
666  }
667  *platform++ = '\0';
668  expected = strchr(platform, '=');
669  if (!expected)
670  {
671  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
672  buf);
673  exit(2);
674  }
675  *expected++ = '\0';
676 
677  /*
678  * if it's for current platform, save it in resultmap list. Note: by
679  * adding at the front of the list, we ensure that in ambiguous cases,
680  * the last match in the resultmap file is used. This mimics the
681  * behavior of the old shell script.
682  */
683  if (string_matches_pattern(host_platform, platform))
684  {
685  _resultmap *entry = pg_malloc(sizeof(_resultmap));
686 
687  entry->test = pg_strdup(buf);
688  entry->type = pg_strdup(file_type);
689  entry->resultfile = pg_strdup(expected);
690  entry->next = resultmap;
691  resultmap = entry;
692  }
693  }
694  fclose(f);
695 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * host_platform
Definition: pg_regress.c:55
static const char * progname
Definition: pg_regress.c:100
struct _resultmap * next
Definition: pg_regress.c:49
static bool string_matches_pattern(const char *str, const char *pattern)
Definition: pg_regress.c:379
#define fprintf
Definition: port.h:219
static _resultmap * resultmap
Definition: pg_regress.c:111
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * test
Definition: pg_regress.c:46
char * type
Definition: pg_regress.c:47
char * inputdir
Definition: pg_regress.c:77
#define strerror
Definition: port.h:228
char * resultfile
Definition: pg_regress.c:48
int i
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ log_child_failure()

static void log_child_failure ( int  exitstatus)
static

Definition at line 1606 of file pg_regress.c.

References _, pg_strsignal(), status(), WEXITSTATUS, WIFEXITED, WIFSIGNALED, and WTERMSIG.

Referenced by run_schedule(), and run_single_test().

1607 {
1608  if (WIFEXITED(exitstatus))
1609  status(_(" (test process exited with exit code %d)"),
1610  WEXITSTATUS(exitstatus));
1611  else if (WIFSIGNALED(exitstatus))
1612  {
1613 #if defined(WIN32)
1614  status(_(" (test process was terminated by exception 0x%X)"),
1615  WTERMSIG(exitstatus));
1616 #else
1617  status(_(" (test process was terminated by signal %d: %s)"),
1618  WTERMSIG(exitstatus), pg_strsignal(WTERMSIG(exitstatus)));
1619 #endif
1620  }
1621  else
1622  status(_(" (test process exited with unrecognized status %d)"),
1623  exitstatus);
1624 }
#define WTERMSIG(w)
Definition: win32_port.h:146
#define WIFEXITED(w)
Definition: win32_port.h:143
const char * pg_strsignal(int signum)
Definition: pgstrsignal.c:42
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define WIFSIGNALED(w)
Definition: win32_port.h:144
#define _(x)
Definition: elog.c:88
#define WEXITSTATUS(w)
Definition: win32_port.h:145

◆ make_directory()

static void make_directory ( const char *  dir)
static

Definition at line 1303 of file pg_regress.c.

References _, fprintf, mkdir, progname, S_IRWXG, S_IRWXO, S_IRWXU, and strerror.

Referenced by convert_sourcefiles_in(), open_result_files(), and regression_main().

1304 {
1305  if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
1306  {
1307  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
1308  progname, dir, strerror(errno));
1309  exit(2);
1310  }
1311 }
static const char * progname
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:219
#define S_IRWXG
Definition: win32_port.h:301
#define strerror
Definition: port.h:228
#define S_IRWXU
Definition: win32_port.h:289
#define mkdir(a, b)
Definition: win32_port.h:63
#define _(x)
Definition: elog.c:88
#define S_IRWXO
Definition: win32_port.h:313

◆ open_result_files()

static void open_result_files ( void  )
static

Definition at line 1946 of file pg_regress.c.

References _, difffilename, directory_exists(), fprintf, logfile, logfilename, make_directory(), MAXPGPATH, outputdir, pg_strdup(), progname, snprintf, and strerror.

Referenced by regression_main().

1947 {
1948  char file[MAXPGPATH];
1949  FILE *difffile;
1950 
1951  /* create outputdir directory if not present */
1954 
1955  /* create the log file (copy of running status output) */
1956  snprintf(file, sizeof(file), "%s/regression.out", outputdir);
1957  logfilename = pg_strdup(file);
1958  logfile = fopen(logfilename, "w");
1959  if (!logfile)
1960  {
1961  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1962  progname, logfilename, strerror(errno));
1963  exit(2);
1964  }
1965 
1966  /* create the diffs file as empty */
1967  snprintf(file, sizeof(file), "%s/regression.diffs", outputdir);
1968  difffilename = pg_strdup(file);
1969  difffile = fopen(difffilename, "w");
1970  if (!difffile)
1971  {
1972  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1973  progname, difffilename, strerror(errno));
1974  exit(2);
1975  }
1976  /* we don't keep the diffs file open continuously */
1977  fclose(difffile);
1978 
1979  /* also create the results directory if not present */
1980  snprintf(file, sizeof(file), "%s/results", outputdir);
1981  if (!directory_exists(file))
1982  make_directory(file);
1983 }
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1290
static const char * progname
Definition: pg_regress.c:100
static void make_directory(const char *dir)
Definition: pg_regress.c:1303
static FILE * logfile
Definition: pg_regress.c:102
#define fprintf
Definition: port.h:219
static char * difffilename
Definition: pg_regress.c:103
#define MAXPGPATH
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * outputdir
Definition: pg_regress.c:78
static char * logfilename
Definition: pg_regress.c:101
#define strerror
Definition: port.h:228
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ psql_command()

static void psql_command ( const char *  database,
const char *  query,
  ... 
)
static

Definition at line 1123 of file pg_regress.c.

References _, generate_unaccent_rules::args, bindir, fprintf, MAXPGPATH, PID_TYPE, snprintf, and vsnprintf.

Referenced by create_database(), create_role(), drop_database_if_exists(), and drop_role_if_exists().

1124 {
1125  char query_formatted[1024];
1126  char query_escaped[2048];
1127  char psql_cmd[MAXPGPATH + 2048];
1128  va_list args;
1129  char *s;
1130  char *d;
1131 
1132  /* Generate the query with insertion of sprintf arguments */
1133  va_start(args, query);
1134  vsnprintf(query_formatted, sizeof(query_formatted), query, args);
1135  va_end(args);
1136 
1137  /* Now escape any shell double-quote metacharacters */
1138  d = query_escaped;
1139  for (s = query_formatted; *s; s++)
1140  {
1141  if (strchr("\\\"$`", *s))
1142  *d++ = '\\';
1143  *d++ = *s;
1144  }
1145  *d = '\0';
1146 
1147  /* And now we can build and execute the shell command */
1148  snprintf(psql_cmd, sizeof(psql_cmd),
1149  "\"%s%spsql\" -X -c \"%s\" \"%s\"",
1150  bindir ? bindir : "",
1151  bindir ? "/" : "",
1152  query_escaped,
1153  database);
1154 
1155  if (system(psql_cmd) != 0)
1156  {
1157  /* psql probably already reported the error */
1158  fprintf(stderr, _("command failed: %s\n"), psql_cmd);
1159  exit(2);
1160  }
1161 }
#define fprintf
Definition: port.h:219
#define vsnprintf
Definition: port.h:214
#define MAXPGPATH
char * bindir
Definition: pg_regress.c:79
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ regression_main()

int regression_main ( int  argc,
char *  argv[],
init_function  ifunc,
test_function  tfunc 
)

Definition at line 2097 of file pg_regress.c.

References _, add_stringlist_item(), bindir, buf, config_auth_datadir, create_database(), create_role(), debug, DEVNULL, difffilename, directory_exists(), dlpath, doputenv(), drop_database_if_exists(), drop_role_if_exists(), encoding, fail_count, fail_ignore_count, file_size(), fprintf, free_stringlist(), get_progname(), get_restricted_token(), getopt_long(), header(), help(), hostname, i, initialize_environment(), inputdir, INVALID_PID, kill, launcher, logfile, logfilename, make_absolute_path(), make_directory(), max_concurrent_tests, max_connections, MAXPGPATH, _stringlist::next, no_argument, nolocale, open_result_files(), optarg, optind, outputdir, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), port, port_specified_by_user, postmaster_pid, postmaster_running, pretty_diff_opts, printf, progname, required_argument, rmtree(), run_schedule(), run_single_test(), set_pglocale_pgservice(), SIGKILL, snprintf, sockdir, spawn_process(), split_to_stringlist(), sprintf, stop_postmaster(), _stringlist::str, strerror, success_count, temp_instance, true, ULONGPID, use_existing, user, and wait_seconds.

Referenced by main().

2098 {
2099  static struct option long_options[] = {
2100  {"help", no_argument, NULL, 'h'},
2101  {"version", no_argument, NULL, 'V'},
2102  {"dbname", required_argument, NULL, 1},
2103  {"debug", no_argument, NULL, 2},
2104  {"inputdir", required_argument, NULL, 3},
2105  {"max-connections", required_argument, NULL, 5},
2106  {"encoding", required_argument, NULL, 6},
2107  {"outputdir", required_argument, NULL, 7},
2108  {"schedule", required_argument, NULL, 8},
2109  {"temp-instance", required_argument, NULL, 9},
2110  {"no-locale", no_argument, NULL, 10},
2111  {"host", required_argument, NULL, 13},
2112  {"port", required_argument, NULL, 14},
2113  {"user", required_argument, NULL, 15},
2114  {"bindir", required_argument, NULL, 16},
2115  {"dlpath", required_argument, NULL, 17},
2116  {"create-role", required_argument, NULL, 18},
2117  {"temp-config", required_argument, NULL, 19},
2118  {"use-existing", no_argument, NULL, 20},
2119  {"launcher", required_argument, NULL, 21},
2120  {"load-extension", required_argument, NULL, 22},
2121  {"config-auth", required_argument, NULL, 24},
2122  {"max-concurrent-tests", required_argument, NULL, 25},
2123  {NULL, 0, NULL, 0}
2124  };
2125 
2126  bool use_unix_sockets;
2127  _stringlist *sl;
2128  int c;
2129  int i;
2130  int option_index;
2131  char buf[MAXPGPATH * 4];
2132  char buf2[MAXPGPATH * 4];
2133 
2134  pg_logging_init(argv[0]);
2135  progname = get_progname(argv[0]);
2136  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_regress"));
2137 
2139 
2140  atexit(stop_postmaster);
2141 
2142 #if !defined(HAVE_UNIX_SOCKETS)
2143  use_unix_sockets = false;
2144 #elif defined(WIN32)
2145 
2146  /*
2147  * We don't use Unix-domain sockets on Windows by default, even if the
2148  * build supports them. (See comment at remove_temp() for a reason.)
2149  * Override at your own risk.
2150  */
2151  use_unix_sockets = getenv("PG_TEST_USE_UNIX_SOCKETS") ? true : false;
2152 #else
2153  use_unix_sockets = true;
2154 #endif
2155 
2156  if (!use_unix_sockets)
2157  hostname = "localhost";
2158 
2159  /*
2160  * We call the initialization function here because that way we can set
2161  * default parameters and let them be overwritten by the commandline.
2162  */
2163  ifunc(argc, argv);
2164 
2165  if (getenv("PG_REGRESS_DIFF_OPTS"))
2166  pretty_diff_opts = getenv("PG_REGRESS_DIFF_OPTS");
2167 
2168  while ((c = getopt_long(argc, argv, "hV", long_options, &option_index)) != -1)
2169  {
2170  switch (c)
2171  {
2172  case 'h':
2173  help();
2174  exit(0);
2175  case 'V':
2176  puts("pg_regress (PostgreSQL) " PG_VERSION);
2177  exit(0);
2178  case 1:
2179 
2180  /*
2181  * If a default database was specified, we need to remove it
2182  * before we add the specified one.
2183  */
2186  break;
2187  case 2:
2188  debug = true;
2189  break;
2190  case 3:
2192  break;
2193  case 5:
2194  max_connections = atoi(optarg);
2195  break;
2196  case 6:
2198  break;
2199  case 7:
2201  break;
2202  case 8:
2204  break;
2205  case 9:
2207  break;
2208  case 10:
2209  nolocale = true;
2210  break;
2211  case 13:
2213  break;
2214  case 14:
2215  port = atoi(optarg);
2216  port_specified_by_user = true;
2217  break;
2218  case 15:
2219  user = pg_strdup(optarg);
2220  break;
2221  case 16:
2222  /* "--bindir=" means to use PATH */
2223  if (strlen(optarg))
2224  bindir = pg_strdup(optarg);
2225  else
2226  bindir = NULL;
2227  break;
2228  case 17:
2229  dlpath = pg_strdup(optarg);
2230  break;
2231  case 18:
2233  break;
2234  case 19:
2236  break;
2237  case 20:
2238  use_existing = true;
2239  break;
2240  case 21:
2242  break;
2243  case 22:
2245  break;
2246  case 24:
2248  break;
2249  case 25:
2250  max_concurrent_tests = atoi(optarg);
2251  break;
2252  default:
2253  /* getopt_long already emitted a complaint */
2254  fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
2255  progname);
2256  exit(2);
2257  }
2258  }
2259 
2260  /*
2261  * if we still have arguments, they are extra tests to run
2262  */
2263  while (argc - optind >= 1)
2264  {
2266  optind++;
2267  }
2268 
2269  if (config_auth_datadir)
2270  {
2271 #ifdef ENABLE_SSPI
2272  if (!use_unix_sockets)
2273  config_sspi_auth(config_auth_datadir, user);
2274 #endif
2275  exit(0);
2276  }
2277 
2279 
2280  /*
2281  * To reduce chances of interference with parallel installations, use
2282  * a port number starting in the private range (49152-65535)
2283  * calculated from the version number. This aids !HAVE_UNIX_SOCKETS
2284  * systems; elsewhere, the use of a private socket directory already
2285  * prevents interference.
2286  */
2287  port = 0xC000 | (PG_VERSION_NUM & 0x3FFF);
2288 
2292 
2293  /*
2294  * Initialization
2295  */
2297 
2299 
2300 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
2301  unlimit_core_size();
2302 #endif
2303 
2304  if (temp_instance)
2305  {
2306  FILE *pg_conf;
2307  const char *env_wait;
2308  int wait_seconds;
2309 
2310  /*
2311  * Prepare the temp instance
2312  */
2313 
2315  {
2316  header(_("removing existing temp instance"));
2317  if (!rmtree(temp_instance, true))
2318  {
2319  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2321  exit(2);
2322  }
2323  }
2324 
2325  header(_("creating temporary instance"));
2326 
2327  /* make the temp instance top directory */
2329 
2330  /* and a directory for log files */
2331  snprintf(buf, sizeof(buf), "%s/log", outputdir);
2332  if (!directory_exists(buf))
2333  make_directory(buf);
2334 
2335  /* initdb */
2336  header(_("initializing database system"));
2337  snprintf(buf, sizeof(buf),
2338  "\"%s%sinitdb\" -D \"%s/data\" --no-clean --no-sync%s%s > \"%s/log/initdb.log\" 2>&1",
2339  bindir ? bindir : "",
2340  bindir ? "/" : "",
2341  temp_instance,
2342  debug ? " --debug" : "",
2343  nolocale ? " --no-locale" : "",
2344  outputdir);
2345  if (system(buf))
2346  {
2347  fprintf(stderr, _("\n%s: initdb failed\nExamine %s/log/initdb.log for the reason.\nCommand was: %s\n"), progname, outputdir, buf);
2348  exit(2);
2349  }
2350 
2351  /*
2352  * Adjust the default postgresql.conf for regression testing. The user
2353  * can specify a file to be appended; in any case we expand logging
2354  * and set max_prepared_transactions to enable testing of prepared
2355  * xacts. (Note: to reduce the probability of unexpected shmmax
2356  * failures, don't set max_prepared_transactions any higher than
2357  * actually needed by the prepared_xacts regression test.)
2358  */
2359  snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_instance);
2360  pg_conf = fopen(buf, "a");
2361  if (pg_conf == NULL)
2362  {
2363  fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno));
2364  exit(2);
2365  }
2366  fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
2367  fputs("log_autovacuum_min_duration = 0\n", pg_conf);
2368  fputs("log_checkpoints = on\n", pg_conf);
2369  fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
2370  fputs("log_lock_waits = on\n", pg_conf);
2371  fputs("log_temp_files = 128kB\n", pg_conf);
2372  fputs("max_prepared_transactions = 2\n", pg_conf);
2373 
2374  for (sl = temp_configs; sl != NULL; sl = sl->next)
2375  {
2376  char *temp_config = sl->str;
2377  FILE *extra_conf;
2378  char line_buf[1024];
2379 
2380  extra_conf = fopen(temp_config, "r");
2381  if (extra_conf == NULL)
2382  {
2383  fprintf(stderr, _("\n%s: could not open \"%s\" to read extra config: %s\n"), progname, temp_config, strerror(errno));
2384  exit(2);
2385  }
2386  while (fgets(line_buf, sizeof(line_buf), extra_conf) != NULL)
2387  fputs(line_buf, pg_conf);
2388  fclose(extra_conf);
2389  }
2390 
2391  fclose(pg_conf);
2392 
2393 #ifdef ENABLE_SSPI
2394  if (!use_unix_sockets)
2395  {
2396  /*
2397  * Since we successfully used the same buffer for the much-longer
2398  * "initdb" command, this can't truncate.
2399  */
2400  snprintf(buf, sizeof(buf), "%s/data", temp_instance);
2401  config_sspi_auth(buf, NULL);
2402  }
2403 #elif !defined(HAVE_UNIX_SOCKETS)
2404 #error Platform has no means to secure the test installation.
2405 #endif
2406 
2407  /*
2408  * Check if there is a postmaster running already.
2409  */
2410  snprintf(buf2, sizeof(buf2),
2411  "\"%s%spsql\" -X postgres <%s 2>%s",
2412  bindir ? bindir : "",
2413  bindir ? "/" : "",
2414  DEVNULL, DEVNULL);
2415 
2416  for (i = 0; i < 16; i++)
2417  {
2418  if (system(buf2) == 0)
2419  {
2420  char s[16];
2421 
2422  if (port_specified_by_user || i == 15)
2423  {
2424  fprintf(stderr, _("port %d apparently in use\n"), port);
2426  fprintf(stderr, _("%s: could not determine an available port\n"), progname);
2427  fprintf(stderr, _("Specify an unused port using the --port option or shut down any conflicting PostgreSQL servers.\n"));
2428  exit(2);
2429  }
2430 
2431  fprintf(stderr, _("port %d apparently in use, trying %d\n"), port, port + 1);
2432  port++;
2433  sprintf(s, "%d", port);
2434  doputenv("PGPORT", s);
2435  }
2436  else
2437  break;
2438  }
2439 
2440  /*
2441  * Start the temp postmaster
2442  */
2443  header(_("starting postmaster"));
2444  snprintf(buf, sizeof(buf),
2445  "\"%s%spostgres\" -D \"%s/data\" -F%s "
2446  "-c \"listen_addresses=%s\" -k \"%s\" "
2447  "> \"%s/log/postmaster.log\" 2>&1",
2448  bindir ? bindir : "",
2449  bindir ? "/" : "",
2450  temp_instance, debug ? " -d 5" : "",
2451  hostname ? hostname : "", sockdir ? sockdir : "",
2452  outputdir);
2454  if (postmaster_pid == INVALID_PID)
2455  {
2456  fprintf(stderr, _("\n%s: could not spawn postmaster: %s\n"),
2457  progname, strerror(errno));
2458  exit(2);
2459  }
2460 
2461  /*
2462  * Wait till postmaster is able to accept connections; normally this
2463  * is only a second or so, but Cygwin is reportedly *much* slower, and
2464  * test builds using Valgrind or similar tools might be too. Hence,
2465  * allow the default timeout of 60 seconds to be overridden from the
2466  * PGCTLTIMEOUT environment variable.
2467  */
2468  env_wait = getenv("PGCTLTIMEOUT");
2469  if (env_wait != NULL)
2470  {
2471  wait_seconds = atoi(env_wait);
2472  if (wait_seconds <= 0)
2473  wait_seconds = 60;
2474  }
2475  else
2476  wait_seconds = 60;
2477 
2478  for (i = 0; i < wait_seconds; i++)
2479  {
2480  /* Done if psql succeeds */
2481  if (system(buf2) == 0)
2482  break;
2483 
2484  /*
2485  * Fail immediately if postmaster has exited
2486  */
2487 #ifndef WIN32
2488  if (waitpid(postmaster_pid, NULL, WNOHANG) == postmaster_pid)
2489 #else
2490  if (WaitForSingleObject(postmaster_pid, 0) == WAIT_OBJECT_0)
2491 #endif
2492  {
2493  fprintf(stderr, _("\n%s: postmaster failed\nExamine %s/log/postmaster.log for the reason\n"), progname, outputdir);
2494  exit(2);
2495  }
2496 
2497  pg_usleep(1000000L);
2498  }
2499  if (i >= wait_seconds)
2500  {
2501  fprintf(stderr, _("\n%s: postmaster did not respond within %d seconds\nExamine %s/log/postmaster.log for the reason\n"),
2502  progname, wait_seconds, outputdir);
2503 
2504  /*
2505  * If we get here, the postmaster is probably wedged somewhere in
2506  * startup. Try to kill it ungracefully rather than leaving a
2507  * stuck postmaster that might interfere with subsequent test
2508  * attempts.
2509  */
2510 #ifndef WIN32
2511  if (kill(postmaster_pid, SIGKILL) != 0 &&
2512  errno != ESRCH)
2513  fprintf(stderr, _("\n%s: could not kill failed postmaster: %s\n"),
2514  progname, strerror(errno));
2515 #else
2516  if (TerminateProcess(postmaster_pid, 255) == 0)
2517  fprintf(stderr, _("\n%s: could not kill failed postmaster: error code %lu\n"),
2518  progname, GetLastError());
2519 #endif
2520 
2521  exit(2);
2522  }
2523 
2524  postmaster_running = true;
2525 
2526 #ifdef _WIN64
2527 /* need a series of two casts to convert HANDLE without compiler warning */
2528 #define ULONGPID(x) (unsigned long) (unsigned long long) (x)
2529 #else
2530 #define ULONGPID(x) (unsigned long) (x)
2531 #endif
2532  printf(_("running on port %d with PID %lu\n"),
2534  }
2535  else
2536  {
2537  /*
2538  * Using an existing installation, so may need to get rid of
2539  * pre-existing database(s) and role(s)
2540  */
2541  if (!use_existing)
2542  {
2543  for (sl = dblist; sl; sl = sl->next)
2545  for (sl = extraroles; sl; sl = sl->next)
2546  drop_role_if_exists(sl->str);
2547  }
2548  }
2549 
2550  /*
2551  * Create the test database(s) and role(s)
2552  */
2553  if (!use_existing)
2554  {
2555  for (sl = dblist; sl; sl = sl->next)
2556  create_database(sl->str);
2557  for (sl = extraroles; sl; sl = sl->next)
2558  create_role(sl->str, dblist);
2559  }
2560 
2561  /*
2562  * Ready to run the tests
2563  */
2564  header(_("running regression test queries"));
2565 
2566  for (sl = schedulelist; sl != NULL; sl = sl->next)
2567  {
2568  run_schedule(sl->str, tfunc);
2569  }
2570 
2571  for (sl = extra_tests; sl != NULL; sl = sl->next)
2572  {
2573  run_single_test(sl->str, tfunc);
2574  }
2575 
2576  /*
2577  * Shut down temp installation's postmaster
2578  */
2579  if (temp_instance)
2580  {
2581  header(_("shutting down postmaster"));
2582  stop_postmaster();
2583  }
2584 
2585  /*
2586  * If there were no errors, remove the temp instance immediately to
2587  * conserve disk space. (If there were errors, we leave the instance in
2588  * place for possible manual investigation.)
2589  */
2590  if (temp_instance && fail_count == 0 && fail_ignore_count == 0)
2591  {
2592  header(_("removing temporary instance"));
2593  if (!rmtree(temp_instance, true))
2594  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2596  }
2597 
2598  fclose(logfile);
2599 
2600  /*
2601  * Emit nice-looking summary message
2602  */
2603  if (fail_count == 0 && fail_ignore_count == 0)
2604  snprintf(buf, sizeof(buf),
2605  _(" All %d tests passed. "),
2606  success_count);
2607  else if (fail_count == 0) /* fail_count=0, fail_ignore_count>0 */
2608  snprintf(buf, sizeof(buf),
2609  _(" %d of %d tests passed, %d failed test(s) ignored. "),
2610  success_count,
2613  else if (fail_ignore_count == 0) /* fail_count>0 && fail_ignore_count=0 */
2614  snprintf(buf, sizeof(buf),
2615  _(" %d of %d tests failed. "),
2616  fail_count,
2618  else
2619  /* fail_count>0 && fail_ignore_count>0 */
2620  snprintf(buf, sizeof(buf),
2621  _(" %d of %d tests failed, %d of these failures ignored. "),
2625 
2626  putchar('\n');
2627  for (i = strlen(buf); i > 0; i--)
2628  putchar('=');
2629  printf("\n%s\n", buf);
2630  for (i = strlen(buf); i > 0; i--)
2631  putchar('=');
2632  putchar('\n');
2633  putchar('\n');
2634 
2635  if (file_size(difffilename) > 0)
2636  {
2637  printf(_("The differences that caused some tests to fail can be viewed in the\n"
2638  "file \"%s\". A copy of the test summary that you see\n"
2639  "above is saved in the file \"%s\".\n\n"),
2641  }
2642  else
2643  {
2644  unlink(difffilename);
2645  unlink(logfilename);
2646  }
2647 
2648  if (fail_count != 0)
2649  exit(1);
2650 
2651  return 0;
2652 }
char * make_absolute_path(const char *path)
Definition: path.c:608
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1290
static void drop_role_if_exists(const char *rolename)
Definition: pg_regress.c:2029
static void drop_database_if_exists(const char *dbname)
Definition: pg_regress.c:1986
static int max_concurrent_tests
Definition: pg_regress.c:83
static const char * sockdir
Definition: pg_regress.c:104
static int wait_seconds
Definition: pg_ctl.c:80
static _stringlist * extra_tests
Definition: pg_regress.c:86
static void help(void)
Definition: pg_regress.c:2048
char * str
Definition: pg_regress.h:26
static void initialize_environment(void)
Definition: pg_regress.c:743
static void run_single_test(const char *test, test_function tfunc)
Definition: pg_regress.c:1881
bool debug
Definition: pg_regress.c:76
const char * get_progname(const char *argv0)
Definition: path.c:453
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:157
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void get_restricted_token(void)
void pg_logging_init(const char *argv0)
Definition: logging.c:81
static void run_schedule(const char *schedule, test_function tfunc)
Definition: pg_regress.c:1630
static char * encoding
Definition: pg_regress.c:84
static const char * progname
Definition: pg_regress.c:100
static void make_directory(const char *dir)
Definition: pg_regress.c:1303
static bool use_existing
Definition: pg_regress.c:90
static bool postmaster_running
Definition: pg_regress.c:114
#define kill(pid, sig)
Definition: win32_port.h:454
#define printf(...)
Definition: port.h:221
static FILE * logfile
Definition: pg_regress.c:102
static void stop_postmaster(void)
Definition: pg_regress.c:260
#define fprintf
Definition: port.h:219
static char * temp_instance
Definition: pg_regress.c:87
char * launcher
Definition: pg_regress.c:80
static void split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
Definition: pg_regress.c:193
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:178
PID_TYPE spawn_process(const char *cmdline)
Definition: pg_regress.c:1169
#define SIGKILL
Definition: win32_port.h:163
#define sprintf
Definition: port.h:217
#define true
Definition: c.h:329
static char * difffilename
Definition: pg_regress.c:103
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
static _stringlist * loadextension
Definition: pg_regress.c:81
#define MAXPGPATH
_stringlist * dblist
Definition: pg_regress.c:75
static _stringlist * extraroles
Definition: pg_regress.c:96
static _stringlist * temp_configs
Definition: pg_regress.c:88
char * c
const char * pretty_diff_opts
Definition: pg_regress.c:68
static char * buf
Definition: pg_test_fsync.c:68
#define INVALID_PID
Definition: pg_regress.h:15
static void create_role(const char *rolename, const _stringlist *granted_dbs)
Definition: pg_regress.c:2036
static char * dlpath
Definition: pg_regress.c:94
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int success_count
Definition: pg_regress.c:116
#define DEVNULL
Definition: port.h:146
static int max_connections
Definition: pg_regress.c:82
static int port
Definition: pg_regress.c:92
static PID_TYPE postmaster_pid
Definition: pg_regress.c:113
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
#define no_argument
Definition: getopt_long.h:24
static char * config_auth_datadir
Definition: pg_regress.c:97
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1157
static int fail_ignore_count
Definition: pg_regress.c:118
static int fail_count
Definition: pg_regress.c:117
char * outputdir
Definition: pg_regress.c:78
static char * logfilename
Definition: pg_regress.c:101
char * inputdir
Definition: pg_regress.c:77
static long file_size(const char *file)
Definition: pg_regress.c:1236
static bool port_specified_by_user
Definition: pg_regress.c:93
char * bindir
Definition: pg_regress.c:79
#define strerror
Definition: port.h:228
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
static void doputenv(const char *var, const char *val)
Definition: pg_regress.c:731
static char * user
Definition: pg_regress.c:95
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
static _stringlist * schedulelist
Definition: pg_regress.c:85
struct _stringlist * next
Definition: pg_regress.h:27
int i
static bool nolocale
Definition: pg_regress.c:89
static void open_result_files(void)
Definition: pg_regress.c:1946
static char * hostname
Definition: pg_regress.c:91
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88
#define ULONGPID(x)
static void create_database(const char *dbname)
Definition: pg_regress.c:1993

◆ replace_string()

void replace_string ( StringInfo  string,
const char *  replace,
const char *  replacement 
)

Definition at line 447 of file pg_regress.c.

References appendStringInfoString(), StringInfoData::data, free, and pg_strdup().

Referenced by convert_sourcefiles_in(), and ecpg_start_test().

448 {
449  int pos = 0;
450  char *ptr;
451 
452  while ((ptr = strstr(string->data + pos, replace)) != NULL)
453  {
454  /* Must copy the remainder of the string out of the StringInfo */
455  char *suffix = pg_strdup(ptr + strlen(replace));
456 
457  /* Truncate StringInfo at start of found string ... */
458  string->len = ptr - string->data;
459  /* ... and append the replacement (this restores the trailing '\0') */
460  appendStringInfoString(string, replacement);
461  /* Next search should start after the replacement */
462  pos = string->len;
463  /* Put back the remainder of the string */
464  appendStringInfoString(string, suffix);
465  free(suffix);
466  }
467 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65

◆ results_differ()

static bool results_differ ( const char *  testname,
const char *  resultsfile,
const char *  default_expectfile 
)
static

Definition at line 1384 of file pg_regress.c.

References _, basic_diff_opts, difffilename, file_exists(), file_line_count(), fprintf, free, get_alternative_expectfile(), get_expectfile(), i, MAXPGPATH, pretty_diff_opts, run_diff(), snprintf, strerror, and strlcpy().

Referenced by run_schedule(), and run_single_test().

1385 {
1386  char expectfile[MAXPGPATH];
1387  char diff[MAXPGPATH];
1388  char cmd[MAXPGPATH * 3];
1389  char best_expect_file[MAXPGPATH];
1390  FILE *difffile;
1391  int best_line_count;
1392  int i;
1393  int l;
1394  const char *platform_expectfile;
1395 
1396  /*
1397  * We can pass either the resultsfile or the expectfile, they should have
1398  * the same type (filename.type) anyway.
1399  */
1400  platform_expectfile = get_expectfile(testname, resultsfile);
1401 
1402  strlcpy(expectfile, default_expectfile, sizeof(expectfile));
1403  if (platform_expectfile)
1404  {
1405  /*
1406  * Replace everything after the last slash in expectfile with what the
1407  * platform_expectfile contains.
1408  */
1409  char *p = strrchr(expectfile, '/');
1410 
1411  if (p)
1412  strcpy(++p, platform_expectfile);
1413  }
1414 
1415  /* Name to use for temporary diff file */
1416  snprintf(diff, sizeof(diff), "%s.diff", resultsfile);
1417 
1418  /* OK, run the diff */
1419  snprintf(cmd, sizeof(cmd),
1420  "diff %s \"%s\" \"%s\" > \"%s\"",
1421  basic_diff_opts, expectfile, resultsfile, diff);
1422 
1423  /* Is the diff file empty? */
1424  if (run_diff(cmd, diff) == 0)
1425  {
1426  unlink(diff);
1427  return false;
1428  }
1429 
1430  /* There may be secondary comparison files that match better */
1431  best_line_count = file_line_count(diff);
1432  strcpy(best_expect_file, expectfile);
1433 
1434  for (i = 0; i <= 9; i++)
1435  {
1436  char *alt_expectfile;
1437 
1438  alt_expectfile = get_alternative_expectfile(expectfile, i);
1439  if (!alt_expectfile)
1440  {
1441  fprintf(stderr, _("Unable to check secondary comparison files: %s\n"),
1442  strerror(errno));
1443  exit(2);
1444  }
1445 
1446  if (!file_exists(alt_expectfile))
1447  {
1448  free(alt_expectfile);
1449  continue;
1450  }
1451 
1452  snprintf(cmd, sizeof(cmd),
1453  "diff %s \"%s\" \"%s\" > \"%s\"",
1454  basic_diff_opts, alt_expectfile, resultsfile, diff);
1455 
1456  if (run_diff(cmd, diff) == 0)
1457  {
1458  unlink(diff);
1459  free(alt_expectfile);
1460  return false;
1461  }
1462 
1463  l = file_line_count(diff);
1464  if (l < best_line_count)
1465  {
1466  /* This diff was a better match than the last one */
1467  best_line_count = l;
1468  strlcpy(best_expect_file, alt_expectfile, sizeof(best_expect_file));
1469  }
1470  free(alt_expectfile);
1471  }
1472 
1473  /*
1474  * fall back on the canonical results file if we haven't tried it yet and
1475  * haven't found a complete match yet.
1476  */
1477 
1478  if (platform_expectfile)
1479  {
1480  snprintf(cmd, sizeof(cmd),
1481  "diff %s \"%s\" \"%s\" > \"%s\"",
1482  basic_diff_opts, default_expectfile, resultsfile, diff);
1483 
1484  if (run_diff(cmd, diff) == 0)
1485  {
1486  /* No diff = no changes = good */
1487  unlink(diff);
1488  return false;
1489  }
1490 
1491  l = file_line_count(diff);
1492  if (l < best_line_count)
1493  {
1494  /* This diff was a better match than the last one */
1495  best_line_count = l;
1496  strlcpy(best_expect_file, default_expectfile, sizeof(best_expect_file));
1497  }
1498  }
1499 
1500  /*
1501  * Use the best comparison file to generate the "pretty" diff, which we
1502  * append to the diffs summary file.
1503  */
1504 
1505  /* Write diff header */
1506  difffile = fopen(difffilename, "a");
1507  if (difffile)
1508  {
1509  fprintf(difffile,
1510  "diff %s %s %s\n",
1511  pretty_diff_opts, best_expect_file, resultsfile);
1512  fclose(difffile);
1513  }
1514 
1515  /* Run diff */
1516  snprintf(cmd, sizeof(cmd),
1517  "diff %s \"%s\" \"%s\" >> \"%s\"",
1518  pretty_diff_opts, best_expect_file, resultsfile, difffilename);
1519  run_diff(cmd, difffilename);
1520 
1521  unlink(diff);
1522  return true;
1523 }
static char * get_alternative_expectfile(const char *expectfile, int i)
Definition: pg_regress.c:1317
#define fprintf
Definition: port.h:219
static char * difffilename
Definition: pg_regress.c:103
#define MAXPGPATH
const char * pretty_diff_opts
Definition: pg_regress.c:68
static int run_diff(const char *cmd, const char *filename)
Definition: pg_regress.c:1351
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static const char * get_expectfile(const char *testname, const char *file)
Definition: pg_regress.c:702
static int file_line_count(const char *file)
Definition: pg_regress.c:1257
#define strerror
Definition: port.h:228
bool file_exists(const char *file)
Definition: pg_regress.c:1279
int i
const char * basic_diff_opts
Definition: pg_regress.c:67
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ run_diff()

static int run_diff ( const char *  cmd,
const char *  filename 
)
static

Definition at line 1351 of file pg_regress.c.

References _, file_size(), fprintf, WEXITSTATUS, and WIFEXITED.

Referenced by results_differ().

1352 {
1353  int r;
1354 
1355  r = system(cmd);
1356  if (!WIFEXITED(r) || WEXITSTATUS(r) > 1)
1357  {
1358  fprintf(stderr, _("diff command failed with status %d: %s\n"), r, cmd);
1359  exit(2);
1360  }
1361 #ifdef WIN32
1362 
1363  /*
1364  * On WIN32, if the 'diff' command cannot be found, system() returns 1,
1365  * but produces nothing to stdout, so we check for that here.
1366  */
1367  if (WEXITSTATUS(r) == 1 && file_size(filename) <= 0)
1368  {
1369  fprintf(stderr, _("diff command not found: %s\n"), cmd);
1370  exit(2);
1371  }
1372 #endif
1373 
1374  return WEXITSTATUS(r);
1375 }
#define fprintf
Definition: port.h:219
#define WIFEXITED(w)
Definition: win32_port.h:143
static long file_size(const char *file)
Definition: pg_regress.c:1236
static char * filename
Definition: pg_dumpall.c:91
#define _(x)
Definition: elog.c:88
#define WEXITSTATUS(w)
Definition: win32_port.h:145

◆ run_schedule()

static void run_schedule ( const char *  schedule,
test_function  tfunc 
)
static

Definition at line 1630 of file pg_regress.c.

References _, add_stringlist_item(), fail_count, fail_ignore_count, fprintf, free_stringlist(), i, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, log_child_failure(), max_concurrent_tests, max_connections, MAX_PARALLEL_TESTS, _stringlist::next, pg_free(), pg_strdup(), PID_TYPE, printf, progname, results_differ(), status(), status_end(), _stringlist::str, strerror, success_count, _resultmap::test, and wait_for_tests().

Referenced by regression_main().

1631 {
1632 #define MAX_PARALLEL_TESTS 100
1633  char *tests[MAX_PARALLEL_TESTS];
1634  _stringlist *resultfiles[MAX_PARALLEL_TESTS];
1635  _stringlist *expectfiles[MAX_PARALLEL_TESTS];
1638  instr_time starttimes[MAX_PARALLEL_TESTS];
1639  instr_time stoptimes[MAX_PARALLEL_TESTS];
1640  int statuses[MAX_PARALLEL_TESTS];
1641  _stringlist *ignorelist = NULL;
1642  char scbuf[1024];
1643  FILE *scf;
1644  int line_num = 0;
1645 
1646  memset(tests, 0, sizeof(tests));
1647  memset(resultfiles, 0, sizeof(resultfiles));
1648  memset(expectfiles, 0, sizeof(expectfiles));
1649  memset(tags, 0, sizeof(tags));
1650 
1651  scf = fopen(schedule, "r");
1652  if (!scf)
1653  {
1654  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1655  progname, schedule, strerror(errno));
1656  exit(2);
1657  }
1658 
1659  while (fgets(scbuf, sizeof(scbuf), scf))
1660  {
1661  char *test = NULL;
1662  char *c;
1663  int num_tests;
1664  bool inword;
1665  int i;
1666 
1667  line_num++;
1668 
1669  /* strip trailing whitespace, especially the newline */
1670  i = strlen(scbuf);
1671  while (i > 0 && isspace((unsigned char) scbuf[i - 1]))
1672  scbuf[--i] = '\0';
1673 
1674  if (scbuf[0] == '\0' || scbuf[0] == '#')
1675  continue;
1676  if (strncmp(scbuf, "test: ", 6) == 0)
1677  test = scbuf + 6;
1678  else if (strncmp(scbuf, "ignore: ", 8) == 0)
1679  {
1680  c = scbuf + 8;
1681  while (*c && isspace((unsigned char) *c))
1682  c++;
1683  add_stringlist_item(&ignorelist, c);
1684 
1685  /*
1686  * Note: ignore: lines do not run the test, they just say that
1687  * failure of this test when run later on is to be ignored. A bit
1688  * odd but that's how the shell-script version did it.
1689  */
1690  continue;
1691  }
1692  else
1693  {
1694  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1695  schedule, line_num, scbuf);
1696  exit(2);
1697  }
1698 
1699  num_tests = 0;
1700  inword = false;
1701  for (c = test;; c++)
1702  {
1703  if (*c == '\0' || isspace((unsigned char) *c))
1704  {
1705  if (inword)
1706  {
1707  /* Reached end of a test name */
1708  char sav;
1709 
1710  if (num_tests >= MAX_PARALLEL_TESTS)
1711  {
1712  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1713  MAX_PARALLEL_TESTS, schedule, line_num, scbuf);
1714  exit(2);
1715  }
1716  sav = *c;
1717  *c = '\0';
1718  tests[num_tests] = pg_strdup(test);
1719  num_tests++;
1720  *c = sav;
1721  inword = false;
1722  }
1723  if (*c == '\0')
1724  break; /* loop exit is here */
1725  }
1726  else if (!inword)
1727  {
1728  /* Start of a test name */
1729  test = c;
1730  inword = true;
1731  }
1732  }
1733 
1734  if (num_tests == 0)
1735  {
1736  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1737  schedule, line_num, scbuf);
1738  exit(2);
1739  }
1740 
1741  if (num_tests == 1)
1742  {
1743  status(_("test %-28s ... "), tests[0]);
1744  pids[0] = (tfunc) (tests[0], &resultfiles[0], &expectfiles[0], &tags[0]);
1745  INSTR_TIME_SET_CURRENT(starttimes[0]);
1746  wait_for_tests(pids, statuses, stoptimes, NULL, 1);
1747  /* status line is finished below */
1748  }
1749  else if (max_concurrent_tests > 0 && max_concurrent_tests < num_tests)
1750  {
1751  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1752  max_concurrent_tests, schedule, line_num, scbuf);
1753  exit(2);
1754  }
1755  else if (max_connections > 0 && max_connections < num_tests)
1756  {
1757  int oldest = 0;
1758 
1759  status(_("parallel group (%d tests, in groups of %d): "),
1760  num_tests, max_connections);
1761  for (i = 0; i < num_tests; i++)
1762  {
1763  if (i - oldest >= max_connections)
1764  {
1765  wait_for_tests(pids + oldest, statuses + oldest,
1766  stoptimes + oldest,
1767  tests + oldest, i - oldest);
1768  oldest = i;
1769  }
1770  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1771  INSTR_TIME_SET_CURRENT(starttimes[i]);
1772  }
1773  wait_for_tests(pids + oldest, statuses + oldest,
1774  stoptimes + oldest,
1775  tests + oldest, i - oldest);
1776  status_end();
1777  }
1778  else
1779  {
1780  status(_("parallel group (%d tests): "), num_tests);
1781  for (i = 0; i < num_tests; i++)
1782  {
1783  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1784  INSTR_TIME_SET_CURRENT(starttimes[i]);
1785  }
1786  wait_for_tests(pids, statuses, stoptimes, tests, num_tests);
1787  status_end();
1788  }
1789 
1790  /* Check results for all tests */
1791  for (i = 0; i < num_tests; i++)
1792  {
1793  _stringlist *rl,
1794  *el,
1795  *tl;
1796  bool differ = false;
1797 
1798  if (num_tests > 1)
1799  status(_(" %-28s ... "), tests[i]);
1800 
1801  /*
1802  * Advance over all three lists simultaneously.
1803  *
1804  * Compare resultfiles[j] with expectfiles[j] always. Tags are
1805  * optional but if there are tags, the tag list has the same
1806  * length as the other two lists.
1807  */
1808  for (rl = resultfiles[i], el = expectfiles[i], tl = tags[i];
1809  rl != NULL; /* rl and el have the same length */
1810  rl = rl->next, el = el->next,
1811  tl = tl ? tl->next : NULL)
1812  {
1813  bool newdiff;
1814 
1815  newdiff = results_differ(tests[i], rl->str, el->str);
1816  if (newdiff && tl)
1817  {
1818  printf("%s ", tl->str);
1819  }
1820  differ |= newdiff;
1821  }
1822 
1823  if (differ)
1824  {
1825  bool ignore = false;
1826  _stringlist *sl;
1827 
1828  for (sl = ignorelist; sl != NULL; sl = sl->next)
1829  {
1830  if (strcmp(tests[i], sl->str) == 0)
1831  {
1832  ignore = true;
1833  break;
1834  }
1835  }
1836  if (ignore)
1837  {
1838  status(_("failed (ignored)"));
1840  }
1841  else
1842  {
1843  status(_("FAILED"));
1844  fail_count++;
1845  }
1846  }
1847  else
1848  {
1849  status(_("ok ")); /* align with FAILED */
1850  success_count++;
1851  }
1852 
1853  if (statuses[i] != 0)
1854  log_child_failure(statuses[i]);
1855 
1856  INSTR_TIME_SUBTRACT(stoptimes[i], starttimes[i]);
1857  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptimes[i]));
1858 
1859  status_end();
1860  }
1861 
1862  for (i = 0; i < num_tests; i++)
1863  {
1864  pg_free(tests[i]);
1865  tests[i] = NULL;
1866  free_stringlist(&resultfiles[i]);
1867  free_stringlist(&expectfiles[i]);
1868  free_stringlist(&tags[i]);
1869  }
1870  }
1871 
1872  free_stringlist(&ignorelist);
1873 
1874  fclose(scf);
1875 }
static int max_concurrent_tests
Definition: pg_regress.c:83
static void test(void)
static void log_child_failure(int exitstatus)
Definition: pg_regress.c:1606
char * str
Definition: pg_regress.h:26
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:157
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static const char * progname
Definition: pg_regress.c:100
static void status_end(void)
Definition: pg_regress.c:248
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:221
#define fprintf
Definition: port.h:219
static bool results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
Definition: pg_regress.c:1384
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:178
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
char * c
#define MAX_PARALLEL_TESTS
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int success_count
Definition: pg_regress.c:116
static int max_connections
Definition: pg_regress.c:82
static int fail_ignore_count
Definition: pg_regress.c:118
static int fail_count
Definition: pg_regress.c:117
#define strerror
Definition: port.h:228
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
struct _stringlist * next
Definition: pg_regress.h:27
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define _(x)
Definition: elog.c:88
static void wait_for_tests(PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
Definition: pg_regress.c:1534

◆ run_single_test()

static void run_single_test ( const char *  test,
test_function  tfunc 
)
static

Definition at line 1881 of file pg_regress.c.

References _, fail_count, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, log_child_failure(), _stringlist::next, PID_TYPE, printf, results_differ(), status(), status_end(), _stringlist::str, success_count, and wait_for_tests().

Referenced by regression_main().

1882 {
1883  PID_TYPE pid;
1884  instr_time starttime;
1885  instr_time stoptime;
1886  int exit_status;
1887  _stringlist *resultfiles = NULL;
1888  _stringlist *expectfiles = NULL;
1889  _stringlist *tags = NULL;
1890  _stringlist *rl,
1891  *el,
1892  *tl;
1893  bool differ = false;
1894 
1895  status(_("test %-28s ... "), test);
1896  pid = (tfunc) (test, &resultfiles, &expectfiles, &tags);
1897  INSTR_TIME_SET_CURRENT(starttime);
1898  wait_for_tests(&pid, &exit_status, &stoptime, NULL, 1);
1899 
1900  /*
1901  * Advance over all three lists simultaneously.
1902  *
1903  * Compare resultfiles[j] with expectfiles[j] always. Tags are optional
1904  * but if there are tags, the tag list has the same length as the other
1905  * two lists.
1906  */
1907  for (rl = resultfiles, el = expectfiles, tl = tags;
1908  rl != NULL; /* rl and el have the same length */
1909  rl = rl->next, el = el->next,
1910  tl = tl ? tl->next : NULL)
1911  {
1912  bool newdiff;
1913 
1914  newdiff = results_differ(test, rl->str, el->str);
1915  if (newdiff && tl)
1916  {
1917  printf("%s ", tl->str);
1918  }
1919  differ |= newdiff;
1920  }
1921 
1922  if (differ)
1923  {
1924  status(_("FAILED"));
1925  fail_count++;
1926  }
1927  else
1928  {
1929  status(_("ok ")); /* align with FAILED */
1930  success_count++;
1931  }
1932 
1933  if (exit_status != 0)
1934  log_child_failure(exit_status);
1935 
1936  INSTR_TIME_SUBTRACT(stoptime, starttime);
1937  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptime));
1938 
1939  status_end();
1940 }
static void test(void)
static void log_child_failure(int exitstatus)
Definition: pg_regress.c:1606
char * str
Definition: pg_regress.h:26
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static void status_end(void)
Definition: pg_regress.c:248
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:221
static bool results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
Definition: pg_regress.c:1384
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
static int success_count
Definition: pg_regress.c:116
static int fail_count
Definition: pg_regress.c:117
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
struct _stringlist * next
Definition: pg_regress.h:27
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define _(x)
Definition: elog.c:88
static void wait_for_tests(PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
Definition: pg_regress.c:1534

◆ spawn_process()

PID_TYPE spawn_process ( const char *  cmdline)

Definition at line 1169 of file pg_regress.c.

References _, fprintf, logfile, progname, psprintf(), shellprog, generate_unaccent_rules::stdout, and strerror.

Referenced by ecpg_start_test(), isolation_start_test(), psql_start_test(), and regression_main().

1170 {
1171 #ifndef WIN32
1172  pid_t pid;
1173 
1174  /*
1175  * Must flush I/O buffers before fork. Ideally we'd use fflush(NULL) here
1176  * ... does anyone still care about systems where that doesn't work?
1177  */
1178  fflush(stdout);
1179  fflush(stderr);
1180  if (logfile)
1181  fflush(logfile);
1182 
1183  pid = fork();
1184  if (pid == -1)
1185  {
1186  fprintf(stderr, _("%s: could not fork: %s\n"),
1187  progname, strerror(errno));
1188  exit(2);
1189  }
1190  if (pid == 0)
1191  {
1192  /*
1193  * In child
1194  *
1195  * Instead of using system(), exec the shell directly, and tell it to
1196  * "exec" the command too. This saves two useless processes per
1197  * parallel test case.
1198  */
1199  char *cmdline2;
1200 
1201  cmdline2 = psprintf("exec %s", cmdline);
1202  execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
1203  fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
1204  progname, shellprog, strerror(errno));
1205  _exit(1); /* not exit() here... */
1206  }
1207  /* in parent */
1208  return pid;
1209 #else
1210  PROCESS_INFORMATION pi;
1211  char *cmdline2;
1212  HANDLE restrictedToken;
1213  const char *comspec;
1214 
1215  /* Find CMD.EXE location using COMSPEC, if it's set */
1216  comspec = getenv("COMSPEC");
1217  if (comspec == NULL)
1218  comspec = "CMD";
1219 
1220  memset(&pi, 0, sizeof(pi));
1221  cmdline2 = psprintf("\"%s\" /c \"%s\"", comspec, cmdline);
1222 
1223  if ((restrictedToken =
1224  CreateRestrictedProcess(cmdline2, &pi)) == 0)
1225  exit(2);
1226 
1227  CloseHandle(pi.hThread);
1228  return pi.hProcess;
1229 #endif
1230 }
static char * shellprog
Definition: pg_regress.c:58
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static const char * progname
Definition: pg_regress.c:100
static FILE * logfile
Definition: pg_regress.c:102
#define fprintf
Definition: port.h:219
#define strerror
Definition: port.h:228
#define _(x)
Definition: elog.c:88

◆ split_to_stringlist()

static void split_to_stringlist ( const char *  s,
const char *  delim,
_stringlist **  listhead 
)
static

Definition at line 193 of file pg_regress.c.

References add_stringlist_item(), free, and pg_strdup().

Referenced by regression_main().

194 {
195  char *sc = pg_strdup(s);
196  char *token = strtok(sc, delim);
197 
198  while (token)
199  {
200  add_stringlist_item(listhead, token);
201  token = strtok(NULL, delim);
202  }
203  free(sc);
204 }
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:157
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65

◆ status()

static void status ( const char *  fmt,
  ... 
)
static

Definition at line 227 of file pg_regress.c.

References logfile, generate_unaccent_rules::stdout, and vfprintf.

Referenced by _bt_first(), _bt_parallel_seize(), _bt_readnextpage(), _bt_steppage(), apw_start_leader_worker(), AtAbort_Portals(), AtCleanup_Portals(), AtEOSubXact_RelationCache(), AtEOXact_RelationCache(), AtPrepare_Locks(), AtSubAbort_Portals(), AtSubCleanup_Portals(), AtSubCommit_Portals(), BackendInitialize(), BaseBackup(), be_lo_lseek(), be_lo_lseek64(), bf_check_supported_key_len(), check_tuple_header_and_visibilty(), check_worker_status(), CheckSCRAMAuth(), ClientAuthentication(), compute_new_xmax_infomask(), dblink_get_connections(), DestroyPartitionDirectory(), do_kill(), do_lo_export(), do_lo_unlink(), DoesMultiXactIdConflict(), ecpg_process_output(), ecpg_store_result(), exec_command(), exec_command_crosstabview(), exec_command_d(), exec_command_edit(), exec_command_ef_ev(), exec_command_g(), exec_command_gdesc(), exec_command_gexec(), exec_command_gset(), exec_command_lo(), exec_command_quit(), exec_command_sf_sv(), exec_command_write(), executeQueryOrDie(), forget_invalid_pages(), forget_invalid_pages_db(), FreezeMultiXactId(), get_collation_actual_version(), get_xid_status(), GetLockmodeName(), GetMultiXactIdHintBits(), HandleSlashCmds(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), HoldPinnedPortals(), InvalidateAttoptCacheCallback(), InvalidateOprCacheCallBack(), InvalidateOprProofCacheCallBack(), InvalidateShippableCacheCallback(), InvalidateTableSpaceCacheCallback(), InvalidateTSCacheCallBack(), libpqrcv_connect(), ListenToWorkers(), lo_read(), lo_write(), LockReassignCurrentOwner(), LockReleaseAll(), LockReleaseCurrentOwner(), LockReleaseSession(), log_child_failure(), logicalrep_partmap_invalidate_cb(), logicalrep_relmap_invalidate_cb(), MultiXactIdCreateFromMembers(), MultiXactIdExpand(), MultiXactIdGetUpdateXid(), mxid_to_string(), parallel_restore(), parseServiceInfo(), parseWorkerResponse(), pg_collation_actual_version(), pg_newlocale_from_collation(), pg_prepared_xact(), pg_start_backup(), pg_stop_backup(), pg_stop_backup_v2(), pg_xact_status(), pgrowlocks(), PortalErrorCleanup(), PortalHashTableDeleteAll(), PostmasterMain(), PostPrepare_Locks(), pqFunctionCall2(), pqFunctionCall3(), pqGetline3(), PQmakeEmptyPGresult(), PQresetPoll(), PQresStatus(), PQsetClientEncoding(), PreCommit_Portals(), RecordNewMultiXact(), rel_sync_cache_publication_cb(), RelationCacheInitializePhase3(), RelationCacheInvalidate(), RelfilenodeMapInvalidateCallback(), RememberToFreeTupleDescAtEOX(), restore_toc_entry(), run_reindex_command(), run_schedule(), run_single_test(), run_vacuum_command(), sepgsql_client_auth(), SerializeEnumBlacklist(), shm_mq_counterparty_gone(), shm_mq_wait_internal(), smgrcloseall(), StandbyReleaseAllLocks(), StandbyReleaseOldLocks(), str_numth(), test_config_settings(), ThereAreNoReadyPortals(), TransactionGroupUpdateXidStatus(), TransactionIdGetStatus(), TypeCacheOpcCallback(), TypeCacheRelCallback(), TypeCacheTypCallback(), uuid_generate_internal(), varstr_abbrev_convert(), varstr_cmp(), varstrfastcmp_locale(), wait_for_postmaster(), wait_for_tests(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForCommands(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToExit(), WaitForReplicationWorkerAttach(), WaitForTerminatingWorkers(), worker_spi_launch(), write_relcache_init_file(), and XLogCheckInvalidPages().

228 {
229  va_list ap;
230 
231  va_start(ap, fmt);
232  vfprintf(stdout, fmt, ap);
233  fflush(stdout);
234  va_end(ap);
235 
236  if (logfile)
237  {
238  va_start(ap, fmt);
239  vfprintf(logfile, fmt, ap);
240  va_end(ap);
241  }
242 }
static FILE * logfile
Definition: pg_regress.c:102
#define vfprintf
Definition: port.h:218

◆ status_end()

static void status_end ( void  )
static

Definition at line 248 of file pg_regress.c.

References fprintf, logfile, and generate_unaccent_rules::stdout.

Referenced by run_schedule(), and run_single_test().

249 {
250  fprintf(stdout, "\n");
251  fflush(stdout);
252  if (logfile)
253  fprintf(logfile, "\n");
254 }
static FILE * logfile
Definition: pg_regress.c:102
#define fprintf
Definition: port.h:219

◆ stop_postmaster()

static void stop_postmaster ( void  )
static

Definition at line 260 of file pg_regress.c.

References _, Assert, bindir, buf, fprintf, MAXPGPATH, mkdtemp(), port, postmaster_running, pqsignal(), progname, psprintf(), SIG_DFL, SIGHUP, SIGPIPE, snprintf, generate_unaccent_rules::stdout, strerror, temp_instance, and UNIXSOCK_PATH.

Referenced by regression_main().

261 {
262  if (postmaster_running)
263  {
264  /* We use pg_ctl to issue the kill and wait for stop */
265  char buf[MAXPGPATH * 2];
266  int r;
267 
268  /* On Windows, system() seems not to force fflush, so... */
269  fflush(stdout);
270  fflush(stderr);
271 
272  snprintf(buf, sizeof(buf),
273  "\"%s%spg_ctl\" stop -D \"%s/data\" -s",
274  bindir ? bindir : "",
275  bindir ? "/" : "",
276  temp_instance);
277  r = system(buf);
278  if (r != 0)
279  {
280  fprintf(stderr, _("\n%s: could not stop postmaster: exit code was %d\n"),
281  progname, r);
282  _exit(2); /* not exit(), that could be recursive */
283  }
284 
285  postmaster_running = false;
286  }
287 }
static const char * progname
Definition: pg_regress.c:100
static bool postmaster_running
Definition: pg_regress.c:114
#define fprintf
Definition: port.h:219
static char * temp_instance
Definition: pg_regress.c:87
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
char * bindir
Definition: pg_regress.c:79
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:88

◆ string_matches_pattern()

static bool string_matches_pattern ( const char *  str,
const char *  pattern 
)
static

Definition at line 379 of file pg_regress.c.

Referenced by load_resultmap().

380 {
381  while (*str && *pattern)
382  {
383  if (*pattern == '.' && pattern[1] == '*')
384  {
385  pattern += 2;
386  /* Trailing .* matches everything. */
387  if (*pattern == '\0')
388  return true;
389 
390  /*
391  * Otherwise, scan for a text position at which we can match the
392  * rest of the pattern.
393  */
394  while (*str)
395  {
396  /*
397  * Optimization to prevent most recursion: don't recurse
398  * unless first pattern char might match this text char.
399  */
400  if (*str == *pattern || *pattern == '.')
401  {
402  if (string_matches_pattern(str, pattern))
403  return true;
404  }
405 
406  str++;
407  }
408 
409  /*
410  * End of text with no match.
411  */
412  return false;
413  }
414  else if (*pattern != '.' && *str != *pattern)
415  {
416  /*
417  * Not the single-character wildcard and no explicit match? Then
418  * time to quit...
419  */
420  return false;
421  }
422 
423  str++;
424  pattern++;
425  }
426 
427  if (*pattern == '\0')
428  return true; /* end of pattern, so declare match */
429 
430  /* End of input string. Do we have matching pattern remaining? */
431  while (*pattern == '.' && pattern[1] == '*')
432  pattern += 2;
433  if (*pattern == '\0')
434  return true; /* end of pattern, so declare match */
435 
436  return false;
437 }
static bool string_matches_pattern(const char *str, const char *pattern)
Definition: pg_regress.c:379

◆ wait_for_tests()

static void wait_for_tests ( PID_TYPE pids,
int *  statuses,
instr_time stoptimes,
char **  names,
int  num_tests 
)
static

Definition at line 1534 of file pg_regress.c.

References _, fprintf, free, i, INSTR_TIME_SET_CURRENT, INVALID_PID, pg_malloc(), PID_TYPE, status(), and strerror.

Referenced by run_schedule(), and run_single_test().

1536 {
1537  int tests_left;
1538  int i;
1539 
1540 #ifdef WIN32
1541  PID_TYPE *active_pids = pg_malloc(num_tests * sizeof(PID_TYPE));
1542 
1543  memcpy(active_pids, pids, num_tests * sizeof(PID_TYPE));
1544 #endif
1545 
1546  tests_left = num_tests;
1547  while (tests_left > 0)
1548  {
1549  PID_TYPE p;
1550 
1551 #ifndef WIN32
1552  int exit_status;
1553 
1554  p = wait(&exit_status);
1555 
1556  if (p == INVALID_PID)
1557  {
1558  fprintf(stderr, _("failed to wait for subprocesses: %s\n"),
1559  strerror(errno));
1560  exit(2);
1561  }
1562 #else
1563  DWORD exit_status;
1564  int r;
1565 
1566  r = WaitForMultipleObjects(tests_left, active_pids, FALSE, INFINITE);
1567  if (r < WAIT_OBJECT_0 || r >= WAIT_OBJECT_0 + tests_left)
1568  {
1569  fprintf(stderr, _("failed to wait for subprocesses: error code %lu\n"),
1570  GetLastError());
1571  exit(2);
1572  }
1573  p = active_pids[r - WAIT_OBJECT_0];
1574  /* compact the active_pids array */
1575  active_pids[r - WAIT_OBJECT_0] = active_pids[tests_left - 1];
1576 #endif /* WIN32 */
1577 
1578  for (i = 0; i < num_tests; i++)
1579  {
1580  if (p == pids[i])
1581  {
1582 #ifdef WIN32
1583  GetExitCodeProcess(pids[i], &exit_status);
1584  CloseHandle(pids[i]);
1585 #endif
1586  pids[i] = INVALID_PID;
1587  statuses[i] = (int) exit_status;
1588  INSTR_TIME_SET_CURRENT(stoptimes[i]);
1589  if (names)
1590  status(" %s", names[i]);
1591  tests_left--;
1592  break;
1593  }
1594  }
1595  }
1596 
1597 #ifdef WIN32
1598  free(active_pids);
1599 #endif
1600 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define PID_TYPE
Definition: pg_regress.h:14
#define fprintf
Definition: port.h:219
#define INVALID_PID
Definition: pg_regress.h:15
#define free(a)
Definition: header.h:65
#define strerror
Definition: port.h:228
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define _(x)
Definition: elog.c:88

Variable Documentation

◆ basic_diff_opts

const char* basic_diff_opts = ""

Definition at line 67 of file pg_regress.c.

Referenced by results_differ().

◆ bindir

char* bindir = PGBINDIR

Definition at line 79 of file pg_regress.c.

Referenced by psql_command(), psql_start_test(), regression_main(), and stop_postmaster().

◆ config_auth_datadir

char* config_auth_datadir = NULL
static

Definition at line 97 of file pg_regress.c.

Referenced by regression_main().

◆ dblist

◆ debug

bool debug = false

Definition at line 76 of file pg_regress.c.

Referenced by regression_main().

◆ difffilename

char* difffilename
static

Definition at line 103 of file pg_regress.c.

Referenced by open_result_files(), regression_main(), and results_differ().

◆ dlpath

char* dlpath = PKGLIBDIR
static

Definition at line 94 of file pg_regress.c.

Referenced by convert_sourcefiles_in(), and regression_main().

◆ encoding

char* encoding = NULL
static

Definition at line 84 of file pg_regress.c.

Referenced by create_database(), initialize_environment(), and regression_main().

◆ extra_tests

_stringlist* extra_tests = NULL
static

Definition at line 86 of file pg_regress.c.

◆ extraroles

_stringlist* extraroles = NULL
static

Definition at line 96 of file pg_regress.c.

◆ fail_count

int fail_count = 0
static

Definition at line 117 of file pg_regress.c.

Referenced by regression_main(), run_schedule(), and run_single_test().

◆ fail_ignore_count

int fail_ignore_count = 0
static

Definition at line 118 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ host_platform

char* host_platform = HOST_TUPLE

Definition at line 55 of file pg_regress.c.

Referenced by load_resultmap().

◆ hostname

char* hostname = NULL
static

Definition at line 91 of file pg_regress.c.

Referenced by initialize_environment(), pqPacketSend(), and regression_main().

◆ inputdir

◆ launcher

char* launcher = NULL

Definition at line 80 of file pg_regress.c.

Referenced by isolation_start_test(), psql_start_test(), and regression_main().

◆ loadextension

_stringlist* loadextension = NULL
static

Definition at line 81 of file pg_regress.c.

◆ logfile

FILE* logfile
static

◆ logfilename

char* logfilename
static

Definition at line 101 of file pg_regress.c.

Referenced by open_result_files(), and regression_main().

◆ max_concurrent_tests

int max_concurrent_tests = 0
static

Definition at line 83 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ max_connections

int max_connections = 0
static

Definition at line 82 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ nolocale

bool nolocale = false
static

Definition at line 89 of file pg_regress.c.

Referenced by create_database(), initialize_environment(), and regression_main().

◆ outputdir

◆ port

◆ port_specified_by_user

bool port_specified_by_user = false
static

Definition at line 93 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_pid

PID_TYPE postmaster_pid = INVALID_PID
static

Definition at line 113 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_running

bool postmaster_running = false
static

Definition at line 114 of file pg_regress.c.

Referenced by regression_main(), and stop_postmaster().

◆ pretty_diff_opts

const char* pretty_diff_opts = "-U3"

Definition at line 68 of file pg_regress.c.

Referenced by regression_main(), and results_differ().

◆ progname

◆ resultmap

_resultmap* resultmap = NULL
static

Definition at line 111 of file pg_regress.c.

Referenced by load_resultmap().

◆ schedulelist

_stringlist* schedulelist = NULL
static

Definition at line 85 of file pg_regress.c.

◆ shellprog

char* shellprog = SHELLPROG
static

Definition at line 58 of file pg_regress.c.

Referenced by spawn_process().

◆ sockdir

const char* sockdir
static

Definition at line 104 of file pg_regress.c.

Referenced by initialize_environment(), and regression_main().

◆ success_count

int success_count = 0
static

Definition at line 116 of file pg_regress.c.

Referenced by regression_main(), run_schedule(), and run_single_test().

◆ temp_configs

_stringlist* temp_configs = NULL
static

Definition at line 88 of file pg_regress.c.

◆ temp_instance

char* temp_instance = NULL
static

Definition at line 87 of file pg_regress.c.

Referenced by initialize_environment(), regression_main(), and stop_postmaster().

◆ use_existing

bool use_existing = false
static

Definition at line 90 of file pg_regress.c.

Referenced by regression_main().

◆ user