Bug 41141 - mod_dbd, apr_dbd_pgsql: Error looking up ... in database mod_dbd, apr_dbd_pgsql: Error looking up ... in database
Summary: mod_dbd, apr_dbd_pgsql: Error looking up ... in database mod_dbd, apr_dbd_pgs...
Status: RESOLVED FIXED
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: Other Modules (show other bugs)
Version: 2.2.3
Hardware: Sun Solaris
: P2 critical (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2006-12-10 04:26 UTC by Jens Hamisch
Modified: 2008-02-18 00:52 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jens Hamisch 2006-12-10 04:26:44 UTC
The authorization using DBD/PGSQL is broken at least in version 2.2.3.
Debugging the code I foigured out, that in dbd_setup_lock a failure
return code is generated if srv->mutex contains the NULL value. This
is incorrect, because the NULL value may also be assigned in success
cases if no mutexes are required (see: dbd_setup_init in the same
source file). I've created the following patch which works fine for
me:


*** ./modules/database/mod_dbd.c.fcs    Sun Dec 10 12:19:47 2006
--- ./modules/database/mod_dbd.c        Sun Dec 10 12:22:01 2006
***************
*** 388,416 ****
      /* several threads could be here at the same time, all trying to
       * initialize the reslist because dbd_setup_init failed to do so
       */
!     if (!svr->mutex) {
!         /* we already logged an error when the mutex couldn't be created */
!         return APR_EGENERAL;
      }

-     rv = apr_thread_mutex_lock(svr->mutex);
-     if (rv != APR_SUCCESS) {
-         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
-                       "DBD: Failed to acquire thread mutex");
-         return rv;
-     }
-
      if (!svr->dbpool) {
          rv2 = dbd_setup(s->process->pool, svr);
      }

!     rv = apr_thread_mutex_unlock(svr->mutex);
!     if (rv != APR_SUCCESS) {
!         ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
!                       "DBD: Failed to release thread mutex");
!         if (rv2 == APR_SUCCESS) {
!             rv2 = rv;
!         }
      }
      return rv2;
  }
--- 388,415 ----
      /* several threads could be here at the same time, all trying to
       * initialize the reslist because dbd_setup_init failed to do so
       */
!     if (svr->mutex) {
!       rv = apr_thread_mutex_lock(svr->mutex);
!       if (rv != APR_SUCCESS) {
!           ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
!                         "DBD: Failed to acquire thread mutex");
!           return rv;
!       }
      }

      if (!svr->dbpool) {
          rv2 = dbd_setup(s->process->pool, svr);
      }

!     if (svr->mutex) {
!       rv = apr_thread_mutex_unlock(svr->mutex);
!       if (rv != APR_SUCCESS) {
!           ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, pool,
!                         "DBD: Failed to release thread mutex");
!           if (rv2 == APR_SUCCESS) {
!               rv2 = rv;
!           }
!       }
      }
      return rv2;
  }
Comment 1 Nick Kew 2006-12-10 05:50:56 UTC
dbd_setup_lock is only ever called if dbd_setup_init failed at server
initialisation.  That's basically so it can recover if the database was down at
apache startup time.

Can you explain how a problem arises for you?  Is it after the database has gone
down and been restarted?

BTW, there may be a bug, but your patch is definitely broken, as it's not
thread-safe.
Comment 2 Jens Hamisch 2006-12-10 23:45:40 UTC
Hi,

psql authentication is working properly in apache 2.2.2.
Because of difficulties with php 5.2.0 - they finally boiled down
to another apache problem issue 41142 - I upgraded to version 2.2.3.

It turned out, that in version 2.2.3 apache was not able any more to
authenticate against the PostgreSQL database. Using a debugger I 
figured out, that

    dbd_setup_init did *not* fail. dbd_setup returns APR_SUCESS. This
        return value is returned by dbd_setup_init in the second if
        statement.

    Therefor srv->mutex was still NULL

    dbd_setup_lock was called afterwards. It failed because of srv->mutex.

The postgres database was setup when apache 2.2.2 was running. It was
not modified since then. After applying my patch, apache including the
authentication seemd ok for me again.

I had another look at the code right now. I turns out, that dbd_setup_lock
is called if svr->dbpool is NULL. However db_setup returns APR_SUCCESS,
thus apr_reslist_create obviously returns APR_SUCCESS. The later function
for my understanding should setup svr->dbpool. And it does so. 

So at the moment I'm stucked ... the only explanation would be some side
effect overriding svr->dbpool ...

Jens

    
Comment 3 Nick Kew 2006-12-11 02:59:37 UTC
Ah, OK.  I think this is probably a side-effect of the fact that inheritance
between the main server config and <VirtualHost>s isn't working correctly.

Can you test-drive it with mod_dbd from /trunk/ ?  That would be useful.
Comment 4 Jens Hamisch 2006-12-11 06:03:13 UTC
(In reply to comment #3)
> Ah, OK.  I think this is probably a side-effect of the fact that inheritance
> between the main server config and <VirtualHost>s isn't working correctly.
> 
> Can you test-drive it with mod_dbd from /trunk/ ?  That would be useful.


I may try that. Could you please point me to the trunk download page
or send the according files via mail to me? I had a look at the apache
home page but did not find any link to the development zone ... maybe
I'm too blind to see it ;-)
Comment 5 Ruediger Pluem 2006-12-11 13:18:30 UTC
As you only need to exchange the file mod_dbd.c 
http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/database/mod_dbd.c?view=co
should be of sufficient help. For more please have a look at the "Source
Repository Information" section of http://httpd.apache.org/dev/.
Comment 6 Jens Hamisch 2006-12-12 13:17:24 UTC
Hi,

I've tried the patch from issue 39985 and the mod_dbd.c
trunk verion now. Both do not work.

The development tree and all information about how I built
and configured apache are on one of my public servers now.
There's also a compiler and debugger installed.

I may grant access to the server to you to have a look at
the situation ... if this would be an approach pls. contact
me personally by mail for the login data.

Jens
Comment 7 Nick Kew 2007-09-10 04:36:14 UTC
2.2.6 has some major mod_dbd fixes.  Does it fix this problem for you?
Comment 8 Nick Kew 2008-02-18 00:52:15 UTC
No response since Comment 7; assuming fixed.