diff options
author | Bruce Momjian <bruce@momjian.us> | 2003-06-14 14:35:42 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2003-06-14 14:35:42 +0000 |
commit | 62b532b73668267eb950f1ba8fed4a8ec45f60ae (patch) | |
tree | c42a68c51ea3e183039ae0c8321bf3429609553c /src/port | |
parent | 02d847fe9f86b0c6ff4658eb9d49fbb98c8fa69a (diff) | |
download | postgresql-62b532b73668267eb950f1ba8fed4a8ec45f60ae.tar.gz |
Add thread.c for libpq threading, and hook it into libpq/configure.
Diffstat (limited to 'src/port')
-rw-r--r-- | src/port/Makefile | 5 | ||||
-rw-r--r-- | src/port/threads.c | 85 |
2 files changed, 89 insertions, 1 deletions
diff --git a/src/port/Makefile b/src/port/Makefile index 04d9e04994..d6de2d8890 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 0000000000..5012cac281 --- /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 +} |