Обсуждение: BUG #6166: configure from source fails with 'This platform is not thread-safe.' but was actually /tmp perms

Поиск
Список
Период
Сортировка
The following bug has been logged online:

Bug reference:      6166
Logged by:          Alex Soto
Email address:      apsoto@gmail.com
PostgreSQL version: 9.0.4
Operating system:   Linux (CentOS release 5.0 (Final))
Description:        configure from source fails with 'This platform is not
thread-safe.' but was actually /tmp perms
Details:

I was trying to build the 9.0.4 source tarball as the postgres user on a
test machine.

The configure step failed with the error:
This platform is not thread-safe.  Check the file 'config.log' or compile
and run src/test/thread/thread_test for the exact reason.
Use --disable-thread-safety to disable thread safety.

As I started looking through the log file I noticed that it failed to write
to the /tmp directory.  Other configure steps had written to /var/tmp, but
this step tried to write to /tmp for some reason.

I fixed by correcting the permissions on the /tmp dir, but I thought the
error message was a little misleading, so I thought I'd report the problem.

The specific configure I ran was:
./configure --disable-integer-datetimes

but I don't think the config option likely makes a difference.
"Alex Soto" <apsoto@gmail.com> writes:
> I was trying to build the 9.0.4 source tarball as the postgres user on a
> test machine.

> The configure step failed with the error:
> This platform is not thread-safe.  Check the file 'config.log' or compile
> and run src/test/thread/thread_test for the exact reason.
> Use --disable-thread-safety to disable thread safety.

> As I started looking through the log file I noticed that it failed to write
> to the /tmp directory.  Other configure steps had written to /var/tmp, but
> this step tried to write to /tmp for some reason.

> I fixed by correcting the permissions on the /tmp dir, but I thought the
> error message was a little misleading, so I thought I'd report the problem.

Hmm ... I can't find any explicit reference to either /tmp or /var/tmp
in our configure script.  It seems like this must be an artifact of your
compiler, or some other tool you're using.

            regards, tom lane
Alex Soto <apsoto@gmail.com> writes:
> Here's the section in the config.log in case it makes a difference

> configure:28808: ./conftest
> Could not create file in /tmp or
> Could not generate failure for create file in /tmp **
> exiting
> configure:28812: $? = 1
> configure: program exited with status 1

[ greps... ]  Oh, that error is coming from src/test/thread/thread_test.c.

I wonder why we have that trying to write to /tmp at all, when all the
other transient junk generated by configure is in the current directory.
Bruce, do you have a good reason for doing it that way?

(The error message seems to be suffering from a bad case of copy-and-
paste-itis, too.)

            regards, tom lane
Tom Lane wrote:
> Alex Soto <apsoto@gmail.com> writes:
> > Here's the section in the config.log in case it makes a difference
>
> > configure:28808: ./conftest
> > Could not create file in /tmp or
> > Could not generate failure for create file in /tmp **
> > exiting
> > configure:28812: $? = 1
> > configure: program exited with status 1
>
> [ greps... ]  Oh, that error is coming from src/test/thread/thread_test.c.
>
> I wonder why we have that trying to write to /tmp at all, when all the
> other transient junk generated by configure is in the current directory.
> Bruce, do you have a good reason for doing it that way?

No idea --- using the current directory is probably best.

> (The error message seems to be suffering from a bad case of copy-and-
> paste-itis, too.)

OK, let me see if I can fix this.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
Tom Lane wrote:
> Alex Soto <apsoto@gmail.com> writes:
> > Here's the section in the config.log in case it makes a difference
>
> > configure:28808: ./conftest
> > Could not create file in /tmp or
> > Could not generate failure for create file in /tmp **
> > exiting
> > configure:28812: $? = 1
> > configure: program exited with status 1
>
> [ greps... ]  Oh, that error is coming from src/test/thread/thread_test.c.
>
> I wonder why we have that trying to write to /tmp at all, when all the
> other transient junk generated by configure is in the current directory.
> Bruce, do you have a good reason for doing it that way?

I have modified the code to use the current directory instead of /tmp.
I also cleaned up the #if defines and added some C comments.

> (The error message seems to be suffering from a bad case of copy-and-
> paste-itis, too.)

Actually, it is accurate.  The code is:

    #ifdef WIN32
        h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
        h2 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
        if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
    #else
        if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0 ||
            open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
    #endif
        {
            fprintf(stderr, "Could not create file in current directory or\n");
            fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
            exit(1);
        }

This code generates an errno == EEXIST in one thread, while another
thread generates errno == ENOENT, and this is how we test for errno
being thread-safe.  If you have a cleaner way to do this, please let me
know.  mkdir()?

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/src/test/thread/thread_test.c b/src/test/thread/thread_test.c
new file mode 100644
index 6a81829..2271ba6
*** a/src/test/thread/thread_test.c
--- b/src/test/thread/thread_test.c
*************** typedef char bool;
*** 52,92 ****
  #include <sys/param.h>
  #endif

- /******************************************************************
-  * Windows Hacks
-  *****************************************************************/
-
  #ifdef WIN32
  #define MAXHOSTNAMELEN 63
  #include <winsock2.h>
-
- int            mkstemp(char *template);
-
- int
- mkstemp(char *template)
- {
-     FILE       *foo;
-
-     mktemp(template);
-     foo = fopen(template, "rw");
-     if (!foo)
-         return -1;
-     else
-         return (int) foo;
- }
  #endif

- /******************************************************************
-  * End Windows Hacks
-  *****************************************************************/
-

  /* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
  #include <signal.h>
  int            sigwait(const sigset_t *set, int *sig);


! #if !defined(ENABLE_THREAD_SAFETY) && !defined(IN_CONFIGURE) && !(defined(WIN32))
  int
  main(int argc, char *argv[])
  {
--- 52,69 ----
  #include <sys/param.h>
  #endif

  #ifdef WIN32
  #define MAXHOSTNAMELEN 63
  #include <winsock2.h>
  #endif


  /* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
  #include <signal.h>
  int            sigwait(const sigset_t *set, int *sig);


! #if !defined(ENABLE_THREAD_SAFETY) && !defined(IN_CONFIGURE) && !defined(WIN32)
  int
  main(int argc, char *argv[])
  {
*************** main(int argc, char *argv[])
*** 99,118 ****
  /* This must be down here because this is the code that uses threads. */
  #include <pthread.h>

  static void func_call_1(void);
  static void func_call_2(void);

- #ifdef WIN32
- #define        TEMP_FILENAME_1 "thread_test.1.XXXXXX"
- #define        TEMP_FILENAME_2 "thread_test.2.XXXXXX"
- #else
- #define        TEMP_FILENAME_1 "/tmp/thread_test.1.XXXXXX"
- #define        TEMP_FILENAME_2 "/tmp/thread_test.2.XXXXXX"
- #endif
-
- static char *temp_filename_1;
- static char *temp_filename_2;
-
  static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;

  static volatile int thread1_done = 0;
--- 76,87 ----
  /* This must be down here because this is the code that uses threads. */
  #include <pthread.h>

+ #define        TEMP_FILENAME_1 "thread_test.1"
+ #define        TEMP_FILENAME_2 "thread_test.2"
+
  static void func_call_1(void);
  static void func_call_2(void);

  static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;

  static volatile int thread1_done = 0;
*************** static char *strerror_p2;
*** 127,139 ****
  static bool strerror_threadsafe = false;
  #endif

! #ifndef WIN32
! #ifndef HAVE_GETPWUID_R
  static struct passwd *passwd_p1;
  static struct passwd *passwd_p2;
  static bool getpwuid_threadsafe = false;
  #endif
- #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
  static struct hostent *hostent_p1;
--- 96,106 ----
  static bool strerror_threadsafe = false;
  #endif

! #if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
  static struct passwd *passwd_p1;
  static struct passwd *passwd_p2;
  static bool getpwuid_threadsafe = false;
  #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
  static struct hostent *hostent_p1;
*************** static bool platform_is_threadsafe = tru
*** 147,157 ****
  int
  main(int argc, char *argv[])
  {
!     pthread_t    thread1,
!                 thread2;
!     int            fd;
      int            rc;
-
  #ifdef WIN32
      WSADATA        wsaData;
      int            err;
--- 114,121 ----
  int
  main(int argc, char *argv[])
  {
!     pthread_t    thread1, thread2;
      int            rc;
  #ifdef WIN32
      WSADATA        wsaData;
      int            err;
*************** main(int argc, char *argv[])
*** 178,194 ****
      }
  #endif

-     /* Make temp filenames, might not have strdup() */
-     temp_filename_1 = malloc(strlen(TEMP_FILENAME_1) + 1);
-     strcpy(temp_filename_1, TEMP_FILENAME_1);
-     fd = mkstemp(temp_filename_1);
-     close(fd);
-
-     temp_filename_2 = malloc(strlen(TEMP_FILENAME_2) + 1);
-     strcpy(temp_filename_2, TEMP_FILENAME_2);
-     fd = mkstemp(temp_filename_2);
-     close(fd);
-
  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
      if (gethostname(myhostname, MAXHOSTNAMELEN) != 0)
      {
--- 142,147 ----
*************** main(int argc, char *argv[])
*** 212,218 ****
      {
          /*
           * strerror() might not be thread-safe, and we already spawned thread
!          * 1 that uses it
           */
          fprintf(stderr, "Failed to create thread 2 **\nexiting\n");
          exit(1);
--- 165,171 ----
      {
          /*
           * strerror() might not be thread-safe, and we already spawned thread
!          * 1 that uses it, so avoid using it.
           */
          fprintf(stderr, "Failed to create thread 2 **\nexiting\n");
          exit(1);
*************** main(int argc, char *argv[])
*** 220,225 ****
--- 173,182 ----

      while (thread1_done == 0 || thread2_done == 0)
          sched_yield();            /* if this is a portability problem, remove it */
+
+     /* Test things while we have thread-local storage */
+
+     /* If we got here, we didn't exit() from a thread */
  #ifdef WIN32
      printf("Your GetLastError() is thread-safe.\n");
  #else
*************** main(int argc, char *argv[])
*** 231,253 ****
          strerror_threadsafe = true;
  #endif

! #ifndef WIN32
! #ifndef HAVE_GETPWUID_R
      if (passwd_p1 != passwd_p2)
          getpwuid_threadsafe = true;
  #endif
- #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
      if (hostent_p1 != hostent_p2)
          gethostbyname_threadsafe = true;
  #endif

      pthread_mutex_unlock(&init_mutex);    /* let children exit  */

      pthread_join(thread1, NULL);    /* clean up children */
      pthread_join(thread2, NULL);

  #ifdef HAVE_STRERROR_R
      printf("Your system has sterror_r();  it does not need strerror().\n");
  #else
--- 188,212 ----
          strerror_threadsafe = true;
  #endif

! #if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
      if (passwd_p1 != passwd_p2)
          getpwuid_threadsafe = true;
  #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
      if (hostent_p1 != hostent_p2)
          gethostbyname_threadsafe = true;
  #endif

+     /* close down threads */
+
      pthread_mutex_unlock(&init_mutex);    /* let children exit  */

      pthread_join(thread1, NULL);    /* clean up children */
      pthread_join(thread2, NULL);

+     /* report results */
+
  #ifdef HAVE_STRERROR_R
      printf("Your system has sterror_r();  it does not need strerror().\n");
  #else
*************** main(int argc, char *argv[])
*** 261,268 ****
      }
  #endif

! #ifndef WIN32
! #ifdef HAVE_GETPWUID_R
      printf("Your system has getpwuid_r();  it does not need getpwuid().\n");
  #else
      printf("Your system uses getpwuid() which is ");
--- 220,228 ----
      }
  #endif

! #ifdef WIN32
!     printf("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n");
! #elif defined(HAVE_GETPWUID_R)
      printf("Your system has getpwuid_r();  it does not need getpwuid().\n");
  #else
      printf("Your system uses getpwuid() which is ");
*************** main(int argc, char *argv[])
*** 274,288 ****
          platform_is_threadsafe = false;
      }
  #endif
- #else
-     printf("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n");
- #endif

  #ifdef HAVE_GETADDRINFO
      printf("Your system has getaddrinfo();  it does not need gethostbyname()\n"
             "  or gethostbyname_r().\n");
! #else
! #ifdef HAVE_GETHOSTBYNAME_R
      printf("Your system has gethostbyname_r();  it does not need gethostbyname().\n");
  #else
      printf("Your system uses gethostbyname which is ");
--- 234,244 ----
          platform_is_threadsafe = false;
      }
  #endif

  #ifdef HAVE_GETADDRINFO
      printf("Your system has getaddrinfo();  it does not need gethostbyname()\n"
             "  or gethostbyname_r().\n");
! #elif defined(HAVE_GETHOSTBYNAME_R)
      printf("Your system has gethostbyname_r();  it does not need gethostbyname().\n");
  #else
      printf("Your system uses gethostbyname which is ");
*************** main(int argc, char *argv[])
*** 294,300 ****
          platform_is_threadsafe = false;
      }
  #endif
- #endif

      if (platform_is_threadsafe)
      {
--- 250,255 ----
*************** func_call_1(void)
*** 317,345 ****
      void       *p;
  #endif
  #ifdef WIN32
!     HANDLE        h1;
!     HANDLE        h2;
  #endif
-     unlink(temp_filename_1);


      /* create, then try to fail on exclusive create open */
  #ifdef WIN32
!     h1 = CreateFile(temp_filename_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
!     h2 = CreateFile(temp_filename_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
      if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
  #else
!     if (open(temp_filename_1, O_RDWR | O_CREAT, 0600) < 0 ||
!         open(temp_filename_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
  #endif
      {
- #ifdef WIN32
          fprintf(stderr, "Could not create file in current directory or\n");
!         fprintf(stderr, "Could not generate failure for create file in current directory **\nexiting\n");
! #else
!         fprintf(stderr, "Could not create file in /tmp or\n");
!         fprintf(stderr, "Could not generate failure for create file in /tmp **\nexiting\n");
! #endif
          exit(1);
      }

--- 272,294 ----
      void       *p;
  #endif
  #ifdef WIN32
!     HANDLE        h1, h2;
  #endif

+     unlink(TEMP_FILENAME_1);

      /* create, then try to fail on exclusive create open */
  #ifdef WIN32
!     h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
!     h2 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
      if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
  #else
!     if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0 ||
!         open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
  #endif
      {
          fprintf(stderr, "Could not create file in current directory or\n");
!         fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
          exit(1);
      }

*************** func_call_1(void)
*** 350,355 ****
--- 299,305 ----
      errno1_set = 1;
      while (errno2_set == 0)
          sched_yield();
+
  #ifdef WIN32
      if (GetLastError() != ERROR_FILE_EXISTS)
  #else
*************** func_call_1(void)
*** 361,382 ****
  #else
          fprintf(stderr, "errno not thread-safe **\nexiting\n");
  #endif
!         unlink(temp_filename_1);
          exit(1);
      }
-     unlink(temp_filename_1);

! #ifndef HAVE_STRERROR_R
!     strerror_p1 = strerror(EACCES);

      /*
       * If strerror() uses sys_errlist, the pointer might change for different
       * errno values, so we don't check to see if it varies within the thread.
       */
  #endif

! #ifndef WIN32
! #ifndef HAVE_GETPWUID_R
      passwd_p1 = getpwuid(0);
      p = getpwuid(1);
      if (passwd_p1 != p)
--- 311,331 ----
  #else
          fprintf(stderr, "errno not thread-safe **\nexiting\n");
  #endif
!         unlink(TEMP_FILENAME_1);
          exit(1);
      }

!     unlink(TEMP_FILENAME_1);

+ #ifndef HAVE_STRERROR_R
      /*
       * If strerror() uses sys_errlist, the pointer might change for different
       * errno values, so we don't check to see if it varies within the thread.
       */
+     strerror_p1 = strerror(EACCES);
  #endif

! #if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
      passwd_p1 = getpwuid(0);
      p = getpwuid(1);
      if (passwd_p1 != p)
*************** func_call_1(void)
*** 385,391 ****
          passwd_p1 = NULL;        /* force thread-safe failure report */
      }
  #endif
- #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
      /* threads do this in opposite order */
--- 334,339 ----
*************** func_call_2(void)
*** 413,425 ****
      void       *p;
  #endif

!     unlink(temp_filename_2);
      /* open non-existant file */
  #ifdef WIN32
!     CreateFile(temp_filename_2, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
      if (GetLastError() != ERROR_FILE_NOT_FOUND)
  #else
!     if (open(temp_filename_2, O_RDONLY, 0600) >= 0)
  #endif
      {
          fprintf(stderr, "Read-only open succeeded without create **\nexiting\n");
--- 361,374 ----
      void       *p;
  #endif

!     unlink(TEMP_FILENAME_2);
!
      /* open non-existant file */
  #ifdef WIN32
!     CreateFile(TEMP_FILENAME_2, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
      if (GetLastError() != ERROR_FILE_NOT_FOUND)
  #else
!     if (open(TEMP_FILENAME_2, O_RDONLY, 0600) >= 0)
  #endif
      {
          fprintf(stderr, "Read-only open succeeded without create **\nexiting\n");
*************** func_call_2(void)
*** 433,438 ****
--- 382,388 ----
      errno2_set = 1;
      while (errno1_set == 0)
          sched_yield();
+
  #ifdef WIN32
      if (GetLastError() != ENOENT)
  #else
*************** func_call_2(void)
*** 444,465 ****
  #else
          fprintf(stderr, "errno not thread-safe **\nexiting\n");
  #endif
!         unlink(temp_filename_2);
          exit(1);
      }
-     unlink(temp_filename_2);

! #ifndef HAVE_STRERROR_R
!     strerror_p2 = strerror(EINVAL);

      /*
       * If strerror() uses sys_errlist, the pointer might change for different
       * errno values, so we don't check to see if it varies within the thread.
       */
  #endif

! #ifndef WIN32
! #ifndef HAVE_GETPWUID_R
      passwd_p2 = getpwuid(2);
      p = getpwuid(3);
      if (passwd_p2 != p)
--- 394,414 ----
  #else
          fprintf(stderr, "errno not thread-safe **\nexiting\n");
  #endif
!         unlink(TEMP_FILENAME_2);
          exit(1);
      }

!     unlink(TEMP_FILENAME_2);

+ #ifndef HAVE_STRERROR_R
      /*
       * If strerror() uses sys_errlist, the pointer might change for different
       * errno values, so we don't check to see if it varies within the thread.
       */
+     strerror_p2 = strerror(EINVAL);
  #endif

! #if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
      passwd_p2 = getpwuid(2);
      p = getpwuid(3);
      if (passwd_p2 != p)
*************** func_call_2(void)
*** 468,474 ****
          passwd_p2 = NULL;        /* force thread-safe failure report */
      }
  #endif
- #endif

  #if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
      /* threads do this in opposite order */
--- 417,422 ----

Bruce Momjian <bruce@momjian.us> writes:
> Tom Lane wrote:
>> (The error message seems to be suffering from a bad case of copy-and-
>> paste-itis, too.)

> Actually, it is accurate.  The code is:

>     #ifdef WIN32
>         h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
>         h2 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
>         if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
>     #else
>         if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0 ||
>             open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
>     #endif
>         {
>             fprintf(stderr, "Could not create file in current directory or\n");
>             fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
>             exit(1);
>         }

> This code generates an errno == EEXIST in one thread, while another
> thread generates errno == ENOENT, and this is how we test for errno
> being thread-safe.  If you have a cleaner way to do this, please let me
> know.  mkdir()?

The problem with that is you're trying to make one error message serve
for two extremely different failure conditions.  I think this should be
coded more like

        if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0)
        {
        report suitable failure message;
        exit(1);
        }
        if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
        {
        report suitable failure message;
        exit(1);
        }

You would probably find that the messages could be a lot more clear and
specific if they were done like that.  Also, a file-related error
message that doesn't provide the filename nor strerror(errno) is pretty
much wrong on its face, in my book.

            regards, tom lane
Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> > Tom Lane wrote:
> >> (The error message seems to be suffering from a bad case of copy-and-
> >> paste-itis, too.)
>
> > Actually, it is accurate.  The code is:
>
> >     #ifdef WIN32
> >         h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
> >         h2 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
> >         if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
> >     #else
> >         if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0 ||
> >             open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
> >     #endif
> >         {
> >             fprintf(stderr, "Could not create file in current directory or\n");
> >             fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
> >             exit(1);
> >         }
>
> > This code generates an errno == EEXIST in one thread, while another
> > thread generates errno == ENOENT, and this is how we test for errno
> > being thread-safe.  If you have a cleaner way to do this, please let me
> > know.  mkdir()?
>
> The problem with that is you're trying to make one error message serve
> for two extremely different failure conditions.  I think this should be
> coded more like
>
>         if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0)
>         {
>         report suitable failure message;
>         exit(1);
>         }
>         if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
>         {
>         report suitable failure message;
>         exit(1);
>         }
>
> You would probably find that the messages could be a lot more clear and
> specific if they were done like that.  Also, a file-related error
> message that doesn't provide the filename nor strerror(errno) is pretty
> much wrong on its face, in my book.

OK, I split apart the two operations with the attached, applied patch.
There must be an easier way to generate two unique errno values in a
platform-indepentent way, but I can't find it.  I did simplify the
second ENOENT generation by just doing unlink twice.

I can't just set errno to a different value, can I?

I am not able to use strerror because I don't know if that is
thread-safe yet.  I do have a C comment about it:

        /*
         * strerror() might not be thread-safe, and we already spawned thread
         * 1 that uses it, so avoid using it.
         */

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/src/test/thread/thread_test.c b/src/test/thread/thread_test.c
new file mode 100644
index 2271ba6..6fef840
*** a/src/test/thread/thread_test.c
--- b/src/test/thread/thread_test.c
*************** main(int argc, char *argv[])
*** 263,268 ****
--- 263,271 ----
      }
  }

