1
/*-------------------------------------------------------------------------
5
* Prototypes and macros around system calls, used to help make
6
* threaded libraries reentrant and safe to use from threaded applications.
8
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
10
* $PostgreSQL: pgsql/src/port/thread.c,v 1.29 2004-12-31 22:03:53 pgsql Exp $
12
*-------------------------------------------------------------------------
17
#include <sys/types.h>
19
#ifdef WIN32_CLIENT_ONLY
24
#if defined(ENABLE_THREAD_SAFETY)
29
* Threading sometimes requires specially-named versions of functions
30
* that return data in static buffers, like strerror_r() instead of
31
* strerror(). Other operating systems use pthread_setspecific()
32
* and pthread_getspecific() internally to allow standard library
33
* functions to return static data to threaded applications. And some
34
* operating systems have neither.
36
* Additional confusion exists because many operating systems that
37
* use pthread_setspecific/pthread_getspecific() also have *_r versions
38
* of standard library functions for compatibility with operating systems
39
* that require them. However, internally, these *_r functions merely
40
* call the thread-safe standard library functions.
42
* For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
43
* getpwuid() calls pthread_setspecific/pthread_getspecific() to return
44
* static data to the caller in a thread-safe manner. However, BSD/OS
45
* also has getpwuid_r(), which merely calls getpwuid() and shifts
46
* around the arguments to match the getpwuid_r() function declaration.
47
* Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
48
* doesn't have strerror_r(), so we can't fall back to only using *_r
49
* functions for threaded programs.
51
* The current setup is to try threading in this order:
53
* use *_r function names if they exit
55
* use non-*_r functions if they are thread-safe
57
* One thread-safe solution for gethostbyname() might be to use getaddrinfo().
59
* Run src/tools/thread to see if your operating system has thread-safe
65
* Wrapper around strerror and strerror_r to use the former if it is
66
* available and also return a more useful value (the error string).
69
pqStrerror(int errnum, char *strerrbuf, size_t buflen)
71
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R)
72
/* reentrant strerror_r is available */
75
if (strerror_r(errnum, strerrbuf, buflen) == 0)
78
return "Unknown error";
81
return strerror_r(errnum, strerrbuf, buflen);
84
/* no strerror_r() available, just use strerror */
85
StrNCpy(strerrbuf, strerror(errnum), buflen);
92
* Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
93
* behaviour, if it is not available or required.
97
pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
98
size_t buflen, struct passwd ** result)
100
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
102
#ifdef GETPWUID_R_5ARG
104
getpwuid_r(uid, resultbuf, buffer, buflen, result);
108
* Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
109
* getpwuid_r(uid, resultbuf, buffer, buflen)
111
*result = getpwuid_r(uid, resultbuf, buffer, buflen);
115
/* no getpwuid_r() available, just use getpwuid() */
116
*result = getpwuid(uid);
119
return (*result == NULL) ? -1 : 0;
124
* Wrapper around gethostbyname() or gethostbyname_r() to mimic
125
* POSIX gethostbyname_r() behaviour, if it is not available or required.
126
* This function is called _only_ by our getaddinfo() portability function.
128
#ifndef HAVE_GETADDRINFO
130
pqGethostbyname(const char *name,
131
struct hostent * resultbuf,
132
char *buffer, size_t buflen,
133
struct hostent ** result,
136
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
139
* broken (well early POSIX draft) gethostbyname_r() which returns
142
*result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
143
return (*result == NULL) ? -1 : 0;
147
/* no gethostbyname_r(), just use gethostbyname() */
148
*result = gethostbyname(name);