~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/port/thread.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2005, PostgreSQL Global Development Group
 
9
 *
 
10
 * $PostgreSQL: pgsql/src/port/thread.c,v 1.29 2004-12-31 22:03:53 pgsql Exp $
 
11
 *
 
12
 *-------------------------------------------------------------------------
 
13
 */
 
14
 
 
15
#include "c.h"
 
16
 
 
17
#include <sys/types.h>
 
18
#include <errno.h>
 
19
#ifdef WIN32_CLIENT_ONLY
 
20
#undef ERROR
 
21
#else
 
22
#include <pwd.h>
 
23
#endif
 
24
#if defined(ENABLE_THREAD_SAFETY)
 
25
#include <pthread.h>
 
26
#endif
 
27
 
 
28
/*
 
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.
 
35
 *
 
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.
 
41
 *
 
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.
 
50
 *
 
51
 *      The current setup is to try threading in this order:
 
52
 *
 
53
 *              use *_r function names if they exit
 
54
 *                      (*_THREADSAFE=ye)
 
55
 *              use non-*_r functions if they are thread-safe
 
56
 *
 
57
 *      One thread-safe solution for gethostbyname() might be to use getaddrinfo().
 
58
 *
 
59
 *      Run src/tools/thread to see if your operating system has thread-safe
 
60
 *      non-*_r functions.
 
61
 */
 
62
 
 
63
 
 
64
/*
 
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).
 
67
 */
 
68
char *
 
69
pqStrerror(int errnum, char *strerrbuf, size_t buflen)
 
70
{
 
71
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_STRERROR_R)
 
72
        /* reentrant strerror_r is available */
 
73
#ifdef STRERROR_R_INT
 
74
        /* SUSv3 version */
 
75
        if (strerror_r(errnum, strerrbuf, buflen) == 0)
 
76
                return strerrbuf;
 
77
        else
 
78
                return "Unknown error";
 
79
#else
 
80
        /* GNU libc */
 
81
        return strerror_r(errnum, strerrbuf, buflen);
 
82
#endif
 
83
#else
 
84
        /* no strerror_r() available, just use strerror */
 
85
        StrNCpy(strerrbuf, strerror(errnum), buflen);
 
86
 
 
87
        return strerrbuf;
 
88
#endif
 
89
}
 
90
 
 
91
/*
 
92
 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
 
93
 * behaviour, if it is not available or required.
 
94
 */
 
95
#ifndef WIN32
 
96
int
 
97
pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
 
98
                   size_t buflen, struct passwd ** result)
 
99
{
 
100
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETPWUID_R)
 
101
 
 
102
#ifdef GETPWUID_R_5ARG
 
103
        /* POSIX version */
 
104
        getpwuid_r(uid, resultbuf, buffer, buflen, result);
 
105
#else
 
106
 
 
107
        /*
 
108
         * Early POSIX draft of getpwuid_r() returns 'struct passwd *'.
 
109
         * getpwuid_r(uid, resultbuf, buffer, buflen)
 
110
         */
 
111
        *result = getpwuid_r(uid, resultbuf, buffer, buflen);
 
112
#endif
 
113
#else
 
114
 
 
115
        /* no getpwuid_r() available, just use getpwuid() */
 
116
        *result = getpwuid(uid);
 
117
#endif
 
118
 
 
119
        return (*result == NULL) ? -1 : 0;
 
120
}
 
121
#endif
 
122
 
 
123
/*
 
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.
 
127
 */
 
128
#ifndef HAVE_GETADDRINFO
 
129
int
 
130
pqGethostbyname(const char *name,
 
131
                                struct hostent * resultbuf,
 
132
                                char *buffer, size_t buflen,
 
133
                                struct hostent ** result,
 
134
                                int *herrno)
 
135
{
 
136
#if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
 
137
 
 
138
        /*
 
139
         * broken (well early POSIX draft) gethostbyname_r() which returns
 
140
         * 'struct hostent *'
 
141
         */
 
142
        *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
 
143
        return (*result == NULL) ? -1 : 0;
 
144
 
 
145
#else
 
146
 
 
147
        /* no gethostbyname_r(), just use gethostbyname() */
 
148
        *result = gethostbyname(name);
 
149
 
 
150
        if (*result != NULL)
 
151
                *herrno = h_errno;
 
152
 
 
153
        if (*result != NULL)
 
154
                return 0;
 
155
        else
 
156
                return -1;
 
157
#endif
 
158
}
 
159
 
 
160
#endif