aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2003-06-14 14:35:42 +0000
committerBruce Momjian <bruce@momjian.us>2003-06-14 14:35:42 +0000
commit62b532b73668267eb950f1ba8fed4a8ec45f60ae (patch)
treec42a68c51ea3e183039ae0c8321bf3429609553c /src
parent02d847fe9f86b0c6ff4658eb9d49fbb98c8fa69a (diff)
downloadpostgresql-62b532b73668267eb950f1ba8fed4a8ec45f60ae.tar.gz
postgresql-62b532b73668267eb950f1ba8fed4a8ec45f60ae.zip
Add thread.c for libpq threading, and hook it into libpq/configure.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.global.in5
-rw-r--r--src/include/pg_config.h.in12
-rw-r--r--src/include/port.h18
-rw-r--r--src/interfaces/libpq/Makefile10
-rw-r--r--src/port/Makefile5
-rw-r--r--src/port/threads.c85
6 files changed, 128 insertions, 7 deletions
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index b08830bb9f8..c1087b743c5 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -1,5 +1,5 @@
# -*-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,
@@ -121,6 +121,7 @@ localedir := @localedir@
#
# 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@
@@ -340,7 +341,7 @@ endif
#
# substitute implementations of the C library
-LIBOBJS = @LIBOBJS@ path.o
+LIBOBJS = @LIBOBJS@ path.o threads.o
ifneq (,$(LIBOBJS))
LIBS += -lpgport
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 60f9536dff0..6e8cac0e99a 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -121,6 +121,9 @@
/* 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
@@ -136,6 +139,9 @@
/* 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
@@ -375,6 +381,9 @@
/* 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
@@ -579,6 +588,9 @@
/* 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
diff --git a/src/include/port.h b/src/include/port.h
index 783d41cc0ed..16f733d67ca 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -6,11 +6,15 @@
* 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);
@@ -98,3 +102,15 @@ extern long random(void);
#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);
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index b6a5ec8f753..2a178b8e9d2 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -4,7 +4,7 @@
#
# 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 $
#
#-------------------------------------------------------------------------
@@ -23,7 +23,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) $(THREAD_CFLAGS) -DFRONTEND -DSYSCO
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
@@ -46,9 +46,13 @@ backend_src = $(top_srcdir)/src/backend
# 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) $< .
diff --git a/src/port/Makefile b/src/port/Makefile
index 04d9e049940..d6de2d88907 100644
--- a/src/port/Makefile
+++ b/src/port/Makefile
@@ -7,7 +7,7 @@
# 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 $
#
#-------------------------------------------------------------------------
@@ -22,5 +22,8 @@ endif
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)
diff --git a/src/port/threads.c b/src/port/threads.c
new file mode 100644
index 00000000000..5012cac281b
--- /dev/null
+++ b/src/port/threads.c
@@ -0,0 +1,85 @@
+/*-------------------------------------------------------------------------
+ *
+ * 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
+}