2003-09-04 03:30:31 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2019-08-19 15:21:39 +08:00
|
|
|
* thread_test.c
|
2020-10-22 00:08:48 +08:00
|
|
|
* libc threading test program
|
2003-09-04 03:30:31 +08:00
|
|
|
*
|
2020-01-02 01:21:45 +08:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2003-09-04 03:30:31 +08:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2020-10-22 00:08:48 +08:00
|
|
|
* config/thread_test.c
|
2003-09-04 03:30:31 +08:00
|
|
|
*
|
|
|
|
* This program tests to see if your standard libc functions use
|
|
|
|
* pthread_setspecific()/pthread_getspecific() to be thread-safe.
|
|
|
|
* See src/port/thread.c for more details.
|
|
|
|
*
|
|
|
|
* This program first tests to see if each function returns a constant
|
|
|
|
* memory pointer within the same thread, then, assuming it does, tests
|
|
|
|
* to see if the pointers are different for different threads. If they
|
|
|
|
* are, the function is thread-safe.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-10-22 00:08:48 +08:00
|
|
|
/* We cannot use c.h, as port.h will not exist yet */
|
2004-04-24 06:21:49 +08:00
|
|
|
|
2004-04-28 01:22:41 +08:00
|
|
|
#include <stdio.h>
|
2003-09-04 03:30:31 +08:00
|
|
|
#include <stdlib.h>
|
2004-04-24 06:21:49 +08:00
|
|
|
#include <unistd.h>
|
2003-09-04 03:30:31 +08:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
2004-03-28 07:02:44 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2020-10-22 00:08:48 +08:00
|
|
|
#include <pthread.h>
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2008-11-19 05:17:26 +08:00
|
|
|
/* CYGWIN requires this for MAXHOSTNAMELEN */
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2005-08-24 05:02:05 +08:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define MAXHOSTNAMELEN 63
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2004-12-19 12:06:23 +08:00
|
|
|
/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
|
|
|
|
#include <signal.h>
|
2005-10-15 10:49:52 +08:00
|
|
|
int sigwait(const sigset_t *set, int *sig);
|
2004-12-19 12:06:23 +08:00
|
|
|
|
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#define TEMP_FILENAME_1 "thread_test.1"
|
|
|
|
#define TEMP_FILENAME_2 "thread_test.2"
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
static void func_call_1(void);
|
|
|
|
static void func_call_2(void);
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2005-03-11 23:25:31 +08:00
|
|
|
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2004-03-28 07:02:44 +08:00
|
|
|
|
2005-03-11 23:25:31 +08:00
|
|
|
static volatile int thread1_done = 0;
|
|
|
|
static volatile int thread2_done = 0;
|
2004-04-05 13:43:06 +08:00
|
|
|
|
2005-03-11 23:25:31 +08:00
|
|
|
static volatile int errno1_set = 0;
|
|
|
|
static volatile int errno2_set = 0;
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#ifndef HAVE_STRERROR_R
|
2005-10-15 10:49:52 +08:00
|
|
|
static char *strerror_p1;
|
|
|
|
static char *strerror_p2;
|
2017-11-29 22:24:24 +08:00
|
|
|
static int strerror_threadsafe = 0;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
|
2005-03-11 23:25:31 +08:00
|
|
|
static struct passwd *passwd_p1;
|
|
|
|
static struct passwd *passwd_p2;
|
2017-11-29 22:24:24 +08:00
|
|
|
static int getpwuid_threadsafe = 0;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
|
2005-03-11 23:25:31 +08:00
|
|
|
static struct hostent *hostent_p1;
|
|
|
|
static struct hostent *hostent_p2;
|
2005-10-15 10:49:52 +08:00
|
|
|
static char myhostname[MAXHOSTNAMELEN];
|
2017-11-29 22:24:24 +08:00
|
|
|
static int gethostbyname_threadsafe = 0;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2017-11-29 22:24:24 +08:00
|
|
|
static int platform_is_threadsafe = 1;
|
2004-04-05 13:43:06 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2003-09-04 03:30:31 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
pthread_t thread1,
|
|
|
|
thread2;
|
2011-05-27 16:51:23 +08:00
|
|
|
int rc;
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
WSADATA wsaData;
|
2005-10-15 10:49:52 +08:00
|
|
|
int err;
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2003-09-04 03:30:31 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
2004-04-24 04:35:50 +08:00
|
|
|
fprintf(stderr, "Usage: %s\n", argv[0]);
|
|
|
|
return 1;
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
|
|
|
|
2004-04-28 03:51:12 +08:00
|
|
|
/* Send stdout to 'config.log' */
|
|
|
|
close(1);
|
|
|
|
dup(5);
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2020-10-19 00:56:43 +08:00
|
|
|
err = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
2005-10-15 10:49:52 +08:00
|
|
|
if (err != 0)
|
|
|
|
{
|
2005-08-24 05:02:05 +08:00
|
|
|
fprintf(stderr, "Cannot start the network subsystem - %d**\nexiting\n", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
|
2004-04-05 09:27:58 +08:00
|
|
|
if (gethostname(myhostname, MAXHOSTNAMELEN) != 0)
|
|
|
|
{
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-02 03:10:30 +08:00
|
|
|
fprintf(stderr, "Cannot get local hostname **\nexiting\n");
|
2004-04-24 04:35:50 +08:00
|
|
|
exit(1);
|
2004-04-05 09:27:58 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
/* Hold lock until we are ready for the child threads to exit. */
|
2004-04-24 04:35:50 +08:00
|
|
|
pthread_mutex_lock(&init_mutex);
|
|
|
|
|
2011-05-27 16:51:23 +08:00
|
|
|
rc = pthread_create(&thread1, NULL, (void *(*) (void *)) func_call_1, NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create thread 1: %s **\nexiting\n",
|
2011-08-22 21:04:48 +08:00
|
|
|
strerror(errno));
|
2011-05-27 16:51:23 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
rc = pthread_create(&thread2, NULL, (void *(*) (void *)) func_call_2, NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* strerror() might not be thread-safe, and we already spawned thread
|
2011-08-21 06:21:53 +08:00
|
|
|
* 1 that uses it, so avoid using it.
|
2011-05-27 16:51:23 +08:00
|
|
|
*/
|
|
|
|
fprintf(stderr, "Failed to create thread 2 **\nexiting\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
while (thread1_done == 0 || thread2_done == 0)
|
2005-10-15 10:49:52 +08:00
|
|
|
sched_yield(); /* if this is a portability problem, remove it */
|
2011-08-21 06:21:53 +08:00
|
|
|
|
|
|
|
/* Test things while we have thread-local storage */
|
|
|
|
|
|
|
|
/* If we got here, we didn't exit() from a thread */
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
printf("Your GetLastError() is thread-safe.\n");
|
|
|
|
#else
|
2004-04-24 07:58:12 +08:00
|
|
|
printf("Your errno is thread-safe.\n");
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-04-24 04:35:50 +08:00
|
|
|
|
|
|
|
#ifndef HAVE_STRERROR_R
|
2004-02-12 05:44:06 +08:00
|
|
|
if (strerror_p1 != strerror_p2)
|
2017-08-16 12:22:32 +08:00
|
|
|
strerror_threadsafe = 1;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2004-02-12 05:44:06 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
|
2004-02-12 05:44:06 +08:00
|
|
|
if (passwd_p1 != passwd_p2)
|
2017-08-16 12:22:32 +08:00
|
|
|
getpwuid_threadsafe = 1;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p1 != hostent_p2)
|
2017-08-16 12:22:32 +08:00
|
|
|
gethostbyname_threadsafe = 1;
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2004-04-05 13:43:06 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
/* close down threads */
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
pthread_mutex_unlock(&init_mutex); /* let children exit */
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
pthread_join(thread1, NULL); /* clean up children */
|
|
|
|
pthread_join(thread2, NULL);
|
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
/* report results */
|
|
|
|
|
2004-04-22 04:51:54 +08:00
|
|
|
#ifdef HAVE_STRERROR_R
|
2018-09-27 00:35:57 +08:00
|
|
|
printf("Your system has strerror_r(); it does not need strerror().\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses strerror() which is ");
|
|
|
|
if (strerror_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
2004-04-26 08:36:18 +08:00
|
|
|
printf("not thread-safe. **\n");
|
2017-08-16 12:22:32 +08:00
|
|
|
platform_is_threadsafe = 0;
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
printf("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n");
|
|
|
|
#elif defined(HAVE_GETPWUID_R)
|
2004-04-24 04:35:50 +08:00
|
|
|
printf("Your system has getpwuid_r(); it does not need getpwuid().\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses getpwuid() which is ");
|
|
|
|
if (getpwuid_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
2004-04-26 08:36:18 +08:00
|
|
|
printf("not thread-safe. **\n");
|
2017-08-16 12:22:32 +08:00
|
|
|
platform_is_threadsafe = 0;
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
2004-04-24 04:35:50 +08:00
|
|
|
printf("Your system has getaddrinfo(); it does not need gethostbyname()\n"
|
|
|
|
" or gethostbyname_r().\n");
|
2011-08-21 06:21:53 +08:00
|
|
|
#elif defined(HAVE_GETHOSTBYNAME_R)
|
2004-04-24 04:35:50 +08:00
|
|
|
printf("Your system has gethostbyname_r(); it does not need gethostbyname().\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses gethostbyname which is ");
|
|
|
|
if (gethostbyname_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
2004-04-26 08:36:18 +08:00
|
|
|
printf("not thread-safe. **\n");
|
2017-08-16 12:22:32 +08:00
|
|
|
platform_is_threadsafe = 0;
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
if (platform_is_threadsafe)
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
2004-04-24 04:35:50 +08:00
|
|
|
printf("\nYour platform is thread-safe.\n");
|
|
|
|
return 0;
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-04-24 04:35:50 +08:00
|
|
|
printf("\n** YOUR PLATFORM IS NOT THREAD-SAFE. **\n");
|
|
|
|
return 1;
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
/*
|
|
|
|
* func_call_1
|
|
|
|
*/
|
2005-03-11 23:25:31 +08:00
|
|
|
static void
|
2004-04-24 04:35:50 +08:00
|
|
|
func_call_1(void)
|
|
|
|
{
|
|
|
|
#if !defined(HAVE_GETPWUID_R) || \
|
|
|
|
(!defined(HAVE_GETADDRINFO) && \
|
|
|
|
!defined(HAVE_GETHOSTBYNAME_R))
|
|
|
|
void *p;
|
|
|
|
#endif
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2011-08-22 09:24:03 +08:00
|
|
|
HANDLE h1;
|
|
|
|
#else
|
2012-06-11 03:20:04 +08:00
|
|
|
int fd;
|
2005-10-15 10:49:52 +08:00
|
|
|
#endif
|
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
unlink(TEMP_FILENAME_1);
|
2005-10-15 10:49:52 +08:00
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
/* Set errno = EEXIST */
|
|
|
|
|
2004-03-28 10:37:31 +08:00
|
|
|
/* create, then try to fail on exclusive create open */
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-08-22 21:04:48 +08:00
|
|
|
/*
|
|
|
|
* It would be great to check errno here but if errno is not thread-safe
|
2012-06-11 03:20:04 +08:00
|
|
|
* we might get a value from the other thread and mis-report the cause of
|
|
|
|
* the failure.
|
2011-08-22 21:04:48 +08:00
|
|
|
*/
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2011-08-22 09:24:03 +08:00
|
|
|
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);
|
|
|
|
}
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
if (CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL)
|
2011-08-22 21:04:48 +08:00
|
|
|
!= INVALID_HANDLE_VALUE)
|
2005-08-24 05:02:05 +08:00
|
|
|
#else
|
2011-08-22 09:24:03 +08:00
|
|
|
if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-03-28 07:02:44 +08:00
|
|
|
{
|
2011-08-22 09:24:03 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not generate failure for exclusive file create of %s in current directory **\nexiting\n",
|
|
|
|
TEMP_FILENAME_1);
|
2004-04-24 04:35:50 +08:00
|
|
|
exit(1);
|
2004-03-28 07:02:44 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
/*
|
2004-04-24 04:35:50 +08:00
|
|
|
* Wait for other thread to set errno. We can't use thread-specific
|
|
|
|
* locking here because it might affect errno.
|
2004-04-05 13:43:06 +08:00
|
|
|
*/
|
2004-03-28 07:02:44 +08:00
|
|
|
errno1_set = 1;
|
|
|
|
while (errno2_set == 0)
|
2004-04-05 22:13:45 +08:00
|
|
|
sched_yield();
|
2011-08-21 06:21:53 +08:00
|
|
|
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2005-10-15 10:49:52 +08:00
|
|
|
if (GetLastError() != ERROR_FILE_EXISTS)
|
2005-08-24 05:02:05 +08:00
|
|
|
#else
|
2005-10-15 10:49:52 +08:00
|
|
|
if (errno != EEXIST)
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-03-28 07:02:44 +08:00
|
|
|
{
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2005-10-15 10:49:52 +08:00
|
|
|
fprintf(stderr, "GetLastError() not thread-safe **\nexiting\n");
|
2005-08-24 05:02:05 +08:00
|
|
|
#else
|
2005-10-15 10:49:52 +08:00
|
|
|
fprintf(stderr, "errno not thread-safe **\nexiting\n");
|
|
|
|
#endif
|
2011-08-21 06:21:53 +08:00
|
|
|
unlink(TEMP_FILENAME_1);
|
2004-04-24 04:35:50 +08:00
|
|
|
exit(1);
|
2004-03-28 07:02:44 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
CloseHandle(h1);
|
|
|
|
#else
|
|
|
|
close(fd);
|
|
|
|
#endif
|
2011-08-21 06:21:53 +08:00
|
|
|
unlink(TEMP_FILENAME_1);
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#ifndef HAVE_STRERROR_R
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* 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.
|
2004-02-12 05:44:06 +08:00
|
|
|
*/
|
2011-08-21 06:21:53 +08:00
|
|
|
strerror_p1 = strerror(EACCES);
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
|
2003-09-04 03:30:31 +08:00
|
|
|
passwd_p1 = getpwuid(0);
|
|
|
|
p = getpwuid(1);
|
|
|
|
if (passwd_p1 != p)
|
|
|
|
{
|
|
|
|
printf("Your getpwuid() changes the static memory area between calls\n");
|
2004-04-24 04:35:50 +08:00
|
|
|
passwd_p1 = NULL; /* force thread-safe failure report */
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
|
2004-04-05 09:27:58 +08:00
|
|
|
/* threads do this in opposite order */
|
|
|
|
hostent_p1 = gethostbyname(myhostname);
|
|
|
|
p = gethostbyname("localhost");
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p1 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
2004-04-24 04:35:50 +08:00
|
|
|
hostent_p1 = NULL; /* force thread-safe failure report */
|
2004-02-12 05:44:06 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
thread1_done = 1;
|
|
|
|
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
|
|
|
|
pthread_mutex_unlock(&init_mutex);
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
/*
|
|
|
|
* func_call_2
|
|
|
|
*/
|
2005-03-11 23:25:31 +08:00
|
|
|
static void
|
2004-04-24 04:35:50 +08:00
|
|
|
func_call_2(void)
|
|
|
|
{
|
|
|
|
#if !defined(HAVE_GETPWUID_R) || \
|
|
|
|
(!defined(HAVE_GETADDRINFO) && \
|
|
|
|
!defined(HAVE_GETHOSTBYNAME_R))
|
|
|
|
void *p;
|
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
unlink(TEMP_FILENAME_2);
|
|
|
|
|
2011-08-22 09:24:03 +08:00
|
|
|
/* Set errno = ENOENT */
|
|
|
|
|
|
|
|
/* This will fail, but we can't check errno yet */
|
|
|
|
if (unlink(TEMP_FILENAME_2) != -1)
|
2004-03-28 07:02:44 +08:00
|
|
|
{
|
2011-08-22 09:24:03 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not generate failure for unlink of %s in current directory **\nexiting\n",
|
|
|
|
TEMP_FILENAME_2);
|
2004-04-24 04:35:50 +08:00
|
|
|
exit(1);
|
2004-03-28 07:02:44 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
/*
|
2004-04-24 04:35:50 +08:00
|
|
|
* Wait for other thread to set errno. We can't use thread-specific
|
|
|
|
* locking here because it might affect errno.
|
2004-04-05 13:43:06 +08:00
|
|
|
*/
|
2004-03-28 07:02:44 +08:00
|
|
|
errno2_set = 1;
|
|
|
|
while (errno1_set == 0)
|
2004-04-05 22:13:45 +08:00
|
|
|
sched_yield();
|
2011-08-21 06:21:53 +08:00
|
|
|
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2005-10-15 10:49:52 +08:00
|
|
|
if (GetLastError() != ENOENT)
|
2005-08-24 05:02:05 +08:00
|
|
|
#else
|
2005-10-15 10:49:52 +08:00
|
|
|
if (errno != ENOENT)
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-03-28 07:02:44 +08:00
|
|
|
{
|
2005-08-24 05:02:05 +08:00
|
|
|
#ifdef WIN32
|
2005-10-15 10:49:52 +08:00
|
|
|
fprintf(stderr, "GetLastError() not thread-safe **\nexiting\n");
|
2005-08-24 05:02:05 +08:00
|
|
|
#else
|
2005-10-15 10:49:52 +08:00
|
|
|
fprintf(stderr, "errno not thread-safe **\nexiting\n");
|
2005-08-24 05:02:05 +08:00
|
|
|
#endif
|
2004-04-24 04:35:50 +08:00
|
|
|
exit(1);
|
2004-03-28 07:02:44 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#ifndef HAVE_STRERROR_R
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* 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.
|
2004-02-12 05:44:06 +08:00
|
|
|
*/
|
2011-08-21 06:21:53 +08:00
|
|
|
strerror_p2 = strerror(EINVAL);
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2011-08-21 06:21:53 +08:00
|
|
|
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
|
2003-09-04 03:30:31 +08:00
|
|
|
passwd_p2 = getpwuid(2);
|
|
|
|
p = getpwuid(3);
|
|
|
|
if (passwd_p2 != p)
|
|
|
|
{
|
|
|
|
printf("Your getpwuid() changes the static memory area between calls\n");
|
2004-04-24 04:35:50 +08:00
|
|
|
passwd_p2 = NULL; /* force thread-safe failure report */
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-24 04:35:50 +08:00
|
|
|
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
|
2004-04-05 09:27:58 +08:00
|
|
|
/* threads do this in opposite order */
|
|
|
|
hostent_p2 = gethostbyname("localhost");
|
|
|
|
p = gethostbyname(myhostname);
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p2 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
2004-04-24 04:35:50 +08:00
|
|
|
hostent_p2 = NULL; /* force thread-safe failure report */
|
2004-02-12 05:44:06 +08:00
|
|
|
}
|
2004-04-24 04:35:50 +08:00
|
|
|
#endif
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
thread2_done = 1;
|
|
|
|
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
|
|
|
|
pthread_mutex_unlock(&init_mutex);
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|