+ /*
+  * func_call_1
+  */
  static void
  func_call_1(void)
  {
*************** func_call_1(void)
*** 272,294 ****
      void       *p;
  #endif
  #ifdef WIN32
!     HANDLE        h1, h2;
  #endif

      unlink(TEMP_FILENAME_1);

      /* create, then try to fail on exclusive create open */
  #ifdef WIN32
!     h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
!     h2 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
!     if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
  #else
!     if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600) < 0 ||
!         open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
  #endif
      {
!         fprintf(stderr, "Could not create file in current directory or\n");
!         fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
          exit(1);
      }

--- 275,312 ----
      void       *p;
  #endif
  #ifdef WIN32
!     HANDLE        h1;
! #else
!     int fd;
  #endif

      unlink(TEMP_FILENAME_1);

+     /* Set errno = EEXIST */
+
      /* create, then try to fail on exclusive create open */
  #ifdef WIN32
!     if ((h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL)) ==
!         INVALID_HANDLE_VALUE)
  #else
!     if ((fd = open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600)) < 0)
  #endif
      {
!         fprintf(stderr, "Could not create file %s in current directory\n",
!                 TEMP_FILENAME_1);
!         exit(1);
!     }
!
! #ifdef WIN32
!     if (CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL)
!         != INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
! #else
!     if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
! #endif
!     {
!         fprintf(stderr,
!                 "Could not generate failure for exclusive file create of %s in current directory **\nexiting\n",
!                 TEMP_FILENAME_1);
          exit(1);
      }

