]> granicus.if.org Git - postgresql/blob - src/test/regress/pg_regress.c
Refer to OS X as "macOS", except for the port name which is still "darwin".
[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 = pg_malloc(sizeof(_stringlist));
155         _stringlist *oldentry;
156
157         newentry->str = pg_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 = pg_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 = pg_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 = pg_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 = pg_malloc(sizeof(_resultmap));
665
666                         entry->test = pg_strdup(buf);
667                         entry->type = pg_strdup(file_type);
668                         entry->resultfile = pg_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, macOS with --enable-nls, and Cygwin with --enable-nls.
742                  * (Use of --enable-nls matters because libintl replaces setlocale().)
743                  * Also, PostgreSQL does not support macOS 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 pg_attribute_unused()
880 static const char *
881 fmtHba(const char *raw)
882 {
883         static char *ret;
884         const char *rp;
885         char       *wp;
886
887         wp = ret = realloc(ret, 3 + strlen(raw) * 2);
888
889         *wp++ = '"';
890         for (rp = raw; *rp; rp++)
891         {
892                 if (*rp == '"')
893                         *wp++ = '"';
894                 *wp++ = *rp;
895         }
896         *wp++ = '"';
897         *wp++ = '\0';
898
899         return ret;
900 }
901
902 #ifdef ENABLE_SSPI
903 /*
904  * Get account and domain/realm names for the current user.  This is based on
905  * pg_SSPI_recvauth().  The returned strings use static storage.
906  */
907 static void
908 current_windows_user(const char **acct, const char **dom)
909 {
910         static char accountname[MAXPGPATH];
911         static char domainname[MAXPGPATH];
912         HANDLE          token;
913         TOKEN_USER *tokenuser;
914         DWORD           retlen;
915         DWORD           accountnamesize = sizeof(accountname);
916         DWORD           domainnamesize = sizeof(domainname);
917         SID_NAME_USE accountnameuse;
918
919         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token))
920         {
921                 fprintf(stderr,
922                                 _("%s: could not open process token: error code %lu\n"),
923                                 progname, GetLastError());
924                 exit(2);
925         }
926
927         if (!GetTokenInformation(token, TokenUser, NULL, 0, &retlen) && GetLastError() != 122)
928         {
929                 fprintf(stderr,
930                                 _("%s: could not get token information buffer size: error code %lu\n"),
931                                 progname, GetLastError());
932                 exit(2);
933         }
934         tokenuser = pg_malloc(retlen);
935         if (!GetTokenInformation(token, TokenUser, tokenuser, retlen, &retlen))
936         {
937                 fprintf(stderr,
938                                 _("%s: could not get token information: error code %lu\n"),
939                                 progname, GetLastError());
940                 exit(2);
941         }
942
943         if (!LookupAccountSid(NULL, tokenuser->User.Sid, accountname, &accountnamesize,
944                                                   domainname, &domainnamesize, &accountnameuse))
945         {
946                 fprintf(stderr,
947                                 _("%s: could not look up account SID: error code %lu\n"),
948                                 progname, GetLastError());
949                 exit(2);
950         }
951
952         free(tokenuser);
953
954         *acct = accountname;
955         *dom = domainname;
956 }
957
958 /*
959  * Rewrite pg_hba.conf and pg_ident.conf to use SSPI authentication.  Permit
960  * the current OS user to authenticate as the bootstrap superuser and as any
961  * user named in a --create-role option.
962  */
963 static void
964 config_sspi_auth(const char *pgdata)
965 {
966         const char *accountname,
967                            *domainname;
968         const char *username;
969         char       *errstr;
970         bool            have_ipv6;
971         char            fname[MAXPGPATH];
972         int                     res;
973         FILE       *hba,
974                            *ident;
975         _stringlist *sl;
976
977         /*
978          * "username", the initdb-chosen bootstrap superuser name, may always
979          * match "accountname", the value SSPI authentication discovers.  The
980          * underlying system functions do not clearly guarantee that.
981          */
982         current_windows_user(&accountname, &domainname);
983         username = get_user_name(&errstr);
984         if (username == NULL)
985         {
986                 fprintf(stderr, "%s: %s\n", progname, errstr);
987                 exit(2);
988         }
989
990         /*
991          * Like initdb.c:setup_config(), determine whether the platform recognizes
992          * ::1 (IPv6 loopback) as a numeric host address string.
993          */
994         {
995                 struct addrinfo *gai_result;
996                 struct addrinfo hints;
997                 WSADATA         wsaData;
998
999                 hints.ai_flags = AI_NUMERICHOST;
1000                 hints.ai_family = AF_UNSPEC;
1001                 hints.ai_socktype = 0;
1002                 hints.ai_protocol = 0;
1003                 hints.ai_addrlen = 0;
1004                 hints.ai_canonname = NULL;
1005                 hints.ai_addr = NULL;
1006                 hints.ai_next = NULL;
1007
1008                 have_ipv6 = (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0 &&
1009                                          getaddrinfo("::1", NULL, &hints, &gai_result) == 0);
1010         }
1011
1012         /* Check a Write outcome and report any error. */
1013 #define CW(cond)        \
1014         do { \
1015                 if (!(cond)) \
1016                 { \
1017                         fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), \
1018                                         progname, fname, strerror(errno)); \
1019                         exit(2); \
1020                 } \
1021         } while (0)
1022
1023         res = snprintf(fname, sizeof(fname), "%s/pg_hba.conf", pgdata);
1024         if (res < 0 || res >= sizeof(fname) - 1)
1025         {
1026                 /*
1027                  * Truncating this name is a fatal error, because we must not fail to
1028                  * overwrite an original trust-authentication pg_hba.conf.
1029                  */
1030                 fprintf(stderr, _("%s: directory name too long\n"), progname);
1031                 exit(2);
1032         }
1033         hba = fopen(fname, "w");
1034         if (hba == NULL)
1035         {
1036                 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1037                                 progname, fname, strerror(errno));
1038                 exit(2);
1039         }
1040         CW(fputs("# Configuration written by config_sspi_auth()\n", hba) >= 0);
1041         CW(fputs("host all all 127.0.0.1/32  sspi include_realm=1 map=regress\n",
1042                          hba) >= 0);
1043         if (have_ipv6)
1044                 CW(fputs("host all all ::1/128  sspi include_realm=1 map=regress\n",
1045                                  hba) >= 0);
1046         CW(fclose(hba) == 0);
1047
1048         snprintf(fname, sizeof(fname), "%s/pg_ident.conf", pgdata);
1049         ident = fopen(fname, "w");
1050         if (ident == NULL)
1051         {
1052                 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1053                                 progname, fname, strerror(errno));
1054                 exit(2);
1055         }
1056         CW(fputs("# Configuration written by config_sspi_auth()\n", ident) >= 0);
1057
1058         /*
1059          * Double-quote for the benefit of account names containing whitespace or
1060          * '#'.  Windows forbids the double-quote character itself, so don't
1061          * bother escaping embedded double-quote characters.
1062          */
1063         CW(fprintf(ident, "regress  \"%s@%s\"  %s\n",
1064                            accountname, domainname, fmtHba(username)) >= 0);
1065         for (sl = extraroles; sl; sl = sl->next)
1066                 CW(fprintf(ident, "regress  \"%s@%s\"  %s\n",
1067                                    accountname, domainname, fmtHba(sl->str)) >= 0);
1068         CW(fclose(ident) == 0);
1069 }
1070 #endif
1071
1072 /*
1073  * Issue a command via psql, connecting to the specified database
1074  *
1075  * Since we use system(), this doesn't return until the operation finishes
1076  */
1077 static void
1078 psql_command(const char *database, const char *query,...)
1079 {
1080         char            query_formatted[1024];
1081         char            query_escaped[2048];
1082         char            psql_cmd[MAXPGPATH + 2048];
1083         va_list         args;
1084         char       *s;
1085         char       *d;
1086
1087         /* Generate the query with insertion of sprintf arguments */
1088         va_start(args, query);
1089         vsnprintf(query_formatted, sizeof(query_formatted), query, args);
1090         va_end(args);
1091
1092         /* Now escape any shell double-quote metacharacters */
1093         d = query_escaped;
1094         for (s = query_formatted; *s; s++)
1095         {
1096                 if (strchr("\\\"$`", *s))
1097                         *d++ = '\\';
1098                 *d++ = *s;
1099         }
1100         *d = '\0';
1101
1102         /* And now we can build and execute the shell command */
1103         snprintf(psql_cmd, sizeof(psql_cmd),
1104                          "\"%s%spsql\" -X -c \"%s\" \"%s\"",
1105                          bindir ? bindir : "",
1106                          bindir ? "/" : "",
1107                          query_escaped,
1108                          database);
1109
1110         if (system(psql_cmd) != 0)
1111         {
1112                 /* psql probably already reported the error */
1113                 fprintf(stderr, _("command failed: %s\n"), psql_cmd);
1114                 exit(2);
1115         }
1116 }
1117
1118 /*
1119  * Spawn a process to execute the given shell command; don't wait for it
1120  *
1121  * Returns the process ID (or HANDLE) so we can wait for it later
1122  */
1123 PID_TYPE
1124 spawn_process(const char *cmdline)
1125 {
1126 #ifndef WIN32
1127         pid_t           pid;
1128
1129         /*
1130          * Must flush I/O buffers before fork.  Ideally we'd use fflush(NULL) here
1131          * ... does anyone still care about systems where that doesn't work?
1132          */
1133         fflush(stdout);
1134         fflush(stderr);
1135         if (logfile)
1136                 fflush(logfile);
1137
1138         pid = fork();
1139         if (pid == -1)
1140         {
1141                 fprintf(stderr, _("%s: could not fork: %s\n"),
1142                                 progname, strerror(errno));
1143                 exit(2);
1144         }
1145         if (pid == 0)
1146         {
1147                 /*
1148                  * In child
1149                  *
1150                  * Instead of using system(), exec the shell directly, and tell it to
1151                  * "exec" the command too.  This saves two useless processes per
1152                  * parallel test case.
1153                  */
1154                 char       *cmdline2;
1155
1156                 cmdline2 = psprintf("exec %s", cmdline);
1157                 execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
1158                 fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
1159                                 progname, shellprog, strerror(errno));
1160                 _exit(1);                               /* not exit() here... */
1161         }
1162         /* in parent */
1163         return pid;
1164 #else
1165         PROCESS_INFORMATION pi;
1166         char       *cmdline2;
1167         HANDLE          restrictedToken;
1168
1169         memset(&pi, 0, sizeof(pi));
1170         cmdline2 = psprintf("cmd /c \"%s\"", cmdline);
1171
1172         if ((restrictedToken =
1173                  CreateRestrictedProcess(cmdline2, &pi, progname)) == 0)
1174                 exit(2);
1175
1176         CloseHandle(pi.hThread);
1177         return pi.hProcess;
1178 #endif
1179 }
1180
1181 /*
1182  * Count bytes in file
1183  */
1184 static long
1185 file_size(const char *file)
1186 {
1187         long            r;
1188         FILE       *f = fopen(file, "r");
1189
1190         if (!f)
1191         {
1192                 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1193                                 progname, file, strerror(errno));
1194                 return -1;
1195         }
1196         fseek(f, 0, SEEK_END);
1197         r = ftell(f);
1198         fclose(f);
1199         return r;
1200 }
1201
1202 /*
1203  * Count lines in file
1204  */
1205 static int
1206 file_line_count(const char *file)
1207 {
1208         int                     c;
1209         int                     l = 0;
1210         FILE       *f = fopen(file, "r");
1211
1212         if (!f)
1213         {
1214                 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1215                                 progname, file, strerror(errno));
1216                 return -1;
1217         }
1218         while ((c = fgetc(f)) != EOF)
1219         {
1220                 if (c == '\n')
1221                         l++;
1222         }
1223         fclose(f);
1224         return l;
1225 }
1226
1227 bool
1228 file_exists(const char *file)
1229 {
1230         FILE       *f = fopen(file, "r");
1231
1232         if (!f)
1233                 return false;
1234         fclose(f);
1235         return true;
1236 }
1237
1238 static bool
1239 directory_exists(const char *dir)
1240 {
1241         struct stat st;
1242
1243         if (stat(dir, &st) != 0)
1244                 return false;
1245         if (S_ISDIR(st.st_mode))
1246                 return true;
1247         return false;
1248 }
1249
1250 /* Create a directory */
1251 static void
1252 make_directory(const char *dir)
1253 {
1254         if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
1255         {
1256                 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
1257                                 progname, dir, strerror(errno));
1258                 exit(2);
1259         }
1260 }
1261
1262 /*
1263  * In: filename.ext, Return: filename_i.ext, where 0 < i <= 9
1264  */
1265 static char *
1266 get_alternative_expectfile(const char *expectfile, int i)
1267 {
1268         char       *last_dot;
1269         int                     ssize = strlen(expectfile) + 2 + 1;
1270         char       *tmp;
1271         char       *s;
1272
1273         if (!(tmp = (char *) malloc(ssize)))
1274                 return NULL;
1275
1276         if (!(s = (char *) malloc(ssize)))
1277         {
1278                 free(tmp);
1279                 return NULL;
1280         }
1281
1282         strcpy(tmp, expectfile);
1283         last_dot = strrchr(tmp, '.');
1284         if (!last_dot)
1285         {
1286                 free(tmp);
1287                 free(s);
1288                 return NULL;
1289         }
1290         *last_dot = '\0';
1291         snprintf(s, ssize, "%s_%d.%s", tmp, i, last_dot + 1);
1292         free(tmp);
1293         return s;
1294 }
1295
1296 /*
1297  * Run a "diff" command and also check that it didn't crash
1298  */
1299 static int
1300 run_diff(const char *cmd, const char *filename)
1301 {
1302         int                     r;
1303
1304         r = system(cmd);
1305         if (!WIFEXITED(r) || WEXITSTATUS(r) > 1)
1306         {
1307                 fprintf(stderr, _("diff command failed with status %d: %s\n"), r, cmd);
1308                 exit(2);
1309         }
1310 #ifdef WIN32
1311
1312         /*
1313          * On WIN32, if the 'diff' command cannot be found, system() returns 1,
1314          * but produces nothing to stdout, so we check for that here.
1315          */
1316         if (WEXITSTATUS(r) == 1 && file_size(filename) <= 0)
1317         {
1318                 fprintf(stderr, _("diff command not found: %s\n"), cmd);
1319                 exit(2);
1320         }
1321 #endif
1322
1323         return WEXITSTATUS(r);
1324 }
1325
1326 /*
1327  * Check the actual result file for the given test against expected results
1328  *
1329  * Returns true if different (failure), false if correct match found.
1330  * In the true case, the diff is appended to the diffs file.
1331  */
1332 static bool
1333 results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
1334 {
1335         char            expectfile[MAXPGPATH];
1336         char            diff[MAXPGPATH];
1337         char            cmd[MAXPGPATH * 3];
1338         char            best_expect_file[MAXPGPATH];
1339         FILE       *difffile;
1340         int                     best_line_count;
1341         int                     i;
1342         int                     l;
1343         const char *platform_expectfile;
1344
1345         /*
1346          * We can pass either the resultsfile or the expectfile, they should have
1347          * the same type (filename.type) anyway.
1348          */
1349         platform_expectfile = get_expectfile(testname, resultsfile);
1350
1351         strlcpy(expectfile, default_expectfile, sizeof(expectfile));
1352         if (platform_expectfile)
1353         {
1354                 /*
1355                  * Replace everything after the last slash in expectfile with what the
1356                  * platform_expectfile contains.
1357                  */
1358                 char       *p = strrchr(expectfile, '/');
1359
1360                 if (p)
1361                         strcpy(++p, platform_expectfile);
1362         }
1363
1364         /* Name to use for temporary diff file */
1365         snprintf(diff, sizeof(diff), "%s.diff", resultsfile);
1366
1367         /* OK, run the diff */
1368         snprintf(cmd, sizeof(cmd),
1369                          "diff %s \"%s\" \"%s\" > \"%s\"",
1370                          basic_diff_opts, expectfile, resultsfile, diff);
1371
1372         /* Is the diff file empty? */
1373         if (run_diff(cmd, diff) == 0)
1374         {
1375                 unlink(diff);
1376                 return false;
1377         }
1378
1379         /* There may be secondary comparison files that match better */
1380         best_line_count = file_line_count(diff);
1381         strcpy(best_expect_file, expectfile);
1382
1383         for (i = 0; i <= 9; i++)
1384         {
1385                 char       *alt_expectfile;
1386
1387                 alt_expectfile = get_alternative_expectfile(expectfile, i);
1388                 if (!alt_expectfile)
1389                 {
1390                         fprintf(stderr, _("Unable to check secondary comparison files: %s\n"),
1391                                         strerror(errno));
1392                         exit(2);
1393                 }
1394
1395                 if (!file_exists(alt_expectfile))
1396                 {
1397                         free(alt_expectfile);
1398                         continue;
1399                 }
1400
1401                 snprintf(cmd, sizeof(cmd),
1402                                  "diff %s \"%s\" \"%s\" > \"%s\"",
1403                                  basic_diff_opts, alt_expectfile, resultsfile, diff);
1404
1405                 if (run_diff(cmd, diff) == 0)
1406                 {
1407                         unlink(diff);
1408                         free(alt_expectfile);
1409                         return false;
1410                 }
1411
1412                 l = file_line_count(diff);
1413                 if (l < best_line_count)
1414                 {
1415                         /* This diff was a better match than the last one */
1416                         best_line_count = l;
1417                         strlcpy(best_expect_file, alt_expectfile, sizeof(best_expect_file));
1418                 }
1419                 free(alt_expectfile);
1420         }
1421
1422         /*
1423          * fall back on the canonical results file if we haven't tried it yet and
1424          * haven't found a complete match yet.
1425          */
1426
1427         if (platform_expectfile)
1428         {
1429                 snprintf(cmd, sizeof(cmd),
1430                                  "diff %s \"%s\" \"%s\" > \"%s\"",
1431                                  basic_diff_opts, default_expectfile, resultsfile, diff);
1432
1433                 if (run_diff(cmd, diff) == 0)
1434                 {
1435                         /* No diff = no changes = good */
1436                         unlink(diff);
1437                         return false;
1438                 }
1439
1440                 l = file_line_count(diff);
1441                 if (l < best_line_count)
1442                 {
1443                         /* This diff was a better match than the last one */
1444                         best_line_count = l;
1445                         strlcpy(best_expect_file, default_expectfile, sizeof(best_expect_file));
1446                 }
1447         }
1448
1449         /*
1450          * Use the best comparison file to generate the "pretty" diff, which we
1451          * append to the diffs summary file.
1452          */
1453         snprintf(cmd, sizeof(cmd),
1454                          "diff %s \"%s\" \"%s\" >> \"%s\"",
1455                          pretty_diff_opts, best_expect_file, resultsfile, difffilename);
1456         run_diff(cmd, difffilename);
1457
1458         /* And append a separator */
1459         difffile = fopen(difffilename, "a");
1460         if (difffile)
1461         {
1462                 fprintf(difffile,
1463                                 "\n======================================================================\n\n");
1464                 fclose(difffile);
1465         }
1466
1467         unlink(diff);
1468         return true;
1469 }
1470
1471 /*
1472  * Wait for specified subprocesses to finish, and return their exit
1473  * statuses into statuses[]
1474  *
1475  * If names isn't NULL, print each subprocess's name as it finishes
1476  *
1477  * Note: it's OK to scribble on the pids array, but not on the names array
1478  */
1479 static void
1480 wait_for_tests(PID_TYPE * pids, int *statuses, char **names, int num_tests)
1481 {
1482         int                     tests_left;
1483         int                     i;
1484
1485 #ifdef WIN32
1486         PID_TYPE   *active_pids = pg_malloc(num_tests * sizeof(PID_TYPE));
1487
1488         memcpy(active_pids, pids, num_tests * sizeof(PID_TYPE));
1489 #endif
1490
1491         tests_left = num_tests;
1492         while (tests_left > 0)
1493         {
1494                 PID_TYPE        p;
1495
1496 #ifndef WIN32
1497                 int                     exit_status;
1498
1499                 p = wait(&exit_status);
1500
1501                 if (p == INVALID_PID)
1502                 {
1503                         fprintf(stderr, _("failed to wait for subprocesses: %s\n"),
1504                                         strerror(errno));
1505                         exit(2);
1506                 }
1507 #else
1508                 DWORD           exit_status;
1509                 int                     r;
1510
1511                 r = WaitForMultipleObjects(tests_left, active_pids, FALSE, INFINITE);
1512                 if (r < WAIT_OBJECT_0 || r >= WAIT_OBJECT_0 + tests_left)
1513                 {
1514                         fprintf(stderr, _("failed to wait for subprocesses: error code %lu\n"),
1515                                         GetLastError());
1516                         exit(2);
1517                 }
1518                 p = active_pids[r - WAIT_OBJECT_0];
1519                 /* compact the active_pids array */
1520                 active_pids[r - WAIT_OBJECT_0] = active_pids[tests_left - 1];
1521 #endif   /* WIN32 */
1522
1523                 for (i = 0; i < num_tests; i++)
1524                 {
1525                         if (p == pids[i])
1526                         {
1527 #ifdef WIN32
1528                                 GetExitCodeProcess(pids[i], &exit_status);
1529                                 CloseHandle(pids[i]);
1530 #endif
1531                                 pids[i] = INVALID_PID;
1532                                 statuses[i] = (int) exit_status;
1533                                 if (names)
1534                                         status(" %s", names[i]);
1535                                 tests_left--;
1536                                 break;
1537                         }
1538                 }
1539         }
1540
1541 #ifdef WIN32
1542         free(active_pids);
1543 #endif
1544 }
1545
1546 /*
1547  * report nonzero exit code from a test process
1548  */
1549 static void
1550 log_child_failure(int exitstatus)
1551 {
1552         if (WIFEXITED(exitstatus))
1553                 status(_(" (test process exited with exit code %d)"),
1554                            WEXITSTATUS(exitstatus));
1555         else if (WIFSIGNALED(exitstatus))
1556         {
1557 #if defined(WIN32)
1558                 status(_(" (test process was terminated by exception 0x%X)"),
1559                            WTERMSIG(exitstatus));
1560 #elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
1561                 status(_(" (test process was terminated by signal %d: %s)"),
1562                            WTERMSIG(exitstatus),
1563                            WTERMSIG(exitstatus) < NSIG ?
1564                            sys_siglist[WTERMSIG(exitstatus)] : "(unknown))");
1565 #else
1566                 status(_(" (test process was terminated by signal %d)"),
1567                            WTERMSIG(exitstatus));
1568 #endif
1569         }
1570         else
1571                 status(_(" (test process exited with unrecognized status %d)"),
1572                            exitstatus);
1573 }
1574
1575 /*
1576  * Run all the tests specified in one schedule file
1577  */
1578 static void
1579 run_schedule(const char *schedule, test_function tfunc)
1580 {
1581 #define MAX_PARALLEL_TESTS 100
1582         char       *tests[MAX_PARALLEL_TESTS];
1583         _stringlist *resultfiles[MAX_PARALLEL_TESTS];
1584         _stringlist *expectfiles[MAX_PARALLEL_TESTS];
1585         _stringlist *tags[MAX_PARALLEL_TESTS];
1586         PID_TYPE        pids[MAX_PARALLEL_TESTS];
1587         int                     statuses[MAX_PARALLEL_TESTS];
1588         _stringlist *ignorelist = NULL;
1589         char            scbuf[1024];
1590         FILE       *scf;
1591         int                     line_num = 0;
1592
1593         memset(resultfiles, 0, sizeof(_stringlist *) * MAX_PARALLEL_TESTS);
1594         memset(expectfiles, 0, sizeof(_stringlist *) * MAX_PARALLEL_TESTS);
1595         memset(tags, 0, sizeof(_stringlist *) * MAX_PARALLEL_TESTS);
1596
1597         scf = fopen(schedule, "r");
1598         if (!scf)
1599         {
1600                 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1601                                 progname, schedule, strerror(errno));
1602                 exit(2);
1603         }
1604
1605         while (fgets(scbuf, sizeof(scbuf), scf))
1606         {
1607                 char       *test = NULL;
1608                 char       *c;
1609                 int                     num_tests;
1610                 bool            inword;
1611                 int                     i;
1612
1613                 line_num++;
1614
1615                 for (i = 0; i < MAX_PARALLEL_TESTS; i++)
1616                 {
1617                         if (resultfiles[i] == NULL)
1618                                 break;
1619                         free_stringlist(&resultfiles[i]);
1620                         free_stringlist(&expectfiles[i]);
1621                         free_stringlist(&tags[i]);
1622                 }
1623
1624                 /* strip trailing whitespace, especially the newline */
1625                 i = strlen(scbuf);
1626                 while (i > 0 && isspace((unsigned char) scbuf[i - 1]))
1627                         scbuf[--i] = '\0';
1628
1629                 if (scbuf[0] == '\0' || scbuf[0] == '#')
1630                         continue;
1631                 if (strncmp(scbuf, "test: ", 6) == 0)
1632                         test = scbuf + 6;
1633                 else if (strncmp(scbuf, "ignore: ", 8) == 0)
1634                 {
1635                         c = scbuf + 8;
1636                         while (*c && isspace((unsigned char) *c))
1637                                 c++;
1638                         add_stringlist_item(&ignorelist, c);
1639
1640                         /*
1641                          * Note: ignore: lines do not run the test, they just say that
1642                          * failure of this test when run later on is to be ignored. A bit
1643                          * odd but that's how the shell-script version did it.
1644                          */
1645                         continue;
1646                 }
1647                 else
1648                 {
1649                         fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1650                                         schedule, line_num, scbuf);
1651                         exit(2);
1652                 }
1653
1654                 num_tests = 0;
1655                 inword = false;
1656                 for (c = test; *c; c++)
1657                 {
1658                         if (isspace((unsigned char) *c))
1659                         {
1660                                 *c = '\0';
1661                                 inword = false;
1662                         }
1663                         else if (!inword)
1664                         {
1665                                 if (num_tests >= MAX_PARALLEL_TESTS)
1666                                 {
1667                                         /* can't print scbuf here, it's already been trashed */
1668                                         fprintf(stderr, _("too many parallel tests in schedule file \"%s\", line %d\n"),
1669                                                         schedule, line_num);
1670                                         exit(2);
1671                                 }
1672                                 tests[num_tests] = c;
1673                                 num_tests++;
1674                                 inword = true;
1675                         }
1676                 }
1677
1678                 if (num_tests == 0)
1679                 {
1680                         fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1681                                         schedule, line_num, scbuf);
1682                         exit(2);
1683                 }
1684
1685                 if (num_tests == 1)
1686                 {
1687                         status(_("test %-24s ... "), tests[0]);
1688                         pids[0] = (tfunc) (tests[0], &resultfiles[0], &expectfiles[0], &tags[0]);
1689                         wait_for_tests(pids, statuses, NULL, 1);
1690                         /* status line is finished below */
1691                 }
1692                 else if (max_connections > 0 && max_connections < num_tests)
1693                 {
1694                         int                     oldest = 0;
1695
1696                         status(_("parallel group (%d tests, in groups of %d): "),
1697                                    num_tests, max_connections);
1698                         for (i = 0; i < num_tests; i++)
1699                         {
1700                                 if (i - oldest >= max_connections)
1701                                 {
1702                                         wait_for_tests(pids + oldest, statuses + oldest,
1703                                                                    tests + oldest, i - oldest);
1704                                         oldest = i;
1705                                 }
1706                                 pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1707                         }
1708                         wait_for_tests(pids + oldest, statuses + oldest,
1709                                                    tests + oldest, i - oldest);
1710                         status_end();
1711                 }
1712                 else
1713                 {
1714                         status(_("parallel group (%d tests): "), num_tests);
1715                         for (i = 0; i < num_tests; i++)
1716                         {
1717                                 pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1718                         }
1719                         wait_for_tests(pids, statuses, tests, num_tests);
1720                         status_end();
1721                 }
1722
1723                 /* Check results for all tests */
1724                 for (i = 0; i < num_tests; i++)
1725                 {
1726                         _stringlist *rl,
1727                                            *el,
1728                                            *tl;
1729                         bool            differ = false;
1730
1731                         if (num_tests > 1)
1732                                 status(_("     %-24s ... "), tests[i]);
1733
1734                         /*
1735                          * Advance over all three lists simultaneously.
1736                          *
1737                          * Compare resultfiles[j] with expectfiles[j] always. Tags are
1738                          * optional but if there are tags, the tag list has the same
1739                          * length as the other two lists.
1740                          */
1741                         for (rl = resultfiles[i], el = expectfiles[i], tl = tags[i];
1742                                  rl != NULL;    /* rl and el have the same length */
1743                                  rl = rl->next, el = el->next)
1744                         {
1745                                 bool            newdiff;
1746
1747                                 if (tl)
1748                                         tl = tl->next;          /* tl has the same length as rl and el
1749                                                                                  * if it exists */
1750
1751                                 newdiff = results_differ(tests[i], rl->str, el->str);
1752                                 if (newdiff && tl)
1753                                 {
1754                                         printf("%s ", tl->str);
1755                                 }
1756                                 differ |= newdiff;
1757                         }
1758
1759                         if (differ)
1760                         {
1761                                 bool            ignore = false;
1762                                 _stringlist *sl;
1763
1764                                 for (sl = ignorelist; sl != NULL; sl = sl->next)
1765                                 {
1766                                         if (strcmp(tests[i], sl->str) == 0)
1767                                         {
1768                                                 ignore = true;
1769                                                 break;
1770                                         }
1771                                 }
1772                                 if (ignore)
1773                                 {
1774                                         status(_("failed (ignored)"));
1775                                         fail_ignore_count++;
1776                                 }
1777                                 else
1778                                 {
1779                                         status(_("FAILED"));
1780                                         fail_count++;
1781                                 }
1782                         }
1783                         else
1784                         {
1785                                 status(_("ok"));
1786                                 success_count++;
1787                         }
1788
1789                         if (statuses[i] != 0)
1790                                 log_child_failure(statuses[i]);
1791
1792                         status_end();
1793                 }
1794         }
1795
1796         free_stringlist(&ignorelist);
1797
1798         fclose(scf);
1799 }
1800
1801 /*
1802  * Run a single test
1803  */
1804 static void
1805 run_single_test(const char *test, test_function tfunc)
1806 {
1807         PID_TYPE        pid;
1808         int                     exit_status;
1809         _stringlist *resultfiles = NULL;
1810         _stringlist *expectfiles = NULL;
1811         _stringlist *tags = NULL;
1812         _stringlist *rl,
1813                            *el,
1814                            *tl;
1815         bool            differ = false;
1816
1817         status(_("test %-24s ... "), test);
1818         pid = (tfunc) (test, &resultfiles, &expectfiles, &tags);
1819         wait_for_tests(&pid, &exit_status, NULL, 1);
1820
1821         /*
1822          * Advance over all three lists simultaneously.
1823          *
1824          * Compare resultfiles[j] with expectfiles[j] always. Tags are optional
1825          * but if there are tags, the tag list has the same length as the other
1826          * two lists.
1827          */
1828         for (rl = resultfiles, el = expectfiles, tl = tags;
1829                  rl != NULL;                    /* rl and el have the same length */
1830                  rl = rl->next, el = el->next)
1831         {
1832                 bool            newdiff;
1833
1834                 if (tl)
1835                         tl = tl->next;          /* tl has the same length as rl and el if it
1836                                                                  * exists */
1837
1838                 newdiff = results_differ(test, rl->str, el->str);
1839                 if (newdiff && tl)
1840                 {
1841                         printf("%s ", tl->str);
1842                 }
1843                 differ |= newdiff;
1844         }
1845
1846         if (differ)
1847         {
1848                 status(_("FAILED"));
1849                 fail_count++;
1850         }
1851         else
1852         {
1853                 status(_("ok"));
1854                 success_count++;
1855         }
1856
1857         if (exit_status != 0)
1858                 log_child_failure(exit_status);
1859
1860         status_end();
1861 }
1862
1863 /*
1864  * Create the summary-output files (making them empty if already existing)
1865  */
1866 static void
1867 open_result_files(void)
1868 {
1869         char            file[MAXPGPATH];
1870         FILE       *difffile;
1871
1872         /* create the log file (copy of running status output) */
1873         snprintf(file, sizeof(file), "%s/regression.out", outputdir);
1874         logfilename = pg_strdup(file);
1875         logfile = fopen(logfilename, "w");
1876         if (!logfile)
1877         {
1878                 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1879                                 progname, logfilename, strerror(errno));
1880                 exit(2);
1881         }
1882
1883         /* create the diffs file as empty */
1884         snprintf(file, sizeof(file), "%s/regression.diffs", outputdir);
1885         difffilename = pg_strdup(file);
1886         difffile = fopen(difffilename, "w");
1887         if (!difffile)
1888         {
1889                 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1890                                 progname, difffilename, strerror(errno));
1891                 exit(2);
1892         }
1893         /* we don't keep the diffs file open continuously */
1894         fclose(difffile);
1895
1896         /* also create the output directory if not present */
1897         snprintf(file, sizeof(file), "%s/results", outputdir);
1898         if (!directory_exists(file))
1899                 make_directory(file);
1900 }
1901
1902 static void
1903 drop_database_if_exists(const char *dbname)
1904 {
1905         header(_("dropping database \"%s\""), dbname);
1906         psql_command("postgres", "DROP DATABASE IF EXISTS \"%s\"", dbname);
1907 }
1908
1909 static void
1910 create_database(const char *dbname)
1911 {
1912         _stringlist *sl;
1913
1914         /*
1915          * We use template0 so that any installation-local cruft in template1 will
1916          * not mess up the tests.
1917          */
1918         header(_("creating database \"%s\""), dbname);
1919         if (encoding)
1920                 psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
1921                                          (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1922         else
1923                 psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
1924                                          (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1925         psql_command(dbname,
1926                                  "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
1927                                  "ALTER DATABASE \"%s\" SET lc_monetary TO 'C';"
1928                                  "ALTER DATABASE \"%s\" SET lc_numeric TO 'C';"
1929                                  "ALTER DATABASE \"%s\" SET lc_time TO 'C';"
1930                         "ALTER DATABASE \"%s\" SET timezone_abbreviations TO 'Default';",
1931                                  dbname, dbname, dbname, dbname, dbname);
1932
1933         /*
1934          * Install any requested procedural languages.  We use CREATE OR REPLACE
1935          * so that this will work whether or not the language is preinstalled.
1936          */
1937         for (sl = loadlanguage; sl != NULL; sl = sl->next)
1938         {
1939                 header(_("installing %s"), sl->str);
1940                 psql_command(dbname, "CREATE OR REPLACE LANGUAGE \"%s\"", sl->str);
1941         }
1942
1943         /*
1944          * Install any requested extensions.  We use CREATE IF NOT EXISTS so that
1945          * this will work whether or not the extension is preinstalled.
1946          */
1947         for (sl = loadextension; sl != NULL; sl = sl->next)
1948         {
1949                 header(_("installing %s"), sl->str);
1950                 psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
1951         }
1952 }
1953
1954 static void
1955 drop_role_if_exists(const char *rolename)
1956 {
1957         header(_("dropping role \"%s\""), rolename);
1958         psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
1959 }
1960
1961 static void
1962 create_role(const char *rolename, const _stringlist *granted_dbs)
1963 {
1964         header(_("creating role \"%s\""), rolename);
1965         psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
1966         for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
1967         {
1968                 psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
1969                                          granted_dbs->str, rolename);
1970         }
1971 }
1972
1973 static void
1974 help(void)
1975 {
1976         printf(_("PostgreSQL regression test driver\n"));
1977         printf(_("\n"));
1978         printf(_("Usage:\n  %s [OPTION]... [EXTRA-TEST]...\n"), progname);
1979         printf(_("\n"));
1980         printf(_("Options:\n"));
1981         printf(_("  --config-auth=DATADIR     update authentication settings for DATADIR\n"));
1982         printf(_("  --create-role=ROLE        create the specified role before testing\n"));
1983         printf(_("  --dbname=DB               use database DB (default \"regression\")\n"));
1984         printf(_("  --debug                   turn on debug mode in programs that are run\n"));
1985         printf(_("  --dlpath=DIR              look for dynamic libraries in DIR\n"));
1986         printf(_("  --encoding=ENCODING       use ENCODING as the encoding\n"));
1987         printf(_("  --inputdir=DIR            take input files from DIR (default \".\")\n"));
1988         printf(_("  --launcher=CMD            use CMD as launcher of psql\n"));
1989         printf(_("  --load-extension=EXT      load the named extension before running the\n"));
1990         printf(_("                            tests; can appear multiple times\n"));
1991         printf(_("  --load-language=LANG      load the named language before running the\n"));
1992         printf(_("                            tests; can appear multiple times\n"));
1993         printf(_("  --max-connections=N       maximum number of concurrent connections\n"));
1994         printf(_("                            (default is 0, meaning unlimited)\n"));
1995         printf(_("  --outputdir=DIR           place output files in DIR (default \".\")\n"));
1996         printf(_("  --schedule=FILE           use test ordering schedule from FILE\n"));
1997         printf(_("                            (can be used multiple times to concatenate)\n"));
1998         printf(_("  --temp-instance=DIR       create a temporary instance in DIR\n"));
1999         printf(_("  --use-existing            use an existing installation\n"));
2000         printf(_("\n"));
2001         printf(_("Options for \"temp-instance\" mode:\n"));
2002         printf(_("  --no-locale               use C locale\n"));
2003         printf(_("  --port=PORT               start postmaster on PORT\n"));
2004         printf(_("  --temp-config=FILE        append contents of FILE to temporary config\n"));
2005         printf(_("\n"));
2006         printf(_("Options for using an existing installation:\n"));
2007         printf(_("  --host=HOST               use postmaster running on HOST\n"));
2008         printf(_("  --port=PORT               use postmaster running at PORT\n"));
2009         printf(_("  --user=USER               connect as USER\n"));
2010         printf(_("\n"));
2011         printf(_("The exit status is 0 if all tests passed, 1 if some tests failed, and 2\n"));
2012         printf(_("if the tests could not be run for some reason.\n"));
2013         printf(_("\n"));
2014         printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
2015 }
2016
2017 int
2018 regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc)
2019 {
2020         static struct option long_options[] = {
2021                 {"help", no_argument, NULL, 'h'},
2022                 {"version", no_argument, NULL, 'V'},
2023                 {"dbname", required_argument, NULL, 1},
2024                 {"debug", no_argument, NULL, 2},
2025                 {"inputdir", required_argument, NULL, 3},
2026                 {"load-language", required_argument, NULL, 4},
2027                 {"max-connections", required_argument, NULL, 5},
2028                 {"encoding", required_argument, NULL, 6},
2029                 {"outputdir", required_argument, NULL, 7},
2030                 {"schedule", required_argument, NULL, 8},
2031                 {"temp-instance", required_argument, NULL, 9},
2032                 {"no-locale", no_argument, NULL, 10},
2033                 {"host", required_argument, NULL, 13},
2034                 {"port", required_argument, NULL, 14},
2035                 {"user", required_argument, NULL, 15},
2036                 {"bindir", required_argument, NULL, 16},
2037                 {"dlpath", required_argument, NULL, 17},
2038                 {"create-role", required_argument, NULL, 18},
2039                 {"temp-config", required_argument, NULL, 19},
2040                 {"use-existing", no_argument, NULL, 20},
2041                 {"launcher", required_argument, NULL, 21},
2042                 {"load-extension", required_argument, NULL, 22},
2043                 {"config-auth", required_argument, NULL, 24},
2044                 {NULL, 0, NULL, 0}
2045         };
2046
2047         _stringlist *sl;
2048         int                     c;
2049         int                     i;
2050         int                     option_index;
2051         char            buf[MAXPGPATH * 4];
2052         char            buf2[MAXPGPATH * 4];
2053
2054         progname = get_progname(argv[0]);
2055         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_regress"));
2056
2057         atexit(stop_postmaster);
2058
2059 #ifndef HAVE_UNIX_SOCKETS
2060         /* no unix domain sockets available, so change default */
2061         hostname = "localhost";
2062 #endif
2063
2064         /*
2065          * We call the initialization function here because that way we can set
2066          * default parameters and let them be overwritten by the commandline.
2067          */
2068         ifunc(argc, argv);
2069
2070         if (getenv("PG_REGRESS_DIFF_OPTS"))
2071                 pretty_diff_opts = getenv("PG_REGRESS_DIFF_OPTS");
2072
2073         while ((c = getopt_long(argc, argv, "hV", long_options, &option_index)) != -1)
2074         {
2075                 switch (c)
2076                 {
2077                         case 'h':
2078                                 help();
2079                                 exit(0);
2080                         case 'V':
2081                                 puts("pg_regress (PostgreSQL) " PG_VERSION);
2082                                 exit(0);
2083                         case 1:
2084
2085                                 /*
2086                                  * If a default database was specified, we need to remove it
2087                                  * before we add the specified one.
2088                                  */
2089                                 free_stringlist(&dblist);
2090                                 split_to_stringlist(optarg, ",", &dblist);
2091                                 break;
2092                         case 2:
2093                                 debug = true;
2094                                 break;
2095                         case 3:
2096                                 inputdir = pg_strdup(optarg);
2097                                 break;
2098                         case 4:
2099                                 add_stringlist_item(&loadlanguage, optarg);
2100                                 break;
2101                         case 5:
2102                                 max_connections = atoi(optarg);
2103                                 break;
2104                         case 6:
2105                                 encoding = pg_strdup(optarg);
2106                                 break;
2107                         case 7:
2108                                 outputdir = pg_strdup(optarg);
2109                                 break;
2110                         case 8:
2111                                 add_stringlist_item(&schedulelist, optarg);
2112                                 break;
2113                         case 9:
2114                                 temp_instance = make_absolute_path(optarg);
2115                                 break;
2116                         case 10:
2117                                 nolocale = true;
2118                                 break;
2119                         case 13:
2120                                 hostname = pg_strdup(optarg);
2121                                 break;
2122                         case 14:
2123                                 port = atoi(optarg);
2124                                 port_specified_by_user = true;
2125                                 break;
2126                         case 15:
2127                                 user = pg_strdup(optarg);
2128                                 break;
2129                         case 16:
2130                                 /* "--bindir=" means to use PATH */
2131                                 if (strlen(optarg))
2132                                         bindir = pg_strdup(optarg);
2133                                 else
2134                                         bindir = NULL;
2135                                 break;
2136                         case 17:
2137                                 dlpath = pg_strdup(optarg);
2138                                 break;
2139                         case 18:
2140                                 split_to_stringlist(optarg, ",", &extraroles);
2141                                 break;
2142                         case 19:
2143                                 add_stringlist_item(&temp_configs, optarg);
2144                                 break;
2145                         case 20:
2146                                 use_existing = true;
2147                                 break;
2148                         case 21:
2149                                 launcher = pg_strdup(optarg);
2150                                 break;
2151                         case 22:
2152                                 add_stringlist_item(&loadextension, optarg);
2153                                 break;
2154                         case 24:
2155                                 config_auth_datadir = pg_strdup(optarg);
2156                                 break;
2157                         default:
2158                                 /* getopt_long already emitted a complaint */
2159                                 fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
2160                                                 progname);
2161                                 exit(2);
2162                 }
2163         }
2164
2165         /*
2166          * if we still have arguments, they are extra tests to run
2167          */
2168         while (argc - optind >= 1)
2169         {
2170                 add_stringlist_item(&extra_tests, argv[optind]);
2171                 optind++;
2172         }
2173
2174         if (config_auth_datadir)
2175         {
2176 #ifdef ENABLE_SSPI
2177                 config_sspi_auth(config_auth_datadir);
2178 #endif
2179                 exit(0);
2180         }
2181
2182         if (temp_instance && !port_specified_by_user)
2183
2184                 /*
2185                  * To reduce chances of interference with parallel installations, use
2186                  * a port number starting in the private range (49152-65535)
2187                  * calculated from the version number.  This aids !HAVE_UNIX_SOCKETS
2188                  * systems; elsewhere, the use of a private socket directory already
2189                  * prevents interference.
2190                  */
2191                 port = 0xC000 | (PG_VERSION_NUM & 0x3FFF);
2192
2193         inputdir = make_absolute_path(inputdir);
2194         outputdir = make_absolute_path(outputdir);
2195         dlpath = make_absolute_path(dlpath);
2196
2197         /*
2198          * Initialization
2199          */
2200         open_result_files();
2201
2202         initialize_environment();
2203
2204 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
2205         unlimit_core_size();
2206 #endif
2207
2208         if (temp_instance)
2209         {
2210                 FILE       *pg_conf;
2211                 const char *env_wait;
2212                 int                     wait_seconds;
2213
2214                 /*
2215                  * Prepare the temp instance
2216                  */
2217
2218                 if (directory_exists(temp_instance))
2219                 {
2220                         header(_("removing existing temp instance"));
2221                         if (!rmtree(temp_instance, true))
2222                         {
2223                                 fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2224                                                 progname, temp_instance);
2225                                 exit(2);
2226                         }
2227                 }
2228
2229                 header(_("creating temporary instance"));
2230
2231                 /* make the temp instance top directory */
2232                 make_directory(temp_instance);
2233
2234                 /* and a directory for log files */
2235                 snprintf(buf, sizeof(buf), "%s/log", outputdir);
2236                 if (!directory_exists(buf))
2237                         make_directory(buf);
2238
2239                 /* initdb */
2240                 header(_("initializing database system"));
2241                 snprintf(buf, sizeof(buf),
2242                                  "\"%s%sinitdb\" -D \"%s/data\" --noclean --nosync%s%s > \"%s/log/initdb.log\" 2>&1",
2243                                  bindir ? bindir : "",
2244                                  bindir ? "/" : "",
2245                                  temp_instance,
2246                                  debug ? " --debug" : "",
2247                                  nolocale ? " --no-locale" : "",
2248                                  outputdir);
2249                 if (system(buf))
2250                 {
2251                         fprintf(stderr, _("\n%s: initdb failed\nExamine %s/log/initdb.log for the reason.\nCommand was: %s\n"), progname, outputdir, buf);
2252                         exit(2);
2253                 }
2254
2255                 /*
2256                  * Adjust the default postgresql.conf for regression testing. The user
2257                  * can specify a file to be appended; in any case we expand logging
2258                  * and set max_prepared_transactions to enable testing of prepared
2259                  * xacts.  (Note: to reduce the probability of unexpected shmmax
2260                  * failures, don't set max_prepared_transactions any higher than
2261                  * actually needed by the prepared_xacts regression test.)
2262                  */
2263                 snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_instance);
2264                 pg_conf = fopen(buf, "a");
2265                 if (pg_conf == NULL)
2266                 {
2267                         fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno));
2268                         exit(2);
2269                 }
2270                 fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
2271                 fputs("log_autovacuum_min_duration = 0\n", pg_conf);
2272                 fputs("log_checkpoints = on\n", pg_conf);
2273                 fputs("log_lock_waits = on\n", pg_conf);
2274                 fputs("log_temp_files = 128kB\n", pg_conf);
2275                 fputs("max_prepared_transactions = 2\n", pg_conf);
2276
2277                 for (sl = temp_configs; sl != NULL; sl = sl->next)
2278                 {
2279                         char       *temp_config = sl->str;
2280                         FILE       *extra_conf;
2281                         char            line_buf[1024];
2282
2283                         extra_conf = fopen(temp_config, "r");
2284                         if (extra_conf == NULL)
2285                         {
2286                                 fprintf(stderr, _("\n%s: could not open \"%s\" to read extra config: %s\n"), progname, temp_config, strerror(errno));
2287                                 exit(2);
2288                         }
2289                         while (fgets(line_buf, sizeof(line_buf), extra_conf) != NULL)
2290                                 fputs(line_buf, pg_conf);
2291                         fclose(extra_conf);
2292                 }
2293
2294                 fclose(pg_conf);
2295
2296 #ifdef ENABLE_SSPI
2297
2298                 /*
2299                  * Since we successfully used the same buffer for the much-longer
2300                  * "initdb" command, this can't truncate.
2301                  */
2302                 snprintf(buf, sizeof(buf), "%s/data", temp_instance);
2303                 config_sspi_auth(buf);
2304 #elif !defined(HAVE_UNIX_SOCKETS)
2305 #error Platform has no means to secure the test installation.
2306 #endif
2307
2308                 /*
2309                  * Check if there is a postmaster running already.
2310                  */
2311                 snprintf(buf2, sizeof(buf2),
2312                                  "\"%s%spsql\" -X postgres <%s 2>%s",
2313                                  bindir ? bindir : "",
2314                                  bindir ? "/" : "",
2315                                  DEVNULL, DEVNULL);
2316
2317                 for (i = 0; i < 16; i++)
2318                 {
2319                         if (system(buf2) == 0)
2320                         {
2321                                 char            s[16];
2322
2323                                 if (port_specified_by_user || i == 15)
2324                                 {
2325                                         fprintf(stderr, _("port %d apparently in use\n"), port);
2326                                         if (!port_specified_by_user)
2327                                                 fprintf(stderr, _("%s: could not determine an available port\n"), progname);
2328                                         fprintf(stderr, _("Specify an unused port using the --port option or shut down any conflicting PostgreSQL servers.\n"));
2329                                         exit(2);
2330                                 }
2331
2332                                 fprintf(stderr, _("port %d apparently in use, trying %d\n"), port, port + 1);
2333                                 port++;
2334                                 sprintf(s, "%d", port);
2335                                 doputenv("PGPORT", s);
2336                         }
2337                         else
2338                                 break;
2339                 }
2340
2341                 /*
2342                  * Start the temp postmaster
2343                  */
2344                 header(_("starting postmaster"));
2345                 snprintf(buf, sizeof(buf),
2346                                  "\"%s%spostgres\" -D \"%s/data\" -F%s "
2347                                  "-c \"listen_addresses=%s\" -k \"%s\" "
2348                                  "> \"%s/log/postmaster.log\" 2>&1",
2349                                  bindir ? bindir : "",
2350                                  bindir ? "/" : "",
2351                                  temp_instance, debug ? " -d 5" : "",
2352                                  hostname ? hostname : "", sockdir ? sockdir : "",
2353                                  outputdir);
2354                 postmaster_pid = spawn_process(buf);
2355                 if (postmaster_pid == INVALID_PID)
2356                 {
2357                         fprintf(stderr, _("\n%s: could not spawn postmaster: %s\n"),
2358                                         progname, strerror(errno));
2359                         exit(2);
2360                 }
2361
2362                 /*
2363                  * Wait till postmaster is able to accept connections; normally this
2364                  * is only a second or so, but Cygwin is reportedly *much* slower, and
2365                  * test builds using Valgrind or similar tools might be too.  Hence,
2366                  * allow the default timeout of 60 seconds to be overridden from the
2367                  * PGCTLTIMEOUT environment variable.
2368                  */
2369                 env_wait = getenv("PGCTLTIMEOUT");
2370                 if (env_wait != NULL)
2371                 {
2372                         wait_seconds = atoi(env_wait);
2373                         if (wait_seconds <= 0)
2374                                 wait_seconds = 60;
2375                 }
2376                 else
2377                         wait_seconds = 60;
2378
2379                 for (i = 0; i < wait_seconds; i++)
2380                 {
2381                         /* Done if psql succeeds */
2382                         if (system(buf2) == 0)
2383                                 break;
2384
2385                         /*
2386                          * Fail immediately if postmaster has exited
2387                          */
2388 #ifndef WIN32
2389                         if (kill(postmaster_pid, 0) != 0)
2390 #else
2391                         if (WaitForSingleObject(postmaster_pid, 0) == WAIT_OBJECT_0)
2392 #endif
2393                         {
2394                                 fprintf(stderr, _("\n%s: postmaster failed\nExamine %s/log/postmaster.log for the reason\n"), progname, outputdir);
2395                                 exit(2);
2396                         }
2397
2398                         pg_usleep(1000000L);
2399                 }
2400                 if (i >= wait_seconds)
2401                 {
2402                         fprintf(stderr, _("\n%s: postmaster did not respond within %d seconds\nExamine %s/log/postmaster.log for the reason\n"),
2403                                         progname, wait_seconds, outputdir);
2404
2405                         /*
2406                          * If we get here, the postmaster is probably wedged somewhere in
2407                          * startup.  Try to kill it ungracefully rather than leaving a
2408                          * stuck postmaster that might interfere with subsequent test
2409                          * attempts.
2410                          */
2411 #ifndef WIN32
2412                         if (kill(postmaster_pid, SIGKILL) != 0 &&
2413                                 errno != ESRCH)
2414                                 fprintf(stderr, _("\n%s: could not kill failed postmaster: %s\n"),
2415                                                 progname, strerror(errno));
2416 #else
2417                         if (TerminateProcess(postmaster_pid, 255) == 0)
2418                                 fprintf(stderr, _("\n%s: could not kill failed postmaster: error code %lu\n"),
2419                                                 progname, GetLastError());
2420 #endif
2421
2422                         exit(2);
2423                 }
2424
2425                 postmaster_running = true;
2426
2427 #ifdef _WIN64
2428 /* need a series of two casts to convert HANDLE without compiler warning */
2429 #define ULONGPID(x) (unsigned long) (unsigned long long) (x)
2430 #else
2431 #define ULONGPID(x) (unsigned long) (x)
2432 #endif
2433                 printf(_("running on port %d with PID %lu\n"),
2434                            port, ULONGPID(postmaster_pid));
2435         }
2436         else
2437         {
2438                 /*
2439                  * Using an existing installation, so may need to get rid of
2440                  * pre-existing database(s) and role(s)
2441                  */
2442                 if (!use_existing)
2443                 {
2444                         for (sl = dblist; sl; sl = sl->next)
2445                                 drop_database_if_exists(sl->str);
2446                         for (sl = extraroles; sl; sl = sl->next)
2447                                 drop_role_if_exists(sl->str);
2448                 }
2449         }
2450
2451         /*
2452          * Create the test database(s) and role(s)
2453          */
2454         if (!use_existing)
2455         {
2456                 for (sl = dblist; sl; sl = sl->next)
2457                         create_database(sl->str);
2458                 for (sl = extraroles; sl; sl = sl->next)
2459                         create_role(sl->str, dblist);
2460         }
2461
2462         /*
2463          * Ready to run the tests
2464          */
2465         header(_("running regression test queries"));
2466
2467         for (sl = schedulelist; sl != NULL; sl = sl->next)
2468         {
2469                 run_schedule(sl->str, tfunc);
2470         }
2471
2472         for (sl = extra_tests; sl != NULL; sl = sl->next)
2473         {
2474                 run_single_test(sl->str, tfunc);
2475         }
2476
2477         /*
2478          * Shut down temp installation's postmaster
2479          */
2480         if (temp_instance)
2481         {
2482                 header(_("shutting down postmaster"));
2483                 stop_postmaster();
2484         }
2485
2486         /*
2487          * If there were no errors, remove the temp instance immediately to
2488          * conserve disk space.  (If there were errors, we leave the instance in
2489          * place for possible manual investigation.)
2490          */
2491         if (temp_instance && fail_count == 0 && fail_ignore_count == 0)
2492         {
2493                 header(_("removing temporary instance"));
2494                 if (!rmtree(temp_instance, true))
2495                         fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2496                                         progname, temp_instance);
2497         }
2498
2499         fclose(logfile);
2500
2501         /*
2502          * Emit nice-looking summary message
2503          */
2504         if (fail_count == 0 && fail_ignore_count == 0)
2505                 snprintf(buf, sizeof(buf),
2506                                  _(" All %d tests passed. "),
2507                                  success_count);
2508         else if (fail_count == 0)       /* fail_count=0, fail_ignore_count>0 */
2509                 snprintf(buf, sizeof(buf),
2510                                  _(" %d of %d tests passed, %d failed test(s) ignored. "),
2511                                  success_count,
2512                                  success_count + fail_ignore_count,
2513                                  fail_ignore_count);
2514         else if (fail_ignore_count == 0)        /* fail_count>0 && fail_ignore_count=0 */
2515                 snprintf(buf, sizeof(buf),
2516                                  _(" %d of %d tests failed. "),
2517                                  fail_count,
2518                                  success_count + fail_count);
2519         else
2520                 /* fail_count>0 && fail_ignore_count>0 */
2521                 snprintf(buf, sizeof(buf),
2522                                  _(" %d of %d tests failed, %d of these failures ignored. "),
2523                                  fail_count + fail_ignore_count,
2524                                  success_count + fail_count + fail_ignore_count,
2525                                  fail_ignore_count);
2526
2527         putchar('\n');
2528         for (i = strlen(buf); i > 0; i--)
2529                 putchar('=');
2530         printf("\n%s\n", buf);
2531         for (i = strlen(buf); i > 0; i--)
2532                 putchar('=');
2533         putchar('\n');
2534         putchar('\n');
2535
2536         if (file_size(difffilename) > 0)
2537         {
2538                 printf(_("The differences that caused some tests to fail can be viewed in the\n"
2539                                  "file \"%s\".  A copy of the test summary that you see\n"
2540                                  "above is saved in the file \"%s\".\n\n"),
2541                            difffilename, logfilename);
2542         }
2543         else
2544         {
2545                 unlink(difffilename);
2546                 unlink(logfilename);
2547         }
2548
2549         if (fail_count != 0)
2550                 exit(1);
2551
2552         return 0;
2553 }