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