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