]> granicus.if.org Git - postgresql/blob - src/port/thread.c
Update copyright for 2009.
[postgresql] / src / port / thread.c
1 /*-------------------------------------------------------------------------
2  *
3  * thread.c
4  *
5  *                Prototypes and macros around system calls, used to help make
6  *                threaded libraries reentrant and safe to use from threaded applications.
7  *
8  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
9  *
10  * $PostgreSQL: pgsql/src/port/thread.c,v 1.40 2009/01/01 17:24:04 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #include "c.h"
16
17 #include <pwd.h>
18 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY)
19 #endif
20
21
22 /*
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.
29  *
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.
35  *
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.
44  *
45  *      The current setup is to try threading in this order:
46  *
47  *              use *_r function names if they exit
48  *                      (*_THREADSAFE=yes)
49  *              use non-*_r functions if they are thread-safe
50  *
51  *      One thread-safe solution for gethostbyname() might be to use getaddrinfo().
52  *
53  *      Run src/test/thread to test if your operating system has thread-safe
54  *      non-*_r functions.
55  */
56
57
58 /*
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).
61  */
62 char *
63 pqStrerror(int errnum, char *strerrbuf, size_t buflen)
64 {
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);
80
81         return strerrbuf;
82 #endif
83 }
84
85 /*
86  * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
87  * behaviour, if it is not available or required.
88  */
89 #ifndef WIN32
90 int
91 pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
92                    size_t buflen, struct passwd ** result)
93 {
94 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
95
96 #ifdef GETPWUID_R_5ARG
97         /* POSIX version */
98         getpwuid_r(uid, resultbuf, buffer, buflen, result);
99 #else
100
101         /*
102          * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
103          * getpwuid_r(uid, resultbuf, buffer, buflen)
104          */
105         *result = getpwuid_r(uid, resultbuf, buffer, buflen);
106 #endif
107 #else
108
109         /* no getpwuid_r() available, just use getpwuid() */
110         *result = getpwuid(uid);
111 #endif
112
113         return (*result == NULL) ? -1 : 0;
114 }
115 #endif
116
117 /*
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.
121  */
122 #ifndef HAVE_GETADDRINFO
123 int
124 pqGethostbyname(const char *name,
125                                 struct hostent * resultbuf,
126                                 char *buffer, size_t buflen,
127                                 struct hostent ** result,
128                                 int *herrno)
129 {
130 #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
131
132         /*
133          * broken (well early POSIX draft) gethostbyname_r() which returns 'struct
134          * hostent *'
135          */
136         *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
137         return (*result == NULL) ? -1 : 0;
138 #else
139
140         /* no gethostbyname_r(), just use gethostbyname() */
141         *result = gethostbyname(name);
142
143         if (*result != NULL)
144                 *herrno = h_errno;
145
146         if (*result != NULL)
147                 return 0;
148         else
149                 return -1;
150 #endif
151 }
152
153 #endif