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