*************** func_call_1(void)
*** 315,320 ****
--- 333,343 ----
          exit(1);
      }

+ #ifdef WIN32
+     CloseHandle(h1);
+ #else
+     close(fd);
+ #endif
      unlink(TEMP_FILENAME_1);

  #ifndef HAVE_STRERROR_R
*************** func_call_1(void)
*** 352,357 ****
--- 375,383 ----
  }


+ /*
+  * func_call_2
+  */
  static void
  func_call_2(void)
  {
*************** func_call_2(void)
*** 363,377 ****

      unlink(TEMP_FILENAME_2);

!     /* open non-existant file */
! #ifdef WIN32
!     CreateFile(TEMP_FILENAME_2, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
!     if (GetLastError() != ERROR_FILE_NOT_FOUND)
! #else
!     if (open(TEMP_FILENAME_2, O_RDONLY, 0600) >= 0)
! #endif
      {
!         fprintf(stderr, "Read-only open succeeded without create **\nexiting\n");
          exit(1);
      }

--- 389,402 ----

      unlink(TEMP_FILENAME_2);

!     /* Set errno = ENOENT */
!
!     /* This will fail, but we can't check errno yet */
!     if (unlink(TEMP_FILENAME_2) != -1)
      {
!         fprintf(stderr,
!                 "Could not generate failure for unlink of %s in current directory **\nexiting\n",
!                 TEMP_FILENAME_2);
          exit(1);
      }

*************** func_call_2(void)
*** 394,405 ****
  #else
          fprintf(stderr, "errno not thread-safe **\nexiting\n");
  #endif
-         unlink(TEMP_FILENAME_2);
          exit(1);
      }

-     unlink(TEMP_FILENAME_2);
-
  #ifndef HAVE_STRERROR_R
      /*
       * If strerror() uses sys_errlist, the pointer might change for different
--- 419,427 ----