Commit a16a0314 authored by Bruce Momjian's avatar Bruce Momjian

Make libpq thread-safe with configure --with-threads option.

Lee Kindness
parent 62b532b7
...@@ -3591,6 +3591,10 @@ rm -f conftest* ...@@ -3591,6 +3591,10 @@ rm -f conftest*
# #
# Pthreads # Pthreads
# #
# For each platform, we need to know about any special compile and link
# libraries, and whether the normal C function names are thread-safe.
#
NEED_REENTRANT_FUNC_NAMES=no
if test "$with_threads" = yes; then if test "$with_threads" = yes; then
echo "$as_me:$LINENO: checking for ANSI C header files" >&5 echo "$as_me:$LINENO: checking for ANSI C header files" >&5
echo $ECHO_N "checking for ANSI C header files... $ECHO_C" >&6 echo $ECHO_N "checking for ANSI C header files... $ECHO_C" >&6
...@@ -3902,13 +3906,19 @@ fi ...@@ -3902,13 +3906,19 @@ fi
case $host_os in case $host_os in
netbsd*|bsdi*) netbsd*|bsdi*)
# these require no special flags or libraries # these require no special flags or libraries
NEED_REENTRANT_FUNC_NAMES=no
;;
freebsd2*|freebsd3*|freebsd4*)
THREAD_CFLAGS="-pthread"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
freebsd*) freebsd*)
THREAD_LIBS="-lc_r" THREAD_LIBS="-lc_r"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS" linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
THREAD_LIBS="-lpthread" THREAD_LIBS="-lpthread"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
*) *)
# other operating systems might fail because they have pthread.h but need # other operating systems might fail because they have pthread.h but need
...@@ -12829,8 +12839,14 @@ fi ...@@ -12829,8 +12839,14 @@ fi
# #
# Check for re-entrant versions of certain functions # Check for re-entrant versions of certain functions
# #
# Include special flags if required # Include special flags if threads are enabled _and_ if required for
# threading on this platform. Some platforms have *_r functions but
# their natively named funcs are threadsafe, and should be used instead.
#
# One trick here is that if the don't call AC_CHECK_FUNCS, the
# functions are marked "not found", which is perfect.
# #
if test "$NEED_REENTRANT_FUNC_NAMES" = yes ; then
_CFLAGS="$CFLAGS" _CFLAGS="$CFLAGS"
_LIB="$LIBS" _LIB="$LIBS"
CFLAGS="$CFLAGS $TREAD_CFLAGS" CFLAGS="$CFLAGS $TREAD_CFLAGS"
...@@ -12915,7 +12931,7 @@ done ...@@ -12915,7 +12931,7 @@ done
CFLAGS="$_CFLAGS" CFLAGS="$_CFLAGS"
LIB="$_LIBS" LIB="$_LIBS"
fi
# This test makes sure that run tests work at all. Sometimes a shared # This test makes sure that run tests work at all. Sometimes a shared
......
dnl Process this file with autoconf to produce a configure script. dnl Process this file with autoconf to produce a configure script.
dnl $Header: /cvsroot/pgsql/configure.in,v 1.262 2003/06/14 14:35:42 momjian Exp $ dnl $Header: /cvsroot/pgsql/configure.in,v 1.263 2003/06/14 17:49:53 momjian Exp $
dnl dnl
dnl Developers, please strive to achieve this order: dnl Developers, please strive to achieve this order:
dnl dnl
...@@ -554,18 +554,28 @@ AC_SUBST(ELF_SYS) ...@@ -554,18 +554,28 @@ AC_SUBST(ELF_SYS)
# #
# Pthreads # Pthreads
# #
# For each platform, we need to know about any special compile and link
# libraries, and whether the normal C function names are thread-safe.
#
NEED_REENTRANT_FUNC_NAMES=no
if test "$with_threads" = yes; then if test "$with_threads" = yes; then
AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([pthread.h not found, required for --with-threads])]) AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([pthread.h not found, required for --with-threads])])
case $host_os in case $host_os in
netbsd*|bsdi*) netbsd*|bsdi*)
# these require no special flags or libraries # these require no special flags or libraries
NEED_REENTRANT_FUNC_NAMES=no
;;
freebsd2*|freebsd3*|freebsd4*)
THREAD_CFLAGS="-pthread"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
freebsd*) freebsd*)
THREAD_LIBS="-lc_r" THREAD_LIBS="-lc_r"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS" linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
THREAD_LIBS="-lpthread" THREAD_LIBS="-lpthread"
NEED_REENTRANT_FUNC_NAMES=yes
;; ;;
*) *)
# other operating systems might fail because they have pthread.h but need # other operating systems might fail because they have pthread.h but need
...@@ -991,8 +1001,14 @@ AC_FUNC_FSEEKO ...@@ -991,8 +1001,14 @@ AC_FUNC_FSEEKO
# #
# Check for re-entrant versions of certain functions # Check for re-entrant versions of certain functions
# #
# Include special flags if required # Include special flags if threads are enabled _and_ if required for
# threading on this platform. Some platforms have *_r functions but
# their natively named funcs are threadsafe, and should be used instead.
#
# One trick here is that if the don't call AC_CHECK_FUNCS, the
# functions are marked "not found", which is perfect.
# #
if test "$NEED_REENTRANT_FUNC_NAMES" = yes ; then
_CFLAGS="$CFLAGS" _CFLAGS="$CFLAGS"
_LIB="$LIBS" _LIB="$LIBS"
CFLAGS="$CFLAGS $TREAD_CFLAGS" CFLAGS="$CFLAGS $TREAD_CFLAGS"
...@@ -1000,7 +1016,7 @@ LIBS="$LIBS $THREAD_LIBS" ...@@ -1000,7 +1016,7 @@ LIBS="$LIBS $THREAD_LIBS"
AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r]) AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
CFLAGS="$_CFLAGS" CFLAGS="$_CFLAGS"
LIB="$_LIBS" LIB="$_LIBS"
fi
# This test makes sure that run tests work at all. Sometimes a shared # This test makes sure that run tests work at all. Sometimes a shared
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes). * exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.79 2003/06/08 17:43:00 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.80 2003/06/14 17:49:53 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -391,8 +391,10 @@ pg_krb5_sendauth(char *PQerrormsg, int sock, ...@@ -391,8 +391,10 @@ pg_krb5_sendauth(char *PQerrormsg, int sock,
flags = fcntl(sock, F_GETFL); flags = fcntl(sock, F_GETFL);
if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK))) if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK)))
{ {
char sebuf[256];
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
libpq_gettext("could not set socket to blocking mode: %s\n"), strerror(errno)); libpq_gettext("could not set socket to blocking mode: %s\n"), pqStrerror(errno, sebuf, sizeof(sebuf)));
krb5_free_principal(pg_krb5_context, server); krb5_free_principal(pg_krb5_context, server);
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -436,9 +438,11 @@ pg_krb5_sendauth(char *PQerrormsg, int sock, ...@@ -436,9 +438,11 @@ pg_krb5_sendauth(char *PQerrormsg, int sock,
if (fcntl(sock, F_SETFL, (long) flags)) if (fcntl(sock, F_SETFL, (long) flags))
{ {
char sebuf[256];
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
libpq_gettext("could not restore non-blocking mode on socket: %s\n"), libpq_gettext("could not restore non-blocking mode on socket: %s\n"),
strerror(errno)); pqStrerror(errno, sebuf, sizeof(sebuf)));
ret = STATUS_ERROR; ret = STATUS_ERROR;
} }
...@@ -495,8 +499,11 @@ pg_local_sendauth(char *PQerrormsg, PGconn *conn) ...@@ -495,8 +499,11 @@ pg_local_sendauth(char *PQerrormsg, PGconn *conn)
if (sendmsg(conn->sock, &msg, 0) == -1) if (sendmsg(conn->sock, &msg, 0) == -1)
{ {
char sebuf[256];
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
"pg_local_sendauth: sendmsg: %s\n", strerror(errno)); "pg_local_sendauth: sendmsg: %s\n",
pqStrerror(errno, sebuf, sizeof(sebuf)));
return STATUS_ERROR; return STATUS_ERROR;
} }
return STATUS_OK; return STATUS_OK;
...@@ -739,10 +746,13 @@ fe_getauthname(char *PQerrormsg) ...@@ -739,10 +746,13 @@ fe_getauthname(char *PQerrormsg)
if (GetUserName(username, &namesize)) if (GetUserName(username, &namesize))
name = username; name = username;
#else #else
struct passwd *pw = getpwuid(geteuid()); char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pw = NULL;
if (pw) if( pqGetpwuid(geteuid(), &pwdstr,
name = pw->pw_name; pwdbuf, sizeof(pwdbuf), &pw) == 0 )
name = pw->pw_name;
#endif #endif
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.247 2003/06/12 08:15:29 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.248 2003/06/14 17:49:53 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -713,9 +713,11 @@ connectMakeNonblocking(PGconn *conn) ...@@ -713,9 +713,11 @@ connectMakeNonblocking(PGconn *conn)
{ {
if (FCNTL_NONBLOCK(conn->sock) < 0) if (FCNTL_NONBLOCK(conn->sock) < 0)
{ {
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to non-blocking mode: %s\n"), libpq_gettext("could not set socket to non-blocking mode: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0; return 0;
} }
...@@ -738,9 +740,11 @@ connectNoDelay(PGconn *conn) ...@@ -738,9 +740,11 @@ connectNoDelay(PGconn *conn)
(char *) &on, (char *) &on,
sizeof(on)) < 0) sizeof(on)) < 0)
{ {
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to TCP no delay mode: %s\n"), libpq_gettext("could not set socket to TCP no delay mode: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0; return 0;
} }
#endif #endif
...@@ -759,6 +763,7 @@ connectFailureMessage(PGconn *conn, int errorno) ...@@ -759,6 +763,7 @@ connectFailureMessage(PGconn *conn, int errorno)
{ {
char hostname[NI_MAXHOST]; char hostname[NI_MAXHOST];
char service[NI_MAXHOST]; char service[NI_MAXHOST];
char sebuf[256];
getnameinfo((struct sockaddr *)&conn->raddr.addr, conn->raddr.salen, getnameinfo((struct sockaddr *)&conn->raddr.addr, conn->raddr.salen,
hostname, sizeof(hostname), service, sizeof(service), hostname, sizeof(hostname), service, sizeof(service),
...@@ -770,7 +775,7 @@ connectFailureMessage(PGconn *conn, int errorno) ...@@ -770,7 +775,7 @@ connectFailureMessage(PGconn *conn, int errorno)
"\tIs the server running locally and accepting\n" "\tIs the server running locally and accepting\n"
"\tconnections on Unix domain socket \"%s\"?\n" "\tconnections on Unix domain socket \"%s\"?\n"
), ),
SOCK_STRERROR(errorno), service); SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)), service);
else else
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext( libpq_gettext(
...@@ -778,7 +783,7 @@ connectFailureMessage(PGconn *conn, int errorno) ...@@ -778,7 +783,7 @@ connectFailureMessage(PGconn *conn, int errorno)
"\tIs the server running on host %s and accepting\n" "\tIs the server running on host %s and accepting\n"
"\tTCP/IP connections on port %s?\n" "\tTCP/IP connections on port %s?\n"
), ),
SOCK_STRERROR(errorno), SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)),
conn->pghostaddr conn->pghostaddr
? conn->pghostaddr ? conn->pghostaddr
: (conn->pghost : (conn->pghost
...@@ -1001,6 +1006,7 @@ PostgresPollingStatusType ...@@ -1001,6 +1006,7 @@ PostgresPollingStatusType
PQconnectPoll(PGconn *conn) PQconnectPoll(PGconn *conn)
{ {
PGresult *res; PGresult *res;
char sebuf[256];
if (conn == NULL) if (conn == NULL)
return PGRES_POLLING_FAILED; return PGRES_POLLING_FAILED;
...@@ -1094,7 +1100,7 @@ keep_going: /* We will come back to here until there ...@@ -1094,7 +1100,7 @@ keep_going: /* We will come back to here until there
} }
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not create socket: %s\n"), libpq_gettext("could not create socket: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
break; break;
} }
...@@ -1200,7 +1206,7 @@ retry_connect: ...@@ -1200,7 +1206,7 @@ retry_connect:
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not get socket error status: %s\n"), libpq_gettext("could not get socket error status: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
goto error_return; goto error_return;
} }
else if (optval != 0) else if (optval != 0)
...@@ -1237,7 +1243,7 @@ retry_connect: ...@@ -1237,7 +1243,7 @@ retry_connect:
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not get client address from socket: %s\n"), libpq_gettext("could not get client address from socket: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
goto error_return; goto error_return;
} }
...@@ -1282,7 +1288,7 @@ retry_connect: ...@@ -1282,7 +1288,7 @@ retry_connect:
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send SSL negotiation packet: %s\n"), libpq_gettext("could not send SSL negotiation packet: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
goto error_return; goto error_return;
} }
/* Ok, wait for response */ /* Ok, wait for response */
...@@ -1317,7 +1323,7 @@ retry_connect: ...@@ -1317,7 +1323,7 @@ retry_connect:
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send startup packet: %s\n"), libpq_gettext("could not send startup packet: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
free(startpacket); free(startpacket);
goto error_return; goto error_return;
} }
...@@ -1357,7 +1363,7 @@ retry_ssl_read: ...@@ -1357,7 +1363,7 @@ retry_ssl_read:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"), libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
goto error_return; goto error_return;
} }
if (nread == 0) if (nread == 0)
...@@ -2037,6 +2043,7 @@ PQrequestCancel(PGconn *conn) ...@@ -2037,6 +2043,7 @@ PQrequestCancel(PGconn *conn)
{ {
int save_errno = SOCK_ERRNO; int save_errno = SOCK_ERRNO;
int tmpsock = -1; int tmpsock = -1;
char sebuf[256];
struct struct
{ {
uint32 packetlen; uint32 packetlen;
...@@ -2115,7 +2122,7 @@ retry4: ...@@ -2115,7 +2122,7 @@ retry4:
return TRUE; return TRUE;
cancel_errReturn: cancel_errReturn:
strcat(conn->errorMessage.data, SOCK_STRERROR(SOCK_ERRNO)); strcat(conn->errorMessage.data, SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
strcat(conn->errorMessage.data, "\n"); strcat(conn->errorMessage.data, "\n");
conn->errorMessage.len = strlen(conn->errorMessage.data); conn->errorMessage.len = strlen(conn->errorMessage.data);
if (tmpsock >= 0) if (tmpsock >= 0)
...@@ -2262,8 +2269,9 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -2262,8 +2269,9 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
*val; *val;
int found_keyword; int found_keyword;
key = strtok(line, "="); key = line;
if (key == NULL) val = strchr(line, '=');
if( val == NULL )
{ {
printfPQExpBuffer(errorMessage, printfPQExpBuffer(errorMessage,
"ERROR: syntax error in service file '%s', line %d\n", "ERROR: syntax error in service file '%s', line %d\n",
...@@ -2272,6 +2280,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -2272,6 +2280,7 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
fclose(f); fclose(f);
return 3; return 3;
} }
*val++ = '\0';
/* /*
* If not already set, set the database name to the * If not already set, set the database name to the
...@@ -2287,8 +2296,6 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -2287,8 +2296,6 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
} }
} }
val = line + strlen(line) + 1;
/* /*
* Set the parameter --- but don't override any * Set the parameter --- but don't override any
* previous explicit setting. * previous explicit setting.
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.41 2002/06/20 20:29:54 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.42 2003/06/14 17:49:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -396,9 +396,10 @@ lo_import(PGconn *conn, const char *filename) ...@@ -396,9 +396,10 @@ lo_import(PGconn *conn, const char *filename)
fd = open(filename, O_RDONLY | PG_BINARY, 0666); fd = open(filename, O_RDONLY | PG_BINARY, 0666);
if (fd < 0) if (fd < 0)
{ /* error */ { /* error */
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open file \"%s\": %s\n"), libpq_gettext("could not open file \"%s\": %s\n"),
filename, strerror(errno)); filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
return InvalidOid; return InvalidOid;
} }
...@@ -479,9 +480,10 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename) ...@@ -479,9 +480,10 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666); fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
if (fd < 0) if (fd < 0)
{ /* error */ { /* error */
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open file \"%s\": %s\n"), libpq_gettext("could not open file \"%s\": %s\n"),
filename, strerror(errno)); filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
(void) lo_close(conn, lobj); (void) lo_close(conn, lobj);
return -1; return -1;
} }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.95 2003/06/12 08:15:29 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.96 2003/06/14 17:49:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -536,6 +536,7 @@ pqReadData(PGconn *conn) ...@@ -536,6 +536,7 @@ pqReadData(PGconn *conn)
{ {
int someread = 0; int someread = 0;
int nread; int nread;
char sebuf[256];
if (conn->sock < 0) if (conn->sock < 0)
{ {
...@@ -606,7 +607,7 @@ retry3: ...@@ -606,7 +607,7 @@ retry3:
#endif #endif
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive data from server: %s\n"), libpq_gettext("could not receive data from server: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return -1; return -1;
} }
if (nread > 0) if (nread > 0)
...@@ -686,7 +687,7 @@ retry4: ...@@ -686,7 +687,7 @@ retry4:
#endif #endif
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive data from server: %s\n"), libpq_gettext("could not receive data from server: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return -1; return -1;
} }
if (nread > 0) if (nread > 0)
...@@ -740,6 +741,7 @@ pqSendSome(PGconn *conn, int len) ...@@ -740,6 +741,7 @@ pqSendSome(PGconn *conn, int len)
while (len > 0) while (len > 0)
{ {
int sent; int sent;
char sebuf[256];
sent = pqsecure_write(conn, ptr, len); sent = pqsecure_write(conn, ptr, len);
...@@ -787,7 +789,7 @@ pqSendSome(PGconn *conn, int len) ...@@ -787,7 +789,7 @@ pqSendSome(PGconn *conn, int len)
default: default:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send data to server: %s\n"), libpq_gettext("could not send data to server: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
/* We don't assume it's a fatal error... */ /* We don't assume it's a fatal error... */
conn->outCount = 0; conn->outCount = 0;
return -1; return -1;
...@@ -963,9 +965,11 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time) ...@@ -963,9 +965,11 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
if (result < 0) if (result < 0)
{ {
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("select() failed: %s\n"), libpq_gettext("select() failed: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
} }
return result; return result;
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-secure.c,v 1.23 2003/06/08 17:43:00 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-secure.c,v 1.24 2003/06/14 17:49:54 momjian Exp $
* *
* NOTES * NOTES
* The client *requires* a valid server certificate. Since * The client *requires* a valid server certificate. Since
...@@ -298,14 +298,19 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len) ...@@ -298,14 +298,19 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len)
*/ */
goto rloop; goto rloop;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
{
char sebuf[256];
if (n == -1) if (n == -1)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: %s\n"), libpq_gettext("SSL SYSCALL error: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
else else
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: EOF detected\n")); libpq_gettext("SSL SYSCALL error: EOF detected\n"));
break; break;
}
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL error: %s\n"), SSLerrmessage()); libpq_gettext("SSL error: %s\n"), SSLerrmessage());
...@@ -360,14 +365,18 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len) ...@@ -360,14 +365,18 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
n = 0; n = 0;
break; break;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
{
char sebuf[256];
if (n == -1) if (n == -1)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: %s\n"), libpq_gettext("SSL SYSCALL error: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
else else
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: EOF detected\n")); libpq_gettext("SSL SYSCALL error: EOF detected\n"));
break; break;
}
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL error: %s\n"), SSLerrmessage()); libpq_gettext("SSL error: %s\n"), SSLerrmessage());
...@@ -418,7 +427,6 @@ verify_cb(int ok, X509_STORE_CTX *ctx) ...@@ -418,7 +427,6 @@ verify_cb(int ok, X509_STORE_CTX *ctx)
#ifdef NOT_USED #ifdef NOT_USED
/* /*
* Verify that common name resolves to peer. * Verify that common name resolves to peer.
* This function is not thread-safe due to gethostbyname().
*/ */
static int static int
verify_peer(PGconn *conn) verify_peer(PGconn *conn)
...@@ -434,9 +442,10 @@ verify_peer(PGconn *conn) ...@@ -434,9 +442,10 @@ verify_peer(PGconn *conn)
len = sizeof(addr); len = sizeof(addr);
if (getpeername(conn->sock, &addr, &len) == -1) if (getpeername(conn->sock, &addr, &len) == -1)
{ {
char sebuf[256];
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("error querying socket: %s\n"), libpq_gettext("error querying socket: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return -1; return -1;
} }
...@@ -514,14 +523,16 @@ verify_peer(PGconn *conn) ...@@ -514,14 +523,16 @@ verify_peer(PGconn *conn)
static DH * static DH *
load_dh_file(int keylength) load_dh_file(int keylength)
{ {
struct passwd *pwd; char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pwd = NULL;
FILE *fp; FILE *fp;
char fnbuf[2048]; char fnbuf[2048];
DH *dh = NULL; DH *dh = NULL;
int codes; int codes;
if ((pwd = getpwuid(getuid())) == NULL) if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 )
return NULL; return NULL;
/* attempt to open file. It's not an error if it doesn't exist. */ /* attempt to open file. It's not an error if it doesn't exist. */
snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/dh%d.pem", snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/dh%d.pem",
...@@ -654,15 +665,19 @@ tmp_dh_cb(SSL *s, int is_export, int keylength) ...@@ -654,15 +665,19 @@ tmp_dh_cb(SSL *s, int is_export, int keylength)
static int static int
client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
{ {
struct passwd *pwd; char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pwd = NULL;
struct stat buf, struct stat buf,
buf2; buf2;
char fnbuf[2048]; char fnbuf[2048];
FILE *fp; FILE *fp;
PGconn *conn = (PGconn *) SSL_get_app_data(ssl); PGconn *conn = (PGconn *) SSL_get_app_data(ssl);
int (*cb) () = NULL; /* how to read user password */ int (*cb) () = NULL; /* how to read user password */
char sebuf[256];
if ((pwd = getpwuid(getuid())) == NULL) if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 )
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not get user information\n")); libpq_gettext("could not get user information\n"));
...@@ -678,7 +693,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) ...@@ -678,7 +693,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open certificate (%s): %s\n"), libpq_gettext("could not open certificate (%s): %s\n"),
fnbuf, strerror(errno)); fnbuf, pqStrerror(errno, sebuf, sizeof(sebuf)));
return -1; return -1;
} }
if (PEM_read_X509(fp, x509, NULL, NULL) == NULL) if (PEM_read_X509(fp, x509, NULL, NULL) == NULL)
...@@ -714,7 +729,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) ...@@ -714,7 +729,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open private key file (%s): %s\n"), libpq_gettext("could not open private key file (%s): %s\n"),
fnbuf, strerror(errno)); fnbuf, pqStrerror(errno, sebuf, sizeof(sebuf)));
X509_free(*x509); X509_free(*x509);
return -1; return -1;
} }
...@@ -758,7 +773,9 @@ static int ...@@ -758,7 +773,9 @@ static int
initialize_SSL(PGconn *conn) initialize_SSL(PGconn *conn)
{ {
struct stat buf; struct stat buf;
struct passwd *pwd; char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pwd = NULL;
char fnbuf[2048]; char fnbuf[2048];
if (!SSL_context) if (!SSL_context)
...@@ -775,7 +792,7 @@ initialize_SSL(PGconn *conn) ...@@ -775,7 +792,7 @@ initialize_SSL(PGconn *conn)
} }
} }
if ((pwd = getpwuid(getuid())) != NULL) if( pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0 )
{ {
snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/root.crt", snprintf(fnbuf, sizeof fnbuf, "%s/.postgresql/root.crt",
pwd->pw_dir); pwd->pw_dir);
...@@ -783,10 +800,11 @@ initialize_SSL(PGconn *conn) ...@@ -783,10 +800,11 @@ initialize_SSL(PGconn *conn)
{ {
return 0; return 0;
#ifdef NOT_USED #ifdef NOT_USED
char sebuf[256];
/* CLIENT CERTIFICATES NOT REQUIRED bjm 2002-09-26 */ /* CLIENT CERTIFICATES NOT REQUIRED bjm 2002-09-26 */
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not read root certificate list (%s): %s\n"), libpq_gettext("could not read root certificate list (%s): %s\n"),
fnbuf, strerror(errno)); fnbuf, pqStrerror(errno, sebuf, sizeof(sebuf)));
return -1; return -1;
#endif #endif
} }
...@@ -846,16 +864,19 @@ open_client_SSL(PGconn *conn) ...@@ -846,16 +864,19 @@ open_client_SSL(PGconn *conn)
return PGRES_POLLING_WRITING; return PGRES_POLLING_WRITING;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
{
char sebuf[256];
if (r == -1) if (r == -1)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: %s\n"), libpq_gettext("SSL SYSCALL error: %s\n"),
SOCK_STRERROR(SOCK_ERRNO)); SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
else else
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL SYSCALL error: EOF detected\n")); libpq_gettext("SSL SYSCALL error: EOF detected\n"));
close_SSL(conn); close_SSL(conn);
return PGRES_POLLING_FAILED; return PGRES_POLLING_FAILED;
}
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("SSL error: %s\n"), SSLerrmessage()); libpq_gettext("SSL error: %s\n"), SSLerrmessage());
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-int.h,v 1.73 2003/06/12 07:36:51 momjian Exp $ * $Id: libpq-int.h,v 1.74 2003/06/14 17:49:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#if defined(WIN32) && (!defined(ssize_t)) #if defined(WIN32) && (!defined(ssize_t))
typedef int ssize_t; /* ssize_t doesn't exist in VC (atleast typedef int ssize_t; /* ssize_t doesn't exist in VC (atleast
* not VC6) */ * not VC6) */
...@@ -448,7 +449,7 @@ __attribute__((format_arg(1))); ...@@ -448,7 +449,7 @@ __attribute__((format_arg(1)));
#define SOCK_STRERROR winsock_strerror #define SOCK_STRERROR winsock_strerror
#else #else
#define SOCK_ERRNO errno #define SOCK_ERRNO errno
#define SOCK_STRERROR strerror #define SOCK_STRERROR pqStrerror
#endif #endif
#endif /* LIBPQ_INT_H */ #endif /* LIBPQ_INT_H */
...@@ -271,13 +271,12 @@ struct MessageDLL ...@@ -271,13 +271,12 @@ struct MessageDLL
*/ */
const char * const char *
winsock_strerror(int err) winsock_strerror(int err, char *strerrbuf, size_t buflen)
{ {
static char buf[512]; /* Not threadsafe */
unsigned long flags; unsigned long flags;
int offs, int offs,
i; i;
int success = LookupWSErrorMessage(err, buf); int success = LookupWSErrorMessage(err, strerrbuf);
for (i = 0; !success && i < DLLS_SIZE; i++) for (i = 0; !success && i < DLLS_SIZE; i++)
{ {
...@@ -302,20 +301,20 @@ winsock_strerror(int err) ...@@ -302,20 +301,20 @@ winsock_strerror(int err)
flags, flags,
dlls[i].handle, err, dlls[i].handle, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, sizeof(buf) - 64, strerrbuf, buflen - 64,
0 0
); );
} }
if (!success) if (!success)
sprintf(buf, "Unknown socket error (0x%08X/%lu)", err, err); sprintf(strerrbuf, "Unknown socket error (0x%08X/%lu)", err, err);
else else
{ {
buf[sizeof(buf) - 1] = '\0'; strerrbuf[buflen - 1] = '\0';
offs = strlen(buf); offs = strlen(strerrbuf);
if (offs > sizeof(buf) - 64) if (offs > buflen - 64)
offs = sizeof(buf) - 64; offs = buflen - 64;
sprintf(buf + offs, " (0x%08X/%lu)", err, err); sprintf(strerrbuf + offs, " (0x%08X/%lu)", err, err);
} }
return buf; return strerrbuf;
} }
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
/* /*
* support for handling Windows Socket errors * support for handling Windows Socket errors
*/ */
extern const char *winsock_strerror(int eno); extern const char *winsock_strerror(int err, char *strerrbuf, size_t buflen);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment