*** auth_pgsql.c.orig mar jul 15 01:31:20 2003 --- auth_pgsql.c mer nov 30 15:34:14 2005 *************** *** 150,155 **** --- 150,179 ---- * debugger. */ PGconn *pg_conn; + PGresult *do_query(PGconn *pg_conn, const char *query) { + int conn_status; + int loop=0; + PGresult *res=NULL; + + + while ((conn_status = (pg_conn ? PQstatus(pg_conn) : CONNECTION_BAD)) + == CONNECTION_BAD) { + PQreset(pg_conn); + if (!pg_conn || (conn_status=PQstatus(pg_conn)) == CONNECTION_BAD) { + sleep(10); + auth_pgsql_init(); + loop++; + if (loop == 5) + break; + } + } + if (loop == 5) + log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s) giving up after %d times", PQerrorMessage(pg_conn),5); + else + res=PQexec(pg_conn,query); + return res; + } + int auth_pgsql_init(void) { char *username = NULL, *password = NULL, *hostname = NULL, *database = NULL, *localhost = "localhost", *s; char *dbconnect = NULL; *************** *** 219,231 **** password ? password : "" ); ! pg_conn = PQconnectdb(dbconnect); ! if (PQstatus(pg_conn) == CONNECTION_BAD) { log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s)", PQerrorMessage(pg_conn)); ! goto fail; ! } ! ret = 1; fail: if (username) strclr(username); --- 243,255 ---- password ? password : "" ); ! pg_conn = PQconnectdb(dbconnect); ! if (PQstatus(pg_conn) == CONNECTION_BAD) { log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s)", PQerrorMessage(pg_conn)); ! goto fail; ! } ! ret=1; fail: if (username) strclr(username); *************** *** 259,265 **** if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_apop: SQL query: %s", query); ! if ((res = PQexec(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { --- 283,289 ---- if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_apop: SQL query: %s", query); ! if ((res = do_query(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { *************** *** 353,359 **** if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_user_pass: SQL query: %s", query); ! if ((res = PQexec(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { --- 377,383 ---- if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_user_pass: SQL query: %s", query); ! if ((res = do_query(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { *************** *** 443,449 **** if (verbose) log_print(LOG_DEBUG, _("auth_pgsql_onlogin: SQL query: %s"), query); ! if ((res = PQexec(pg_conn, query))) { /* XXX transactions? */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_print(LOG_ERR, "auth_pgsql_onlogin: bad status after PQexec: %s", pg_strerror(pg_conn)); } else if (PQntuples(res)) { --- 467,473 ---- if (verbose) log_print(LOG_DEBUG, _("auth_pgsql_onlogin: SQL query: %s"), query); ! if ((res = do_query(pg_conn, query))) { /* XXX transactions? */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_print(LOG_ERR, "auth_pgsql_onlogin: bad status after PQexec: %s", pg_strerror(pg_conn)); } else if (PQntuples(res)) {