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