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-2008, PostgreSQL Global Development Group
12 *-------------------------------------------------------------------------
18 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY)
23 * Threading sometimes requires specially-named versions of functions
24 * that return data in static buffers, like strerror_r() instead of
25 * strerror(). Other operating systems use pthread_setspecific()
26 * and pthread_getspecific() internally to allow standard library
27 * functions to return static data to threaded applications. And some
28 * operating systems have neither.
30 * Additional confusion exists because many operating systems that
31 * use pthread_setspecific/pthread_getspecific() also have *_r versions
32 * of standard library functions for compatibility with operating systems
33 * that require them. However, internally, these *_r functions merely
34 * call the thread-safe standard library functions.
36 * For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
37 * getpwuid() calls pthread_setspecific/pthread_getspecific() to return
38 * static data to the caller in a thread-safe manner. However, BSD/OS
39 * also has getpwuid_r(), which merely calls getpwuid() and shifts
40 * around the arguments to match the getpwuid_r() function declaration.
41 * Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
42 * doesn't have strerror_r(), so we can't fall back to only using *_r
43 * functions for threaded programs.
45 * The current setup is to try threading in this order:
47 * use *_r function names if they exit
49 * use non-*_r functions if they are thread-safe
51 * One thread-safe solution for gethostbyname() might be to use getaddrinfo().
53 * Run src/test/thread to test if your operating system has thread-safe
59 * Wrapper around strerror and strerror_r to use the former if it is
60 * available and also return a more useful value (the error string).
63 pqStrerror(int errnum
, char *strerrbuf
, size_t buflen
)
65 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R)
66 /* reentrant strerror_r is available */
69 if (strerror_r(errnum
, strerrbuf
, buflen
) == 0)
72 return "Unknown error";
75 return strerror_r(errnum
, strerrbuf
, buflen
);
78 /* no strerror_r() available, just use strerror */
79 strlcpy(strerrbuf
, strerror(errnum
), buflen
);
86 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
87 * behaviour, if it is not available or required.
91 pqGetpwuid(uid_t uid
, struct passwd
* resultbuf
, char *buffer
,
92 size_t buflen
, struct passwd
** result
)
94 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
96 #ifdef GETPWUID_R_5ARG
98 getpwuid_r(uid
, resultbuf
, buffer
, buflen
, result
);
102 * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
103 * getpwuid_r(uid, resultbuf, buffer, buflen)
105 *result
= getpwuid_r(uid
, resultbuf
, buffer
, buflen
);
109 /* no getpwuid_r() available, just use getpwuid() */
110 *result
= getpwuid(uid
);
113 return (*result
== NULL
) ? -1 : 0;
118 * Wrapper around gethostbyname() or gethostbyname_r() to mimic
119 * POSIX gethostbyname_r() behaviour, if it is not available or required.
120 * This function is called _only_ by our getaddinfo() portability function.
122 #ifndef HAVE_GETADDRINFO
124 pqGethostbyname(const char *name
,
125 struct hostent
* resultbuf
,
126 char *buffer
, size_t buflen
,
127 struct hostent
** result
,
130 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
133 * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
136 *result
= gethostbyname_r(name
, resultbuf
, buffer
, buflen
, herrno
);
137 return (*result
== NULL
) ? -1 : 0;
140 /* no gethostbyname_r(), just use gethostbyname() */
141 *result
= gethostbyname(name
);