case $withval in
yes)
- :
+
+cat >>confdefs.h <<\_ACEOF
+#define USE_THREADS 1
+_ACEOF
+
;;
no)
:
fi;
+
echo "$as_me:$LINENO: result: $with_threads" >&5
echo "${ECHO_T}$with_threads" >&6
# these require no special flags or libraries
;;
freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
- freebsd*) THREAD_LIBS="-lc_r" ;;
- linux*) THREAD_LIBS="-lpthread"
- THREAD_CFLAGS="-D_REENTRANT" ;;
+ freebsd*)
+ THREAD_LIBS="-lc_r"
+ ;;
+ linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
+ THREAD_LIBS="-lpthread"
+ ;;
*)
# other operating systems might fail because they have pthread.h but need
# special libs we don't know about yet.
or libraries required for threading support.
" >&2;}
{ (exit 1); exit 1; }; }
- esac
+esac
fi
fi
+#
+# Check for re-entrant versions of certain functions
+#
+# Include special flags if required
+#
+_CFLAGS="$CFLAGS"
+_LIB="$LIBS"
+CFLAGS="$CFLAGS $TREAD_CFLAGS"
+LIBS="$LIBS $THREAD_LIBS"
+
+
+
+for ac_func in strerror_r getpwuid_r gethostbyname_r
+do
+as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
+echo "$as_me:$LINENO: checking for $ac_func" >&5
+echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6
+if eval "test \"\${$as_ac_var+set}\" = set"; then
+ echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+ cat >conftest.$ac_ext <<_ACEOF
+#line $LINENO "configure"
+#include "confdefs.h"
+/* System header to define __stub macros and hopefully few prototypes,
+ which can conflict with char $ac_func (); below. */
+#include <assert.h>
+/* Override any gcc2 internal prototype to avoid an error. */
+#ifdef __cplusplus
+extern "C"
+#endif
+/* We use char because int might match the return type of a gcc2
+ builtin and then its argument prototype would still apply. */
+char $ac_func ();
+char (*f) ();
+
+#ifdef F77_DUMMY_MAIN
+# ifdef __cplusplus
+ extern "C"
+# endif
+ int F77_DUMMY_MAIN() { return 1; }
+#endif
+int
+main ()
+{
+/* The GNU C library defines this for functions which it implements
+ to always fail with ENOSYS. Some functions are actually named
+ something starting with __ and the normal name is an alias. */
+#if defined (__stub_$ac_func) || defined (__stub___$ac_func)
+choke me
+#else
+f = $ac_func;
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+ (eval $ac_link) 2>&5
+ ac_status=$?
+ echo "$as_me:$LINENO: \$? = $ac_status" >&5
+ (exit $ac_status); } &&
+ { ac_try='test -s conftest$ac_exeext'
+ { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+ (eval $ac_try) 2>&5
+ ac_status=$?
+ echo "$as_me:$LINENO: \$? = $ac_status" >&5
+ (exit $ac_status); }; }; then
+ eval "$as_ac_var=yes"
+else
+ echo "$as_me: failed program was:" >&5
+cat conftest.$ac_ext >&5
+eval "$as_ac_var=no"
+fi
+rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_var'}'`" >&5
+echo "${ECHO_T}`eval echo '${'$as_ac_var'}'`" >&6
+if test `eval echo '${'$as_ac_var'}'` = yes; then
+ cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+done
+
+CFLAGS="$_CFLAGS"
+LIB="$_LIBS"
+
+
# This test makes sure that run tests work at all. Sometimes a shared
# library is found by the linker, but the runtime linker can't find it.
s,@with_rendezvous@,$with_rendezvous,;t t
s,@with_openssl@,$with_openssl,;t t
s,@ELF_SYS@,$ELF_SYS,;t t
-s,@THREAD_LIBS@,$THREAD_LIBS,;t t
s,@THREAD_CFLAGS@,$THREAD_CFLAGS,;t t
+s,@THREAD_LIBS@,$THREAD_LIBS,;t t
s,@AWK@,$AWK,;t t
s,@FLEX@,$FLEX,;t t
s,@FLEXFLAGS@,$FLEXFLAGS,;t t
dnl Process this file with autoconf to produce a configure script.
-dnl $Header: /cvsroot/pgsql/configure.in,v 1.261 2003/06/13 23:10:07 momjian Exp $
+dnl $Header: /cvsroot/pgsql/configure.in,v 1.262 2003/06/14 14:35:42 momjian Exp $
dnl
dnl Developers, please strive to achieve this order:
dnl
# Enable libpq to be thread-safe
#
AC_MSG_CHECKING([allow threaded libpq])
-PGAC_ARG_BOOL(with, threads, no, [ --with-threads allow libpq to be thread-safe])
+PGAC_ARG_BOOL(with, threads, no, [ --with-threads allow libpq to be thread-safe],
+ [AC_DEFINE([USE_THREADS], 1, [Define to 1 to build libpq with threads. (--with-threads)])])
+
AC_MSG_RESULT([$with_threads])
AC_SUBST(with_threads)
# these require no special flags or libraries
;;
freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
- freebsd*) THREAD_LIBS="-lc_r" ;;
- linux*) THREAD_LIBS="-lpthread"
- THREAD_CFLAGS="-D_REENTRANT" ;;
+ freebsd*)
+ THREAD_LIBS="-lc_r"
+ ;;
+ linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
+ THREAD_LIBS="-lpthread"
+ ;;
*)
# other operating systems might fail because they have pthread.h but need
# special libs we don't know about yet.
so it can be added to the next release. Report any compile or link flags,
or libraries required for threading support.
])
- esac
+esac
fi
-AC_SUBST(THREAD_LIBS)
AC_SUBST(THREAD_CFLAGS)
+AC_SUBST(THREAD_LIBS)
#
# Assignments
AC_FUNC_FSEEKO
+#
+# Check for re-entrant versions of certain functions
+#
+# Include special flags if required
+#
+_CFLAGS="$CFLAGS"
+_LIB="$LIBS"
+CFLAGS="$CFLAGS $TREAD_CFLAGS"
+LIBS="$LIBS $THREAD_LIBS"
+AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
+CFLAGS="$_CFLAGS"
+LIB="$_LIBS"
+
+
# This test makes sure that run tests work at all. Sometimes a shared
# library is found by the linker, but the runtime linker can't find it.
# -*-makefile-*-
-# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.163 2003/05/27 16:36:50 momjian Exp $
+# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.164 2003/06/14 14:35:42 momjian Exp $
#------------------------------------------------------------------------------
# All PostgreSQL makefiles include this file and use the variables it sets,
#
# Records the choice of the various --enable-xxx and --with-xxx options.
+with_threads = @with_threads@
with_java = @with_java@
with_perl = @with_perl@
with_python = @with_python@
#
# substitute implementations of the C library
-LIBOBJS = @LIBOBJS@ path.o
+LIBOBJS = @LIBOBJS@ path.o threads.o
ifneq (,$(LIBOBJS))
LIBS += -lpgport
/* Define to 1 if you have the `getaddrinfo' function. */
#undef HAVE_GETADDRINFO
+/* Define to 1 if you have the `gethostbyname_r' function. */
+#undef HAVE_GETHOSTBYNAME_R
+
/* Define to 1 if you have the `gethostname' function. */
#undef HAVE_GETHOSTNAME
/* Define to 1 if you have the `getpeereid' function. */
#undef HAVE_GETPEEREID
+/* Define to 1 if you have the `getpwuid_r' function. */
+#undef HAVE_GETPWUID_R
+
/* Define to 1 if you have the `getrusage' function. */
#undef HAVE_GETRUSAGE
/* Define to 1 if you have the `strerror' function. */
#undef HAVE_STRERROR
+/* Define to 1 if you have the `strerror_r' function. */
+#undef HAVE_STRERROR_R
+
/* Define to 1 if cpp supports the ANSI # stringizing operator. */
#undef HAVE_STRINGIZE
/* Define to select SysV-style shared memory. */
#undef USE_SYSV_SHARED_MEMORY
+/* Define to 1 to build libpq with threads. (--with-threads) */
+#undef USE_THREADS
+
/* Define to select unnamed POSIX semaphores. */
#undef USE_UNNAMED_POSIX_SEMAPHORES
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: port.h,v 1.6 2003/06/12 08:15:29 momjian Exp $
+ * $Id: port.h,v 1.7 2003/06/14 14:35:42 momjian Exp $
*
*-------------------------------------------------------------------------
*/
+/* for thread.c */
+#include <pwd.h>
+#include <netdb.h>
+
/* Portable path handling for Unix/Win32 */
bool is_absolute_path(const char *filename);
char *first_path_separator(const char *filename);
#ifndef HAVE_SRANDOM
extern void srandom(unsigned int seed);
#endif
+
+/* thread.h */
+extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
+
+extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
+ size_t buflen, struct passwd **result);
+
+extern int pqGethostbyname(const char *name,
+ struct hostent *resbuf,
+ char *buf, size_t buflen,
+ struct hostent **result,
+ int *herrno);
#
# Copyright (c) 1994, Regents of the University of California
#
-# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.81 2003/06/12 17:31:50 momjian Exp $
+# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.82 2003/06/14 14:35:42 momjian Exp $
#
#-------------------------------------------------------------------------
OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
dllist.o md5.o ip.o wchar.o encnames.o \
- $(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o, $(LIBOBJS))
+ $(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o threads.o, $(LIBOBJS))
# Add libraries that libpq depends (or might depend) on into the
# For port modules, this only happens if configure decides the module
# is needed (see filter hack in OBJS, above).
-crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c: %.c : $(top_srcdir)/src/port/%.c
+crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c threads.c: %.c : $(top_srcdir)/src/port/%.c
rm -f $@ && $(LN_S) $< .
+# compile this with thread flags
+thread.o: thread.c
+ $(CC) $(CFLAGS) $(THREAD_CFLAGS) -c thread.c
+
dllist.c: $(backend_src)/lib/dllist.c
rm -f $@ && $(LN_S) $< .
# with broken/missing library files.
# IDENTIFICATION
-# $Header: /cvsroot/pgsql/src/port/Makefile,v 1.3 2002/07/27 20:10:05 petere Exp $
+# $Header: /cvsroot/pgsql/src/port/Makefile,v 1.4 2003/06/14 14:35:42 momjian Exp $
#
#-------------------------------------------------------------------------
libpgport.a: $(LIBOBJS)
$(AR) crs $@ $^
+thread.o: thread.c
+ $(CC) $(CFLAGS) $(THREAD_CFLAGS) -c thread.c
+
clean distclean maintainer-clean:
rm -f libpgport.a $(LIBOBJS)
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * threads.c
+ *
+ * Prototypes and macros around system calls, used to help make
+ * threaded libraries reentrant and safe to use from threaded applications.
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ *
+ * $Id: threads.c,v 1.1 2003/06/14 14:35:42 momjian Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+/*
+ * Wrapper around strerror and strerror_r to use the former if it is
+ * available and also return a more useful value (the error string).
+ */
+char *
+pqStrerror(int errnum, char *strerrbuf, size_t buflen)
+{
+#if defined(USE_THREADS) && defined(HAVE_STRERROR_R)
+ /* reentrant strerror_r is available */
+ /* some early standards had strerror_r returning char * */
+ strerror_r(errnum, strerrbuf, buflen);
+ return (strerrbuf);
+#else
+ /* no strerror_r() available, just use strerror */
+ return strerror(errnum);
+#endif
+}
+
+/*
+ * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
+ * behaviour, if it is not available.
+ */
+int
+pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
+ size_t buflen, struct passwd ** result)
+{
+#if defined(USE_THREADS) && defined(HAVE_GETPWUID_R)
+ /*
+ * broken (well early POSIX draft) getpwuid_r() which returns 'struct
+ * passwd *'
+ */
+ *result = getpwuid_r(uid, resultbuf, buffer, buflen);
+#else
+ /* no getpwuid_r() available, just use getpwuid() */
+ *result = getpwuid(uid);
+#endif
+ return (*result == NULL) ? -1 : 0;
+}
+
+/*
+ * Wrapper around gethostbyname() or gethostbyname_r() to mimic
+ * POSIX gethostbyname_r() behaviour, if it is not available.
+ */
+int
+pqGethostbyname(const char *name,
+ struct hostent * resbuf,
+ char *buf, size_t buflen,
+ struct hostent ** result,
+ int *herrno)
+{
+#if defined(USE_THREADS) && defined(HAVE_GETHOSTBYNAME_R)
+ /*
+ * broken (well early POSIX draft) gethostbyname_r() which returns
+ * 'struct hostent *'
+ */
+ *result = gethostbyname_r(name, resbuf, buf, buflen, herrno);
+ return (*result == NULL) ? -1 : 0;
+#else
+ /* no gethostbyname_r(), just use gethostbyname() */
+ *result = gethostbyname(name);
+ if (*result != NULL)
+ return 0;
+ else
+ {
+ *herrno = h_errno;
+ return -1;
+ }
+#endif
+}