PostgreSQL Source Code  git master
pg_regress.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_regress --- regression test driver
4  *
5  * This is a C implementation of the previous shell script for running
6  * the regression tests, and should be mostly compatible with it.
7  * Initial author of C translation: Magnus Hagander
8  *
9  * This code is released under the terms of the PostgreSQL License.
10  *
11  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * src/test/regress/pg_regress.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 
19 #include "postgres_fe.h"
20 
21 #include <ctype.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <signal.h>
25 #include <unistd.h>
26 
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #endif
31 
32 #include "common/logging.h"
34 #include "common/username.h"
35 #include "getopt_long.h"
36 #include "libpq/pqcomm.h" /* needed for UNIXSOCK_PATH() */
37 #include "pg_config_paths.h"
38 #include "pg_regress.h"
39 #include "portability/instr_time.h"
40 
41 /* for resultmap we need a list of pairs of strings */
42 typedef struct _resultmap
43 {
44  char *test;
45  char *type;
46  char *resultfile;
47  struct _resultmap *next;
48 } _resultmap;
49 
50 /*
51  * Values obtained from Makefile.
52  */
53 char *host_platform = HOST_TUPLE;
54 
55 #ifndef WIN32 /* not used in WIN32 case */
56 static char *shellprog = SHELLPROG;
57 #endif
58 
59 /*
60  * On Windows we use -w in diff switches to avoid problems with inconsistent
61  * newline representation. The actual result files will generally have
62  * Windows-style newlines, but the comparison files might or might not.
63  */
64 #ifndef WIN32
65 const char *basic_diff_opts = "";
66 const char *pretty_diff_opts = "-U3";
67 #else
68 const char *basic_diff_opts = "-w";
69 const char *pretty_diff_opts = "-w -U3";
70 #endif
71 
72 /* options settable from command line */
74 bool debug = false;
75 char *inputdir = ".";
76 char *outputdir = ".";
77 char *bindir = PGBINDIR;
78 char *launcher = NULL;
79 static _stringlist *loadlanguage = NULL;
80 static _stringlist *loadextension = NULL;
81 static int max_connections = 0;
82 static int max_concurrent_tests = 0;
83 static char *encoding = NULL;
84 static _stringlist *schedulelist = NULL;
85 static _stringlist *extra_tests = NULL;
86 static char *temp_instance = NULL;
87 static _stringlist *temp_configs = NULL;
88 static bool nolocale = false;
89 static bool use_existing = false;
90 static char *hostname = NULL;
91 static int port = -1;
92 static bool port_specified_by_user = false;
93 static char *dlpath = PKGLIBDIR;
94 static char *user = NULL;
95 static _stringlist *extraroles = NULL;
96 static char *config_auth_datadir = NULL;
97 
98 /* internal variables */
99 static const char *progname;
100 static char *logfilename;
101 static FILE *logfile;
102 static char *difffilename;
103 static const char *sockdir;
104 #ifdef HAVE_UNIX_SOCKETS
105 static const char *temp_sockdir;
106 static char sockself[MAXPGPATH];
107 static char socklock[MAXPGPATH];
108 #endif
109 
110 static _resultmap *resultmap = NULL;
111 
113 static bool postmaster_running = false;
114 
115 static int success_count = 0;
116 static int fail_count = 0;
117 static int fail_ignore_count = 0;
118 
119 static bool directory_exists(const char *dir);
120 static void make_directory(const char *dir);
121 
122 static void header(const char *fmt,...) pg_attribute_printf(1, 2);
123 static void status(const char *fmt,...) pg_attribute_printf(1, 2);
124 static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2, 3);
125 
126 /*
127  * allow core files if possible.
128  */
129 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
130 static void
131 unlimit_core_size(void)
132 {
133  struct rlimit lim;
134 
135  getrlimit(RLIMIT_CORE, &lim);
136  if (lim.rlim_max == 0)
137  {
138  fprintf(stderr,
139  _("%s: could not set core size: disallowed by hard limit\n"),
140  progname);
141  return;
142  }
143  else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
144  {
145  lim.rlim_cur = lim.rlim_max;
146  setrlimit(RLIMIT_CORE, &lim);
147  }
148 }
149 #endif
150 
151 
152 /*
153  * Add an item at the end of a stringlist.
154  */
155 void
156 add_stringlist_item(_stringlist **listhead, const char *str)
157 {
158  _stringlist *newentry = pg_malloc(sizeof(_stringlist));
159  _stringlist *oldentry;
160 
161  newentry->str = pg_strdup(str);
162  newentry->next = NULL;
163  if (*listhead == NULL)
164  *listhead = newentry;
165  else
166  {
167  for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
168  /* skip */ ;
169  oldentry->next = newentry;
170  }
171 }
172 
173 /*
174  * Free a stringlist.
175  */
176 static void
178 {
179  if (listhead == NULL || *listhead == NULL)
180  return;
181  if ((*listhead)->next != NULL)
182  free_stringlist(&((*listhead)->next));
183  free((*listhead)->str);
184  free(*listhead);
185  *listhead = NULL;
186 }
187 
188 /*
189  * Split a delimited string into a stringlist
190  */
191 static void
192 split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
193 {
194  char *sc = pg_strdup(s);
195  char *token = strtok(sc, delim);
196 
197  while (token)
198  {
199  add_stringlist_item(listhead, token);
200  token = strtok(NULL, delim);
201  }
202  free(sc);
203 }
204 
205 /*
206  * Print a progress banner on stdout.
207  */
208 static void
209 header(const char *fmt,...)
210 {
211  char tmp[64];
212  va_list ap;
213 
214  va_start(ap, fmt);
215  vsnprintf(tmp, sizeof(tmp), fmt, ap);
216  va_end(ap);
217 
218  fprintf(stdout, "============== %-38s ==============\n", tmp);
219  fflush(stdout);
220 }
221 
222 /*
223  * Print "doing something ..." --- supplied text should not end with newline
224  */
225 static void
226 status(const char *fmt,...)
227 {
228  va_list ap;
229 
230  va_start(ap, fmt);
231  vfprintf(stdout, fmt, ap);
232  fflush(stdout);
233  va_end(ap);
234 
235  if (logfile)
236  {
237  va_start(ap, fmt);
238  vfprintf(logfile, fmt, ap);
239  va_end(ap);
240  }
241 }
242 
243 /*
244  * Done "doing something ..."
245  */
246 static void
248 {
249  fprintf(stdout, "\n");
250  fflush(stdout);
251  if (logfile)
252  fprintf(logfile, "\n");
253 }
254 
255 /*
256  * shut down temp postmaster
257  */
258 static void
260 {
261  if (postmaster_running)
262  {
263  /* We use pg_ctl to issue the kill and wait for stop */
264  char buf[MAXPGPATH * 2];
265  int r;
266 
267  /* On Windows, system() seems not to force fflush, so... */
268  fflush(stdout);
269  fflush(stderr);
270 
271  snprintf(buf, sizeof(buf),
272  "\"%s%spg_ctl\" stop -D \"%s/data\" -s",
273  bindir ? bindir : "",
274  bindir ? "/" : "",
275  temp_instance);
276  r = system(buf);
277  if (r != 0)
278  {
279  fprintf(stderr, _("\n%s: could not stop postmaster: exit code was %d\n"),
280  progname, r);
281  _exit(2); /* not exit(), that could be recursive */
282  }
283 
284  postmaster_running = false;
285  }
286 }
287 
288 #ifdef HAVE_UNIX_SOCKETS
289 /*
290  * Remove the socket temporary directory. pg_regress never waits for a
291  * postmaster exit, so it is indeterminate whether the postmaster has yet to
292  * unlink the socket and lock file. Unlink them here so we can proceed to
293  * remove the directory. Ignore errors; leaking a temporary directory is
294  * unimportant. This can run from a signal handler. The code is not
295  * acceptable in a Windows signal handler (see initdb.c:trapsig()), but
296  * Windows is not a HAVE_UNIX_SOCKETS platform.
297  */
298 static void
299 remove_temp(void)
300 {
301  Assert(temp_sockdir);
302  unlink(sockself);
303  unlink(socklock);
304  rmdir(temp_sockdir);
305 }
306 
307 /*
308  * Signal handler that calls remove_temp() and reraises the signal.
309  */
310 static void
311 signal_remove_temp(int signum)
312 {
313  remove_temp();
314 
315  pqsignal(signum, SIG_DFL);
316  raise(signum);
317 }
318 
319 /*
320  * Create a temporary directory suitable for the server's Unix-domain socket.
321  * The directory will have mode 0700 or stricter, so no other OS user can open
322  * our socket to exploit our use of trust authentication. Most systems
323  * constrain the length of socket paths well below _POSIX_PATH_MAX, so we
324  * place the directory under /tmp rather than relative to the possibly-deep
325  * current working directory.
326  *
327  * Compared to using the compiled-in DEFAULT_PGSOCKET_DIR, this also permits
328  * testing to work in builds that relocate it to a directory not writable to
329  * the build/test user.
330  */
331 static const char *
332 make_temp_sockdir(void)
333 {
334  char *template = pg_strdup("/tmp/pg_regress-XXXXXX");
335 
336  temp_sockdir = mkdtemp(template);
337  if (temp_sockdir == NULL)
338  {
339  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
340  progname, template, strerror(errno));
341  exit(2);
342  }
343 
344  /* Stage file names for remove_temp(). Unsafe in a signal handler. */
345  UNIXSOCK_PATH(sockself, port, temp_sockdir);
346  snprintf(socklock, sizeof(socklock), "%s.lock", sockself);
347 
348  /* Remove the directory during clean exit. */
349  atexit(remove_temp);
350 
351  /*
352  * Remove the directory before dying to the usual signals. Omit SIGQUIT,
353  * preserving it as a quick, untidy exit.
354  */
355  pqsignal(SIGHUP, signal_remove_temp);
356  pqsignal(SIGINT, signal_remove_temp);
357  pqsignal(SIGPIPE, signal_remove_temp);
358  pqsignal(SIGTERM, signal_remove_temp);
359 
360  return temp_sockdir;
361 }
362 #endif /* HAVE_UNIX_SOCKETS */
363 
364 /*
365  * Check whether string matches pattern
366  *
367  * In the original shell script, this function was implemented using expr(1),
368  * which provides basic regular expressions restricted to match starting at
369  * the string start (in conventional regex terms, there's an implicit "^"
370  * at the start of the pattern --- but no implicit "$" at the end).
371  *
372  * For now, we only support "." and ".*" as non-literal metacharacters,
373  * because that's all that anyone has found use for in resultmap. This
374  * code could be extended if more functionality is needed.
375  */
376 static bool
377 string_matches_pattern(const char *str, const char *pattern)
378 {
379  while (*str && *pattern)
380  {
381  if (*pattern == '.' && pattern[1] == '*')
382  {
383  pattern += 2;
384  /* Trailing .* matches everything. */
385  if (*pattern == '\0')
386  return true;
387 
388  /*
389  * Otherwise, scan for a text position at which we can match the
390  * rest of the pattern.
391  */
392  while (*str)
393  {
394  /*
395  * Optimization to prevent most recursion: don't recurse
396  * unless first pattern char might match this text char.
397  */
398  if (*str == *pattern || *pattern == '.')
399  {
400  if (string_matches_pattern(str, pattern))
401  return true;
402  }
403 
404  str++;
405  }
406 
407  /*
408  * End of text with no match.
409  */
410  return false;
411  }
412  else if (*pattern != '.' && *str != *pattern)
413  {
414  /*
415  * Not the single-character wildcard and no explicit match? Then
416  * time to quit...
417  */
418  return false;
419  }
420 
421  str++;
422  pattern++;
423  }
424 
425  if (*pattern == '\0')
426  return true; /* end of pattern, so declare match */
427 
428  /* End of input string. Do we have matching pattern remaining? */
429  while (*pattern == '.' && pattern[1] == '*')
430  pattern += 2;
431  if (*pattern == '\0')
432  return true; /* end of pattern, so declare match */
433 
434  return false;
435 }
436 
437 /*
438  * Replace all occurrences of a string in a string with a different string.
439  * NOTE: Assumes there is enough room in the target buffer!
440  */
441 void
442 replace_string(char *string, const char *replace, const char *replacement)
443 {
444  char *ptr;
445 
446  while ((ptr = strstr(string, replace)) != NULL)
447  {
448  char *dup = pg_strdup(string);
449 
450  strlcpy(string, dup, ptr - string + 1);
451  strcat(string, replacement);
452  strcat(string, dup + (ptr - string) + strlen(replace));
453  free(dup);
454  }
455 }
456 
457 /*
458  * Convert *.source found in the "source" directory, replacing certain tokens
459  * in the file contents with their intended values, and put the resulting files
460  * in the "dest" directory, replacing the ".source" prefix in their names with
461  * the given suffix.
462  */
463 static void
464 convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
465 {
466  char testtablespace[MAXPGPATH];
467  char indir[MAXPGPATH];
468  struct stat st;
469  int ret;
470  char **name;
471  char **names;
472  int count = 0;
473 
474  snprintf(indir, MAXPGPATH, "%s/%s", inputdir, source_subdir);
475 
476  /* Check that indir actually exists and is a directory */
477  ret = stat(indir, &st);
478  if (ret != 0 || !S_ISDIR(st.st_mode))
479  {
480  /*
481  * No warning, to avoid noise in tests that do not have these
482  * directories; for example, ecpg, contrib and src/pl.
483  */
484  return;
485  }
486 
487  names = pgfnames(indir);
488  if (!names)
489  /* Error logged in pgfnames */
490  exit(2);
491 
492  snprintf(testtablespace, MAXPGPATH, "%s/testtablespace", outputdir);
493 
494 #ifdef WIN32
495 
496  /*
497  * On Windows only, clean out the test tablespace dir, or create it if it
498  * doesn't exist. On other platforms we expect the Makefile to take care
499  * of that. (We don't migrate that functionality in here because it'd be
500  * harder to cope with platform-specific issues such as SELinux.)
501  *
502  * XXX it would be better if pg_regress.c had nothing at all to do with
503  * testtablespace, and this were handled by a .BAT file or similar on
504  * Windows. See pgsql-hackers discussion of 2008-01-18.
505  */
506  if (directory_exists(testtablespace))
507  if (!rmtree(testtablespace, true))
508  {
509  fprintf(stderr, _("\n%s: could not remove test tablespace \"%s\"\n"),
510  progname, testtablespace);
511  exit(2);
512  }
513  make_directory(testtablespace);
514 #endif
515 
516  /* finally loop on each file and do the replacement */
517  for (name = names; *name; name++)
518  {
519  char srcfile[MAXPGPATH];
520  char destfile[MAXPGPATH];
521  char prefix[MAXPGPATH];
522  FILE *infile,
523  *outfile;
524  char line[1024];
525 
526  /* reject filenames not finishing in ".source" */
527  if (strlen(*name) < 8)
528  continue;
529  if (strcmp(*name + strlen(*name) - 7, ".source") != 0)
530  continue;
531 
532  count++;
533 
534  /* build the full actual paths to open */
535  snprintf(prefix, strlen(*name) - 6, "%s", *name);
536  snprintf(srcfile, MAXPGPATH, "%s/%s", indir, *name);
537  snprintf(destfile, MAXPGPATH, "%s/%s/%s.%s", dest_dir, dest_subdir,
538  prefix, suffix);
539 
540  infile = fopen(srcfile, "r");
541  if (!infile)
542  {
543  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
544  progname, srcfile, strerror(errno));
545  exit(2);
546  }
547  outfile = fopen(destfile, "w");
548  if (!outfile)
549  {
550  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
551  progname, destfile, strerror(errno));
552  exit(2);
553  }
554  while (fgets(line, sizeof(line), infile))
555  {
556  replace_string(line, "@abs_srcdir@", inputdir);
557  replace_string(line, "@abs_builddir@", outputdir);
558  replace_string(line, "@testtablespace@", testtablespace);
559  replace_string(line, "@libdir@", dlpath);
560  replace_string(line, "@DLSUFFIX@", DLSUFFIX);
561  fputs(line, outfile);
562  }
563  fclose(infile);
564  fclose(outfile);
565  }
566 
567  /*
568  * If we didn't process any files, complain because it probably means
569  * somebody neglected to pass the needed --inputdir argument.
570  */
571  if (count <= 0)
572  {
573  fprintf(stderr, _("%s: no *.source files found in \"%s\"\n"),
574  progname, indir);
575  exit(2);
576  }
577 
578  pgfnames_cleanup(names);
579 }
580 
581 /* Create the .sql and .out files from the .source files, if any */
582 static void
584 {
585  convert_sourcefiles_in("input", outputdir, "sql", "sql");
586  convert_sourcefiles_in("output", outputdir, "expected", "out");
587 }
588 
589 /*
590  * Scan resultmap file to find which platform-specific expected files to use.
591  *
592  * The format of each line of the file is
593  * testname/hostplatformpattern=substitutefile
594  * where the hostplatformpattern is evaluated per the rules of expr(1),
595  * namely, it is a standard regular expression with an implicit ^ at the start.
596  * (We currently support only a very limited subset of regular expressions,
597  * see string_matches_pattern() above.) What hostplatformpattern will be
598  * matched against is the config.guess output. (In the shell-script version,
599  * we also provided an indication of whether gcc or another compiler was in
600  * use, but that facility isn't used anymore.)
601  */
602 static void
604 {
605  char buf[MAXPGPATH];
606  FILE *f;
607 
608  /* scan the file ... */
609  snprintf(buf, sizeof(buf), "%s/resultmap", inputdir);
610  f = fopen(buf, "r");
611  if (!f)
612  {
613  /* OK if it doesn't exist, else complain */
614  if (errno == ENOENT)
615  return;
616  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
617  progname, buf, strerror(errno));
618  exit(2);
619  }
620 
621  while (fgets(buf, sizeof(buf), f))
622  {
623  char *platform;
624  char *file_type;
625  char *expected;
626  int i;
627 
628  /* strip trailing whitespace, especially the newline */
629  i = strlen(buf);
630  while (i > 0 && isspace((unsigned char) buf[i - 1]))
631  buf[--i] = '\0';
632 
633  /* parse out the line fields */
634  file_type = strchr(buf, ':');
635  if (!file_type)
636  {
637  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
638  buf);
639  exit(2);
640  }
641  *file_type++ = '\0';
642 
643  platform = strchr(file_type, ':');
644  if (!platform)
645  {
646  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
647  buf);
648  exit(2);
649  }
650  *platform++ = '\0';
651  expected = strchr(platform, '=');
652  if (!expected)
653  {
654  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
655  buf);
656  exit(2);
657  }
658  *expected++ = '\0';
659 
660  /*
661  * if it's for current platform, save it in resultmap list. Note: by
662  * adding at the front of the list, we ensure that in ambiguous cases,
663  * the last match in the resultmap file is used. This mimics the
664  * behavior of the old shell script.
665  */
666  if (string_matches_pattern(host_platform, platform))
667  {
668  _resultmap *entry = pg_malloc(sizeof(_resultmap));
669 
670  entry->test = pg_strdup(buf);
671  entry->type = pg_strdup(file_type);
672  entry->resultfile = pg_strdup(expected);
673  entry->next = resultmap;
674  resultmap = entry;
675  }
676  }
677  fclose(f);
678 }
679 
680 /*
681  * Check in resultmap if we should be looking at a different file
682  */
683 static
684 const char *
685 get_expectfile(const char *testname, const char *file)
686 {
687  char *file_type;
688  _resultmap *rm;
689 
690  /*
691  * Determine the file type from the file name. This is just what is
692  * following the last dot in the file name.
693  */
694  if (!file || !(file_type = strrchr(file, '.')))
695  return NULL;
696 
697  file_type++;
698 
699  for (rm = resultmap; rm != NULL; rm = rm->next)
700  {
701  if (strcmp(testname, rm->test) == 0 && strcmp(file_type, rm->type) == 0)
702  {
703  return rm->resultfile;
704  }
705  }
706 
707  return NULL;
708 }
709 
710 /*
711  * Handy subroutine for setting an environment variable "var" to "val"
712  */
713 static void
714 doputenv(const char *var, const char *val)
715 {
716  char *s;
717 
718  s = psprintf("%s=%s", var, val);
719  putenv(s);
720 }
721 
722 /*
723  * Prepare environment variables for running regression tests
724  */
725 static void
727 {
728  /*
729  * Set default application_name. (The test_function may choose to
730  * override this, but if it doesn't, we have something useful in place.)
731  */
732  putenv("PGAPPNAME=pg_regress");
733 
734  if (nolocale)
735  {
736  /*
737  * Clear out any non-C locale settings
738  */
739  unsetenv("LC_COLLATE");
740  unsetenv("LC_CTYPE");
741  unsetenv("LC_MONETARY");
742  unsetenv("LC_NUMERIC");
743  unsetenv("LC_TIME");
744  unsetenv("LANG");
745 
746  /*
747  * Most platforms have adopted the POSIX locale as their
748  * implementation-defined default locale. Exceptions include native
749  * Windows, macOS with --enable-nls, and Cygwin with --enable-nls.
750  * (Use of --enable-nls matters because libintl replaces setlocale().)
751  * Also, PostgreSQL does not support macOS with locale environment
752  * variables unset; see PostmasterMain().
753  */
754 #if defined(WIN32) || defined(__CYGWIN__) || defined(__darwin__)
755  putenv("LANG=C");
756 #endif
757  }
758 
759  /*
760  * Set translation-related settings to English; otherwise psql will
761  * produce translated messages and produce diffs. (XXX If we ever support
762  * translation of pg_regress, this needs to be moved elsewhere, where psql
763  * is actually called.)
764  */
765  unsetenv("LANGUAGE");
766  unsetenv("LC_ALL");
767  putenv("LC_MESSAGES=C");
768 
769  /*
770  * Set encoding as requested
771  */
772  if (encoding)
773  doputenv("PGCLIENTENCODING", encoding);
774  else
775  unsetenv("PGCLIENTENCODING");
776 
777  /*
778  * Set timezone and datestyle for datetime-related tests
779  */
780  putenv("PGTZ=PST8PDT");
781  putenv("PGDATESTYLE=Postgres, MDY");
782 
783  /*
784  * Likewise set intervalstyle to ensure consistent results. This is a bit
785  * more painful because we must use PGOPTIONS, and we want to preserve the
786  * user's ability to set other variables through that.
787  */
788  {
789  const char *my_pgoptions = "-c intervalstyle=postgres_verbose";
790  const char *old_pgoptions = getenv("PGOPTIONS");
791  char *new_pgoptions;
792 
793  if (!old_pgoptions)
794  old_pgoptions = "";
795  new_pgoptions = psprintf("PGOPTIONS=%s %s",
796  old_pgoptions, my_pgoptions);
797  putenv(new_pgoptions);
798  }
799 
800  if (temp_instance)
801  {
802  /*
803  * Clear out any environment vars that might cause psql to connect to
804  * the wrong postmaster, or otherwise behave in nondefault ways. (Note
805  * we also use psql's -X switch consistently, so that ~/.psqlrc files
806  * won't mess things up.) Also, set PGPORT to the temp port, and set
807  * PGHOST depending on whether we are using TCP or Unix sockets.
808  */
809  unsetenv("PGDATABASE");
810  unsetenv("PGUSER");
811  unsetenv("PGSERVICE");
812  unsetenv("PGSSLMODE");
813  unsetenv("PGREQUIRESSL");
814  unsetenv("PGCONNECT_TIMEOUT");
815  unsetenv("PGDATA");
816 #ifdef HAVE_UNIX_SOCKETS
817  if (hostname != NULL)
818  doputenv("PGHOST", hostname);
819  else
820  {
821  sockdir = getenv("PG_REGRESS_SOCK_DIR");
822  if (!sockdir)
823  sockdir = make_temp_sockdir();
824  doputenv("PGHOST", sockdir);
825  }
826 #else
827  Assert(hostname != NULL);
828  doputenv("PGHOST", hostname);
829 #endif
830  unsetenv("PGHOSTADDR");
831  if (port != -1)
832  {
833  char s[16];
834 
835  sprintf(s, "%d", port);
836  doputenv("PGPORT", s);
837  }
838  }
839  else
840  {
841  const char *pghost;
842  const char *pgport;
843 
844  /*
845  * When testing an existing install, we honor existing environment
846  * variables, except if they're overridden by command line options.
847  */
848  if (hostname != NULL)
849  {
850  doputenv("PGHOST", hostname);
851  unsetenv("PGHOSTADDR");
852  }
853  if (port != -1)
854  {
855  char s[16];
856 
857  sprintf(s, "%d", port);
858  doputenv("PGPORT", s);
859  }
860  if (user != NULL)
861  doputenv("PGUSER", user);
862 
863  /*
864  * However, we *don't* honor PGDATABASE, since we certainly don't wish
865  * to connect to whatever database the user might like as default.
866  * (Most tests override PGDATABASE anyway, but there are some ECPG
867  * test cases that don't.)
868  */
869  unsetenv("PGDATABASE");
870 
871  /*
872  * Report what we're connecting to
873  */
874  pghost = getenv("PGHOST");
875  pgport = getenv("PGPORT");
876 #ifndef HAVE_UNIX_SOCKETS
877  if (!pghost)
878  pghost = "localhost";
879 #endif
880 
881  if (pghost && pgport)
882  printf(_("(using postmaster on %s, port %s)\n"), pghost, pgport);
883  if (pghost && !pgport)
884  printf(_("(using postmaster on %s, default port)\n"), pghost);
885  if (!pghost && pgport)
886  printf(_("(using postmaster on Unix socket, port %s)\n"), pgport);
887  if (!pghost && !pgport)
888  printf(_("(using postmaster on Unix socket, default port)\n"));
889  }
890 
892  load_resultmap();
893 }
894 
895 #ifdef ENABLE_SSPI
896 
897 /* support for config_sspi_auth() */
898 static const char *
899 fmtHba(const char *raw)
900 {
901  static char *ret;
902  const char *rp;
903  char *wp;
904 
905  wp = ret = realloc(ret, 3 + strlen(raw) * 2);
906 
907  *wp++ = '"';
908  for (rp = raw; *rp; rp++)
909  {
910  if (*rp == '"')
911  *wp++ = '"';
912  *wp++ = *rp;
913  }
914  *wp++ = '"';
915  *wp++ = '\0';
916 
917  return ret;
918 }
919 
920 /*
921  * Get account and domain/realm names for the current user. This is based on
922  * pg_SSPI_recvauth(). The returned strings use static storage.
923  */
924 static void
925 current_windows_user(const char **acct, const char **dom)
926 {
927  static char accountname[MAXPGPATH];
928  static char domainname[MAXPGPATH];
929  HANDLE token;
930  TOKEN_USER *tokenuser;
931  DWORD retlen;
932  DWORD accountnamesize = sizeof(accountname);
933  DWORD domainnamesize = sizeof(domainname);
934  SID_NAME_USE accountnameuse;
935 
936  if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token))
937  {
938  fprintf(stderr,
939  _("%s: could not open process token: error code %lu\n"),
940  progname, GetLastError());
941  exit(2);
942  }
943 
944  if (!GetTokenInformation(token, TokenUser, NULL, 0, &retlen) && GetLastError() != 122)
945  {
946  fprintf(stderr,
947  _("%s: could not get token information buffer size: error code %lu\n"),
948  progname, GetLastError());
949  exit(2);
950  }
951  tokenuser = pg_malloc(retlen);
952  if (!GetTokenInformation(token, TokenUser, tokenuser, retlen, &retlen))
953  {
954  fprintf(stderr,
955  _("%s: could not get token information: error code %lu\n"),
956  progname, GetLastError());
957  exit(2);
958  }
959 
960  if (!LookupAccountSid(NULL, tokenuser->User.Sid, accountname, &accountnamesize,
961  domainname, &domainnamesize, &accountnameuse))
962  {
963  fprintf(stderr,
964  _("%s: could not look up account SID: error code %lu\n"),
965  progname, GetLastError());
966  exit(2);
967  }
968 
969  free(tokenuser);
970 
971  *acct = accountname;
972  *dom = domainname;
973 }
974 
975 /*
976  * Rewrite pg_hba.conf and pg_ident.conf to use SSPI authentication. Permit
977  * the current OS user to authenticate as the bootstrap superuser and as any
978  * user named in a --create-role option.
979  *
980  * In --config-auth mode, the --user switch can be used to specify the
981  * bootstrap superuser's name, otherwise we assume it is the default.
982  */
983 static void
984 config_sspi_auth(const char *pgdata, const char *superuser_name)
985 {
986  const char *accountname,
987  *domainname;
988  char *errstr;
989  bool have_ipv6;
990  char fname[MAXPGPATH];
991  int res;
992  FILE *hba,
993  *ident;
994  _stringlist *sl;
995 
996  /* Find out the name of the current OS user */
997  current_windows_user(&accountname, &domainname);
998 
999  /* Determine the bootstrap superuser's name */
1000  if (superuser_name == NULL)
1001  {
1002  /*
1003  * Compute the default superuser name the same way initdb does.
1004  *
1005  * It's possible that this result always matches "accountname", the
1006  * value SSPI authentication discovers. But the underlying system
1007  * functions do not clearly guarantee that.
1008  */
1009  superuser_name = get_user_name(&errstr);
1010  if (superuser_name == NULL)
1011  {
1012  fprintf(stderr, "%s: %s\n", progname, errstr);
1013  exit(2);
1014  }
1015  }
1016 
1017  /*
1018  * Like initdb.c:setup_config(), determine whether the platform recognizes
1019  * ::1 (IPv6 loopback) as a numeric host address string.
1020  */
1021  {
1022  struct addrinfo *gai_result;
1023  struct addrinfo hints;
1024  WSADATA wsaData;
1025 
1026  hints.ai_flags = AI_NUMERICHOST;
1027  hints.ai_family = AF_UNSPEC;
1028  hints.ai_socktype = 0;
1029  hints.ai_protocol = 0;
1030  hints.ai_addrlen = 0;
1031  hints.ai_canonname = NULL;
1032  hints.ai_addr = NULL;
1033  hints.ai_next = NULL;
1034 
1035  have_ipv6 = (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0 &&
1036  getaddrinfo("::1", NULL, &hints, &gai_result) == 0);
1037  }
1038 
1039  /* Check a Write outcome and report any error. */
1040 #define CW(cond) \
1041  do { \
1042  if (!(cond)) \
1043  { \
1044  fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), \
1045  progname, fname, strerror(errno)); \
1046  exit(2); \
1047  } \
1048  } while (0)
1049 
1050  res = snprintf(fname, sizeof(fname), "%s/pg_hba.conf", pgdata);
1051  if (res < 0 || res >= sizeof(fname))
1052  {
1053  /*
1054  * Truncating this name is a fatal error, because we must not fail to
1055  * overwrite an original trust-authentication pg_hba.conf.
1056  */
1057  fprintf(stderr, _("%s: directory name too long\n"), progname);
1058  exit(2);
1059  }
1060  hba = fopen(fname, "w");
1061  if (hba == NULL)
1062  {
1063  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1064  progname, fname, strerror(errno));
1065  exit(2);
1066  }
1067  CW(fputs("# Configuration written by config_sspi_auth()\n", hba) >= 0);
1068  CW(fputs("host all all 127.0.0.1/32 sspi include_realm=1 map=regress\n",
1069  hba) >= 0);
1070  if (have_ipv6)
1071  CW(fputs("host all all ::1/128 sspi include_realm=1 map=regress\n",
1072  hba) >= 0);
1073  CW(fclose(hba) == 0);
1074 
1075  snprintf(fname, sizeof(fname), "%s/pg_ident.conf", pgdata);
1076  ident = fopen(fname, "w");
1077  if (ident == NULL)
1078  {
1079  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1080  progname, fname, strerror(errno));
1081  exit(2);
1082  }
1083  CW(fputs("# Configuration written by config_sspi_auth()\n", ident) >= 0);
1084 
1085  /*
1086  * Double-quote for the benefit of account names containing whitespace or
1087  * '#'. Windows forbids the double-quote character itself, so don't
1088  * bother escaping embedded double-quote characters.
1089  */
1090  CW(fprintf(ident, "regress \"%s@%s\" %s\n",
1091  accountname, domainname, fmtHba(superuser_name)) >= 0);
1092  for (sl = extraroles; sl; sl = sl->next)
1093  CW(fprintf(ident, "regress \"%s@%s\" %s\n",
1094  accountname, domainname, fmtHba(sl->str)) >= 0);
1095  CW(fclose(ident) == 0);
1096 }
1097 
1098 #endif /* ENABLE_SSPI */
1099 
1100 /*
1101  * Issue a command via psql, connecting to the specified database
1102  *
1103  * Since we use system(), this doesn't return until the operation finishes
1104  */
1105 static void
1106 psql_command(const char *database, const char *query,...)
1107 {
1108  char query_formatted[1024];
1109  char query_escaped[2048];
1110  char psql_cmd[MAXPGPATH + 2048];
1111  va_list args;
1112  char *s;
1113  char *d;
1114 
1115  /* Generate the query with insertion of sprintf arguments */
1116  va_start(args, query);
1117  vsnprintf(query_formatted, sizeof(query_formatted), query, args);
1118  va_end(args);
1119 
1120  /* Now escape any shell double-quote metacharacters */
1121  d = query_escaped;
1122  for (s = query_formatted; *s; s++)
1123  {
1124  if (strchr("\\\"$`", *s))
1125  *d++ = '\\';
1126  *d++ = *s;
1127  }
1128  *d = '\0';
1129 
1130  /* And now we can build and execute the shell command */
1131  snprintf(psql_cmd, sizeof(psql_cmd),
1132  "\"%s%spsql\" -X -c \"%s\" \"%s\"",
1133  bindir ? bindir : "",
1134  bindir ? "/" : "",
1135  query_escaped,
1136  database);
1137 
1138  if (system(psql_cmd) != 0)
1139  {
1140  /* psql probably already reported the error */
1141  fprintf(stderr, _("command failed: %s\n"), psql_cmd);
1142  exit(2);
1143  }
1144 }
1145 
1146 /*
1147  * Spawn a process to execute the given shell command; don't wait for it
1148  *
1149  * Returns the process ID (or HANDLE) so we can wait for it later
1150  */
1151 PID_TYPE
1152 spawn_process(const char *cmdline)
1153 {
1154 #ifndef WIN32
1155  pid_t pid;
1156 
1157  /*
1158  * Must flush I/O buffers before fork. Ideally we'd use fflush(NULL) here
1159  * ... does anyone still care about systems where that doesn't work?
1160  */
1161  fflush(stdout);
1162  fflush(stderr);
1163  if (logfile)
1164  fflush(logfile);
1165 
1166  pid = fork();
1167  if (pid == -1)
1168  {
1169  fprintf(stderr, _("%s: could not fork: %s\n"),
1170  progname, strerror(errno));
1171  exit(2);
1172  }
1173  if (pid == 0)
1174  {
1175  /*
1176  * In child
1177  *
1178  * Instead of using system(), exec the shell directly, and tell it to
1179  * "exec" the command too. This saves two useless processes per
1180  * parallel test case.
1181  */
1182  char *cmdline2;
1183 
1184  cmdline2 = psprintf("exec %s", cmdline);
1185  execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
1186  fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
1187  progname, shellprog, strerror(errno));
1188  _exit(1); /* not exit() here... */
1189  }
1190  /* in parent */
1191  return pid;
1192 #else
1193  PROCESS_INFORMATION pi;
1194  char *cmdline2;
1195  HANDLE restrictedToken;
1196  const char *comspec;
1197 
1198  /* Find CMD.EXE location using COMSPEC, if it's set */
1199  comspec = getenv("COMSPEC");
1200  if (comspec == NULL)
1201  comspec = "CMD";
1202 
1203  memset(&pi, 0, sizeof(pi));
1204  cmdline2 = psprintf("\"%s\" /c \"%s\"", comspec, cmdline);
1205 
1206  if ((restrictedToken =
1207  CreateRestrictedProcess(cmdline2, &pi)) == 0)
1208  exit(2);
1209 
1210  CloseHandle(pi.hThread);
1211  return pi.hProcess;
1212 #endif
1213 }
1214 
1215 /*
1216  * Count bytes in file
1217  */
1218 static long
1219 file_size(const char *file)
1220 {
1221  long r;
1222  FILE *f = fopen(file, "r");
1223 
1224  if (!f)
1225  {
1226  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1227  progname, file, strerror(errno));
1228  return -1;
1229  }
1230  fseek(f, 0, SEEK_END);
1231  r = ftell(f);
1232  fclose(f);
1233  return r;
1234 }
1235 
1236 /*
1237  * Count lines in file
1238  */
1239 static int
1240 file_line_count(const char *file)
1241 {
1242  int c;
1243  int l = 0;
1244  FILE *f = fopen(file, "r");
1245 
1246  if (!f)
1247  {
1248  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1249  progname, file, strerror(errno));
1250  return -1;
1251  }
1252  while ((c = fgetc(f)) != EOF)
1253  {
1254  if (c == '\n')
1255  l++;
1256  }
1257  fclose(f);
1258  return l;
1259 }
1260 
1261 bool
1262 file_exists(const char *file)
1263 {
1264  FILE *f = fopen(file, "r");
1265 
1266  if (!f)
1267  return false;
1268  fclose(f);
1269  return true;
1270 }
1271 
1272 static bool
1273 directory_exists(const char *dir)
1274 {
1275  struct stat st;
1276 
1277  if (stat(dir, &st) != 0)
1278  return false;
1279  if (S_ISDIR(st.st_mode))
1280  return true;
1281  return false;
1282 }
1283 
1284 /* Create a directory */
1285 static void
1286 make_directory(const char *dir)
1287 {
1288  if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
1289  {
1290  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
1291  progname, dir, strerror(errno));
1292  exit(2);
1293  }
1294 }
1295 
1296 /*
1297  * In: filename.ext, Return: filename_i.ext, where 0 < i <= 9
1298  */
1299 static char *
1300 get_alternative_expectfile(const char *expectfile, int i)
1301 {
1302  char *last_dot;
1303  int ssize = strlen(expectfile) + 2 + 1;
1304  char *tmp;
1305  char *s;
1306 
1307  if (!(tmp = (char *) malloc(ssize)))
1308  return NULL;
1309 
1310  if (!(s = (char *) malloc(ssize)))
1311  {
1312  free(tmp);
1313  return NULL;
1314  }
1315 
1316  strcpy(tmp, expectfile);
1317  last_dot = strrchr(tmp, '.');
1318  if (!last_dot)
1319  {
1320  free(tmp);
1321  free(s);
1322  return NULL;
1323  }
1324  *last_dot = '\0';
1325  snprintf(s, ssize, "%s_%d.%s", tmp, i, last_dot + 1);
1326  free(tmp);
1327  return s;
1328 }
1329 
1330 /*
1331  * Run a "diff" command and also check that it didn't crash
1332  */
1333 static int
1334 run_diff(const char *cmd, const char *filename)
1335 {
1336  int r;
1337 
1338  r = system(cmd);
1339  if (!WIFEXITED(r) || WEXITSTATUS(r) > 1)
1340  {
1341  fprintf(stderr, _("diff command failed with status %d: %s\n"), r, cmd);
1342  exit(2);
1343  }
1344 #ifdef WIN32
1345 
1346  /*
1347  * On WIN32, if the 'diff' command cannot be found, system() returns 1,
1348  * but produces nothing to stdout, so we check for that here.
1349  */
1350  if (WEXITSTATUS(r) == 1 && file_size(filename) <= 0)
1351  {
1352  fprintf(stderr, _("diff command not found: %s\n"), cmd);
1353  exit(2);
1354  }
1355 #endif
1356 
1357  return WEXITSTATUS(r);
1358 }
1359 
1360 /*
1361  * Check the actual result file for the given test against expected results
1362  *
1363  * Returns true if different (failure), false if correct match found.
1364  * In the true case, the diff is appended to the diffs file.
1365  */
1366 static bool
1367 results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
1368 {
1369  char expectfile[MAXPGPATH];
1370  char diff[MAXPGPATH];
1371  char cmd[MAXPGPATH * 3];
1372  char best_expect_file[MAXPGPATH];
1373  FILE *difffile;
1374  int best_line_count;
1375  int i;
1376  int l;
1377  const char *platform_expectfile;
1378 
1379  /*
1380  * We can pass either the resultsfile or the expectfile, they should have
1381  * the same type (filename.type) anyway.
1382  */
1383  platform_expectfile = get_expectfile(testname, resultsfile);
1384 
1385  strlcpy(expectfile, default_expectfile, sizeof(expectfile));
1386  if (platform_expectfile)
1387  {
1388  /*
1389  * Replace everything after the last slash in expectfile with what the
1390  * platform_expectfile contains.
1391  */
1392  char *p = strrchr(expectfile, '/');
1393 
1394  if (p)
1395  strcpy(++p, platform_expectfile);
1396  }
1397 
1398  /* Name to use for temporary diff file */
1399  snprintf(diff, sizeof(diff), "%s.diff", resultsfile);
1400 
1401  /* OK, run the diff */
1402  snprintf(cmd, sizeof(cmd),
1403  "diff %s \"%s\" \"%s\" > \"%s\"",
1404  basic_diff_opts, expectfile, resultsfile, diff);
1405 
1406  /* Is the diff file empty? */
1407  if (run_diff(cmd, diff) == 0)
1408  {
1409  unlink(diff);
1410  return false;
1411  }
1412 
1413  /* There may be secondary comparison files that match better */
1414  best_line_count = file_line_count(diff);
1415  strcpy(best_expect_file, expectfile);
1416 
1417  for (i = 0; i <= 9; i++)
1418  {
1419  char *alt_expectfile;
1420 
1421  alt_expectfile = get_alternative_expectfile(expectfile, i);
1422  if (!alt_expectfile)
1423  {
1424  fprintf(stderr, _("Unable to check secondary comparison files: %s\n"),
1425  strerror(errno));
1426  exit(2);
1427  }
1428 
1429  if (!file_exists(alt_expectfile))
1430  {
1431  free(alt_expectfile);
1432  continue;
1433  }
1434 
1435  snprintf(cmd, sizeof(cmd),
1436  "diff %s \"%s\" \"%s\" > \"%s\"",
1437  basic_diff_opts, alt_expectfile, resultsfile, diff);
1438 
1439  if (run_diff(cmd, diff) == 0)
1440  {
1441  unlink(diff);
1442  free(alt_expectfile);
1443  return false;
1444  }
1445 
1446  l = file_line_count(diff);
1447  if (l < best_line_count)
1448  {
1449  /* This diff was a better match than the last one */
1450  best_line_count = l;
1451  strlcpy(best_expect_file, alt_expectfile, sizeof(best_expect_file));
1452  }
1453  free(alt_expectfile);
1454  }
1455 
1456  /*
1457  * fall back on the canonical results file if we haven't tried it yet and
1458  * haven't found a complete match yet.
1459  */
1460 
1461  if (platform_expectfile)
1462  {
1463  snprintf(cmd, sizeof(cmd),
1464  "diff %s \"%s\" \"%s\" > \"%s\"",
1465  basic_diff_opts, default_expectfile, resultsfile, diff);
1466 
1467  if (run_diff(cmd, diff) == 0)
1468  {
1469  /* No diff = no changes = good */
1470  unlink(diff);
1471  return false;
1472  }
1473 
1474  l = file_line_count(diff);
1475  if (l < best_line_count)
1476  {
1477  /* This diff was a better match than the last one */
1478  best_line_count = l;
1479  strlcpy(best_expect_file, default_expectfile, sizeof(best_expect_file));
1480  }
1481  }
1482 
1483  /*
1484  * Use the best comparison file to generate the "pretty" diff, which we
1485  * append to the diffs summary file.
1486  */
1487 
1488  /* Write diff header */
1489  difffile = fopen(difffilename, "a");
1490  if (difffile)
1491  {
1492  fprintf(difffile,
1493  "diff %s %s %s\n",
1494  pretty_diff_opts, best_expect_file, resultsfile);
1495  fclose(difffile);
1496  }
1497 
1498  /* Run diff */
1499  snprintf(cmd, sizeof(cmd),
1500  "diff %s \"%s\" \"%s\" >> \"%s\"",
1501  pretty_diff_opts, best_expect_file, resultsfile, difffilename);
1502  run_diff(cmd, difffilename);
1503 
1504  unlink(diff);
1505  return true;
1506 }
1507 
1508 /*
1509  * Wait for specified subprocesses to finish, and return their exit
1510  * statuses into statuses[] and stop times into stoptimes[]
1511  *
1512  * If names isn't NULL, print each subprocess's name as it finishes
1513  *
1514  * Note: it's OK to scribble on the pids array, but not on the names array
1515  */
1516 static void
1517 wait_for_tests(PID_TYPE * pids, int *statuses, instr_time *stoptimes,
1518  char **names, int num_tests)
1519 {
1520  int tests_left;
1521  int i;
1522 
1523 #ifdef WIN32
1524  PID_TYPE *active_pids = pg_malloc(num_tests * sizeof(PID_TYPE));
1525 
1526  memcpy(active_pids, pids, num_tests * sizeof(PID_TYPE));
1527 #endif
1528 
1529  tests_left = num_tests;
1530  while (tests_left > 0)
1531  {
1532  PID_TYPE p;
1533 
1534 #ifndef WIN32
1535  int exit_status;
1536 
1537  p = wait(&exit_status);
1538 
1539  if (p == INVALID_PID)
1540  {
1541  fprintf(stderr, _("failed to wait for subprocesses: %s\n"),
1542  strerror(errno));
1543  exit(2);
1544  }
1545 #else
1546  DWORD exit_status;
1547  int r;
1548 
1549  r = WaitForMultipleObjects(tests_left, active_pids, FALSE, INFINITE);
1550  if (r < WAIT_OBJECT_0 || r >= WAIT_OBJECT_0 + tests_left)
1551  {
1552  fprintf(stderr, _("failed to wait for subprocesses: error code %lu\n"),
1553  GetLastError());
1554  exit(2);
1555  }
1556  p = active_pids[r - WAIT_OBJECT_0];
1557  /* compact the active_pids array */
1558  active_pids[r - WAIT_OBJECT_0] = active_pids[tests_left - 1];
1559 #endif /* WIN32 */
1560 
1561  for (i = 0; i < num_tests; i++)
1562  {
1563  if (p == pids[i])
1564  {
1565 #ifdef WIN32
1566  GetExitCodeProcess(pids[i], &exit_status);
1567  CloseHandle(pids[i]);
1568 #endif
1569  pids[i] = INVALID_PID;
1570  statuses[i] = (int) exit_status;
1571  INSTR_TIME_SET_CURRENT(stoptimes[i]);
1572  if (names)
1573  status(" %s", names[i]);
1574  tests_left--;
1575  break;
1576  }
1577  }
1578  }
1579 
1580 #ifdef WIN32
1581  free(active_pids);
1582 #endif
1583 }
1584 
1585 /*
1586  * report nonzero exit code from a test process
1587  */
1588 static void
1589 log_child_failure(int exitstatus)
1590 {
1591  if (WIFEXITED(exitstatus))
1592  status(_(" (test process exited with exit code %d)"),
1593  WEXITSTATUS(exitstatus));
1594  else if (WIFSIGNALED(exitstatus))
1595  {
1596 #if defined(WIN32)
1597  status(_(" (test process was terminated by exception 0x%X)"),
1598  WTERMSIG(exitstatus));
1599 #else
1600  status(_(" (test process was terminated by signal %d: %s)"),
1601  WTERMSIG(exitstatus), pg_strsignal(WTERMSIG(exitstatus)));
1602 #endif
1603  }
1604  else
1605  status(_(" (test process exited with unrecognized status %d)"),
1606  exitstatus);
1607 }
1608 
1609 /*
1610  * Run all the tests specified in one schedule file
1611  */
1612 static void
1613 run_schedule(const char *schedule, test_function tfunc)
1614 {
1615 #define MAX_PARALLEL_TESTS 100
1616  char *tests[MAX_PARALLEL_TESTS];
1617  _stringlist *resultfiles[MAX_PARALLEL_TESTS];
1618  _stringlist *expectfiles[MAX_PARALLEL_TESTS];
1621  instr_time starttimes[MAX_PARALLEL_TESTS];
1622  instr_time stoptimes[MAX_PARALLEL_TESTS];
1623  int statuses[MAX_PARALLEL_TESTS];
1624  _stringlist *ignorelist = NULL;
1625  char scbuf[1024];
1626  FILE *scf;
1627  int line_num = 0;
1628 
1629  memset(tests, 0, sizeof(tests));
1630  memset(resultfiles, 0, sizeof(resultfiles));
1631  memset(expectfiles, 0, sizeof(expectfiles));
1632  memset(tags, 0, sizeof(tags));
1633 
1634  scf = fopen(schedule, "r");
1635  if (!scf)
1636  {
1637  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1638  progname, schedule, strerror(errno));
1639  exit(2);
1640  }
1641 
1642  while (fgets(scbuf, sizeof(scbuf), scf))
1643  {
1644  char *test = NULL;
1645  char *c;
1646  int num_tests;
1647  bool inword;
1648  int i;
1649 
1650  line_num++;
1651 
1652  /* strip trailing whitespace, especially the newline */
1653  i = strlen(scbuf);
1654  while (i > 0 && isspace((unsigned char) scbuf[i - 1]))
1655  scbuf[--i] = '\0';
1656 
1657  if (scbuf[0] == '\0' || scbuf[0] == '#')
1658  continue;
1659  if (strncmp(scbuf, "test: ", 6) == 0)
1660  test = scbuf + 6;
1661  else if (strncmp(scbuf, "ignore: ", 8) == 0)
1662  {
1663  c = scbuf + 8;
1664  while (*c && isspace((unsigned char) *c))
1665  c++;
1666  add_stringlist_item(&ignorelist, c);
1667 
1668  /*
1669  * Note: ignore: lines do not run the test, they just say that
1670  * failure of this test when run later on is to be ignored. A bit
1671  * odd but that's how the shell-script version did it.
1672  */
1673  continue;
1674  }
1675  else
1676  {
1677  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1678  schedule, line_num, scbuf);
1679  exit(2);
1680  }
1681 
1682  num_tests = 0;
1683  inword = false;
1684  for (c = test;; c++)
1685  {
1686  if (*c == '\0' || isspace((unsigned char) *c))
1687  {
1688  if (inword)
1689  {
1690  /* Reached end of a test name */
1691  char sav;
1692 
1693  if (num_tests >= MAX_PARALLEL_TESTS)
1694  {
1695  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1696  MAX_PARALLEL_TESTS, schedule, line_num, scbuf);
1697  exit(2);
1698  }
1699  sav = *c;
1700  *c = '\0';
1701  tests[num_tests] = pg_strdup(test);
1702  num_tests++;
1703  *c = sav;
1704  inword = false;
1705  }
1706  if (*c == '\0')
1707  break; /* loop exit is here */
1708  }
1709  else if (!inword)
1710  {
1711  /* Start of a test name */
1712  test = c;
1713  inword = true;
1714  }
1715  }
1716 
1717  if (num_tests == 0)
1718  {
1719  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1720  schedule, line_num, scbuf);
1721  exit(2);
1722  }
1723 
1724  if (num_tests == 1)
1725  {
1726  status(_("test %-28s ... "), tests[0]);
1727  pids[0] = (tfunc) (tests[0], &resultfiles[0], &expectfiles[0], &tags[0]);
1728  INSTR_TIME_SET_CURRENT(starttimes[0]);
1729  wait_for_tests(pids, statuses, stoptimes, NULL, 1);
1730  /* status line is finished below */
1731  }
1732  else if (max_concurrent_tests > 0 && max_concurrent_tests < num_tests)
1733  {
1734  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1735  max_concurrent_tests, schedule, line_num, scbuf);
1736  exit(2);
1737  }
1738  else if (max_connections > 0 && max_connections < num_tests)
1739  {
1740  int oldest = 0;
1741 
1742  status(_("parallel group (%d tests, in groups of %d): "),
1743  num_tests, max_connections);
1744  for (i = 0; i < num_tests; i++)
1745  {
1746  if (i - oldest >= max_connections)
1747  {
1748  wait_for_tests(pids + oldest, statuses + oldest,
1749  stoptimes + oldest,
1750  tests + oldest, i - oldest);
1751  oldest = i;
1752  }
1753  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1754  INSTR_TIME_SET_CURRENT(starttimes[i]);
1755  }
1756  wait_for_tests(pids + oldest, statuses + oldest,
1757  stoptimes + oldest,
1758  tests + oldest, i - oldest);
1759  status_end();
1760  }
1761  else
1762  {
1763  status(_("parallel group (%d tests): "), num_tests);
1764  for (i = 0; i < num_tests; i++)
1765  {
1766  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1767  INSTR_TIME_SET_CURRENT(starttimes[i]);
1768  }
1769  wait_for_tests(pids, statuses, stoptimes, tests, num_tests);
1770  status_end();
1771  }
1772 
1773  /* Check results for all tests */
1774  for (i = 0; i < num_tests; i++)
1775  {
1776  _stringlist *rl,
1777  *el,
1778  *tl;
1779  bool differ = false;
1780 
1781  if (num_tests > 1)
1782  status(_(" %-28s ... "), tests[i]);
1783 
1784  /*
1785  * Advance over all three lists simultaneously.
1786  *
1787  * Compare resultfiles[j] with expectfiles[j] always. Tags are
1788  * optional but if there are tags, the tag list has the same
1789  * length as the other two lists.
1790  */
1791  for (rl = resultfiles[i], el = expectfiles[i], tl = tags[i];
1792  rl != NULL; /* rl and el have the same length */
1793  rl = rl->next, el = el->next,
1794  tl = tl ? tl->next : NULL)
1795  {
1796  bool newdiff;
1797 
1798  newdiff = results_differ(tests[i], rl->str, el->str);
1799  if (newdiff && tl)
1800  {
1801  printf("%s ", tl->str);
1802  }
1803  differ |= newdiff;
1804  }
1805 
1806  if (differ)
1807  {
1808  bool ignore = false;
1809  _stringlist *sl;
1810 
1811  for (sl = ignorelist; sl != NULL; sl = sl->next)
1812  {
1813  if (strcmp(tests[i], sl->str) == 0)
1814  {
1815  ignore = true;
1816  break;
1817  }
1818  }
1819  if (ignore)
1820  {
1821  status(_("failed (ignored)"));
1823  }
1824  else
1825  {
1826  status(_("FAILED"));
1827  fail_count++;
1828  }
1829  }
1830  else
1831  {
1832  status(_("ok ")); /* align with FAILED */
1833  success_count++;
1834  }
1835 
1836  if (statuses[i] != 0)
1837  log_child_failure(statuses[i]);
1838 
1839  INSTR_TIME_SUBTRACT(stoptimes[i], starttimes[i]);
1840  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptimes[i]));
1841 
1842  status_end();
1843  }
1844 
1845  for (i = 0; i < num_tests; i++)
1846  {
1847  pg_free(tests[i]);
1848  tests[i] = NULL;
1849  free_stringlist(&resultfiles[i]);
1850  free_stringlist(&expectfiles[i]);
1851  free_stringlist(&tags[i]);
1852  }
1853  }
1854 
1855  free_stringlist(&ignorelist);
1856 
1857  fclose(scf);
1858 }
1859 
1860 /*
1861  * Run a single test
1862  */
1863 static void
1865 {
1866  PID_TYPE pid;
1867  instr_time starttime;
1868  instr_time stoptime;
1869  int exit_status;
1870  _stringlist *resultfiles = NULL;
1871  _stringlist *expectfiles = NULL;
1872  _stringlist *tags = NULL;
1873  _stringlist *rl,
1874  *el,
1875  *tl;
1876  bool differ = false;
1877 
1878  status(_("test %-28s ... "), test);
1879  pid = (tfunc) (test, &resultfiles, &expectfiles, &tags);
1880  INSTR_TIME_SET_CURRENT(starttime);
1881  wait_for_tests(&pid, &exit_status, &stoptime, NULL, 1);
1882 
1883  /*
1884  * Advance over all three lists simultaneously.
1885  *
1886  * Compare resultfiles[j] with expectfiles[j] always. Tags are optional
1887  * but if there are tags, the tag list has the same length as the other
1888  * two lists.
1889  */
1890  for (rl = resultfiles, el = expectfiles, tl = tags;
1891  rl != NULL; /* rl and el have the same length */
1892  rl = rl->next, el = el->next,
1893  tl = tl ? tl->next : NULL)
1894  {
1895  bool newdiff;
1896 
1897  newdiff = results_differ(test, rl->str, el->str);
1898  if (newdiff && tl)
1899  {
1900  printf("%s ", tl->str);
1901  }
1902  differ |= newdiff;
1903  }
1904 
1905  if (differ)
1906  {
1907  status(_("FAILED"));
1908  fail_count++;
1909  }
1910  else
1911  {
1912  status(_("ok ")); /* align with FAILED */
1913  success_count++;
1914  }
1915 
1916  if (exit_status != 0)
1917  log_child_failure(exit_status);
1918 
1919  INSTR_TIME_SUBTRACT(stoptime, starttime);
1920  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptime));
1921 
1922  status_end();
1923 }
1924 
1925 /*
1926  * Create the summary-output files (making them empty if already existing)
1927  */
1928 static void
1930 {
1931  char file[MAXPGPATH];
1932  FILE *difffile;
1933 
1934  /* create outputdir directory if not present */
1937 
1938  /* create the log file (copy of running status output) */
1939  snprintf(file, sizeof(file), "%s/regression.out", outputdir);
1940  logfilename = pg_strdup(file);
1941  logfile = fopen(logfilename, "w");
1942  if (!logfile)
1943  {
1944  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1945  progname, logfilename, strerror(errno));
1946  exit(2);
1947  }
1948 
1949  /* create the diffs file as empty */
1950  snprintf(file, sizeof(file), "%s/regression.diffs", outputdir);
1951  difffilename = pg_strdup(file);
1952  difffile = fopen(difffilename, "w");
1953  if (!difffile)
1954  {
1955  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1956  progname, difffilename, strerror(errno));
1957  exit(2);
1958  }
1959  /* we don't keep the diffs file open continuously */
1960  fclose(difffile);
1961 
1962  /* also create the results directory if not present */
1963  snprintf(file, sizeof(file), "%s/results", outputdir);
1964  if (!directory_exists(file))
1965  make_directory(file);
1966 }
1967 
1968 static void
1970 {
1971  header(_("dropping database \"%s\""), dbname);
1972  psql_command("postgres", "DROP DATABASE IF EXISTS \"%s\"", dbname);
1973 }
1974 
1975 static void
1977 {
1978  _stringlist *sl;
1979 
1980  /*
1981  * We use template0 so that any installation-local cruft in template1 will
1982  * not mess up the tests.
1983  */
1984  header(_("creating database \"%s\""), dbname);
1985  if (encoding)
1986  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
1987  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1988  else
1989  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
1990  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1991  psql_command(dbname,
1992  "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
1993  "ALTER DATABASE \"%s\" SET lc_monetary TO 'C';"
1994  "ALTER DATABASE \"%s\" SET lc_numeric TO 'C';"
1995  "ALTER DATABASE \"%s\" SET lc_time TO 'C';"
1996  "ALTER DATABASE \"%s\" SET bytea_output TO 'hex';"
1997  "ALTER DATABASE \"%s\" SET timezone_abbreviations TO 'Default';",
1998  dbname, dbname, dbname, dbname, dbname, dbname);
1999 
2000  /*
2001  * Install any requested procedural languages. We use CREATE OR REPLACE
2002  * so that this will work whether or not the language is preinstalled.
2003  */
2004  for (sl = loadlanguage; sl != NULL; sl = sl->next)
2005  {
2006  header(_("installing %s"), sl->str);
2007  psql_command(dbname, "CREATE OR REPLACE LANGUAGE \"%s\"", sl->str);
2008  }
2009 
2010  /*
2011  * Install any requested extensions. We use CREATE IF NOT EXISTS so that
2012  * this will work whether or not the extension is preinstalled.
2013  */
2014  for (sl = loadextension; sl != NULL; sl = sl->next)
2015  {
2016  header(_("installing %s"), sl->str);
2017  psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
2018  }
2019 }
2020 
2021 static void
2022 drop_role_if_exists(const char *rolename)
2023 {
2024  header(_("dropping role \"%s\""), rolename);
2025  psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
2026 }
2027 
2028 static void
2029 create_role(const char *rolename, const _stringlist *granted_dbs)
2030 {
2031  header(_("creating role \"%s\""), rolename);
2032  psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
2033  for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
2034  {
2035  psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
2036  granted_dbs->str, rolename);
2037  }
2038 }
2039 
2040 static void
2041 help(void)
2042 {
2043  printf(_("PostgreSQL regression test driver\n"));
2044  printf(_("\n"));
2045  printf(_("Usage:\n %s [OPTION]... [EXTRA-TEST]...\n"), progname);
2046  printf(_("\n"));
2047  printf(_("Options:\n"));
2048  printf(_(" --bindir=BINPATH use BINPATH for programs that are run;\n"));
2049  printf(_(" if empty, use PATH from the environment\n"));
2050  printf(_(" --config-auth=DATADIR update authentication settings for DATADIR\n"));
2051  printf(_(" --create-role=ROLE create the specified role before testing\n"));
2052  printf(_(" --dbname=DB use database DB (default \"regression\")\n"));
2053  printf(_(" --debug turn on debug mode in programs that are run\n"));
2054  printf(_(" --dlpath=DIR look for dynamic libraries in DIR\n"));
2055  printf(_(" --encoding=ENCODING use ENCODING as the encoding\n"));
2056  printf(_(" -h, --help show this help, then exit\n"));
2057  printf(_(" --inputdir=DIR take input files from DIR (default \".\")\n"));
2058  printf(_(" --launcher=CMD use CMD as launcher of psql\n"));
2059  printf(_(" --load-extension=EXT load the named extension before running the\n"));
2060  printf(_(" tests; can appear multiple times\n"));
2061  printf(_(" --load-language=LANG load the named language before running the\n"));
2062  printf(_(" tests; can appear multiple times\n"));
2063  printf(_(" --max-connections=N maximum number of concurrent connections\n"));
2064  printf(_(" (default is 0, meaning unlimited)\n"));
2065  printf(_(" --max-concurrent-tests=N maximum number of concurrent tests in schedule\n"));
2066  printf(_(" (default is 0, meaning unlimited)\n"));
2067  printf(_(" --outputdir=DIR place output files in DIR (default \".\")\n"));
2068  printf(_(" --schedule=FILE use test ordering schedule from FILE\n"));
2069  printf(_(" (can be used multiple times to concatenate)\n"));
2070  printf(_(" --temp-instance=DIR create a temporary instance in DIR\n"));
2071  printf(_(" --use-existing use an existing installation\n"));
2072  printf(_(" -V, --version output version information, then exit\n"));
2073  printf(_("\n"));
2074  printf(_("Options for \"temp-instance\" mode:\n"));
2075  printf(_(" --no-locale use C locale\n"));
2076  printf(_(" --port=PORT start postmaster on PORT\n"));
2077  printf(_(" --temp-config=FILE append contents of FILE to temporary config\n"));
2078  printf(_("\n"));
2079  printf(_("Options for using an existing installation:\n"));
2080  printf(_(" --host=HOST use postmaster running on HOST\n"));
2081  printf(_(" --port=PORT use postmaster running at PORT\n"));
2082  printf(_(" --user=USER connect as USER\n"));
2083  printf(_("\n"));
2084  printf(_("The exit status is 0 if all tests passed, 1 if some tests failed, and 2\n"));
2085  printf(_("if the tests could not be run for some reason.\n"));
2086  printf(_("\n"));
2087  printf(_("Report bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
2088 }
2089 
2090 int
2091 regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc)
2092 {
2093  static struct option long_options[] = {
2094  {"help", no_argument, NULL, 'h'},
2095  {"version", no_argument, NULL, 'V'},
2096  {"dbname", required_argument, NULL, 1},
2097  {"debug", no_argument, NULL, 2},
2098  {"inputdir", required_argument, NULL, 3},
2099  {"load-language", required_argument, NULL, 4},
2100  {"max-connections", required_argument, NULL, 5},
2101  {"encoding", required_argument, NULL, 6},
2102  {"outputdir", required_argument, NULL, 7},
2103  {"schedule", required_argument, NULL, 8},
2104  {"temp-instance", required_argument, NULL, 9},
2105  {"no-locale", no_argument, NULL, 10},
2106  {"host", required_argument, NULL, 13},
2107  {"port", required_argument, NULL, 14},
2108  {"user", required_argument, NULL, 15},
2109  {"bindir", required_argument, NULL, 16},
2110  {"dlpath", required_argument, NULL, 17},
2111  {"create-role", required_argument, NULL, 18},
2112  {"temp-config", required_argument, NULL, 19},
2113  {"use-existing", no_argument, NULL, 20},
2114  {"launcher", required_argument, NULL, 21},
2115  {"load-extension", required_argument, NULL, 22},
2116  {"config-auth", required_argument, NULL, 24},
2117  {"max-concurrent-tests", required_argument, NULL, 25},
2118  {NULL, 0, NULL, 0}
2119  };
2120 
2121  _stringlist *sl;
2122  int c;
2123  int i;
2124  int option_index;
2125  char buf[MAXPGPATH * 4];
2126  char buf2[MAXPGPATH * 4];
2127 
2128  pg_logging_init(argv[0]);
2129  progname = get_progname(argv[0]);
2130  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_regress"));
2131 
2133 
2134  atexit(stop_postmaster);
2135 
2136 #ifndef HAVE_UNIX_SOCKETS
2137  /* no unix domain sockets available, so change default */
2138  hostname = "localhost";
2139 #endif
2140 
2141  /*
2142  * We call the initialization function here because that way we can set
2143  * default parameters and let them be overwritten by the commandline.
2144  */
2145  ifunc(argc, argv);
2146 
2147  if (getenv("PG_REGRESS_DIFF_OPTS"))
2148  pretty_diff_opts = getenv("PG_REGRESS_DIFF_OPTS");
2149 
2150  while ((c = getopt_long(argc, argv, "hV", long_options, &option_index)) != -1)
2151  {
2152  switch (c)
2153  {
2154  case 'h':
2155  help();
2156  exit(0);
2157  case 'V':
2158  puts("pg_regress (PostgreSQL) " PG_VERSION);
2159  exit(0);
2160  case 1:
2161 
2162  /*
2163  * If a default database was specified, we need to remove it
2164  * before we add the specified one.
2165  */
2166  free_stringlist(&dblist);
2167  split_to_stringlist(optarg, ",", &dblist);
2168  break;
2169  case 2:
2170  debug = true;
2171  break;
2172  case 3:
2174  break;
2175  case 4:
2176  add_stringlist_item(&loadlanguage, optarg);
2177  break;
2178  case 5:
2179  max_connections = atoi(optarg);
2180  break;
2181  case 6:
2183  break;
2184  case 7:
2186  break;
2187  case 8:
2188  add_stringlist_item(&schedulelist, optarg);
2189  break;
2190  case 9:
2192  break;
2193  case 10:
2194  nolocale = true;
2195  break;
2196  case 13:
2198  break;
2199  case 14:
2200  port = atoi(optarg);
2201  port_specified_by_user = true;
2202  break;
2203  case 15:
2204  user = pg_strdup(optarg);
2205  break;
2206  case 16:
2207  /* "--bindir=" means to use PATH */
2208  if (strlen(optarg))
2209  bindir = pg_strdup(optarg);
2210  else
2211  bindir = NULL;
2212  break;
2213  case 17:
2214  dlpath = pg_strdup(optarg);
2215  break;
2216  case 18:
2217  split_to_stringlist(optarg, ",", &extraroles);
2218  break;
2219  case 19:
2220  add_stringlist_item(&temp_configs, optarg);
2221  break;
2222  case 20:
2223  use_existing = true;
2224  break;
2225  case 21:
2227  break;
2228  case 22:
2229  add_stringlist_item(&loadextension, optarg);
2230  break;
2231  case 24:
2233  break;
2234  case 25:
2235  max_concurrent_tests = atoi(optarg);
2236  break;
2237  default:
2238  /* getopt_long already emitted a complaint */
2239  fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
2240  progname);
2241  exit(2);
2242  }
2243  }
2244 
2245  /*
2246  * if we still have arguments, they are extra tests to run
2247  */
2248  while (argc - optind >= 1)
2249  {
2250  add_stringlist_item(&extra_tests, argv[optind]);
2251  optind++;
2252  }
2253 
2254  if (config_auth_datadir)
2255  {
2256 #ifdef ENABLE_SSPI
2257  config_sspi_auth(config_auth_datadir, user);
2258 #endif
2259  exit(0);
2260  }
2261 
2263 
2264  /*
2265  * To reduce chances of interference with parallel installations, use
2266  * a port number starting in the private range (49152-65535)
2267  * calculated from the version number. This aids !HAVE_UNIX_SOCKETS
2268  * systems; elsewhere, the use of a private socket directory already
2269  * prevents interference.
2270  */
2271  port = 0xC000 | (PG_VERSION_NUM & 0x3FFF);
2272 
2276 
2277  /*
2278  * Initialization
2279  */
2281 
2283 
2284 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
2285  unlimit_core_size();
2286 #endif
2287 
2288  if (temp_instance)
2289  {
2290  FILE *pg_conf;
2291  const char *env_wait;
2292  int wait_seconds;
2293 
2294  /*
2295  * Prepare the temp instance
2296  */
2297 
2299  {
2300  header(_("removing existing temp instance"));
2301  if (!rmtree(temp_instance, true))
2302  {
2303  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2305  exit(2);
2306  }
2307  }
2308 
2309  header(_("creating temporary instance"));
2310 
2311  /* make the temp instance top directory */
2313 
2314  /* and a directory for log files */
2315  snprintf(buf, sizeof(buf), "%s/log", outputdir);
2316  if (!directory_exists(buf))
2317  make_directory(buf);
2318 
2319  /* initdb */
2320  header(_("initializing database system"));
2321  snprintf(buf, sizeof(buf),
2322  "\"%s%sinitdb\" -D \"%s/data\" --no-clean --no-sync%s%s > \"%s/log/initdb.log\" 2>&1",
2323  bindir ? bindir : "",
2324  bindir ? "/" : "",
2325  temp_instance,
2326  debug ? " --debug" : "",
2327  nolocale ? " --no-locale" : "",
2328  outputdir);
2329  if (system(buf))
2330  {
2331  fprintf(stderr, _("\n%s: initdb failed\nExamine %s/log/initdb.log for the reason.\nCommand was: %s\n"), progname, outputdir, buf);
2332  exit(2);
2333  }
2334 
2335  /*
2336  * Adjust the default postgresql.conf for regression testing. The user
2337  * can specify a file to be appended; in any case we expand logging
2338  * and set max_prepared_transactions to enable testing of prepared
2339  * xacts. (Note: to reduce the probability of unexpected shmmax
2340  * failures, don't set max_prepared_transactions any higher than
2341  * actually needed by the prepared_xacts regression test.)
2342  */
2343  snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_instance);
2344  pg_conf = fopen(buf, "a");
2345  if (pg_conf == NULL)
2346  {
2347  fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno));
2348  exit(2);
2349  }
2350  fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
2351  fputs("log_autovacuum_min_duration = 0\n", pg_conf);
2352  fputs("log_checkpoints = on\n", pg_conf);
2353  fputs("log_line_prefix = '%m [%p] %q%a '\n", pg_conf);
2354  fputs("log_lock_waits = on\n", pg_conf);
2355  fputs("log_temp_files = 128kB\n", pg_conf);
2356  fputs("max_prepared_transactions = 2\n", pg_conf);
2357 
2358  for (sl = temp_configs; sl != NULL; sl = sl->next)
2359  {
2360  char *temp_config = sl->str;
2361  FILE *extra_conf;
2362  char line_buf[1024];
2363 
2364  extra_conf = fopen(temp_config, "r");
2365  if (extra_conf == NULL)
2366  {
2367  fprintf(stderr, _("\n%s: could not open \"%s\" to read extra config: %s\n"), progname, temp_config, strerror(errno));
2368  exit(2);
2369  }
2370  while (fgets(line_buf, sizeof(line_buf), extra_conf) != NULL)
2371  fputs(line_buf, pg_conf);
2372  fclose(extra_conf);
2373  }
2374 
2375  fclose(pg_conf);
2376 
2377 #ifdef ENABLE_SSPI
2378 
2379  /*
2380  * Since we successfully used the same buffer for the much-longer
2381  * "initdb" command, this can't truncate.
2382  */
2383  snprintf(buf, sizeof(buf), "%s/data", temp_instance);
2384  config_sspi_auth(buf, NULL);
2385 #elif !defined(HAVE_UNIX_SOCKETS)
2386 #error Platform has no means to secure the test installation.
2387 #endif
2388 
2389  /*
2390  * Check if there is a postmaster running already.
2391  */
2392  snprintf(buf2, sizeof(buf2),
2393  "\"%s%spsql\" -X postgres <%s 2>%s",
2394  bindir ? bindir : "",
2395  bindir ? "/" : "",
2396  DEVNULL, DEVNULL);
2397 
2398  for (i = 0; i < 16; i++)
2399  {
2400  if (system(buf2) == 0)
2401  {
2402  char s[16];
2403 
2404  if (port_specified_by_user || i == 15)
2405  {
2406  fprintf(stderr, _("port %d apparently in use\n"), port);
2408  fprintf(stderr, _("%s: could not determine an available port\n"), progname);
2409  fprintf(stderr, _("Specify an unused port using the --port option or shut down any conflicting PostgreSQL servers.\n"));
2410  exit(2);
2411  }
2412 
2413  fprintf(stderr, _("port %d apparently in use, trying %d\n"), port, port + 1);
2414  port++;
2415  sprintf(s, "%d", port);
2416  doputenv("PGPORT", s);
2417  }
2418  else
2419  break;
2420  }
2421 
2422  /*
2423  * Start the temp postmaster
2424  */
2425  header(_("starting postmaster"));
2426  snprintf(buf, sizeof(buf),
2427  "\"%s%spostgres\" -D \"%s/data\" -F%s "
2428  "-c \"listen_addresses=%s\" -k \"%s\" "
2429  "> \"%s/log/postmaster.log\" 2>&1",
2430  bindir ? bindir : "",
2431  bindir ? "/" : "",
2432  temp_instance, debug ? " -d 5" : "",
2433  hostname ? hostname : "", sockdir ? sockdir : "",
2434  outputdir);
2436  if (postmaster_pid == INVALID_PID)
2437  {
2438  fprintf(stderr, _("\n%s: could not spawn postmaster: %s\n"),
2439  progname, strerror(errno));
2440  exit(2);
2441  }
2442 
2443  /*
2444  * Wait till postmaster is able to accept connections; normally this
2445  * is only a second or so, but Cygwin is reportedly *much* slower, and
2446  * test builds using Valgrind or similar tools might be too. Hence,
2447  * allow the default timeout of 60 seconds to be overridden from the
2448  * PGCTLTIMEOUT environment variable.
2449  */
2450  env_wait = getenv("PGCTLTIMEOUT");
2451  if (env_wait != NULL)
2452  {
2453  wait_seconds = atoi(env_wait);
2454  if (wait_seconds <= 0)
2455  wait_seconds = 60;
2456  }
2457  else
2458  wait_seconds = 60;
2459 
2460  for (i = 0; i < wait_seconds; i++)
2461  {
2462  /* Done if psql succeeds */
2463  if (system(buf2) == 0)
2464  break;
2465 
2466  /*
2467  * Fail immediately if postmaster has exited
2468  */
2469 #ifndef WIN32
2470  if (waitpid(postmaster_pid, NULL, WNOHANG) == postmaster_pid)
2471 #else
2472  if (WaitForSingleObject(postmaster_pid, 0) == WAIT_OBJECT_0)
2473 #endif
2474  {
2475  fprintf(stderr, _("\n%s: postmaster failed\nExamine %s/log/postmaster.log for the reason\n"), progname, outputdir);
2476  exit(2);
2477  }
2478 
2479  pg_usleep(1000000L);
2480  }
2481  if (i >= wait_seconds)
2482  {
2483  fprintf(stderr, _("\n%s: postmaster did not respond within %d seconds\nExamine %s/log/postmaster.log for the reason\n"),
2484  progname, wait_seconds, outputdir);
2485 
2486  /*
2487  * If we get here, the postmaster is probably wedged somewhere in
2488  * startup. Try to kill it ungracefully rather than leaving a
2489  * stuck postmaster that might interfere with subsequent test
2490  * attempts.
2491  */
2492 #ifndef WIN32
2493  if (kill(postmaster_pid, SIGKILL) != 0 &&
2494  errno != ESRCH)
2495  fprintf(stderr, _("\n%s: could not kill failed postmaster: %s\n"),
2496  progname, strerror(errno));
2497 #else
2498  if (TerminateProcess(postmaster_pid, 255) == 0)
2499  fprintf(stderr, _("\n%s: could not kill failed postmaster: error code %lu\n"),
2500  progname, GetLastError());
2501 #endif
2502 
2503  exit(2);
2504  }
2505 
2506  postmaster_running = true;
2507 
2508 #ifdef _WIN64
2509 /* need a series of two casts to convert HANDLE without compiler warning */
2510 #define ULONGPID(x) (unsigned long) (unsigned long long) (x)
2511 #else
2512 #define ULONGPID(x) (unsigned long) (x)
2513 #endif
2514  printf(_("running on port %d with PID %lu\n"),
2516  }
2517  else
2518  {
2519  /*
2520  * Using an existing installation, so may need to get rid of
2521  * pre-existing database(s) and role(s)
2522  */
2523  if (!use_existing)
2524  {
2525  for (sl = dblist; sl; sl = sl->next)
2527  for (sl = extraroles; sl; sl = sl->next)
2528  drop_role_if_exists(sl->str);
2529  }
2530  }
2531 
2532  /*
2533  * Create the test database(s) and role(s)
2534  */
2535  if (!use_existing)
2536  {
2537  for (sl = dblist; sl; sl = sl->next)
2538  create_database(sl->str);
2539  for (sl = extraroles; sl; sl = sl->next)
2540  create_role(sl->str, dblist);
2541  }
2542 
2543  /*
2544  * Ready to run the tests
2545  */
2546  header(_("running regression test queries"));
2547 
2548  for (sl = schedulelist; sl != NULL; sl = sl->next)
2549  {
2550  run_schedule(sl->str, tfunc);
2551  }
2552 
2553  for (sl = extra_tests; sl != NULL; sl = sl->next)
2554  {
2555  run_single_test(sl->str, tfunc);
2556  }
2557 
2558  /*
2559  * Shut down temp installation's postmaster
2560  */
2561  if (temp_instance)
2562  {
2563  header(_("shutting down postmaster"));
2564  stop_postmaster();
2565  }
2566 
2567  /*
2568  * If there were no errors, remove the temp instance immediately to
2569  * conserve disk space. (If there were errors, we leave the instance in
2570  * place for possible manual investigation.)
2571  */
2572  if (temp_instance && fail_count == 0 && fail_ignore_count == 0)
2573  {
2574  header(_("removing temporary instance"));
2575  if (!rmtree(temp_instance, true))
2576  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2578  }
2579 
2580  fclose(logfile);
2581 
2582  /*
2583  * Emit nice-looking summary message
2584  */
2585  if (fail_count == 0 && fail_ignore_count == 0)
2586  snprintf(buf, sizeof(buf),
2587  _(" All %d tests passed. "),
2588  success_count);
2589  else if (fail_count == 0) /* fail_count=0, fail_ignore_count>0 */
2590  snprintf(buf, sizeof(buf),
2591  _(" %d of %d tests passed, %d failed test(s) ignored. "),
2592  success_count,
2595  else if (fail_ignore_count == 0) /* fail_count>0 && fail_ignore_count=0 */
2596  snprintf(buf, sizeof(buf),
2597  _(" %d of %d tests failed. "),
2598  fail_count,
2600  else
2601  /* fail_count>0 && fail_ignore_count>0 */
2602  snprintf(buf, sizeof(buf),
2603  _(" %d of %d tests failed, %d of these failures ignored. "),
2607 
2608  putchar('\n');
2609  for (i = strlen(buf); i > 0; i--)
2610  putchar('=');
2611  printf("\n%s\n", buf);
2612  for (i = strlen(buf); i > 0; i--)
2613  putchar('=');
2614  putchar('\n');
2615  putchar('\n');
2616 
2617  if (file_size(difffilename) > 0)
2618  {
2619  printf(_("The differences that caused some tests to fail can be viewed in the\n"
2620  "file \"%s\". A copy of the test summary that you see\n"
2621  "above is saved in the file \"%s\".\n\n"),
2623  }
2624  else
2625  {
2626  unlink(difffilename);
2627  unlink(logfilename);
2628  }
2629 
2630  if (fail_count != 0)
2631  exit(1);
2632 
2633  return 0;
2634 }
static char * shellprog
Definition: pg_regress.c:56
char * make_absolute_path(const char *path)
Definition: path.c:608
#define getaddrinfo
Definition: getaddrinfo.h:136
static char * get_alternative_expectfile(const char *expectfile, int i)
Definition: pg_regress.c:1300
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1273
static void drop_role_if_exists(const char *rolename)
Definition: pg_regress.c:2022
PID_TYPE(* test_function)(const char *, _stringlist **, _stringlist **, _stringlist **)
Definition: pg_regress.h:28
static void drop_database_if_exists(const char *dbname)
Definition: pg_regress.c:1969
#define UNIXSOCK_PATH(path, port, sockdir)
Definition: pqcomm.h:70
char ** pgfnames(const char *path)
Definition: pgfnames.c:37
static int max_concurrent_tests
Definition: pg_regress.c:82
#define WTERMSIG(w)
Definition: win32_port.h:141
static const char * sockdir
Definition: pg_regress.c:103
static int wait_seconds
Definition: pg_ctl.c:80
static void help(void)
Definition: pg_regress.c:2041
static _stringlist * extra_tests
Definition: pg_regress.c:85
static void log_child_failure(int exitstatus)
Definition: pg_regress.c:1589
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * str
Definition: pg_regress.h:24
static void initialize_environment(void)
Definition: pg_regress.c:726
static void run_single_test(const char *test, test_function tfunc)
Definition: pg_regress.c:1864
char * host_platform
Definition: pg_regress.c:53
bool debug
Definition: pg_regress.c:74
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:156
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:39
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void run_schedule(const char *schedule, test_function tfunc)
Definition: pg_regress.c:1613
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
static char * encoding
Definition: pg_regress.c:83
struct timeval instr_time
Definition: instr_time.h:150
static const char * progname
Definition: pg_regress.c:99
struct _resultmap * next
Definition: pg_regress.c:47
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
static void status_end(void)
Definition: pg_regress.c:247
static bool use_existing
Definition: pg_regress.c:89
static bool postmaster_running
Definition: pg_regress.c:113
#define PID_TYPE
Definition: pg_regress.h:14
#define kill(pid, sig)
Definition: win32_port.h:426
#define printf(...)
Definition: port.h:198
static FILE * logfile
Definition: pg_regress.c:101
#define SIGPIPE
Definition: win32_port.h:159
static bool string_matches_pattern(const char *str, const char *pattern)
Definition: pg_regress.c:377
static void stop_postmaster(void)
Definition: pg_regress.c:259
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
#define fprintf
Definition: port.h:196
static char * temp_instance
Definition: pg_regress.c:86
char * launcher
Definition: pg_regress.c:78
static void convert_sourcefiles(void)
Definition: pg_regress.c:583
static bool results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
Definition: pg_regress.c:1367
static void split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
Definition: pg_regress.c:192
void(* init_function)(int argc, char **argv)
Definition: pg_regress.h:32
static void convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
Definition: pg_regress.c:464
#define pg_attribute_printf(f, a)
Definition: c.h:132
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:177
static _stringlist * loadlanguage
Definition: pg_regress.c:79
PID_TYPE spawn_process(const char *cmdline)
Definition: pg_regress.c:1152
const char * get_user_name(char **errstr)
Definition: username.c:31
#define malloc(a)
Definition: header.h:50
#define SIGKILL
Definition: win32_port.h:158
#define sprintf
Definition: port.h:194
static char * difffilename
Definition: pg_regress.c:102
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
static _resultmap * resultmap
Definition: pg_regress.c:110
int optind
Definition: getopt.c:50
static _stringlist * loadextension
Definition: pg_regress.c:80
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
#define vsnprintf
Definition: port.h:191
#define MAXPGPATH
void replace_string(char *string, const char *replace, const char *replacement)
Definition: pg_regress.c:442
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1106
_stringlist * dblist
Definition: pg_regress.c:73
char * ai_canonname
Definition: getaddrinfo.h:106
static _stringlist * extraroles
Definition: pg_regress.c:95
static _stringlist * temp_configs
Definition: pg_regress.c:87
char * c
const char * pretty_diff_opts
Definition: pg_regress.c:66
static char * buf
Definition: pg_test_fsync.c:67
#define MAX_PARALLEL_TESTS
#define S_IRWXG
Definition: win32_port.h:281
#define INVALID_PID
Definition: pg_regress.h:15
#define SIGHUP
Definition: win32_port.h:154
static void create_role(const char *rolename, const _stringlist *granted_dbs)
Definition: pg_regress.c:2029
static char * dlpath
Definition: pg_regress.c:93
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int success_count
Definition: pg_regress.c:115
#define DEVNULL
Definition: port.h:123
static int run_diff(const char *cmd, const char *filename)
Definition: pg_regress.c:1334
static int max_connections
Definition: pg_regress.c:81
static int port
Definition: pg_regress.c:91
static PID_TYPE postmaster_pid
Definition: pg_regress.c:112
char * pghost
Definition: pgbench.c:243
#define stat(a, b)
Definition: win32_port.h:255
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:96
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1166
static int fail_ignore_count
Definition: pg_regress.c:117
#define WIFEXITED(w)
Definition: win32_port.h:138
static int fail_count
Definition: pg_regress.c:116
char * outputdir
Definition: pg_regress.c:76
static char * logfilename
Definition: pg_regress.c:100
char * test
Definition: pg_regress.c:44
char * type
Definition: pg_regress.c:45
char * inputdir
Definition: pg_regress.c:75
static void infile(const char *filename)
Definition: zic.c:1233
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define free(a)
Definition: header.h:65
int ai_protocol
Definition: getaddrinfo.h:103
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define SIG_DFL
Definition: win32_port.h:149
static long file_size(const char *file)
Definition: pg_regress.c:1219
#define Assert(condition)
Definition: c.h:739
void pgfnames_cleanup(char **filenames)
Definition: pgfnames.c:86
static bool port_specified_by_user
Definition: pg_regress.c:92
static const char * get_expectfile(const char *testname, const char *file)
Definition: pg_regress.c:685
char * bindir
Definition: pg_regress.c:77
const char * pg_strsignal(int signum)
Definition: pgstrsignal.c:42
static int file_line_count(const char *file)
Definition: pg_regress.c:1240
int ai_socktype
Definition: getaddrinfo.h:102
#define strerror
Definition: port.h:205
char * dbname
Definition: streamutil.c:50
static void load_resultmap(void)
Definition: pg_regress.c:603
char * mkdtemp(char *path)
Definition: mkdtemp.c:286
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define putenv(x)
Definition: win32_port.h:474
#define realloc(a, b)
Definition: header.h:60
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:209
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
const char * name
Definition: encode.c:521
char * resultfile
Definition: pg_regress.c:46
#define S_ISDIR(m)
Definition: win32_port.h:296
static void doputenv(const char *var, const char *val)
Definition: pg_regress.c:714
struct _resultmap _resultmap
static char * filename
Definition: pg_dumpall.c:90
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
static char * user
Definition: pg_regress.c:94
#define S_IRWXU
Definition: win32_port.h:269
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
char * optarg
Definition: getopt.c:52
static _stringlist * schedulelist
Definition: pg_regress.c:84
bool file_exists(const char *file)
Definition: pg_regress.c:1262
struct _stringlist * next
Definition: pg_regress.h:25
int i
static bool nolocale
Definition: pg_regress.c:88
const char * basic_diff_opts
Definition: pg_regress.c:65
static void open_result_files(void)
Definition: pg_regress.c:1929
static char * outfile
size_t ai_addrlen
Definition: getaddrinfo.h:104
#define mkdir(a, b)
Definition: win32_port.h:58
#define vfprintf
Definition: port.h:195
int ai_flags
Definition: getaddrinfo.h:100
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226
static char * hostname
Definition: pg_regress.c:90
#define WIFSIGNALED(w)
Definition: win32_port.h:139
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87
char * pgport
Definition: pgbench.c:244
long val
Definition: informix.c:664
struct sockaddr * ai_addr
Definition: getaddrinfo.h:105
#define S_IRWXO
Definition: win32_port.h:293
#define unsetenv(x)
Definition: win32_port.h:475
#define ULONGPID(x)
#define WEXITSTATUS(w)
Definition: win32_port.h:140
static void wait_for_tests(PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
Definition: pg_regress.c:1517
static void create_database(const char *dbname)
Definition: pg_regress.c:1976
int regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc)
Definition: pg_regress.c:2091
int ai_family
Definition: getaddrinfo.h:101