77 *
88 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
99 *
10- * $Id: thread.c,v 1.2 2003/08/08 03:09:56 momjian Exp $
10+ * $Id: thread.c,v 1.3 2003/08/14 05:27:18 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1414
1515#include "postgres.h"
1616
17+ /*
18+ *Threading sometimes requires specially-named versions of functions
19+ *that return data in static buffers, like strerror_r() instead of
20+ *strerror(). Other operating systems use pthread_setspecific()
21+ *and pthread_getspecific() internally to allow standard library
22+ *functions to return static data to threaded applications.
23+ *
24+ *Additional confusion exists because many operating systems that
25+ *use pthread_setspecific/pthread_getspecific() also have *_r versions
26+ *of standard library functions for compatibility with operating systems
27+ *that require them. However, internally, these *_r functions merely
28+ *call the thread-safe standard library functions.
29+ *
30+ *For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
31+ *getpwuid() calls pthread_setspecific/pthread_getspecific() to return
32+ *static data to the caller in a thread-safe manner. However, BSD/OS
33+ *also has getpwuid_r(), which merely calls getpwuid() and shifts
34+ *around the arguments to match the getpwuid_r() function declaration.
35+ *Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
36+ *doesn't have strerror_r(), so we can't fall back to only using *_r
37+ *functions for threaded programs.
38+ *
39+ *The current setup is to assume either all standard functions are
40+ *thread-safe (NEED_REENTRANT_FUNC_NAMES=no), or the operating system
41+ *requires reentrant function names (NEED_REENTRANT_FUNC_NAMES=yes).
42+ */
43+
44+
1745/*
1846 * Wrapper around strerror and strerror_r to use the former if it is
1947 * available and also return a more useful value (the error string).
@@ -34,19 +62,20 @@ pqStrerror(int errnum, char *strerrbuf, size_t buflen)
3462
3563/*
3664 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
37- * behaviour, if it is not available.
65+ * behaviour, if it is not available or required .
3866 */
3967int
40- pqGetpwuid (uid_t uid ,struct passwd * resultbuf ,char * buffer ,
41- size_t buflen ,struct passwd * * result )
68+ pqGetpwuid (uid_t uid ,struct passwd * resultbuf ,char * buffer ,
69+ size_t buflen ,struct passwd * * result )
4270{
4371#if defined(USE_THREADS )&& defined(HAVE_GETPWUID_R )
44-
4572/*
46- * broken (well early POSIX draft) getpwuid_r() which returns 'struct
47- * passwd *'
73+ * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
74+ * getpwuid_r(uid, resultbuf, buffer, buflen)
75+ * Do we need to support it? bjm 2003-08-14
4876 */
49- * result = getpwuid_r (uid ,resultbuf ,buffer ,buflen );
77+ /* POSIX version */
78+ getpwuid_r (uid ,resultbuf ,buffer ,buflen ,result );
5079#else
5180/* no getpwuid_r() available, just use getpwuid() */
5281* result = getpwuid (uid );
@@ -56,13 +85,13 @@ pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
5685
5786/*
5887 * Wrapper around gethostbyname() or gethostbyname_r() to mimic
59- * POSIX gethostbyname_r() behaviour, if it is not available.
88+ * POSIX gethostbyname_r() behaviour, if it is not available or required .
6089 */
6190int
6291pqGethostbyname (const char * name ,
63- struct hostent * resbuf ,
92+ struct hostent * resbuf ,
6493char * buf ,size_t buflen ,
65- struct hostent * * result ,
94+ struct hostent * * result ,
6695int * herrno )
6796{
6897#if defined(USE_THREADS )&& defined(HAVE_GETHOSTBYNAME_R )