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