Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / port / thread.c
blob20ee7d5a6fcd2603a035c7c02bf58b29fb64d186
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-2009, PostgreSQL Global Development Group
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
15 #include "c.h"
17 #include <pwd.h>
21 * Threading sometimes requires specially-named versions of functions
22 * that return data in static buffers, like strerror_r() instead of
23 * strerror(). Other operating systems use pthread_setspecific()
24 * and pthread_getspecific() internally to allow standard library
25 * functions to return static data to threaded applications. And some
26 * operating systems have neither.
28 * Additional confusion exists because many operating systems that
29 * use pthread_setspecific/pthread_getspecific() also have *_r versions
30 * of standard library functions for compatibility with operating systems
31 * that require them. However, internally, these *_r functions merely
32 * call the thread-safe standard library functions.
34 * For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
35 * getpwuid() calls pthread_setspecific/pthread_getspecific() to return
36 * static data to the caller in a thread-safe manner. However, BSD/OS
37 * also has getpwuid_r(), which merely calls getpwuid() and shifts
38 * around the arguments to match the getpwuid_r() function declaration.
39 * Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
40 * doesn't have strerror_r(), so we can't fall back to only using *_r
41 * functions for threaded programs.
43 * The current setup is to try threading in this order:
45 * use *_r function names if they exit
46 * (*_THREADSAFE=yes)
47 * use non-*_r functions if they are thread-safe
49 * One thread-safe solution for gethostbyname() might be to use getaddrinfo().
51 * Run src/test/thread to test if your operating system has thread-safe
52 * non-*_r functions.
57 * Wrapper around strerror and strerror_r to use the former if it is
58 * available and also return a more useful value (the error string).
60 char *
61 pqStrerror(int errnum, char *strerrbuf, size_t buflen)
63 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R)
64 /* reentrant strerror_r is available */
65 #ifdef STRERROR_R_INT
66 /* SUSv3 version */
67 if (strerror_r(errnum, strerrbuf, buflen) == 0)
68 return strerrbuf;
69 else
70 return "Unknown error";
71 #else
72 /* GNU libc */
73 return strerror_r(errnum, strerrbuf, buflen);
74 #endif
75 #else
76 /* no strerror_r() available, just use strerror */
77 strlcpy(strerrbuf, strerror(errnum), buflen);
79 return strerrbuf;
80 #endif
84 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
85 * behaviour, if it is not available or required.
87 #ifndef WIN32
88 int
89 pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
90 size_t buflen, struct passwd ** result)
92 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
94 #ifdef GETPWUID_R_5ARG
95 /* POSIX version */
96 getpwuid_r(uid, resultbuf, buffer, buflen, result);
97 #else
100 * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
101 * getpwuid_r(uid, resultbuf, buffer, buflen)
103 *result = getpwuid_r(uid, resultbuf, buffer, buflen);
104 #endif
105 #else
107 /* no getpwuid_r() available, just use getpwuid() */
108 *result = getpwuid(uid);
109 #endif
111 return (*result == NULL) ? -1 : 0;
113 #endif
116 * Wrapper around gethostbyname() or gethostbyname_r() to mimic
117 * POSIX gethostbyname_r() behaviour, if it is not available or required.
118 * This function is called _only_ by our getaddinfo() portability function.
120 #ifndef HAVE_GETADDRINFO
122 pqGethostbyname(const char *name,
123 struct hostent * resultbuf,
124 char *buffer, size_t buflen,
125 struct hostent ** result,
126 int *herrno)
128 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
131 * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
132 * hostent *'
134 *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
135 return (*result == NULL) ? -1 : 0;
136 #else
138 /* no gethostbyname_r(), just use gethostbyname() */
139 *result = gethostbyname(name);
141 if (*result != NULL)
142 *herrno = h_errno;
144 if (*result != NULL)
145 return 0;
146 else
147 return -1;
148 #endif
151 #endif