thread.c 4.51 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
Bruce Momjian's avatar
Bruce Momjian committed
3
 * thread.c
4 5 6 7
 *
 *		  Prototypes and macros around system calls, used to help make
 *		  threaded libraries reentrant and safe to use from threaded applications.
 *
8
 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
9
 *
10
 * $PostgreSQL: pgsql/src/port/thread.c,v 1.35 2006/09/27 18:40:10 tgl Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */

15
#include "c.h"
16

17
#include <pwd.h>
18
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY)
Bruce Momjian's avatar
Bruce Momjian committed
19 20 21
#ifdef WIN32
#include "pthread-win32.h"
#else
22
#include <pthread.h>
23
#endif
Bruce Momjian's avatar
Bruce Momjian committed
24
#endif
25

26

Bruce Momjian's avatar
Bruce Momjian committed
27 28 29 30 31
/*
 *	Threading sometimes requires specially-named versions of functions
 *	that return data in static buffers, like strerror_r() instead of
 *	strerror().  Other operating systems use pthread_setspecific()
 *	and pthread_getspecific() internally to allow standard library
32
 *	functions to return static data to threaded applications. And some
33
 *	operating systems have neither.
Bruce Momjian's avatar
Bruce Momjian committed
34 35 36 37
 *
 *	Additional confusion exists because many operating systems that
 *	use pthread_setspecific/pthread_getspecific() also have *_r versions
 *	of standard library functions for compatibility with operating systems
Bruce Momjian's avatar
Bruce Momjian committed
38
 *	that require them.	However, internally, these *_r functions merely
Bruce Momjian's avatar
Bruce Momjian committed
39 40 41 42
 *	call the thread-safe standard library functions.
 *
 *	For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid().  Internally,
 *	getpwuid() calls pthread_setspecific/pthread_getspecific() to return
Bruce Momjian's avatar
Bruce Momjian committed
43
 *	static data to the caller in a thread-safe manner.	However, BSD/OS
Bruce Momjian's avatar
Bruce Momjian committed
44 45 46 47 48 49
 *	also has getpwuid_r(), which merely calls getpwuid() and shifts
 *	around the arguments to match the getpwuid_r() function declaration.
 *	Therefore, while BSD/OS has getpwuid_r(), it isn't required.  It also
 *	doesn't have strerror_r(), so we can't fall back to only using *_r
 *	functions for threaded programs.
 *
50 51
 *	The current setup is to try threading in this order:
 *
52 53 54
 *		use *_r function names if they exit
 *			(*_THREADSAFE=ye)
 *		use non-*_r functions if they are thread-safe
55 56 57
 *
 *	One thread-safe solution for gethostbyname() might be to use getaddrinfo().
 *
58
 *	Run src/tools/thread to see if your operating system has thread-safe
59
 *	non-*_r functions.
Bruce Momjian's avatar
Bruce Momjian committed
60
 */
Bruce Momjian's avatar
Bruce Momjian committed
61

Bruce Momjian's avatar
Bruce Momjian committed
62

63 64 65 66 67 68 69
/*
 * Wrapper around strerror and strerror_r to use the former if it is
 * available and also return a more useful value (the error string).
 */
char *
pqStrerror(int errnum, char *strerrbuf, size_t buflen)
{
70
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R)
71
	/* reentrant strerror_r is available */
72 73 74 75 76
#ifdef STRERROR_R_INT
	/* SUSv3 version */
	if (strerror_r(errnum, strerrbuf, buflen) == 0)
		return strerrbuf;
	else
77
		return "Unknown error";
78 79 80 81
#else
	/* GNU libc */
	return strerror_r(errnum, strerrbuf, buflen);
#endif
82 83
#else
	/* no strerror_r() available, just use strerror */
84
	strlcpy(strerrbuf, strerror(errnum), buflen);
85 86

	return strerrbuf;
87 88 89 90 91
#endif
}

/*
 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
Bruce Momjian's avatar
Bruce Momjian committed
92
 * behaviour, if it is not available or required.
93
 */
94
#ifndef WIN32
95
int
Bruce Momjian's avatar
Bruce Momjian committed
96 97
pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
		   size_t buflen, struct passwd ** result)
98
{
99
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
100 101 102 103 104

#ifdef GETPWUID_R_5ARG
	/* POSIX version */
	getpwuid_r(uid, resultbuf, buffer, buflen, result);
#else
105

106
	/*
Bruce Momjian's avatar
Bruce Momjian committed
107
	 * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
Bruce Momjian's avatar
Bruce Momjian committed
108
	 * getpwuid_r(uid, resultbuf, buffer, buflen)
109
	 */
Bruce Momjian's avatar
Bruce Momjian committed
110
	*result = getpwuid_r(uid, resultbuf, buffer, buflen);
111
#endif
112
#else
113

114 115 116
	/* no getpwuid_r() available, just use getpwuid() */
	*result = getpwuid(uid);
#endif
117

118 119
	return (*result == NULL) ? -1 : 0;
}
120
#endif
121 122 123

/*
 * Wrapper around gethostbyname() or gethostbyname_r() to mimic
Bruce Momjian's avatar
Bruce Momjian committed
124
 * POSIX gethostbyname_r() behaviour, if it is not available or required.
125
 * This function is called _only_ by our getaddinfo() portability function.
126
 */
127
#ifndef HAVE_GETADDRINFO
128 129
int
pqGethostbyname(const char *name,
Bruce Momjian's avatar
Bruce Momjian committed
130
				struct hostent * resultbuf,
131
				char *buffer, size_t buflen,
Bruce Momjian's avatar
Bruce Momjian committed
132
				struct hostent ** result,
133 134
				int *herrno)
{
135
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
Bruce Momjian's avatar
Bruce Momjian committed
136

137
	/*
138 139
	 * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
	 * hostent *'
140
	 */
141
	*result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
142 143
	return (*result == NULL) ? -1 : 0;
#else
144

145 146
	/* no gethostbyname_r(), just use gethostbyname() */
	*result = gethostbyname(name);
147 148 149

	if (*result != NULL)
		*herrno = h_errno;
Bruce Momjian's avatar
Bruce Momjian committed
150

151 152 153 154 155 156
	if (*result != NULL)
		return 0;
	else
		return -1;
#endif
}
Bruce Momjian's avatar
Bruce Momjian committed
157

158
#endif