Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / port / thread.c
blob520161fca7fb7aba969e397bb5413f66702d4f73
1 /*-------------------------------------------------------------------------
3 * thread.c
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
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
15 #include "c.h"
17 #include <pwd.h>
18 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY)
19 #endif
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
48 * (*_THREADSAFE=yes)
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
54 * non-*_r functions.
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).
62 char *
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 */
67 #ifdef STRERROR_R_INT
68 /* SUSv3 version */
69 if (strerror_r(errnum, strerrbuf, buflen) == 0)
70 return strerrbuf;
71 else
72 return "Unknown error";
73 #else
74 /* GNU libc */
75 return strerror_r(errnum, strerrbuf, buflen);
76 #endif
77 #else
78 /* no strerror_r() available, just use strerror */
79 strlcpy(strerrbuf, strerror(errnum), buflen);
81 return strerrbuf;
82 #endif
86 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
87 * behaviour, if it is not available or required.
89 #ifndef WIN32
90 int
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
97 /* POSIX version */
98 getpwuid_r(uid, resultbuf, buffer, buflen, result);
99 #else
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);
106 #endif
107 #else
109 /* no getpwuid_r() available, just use getpwuid() */
110 *result = getpwuid(uid);
111 #endif
113 return (*result == NULL) ? -1 : 0;
115 #endif
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,
128 int *herrno)
130 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
133 * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
134 * hostent *'
136 *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
137 return (*result == NULL) ? -1 : 0;
138 #else
140 /* no gethostbyname_r(), just use gethostbyname() */
141 *result = gethostbyname(name);
143 if (*result != NULL)
144 *herrno = h_errno;
146 if (*result != NULL)
147 return 0;
148 else
149 return -1;
150 #endif
153 #endif