]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
make sure NULL pointers are not dereferenced
[openldap] / servers / slapd / daemon.c
index 0e9f220b707700048235e79ec516447982c8fff9..4e93170bc1eb0b5527b300b455df6d92f55c1d54 100644 (file)
@@ -339,9 +339,13 @@ static struct slap_daemon {
 static char** slapd_srvurls = NULL;
 static SLPHandle slapd_hslp = 0;
 int slapd_register_slp = 0;
+char *slapd_slp_attrs = NULL;
+
+static SLPError slapd_slp_cookie;
 
 void slapd_slp_init( const char* urls ) {
        int i;
+       SLPError err;
 
        slapd_srvurls = ldap_str2charray( urls, " " );
 
@@ -376,7 +380,12 @@ void slapd_slp_init( const char* urls ) {
        }
 
        /* open the SLP handle */
-       SLPOpen( "en", 0, &slapd_hslp );
+       err = SLPOpen( "en", 0, &slapd_hslp );
+
+       if (err != SLP_OK) {
+               Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
+                       (long)err, 0, 0 );
+       }
 }
 
 void slapd_slp_deinit() {
@@ -394,11 +403,13 @@ void slapd_slp_regreport(
        SLPError errcode,
        void* cookie )
 {
-       /* empty report */
+       /* return the error code in the cookie */
+       *(SLPError*)cookie = errcode; 
 }
 
 void slapd_slp_reg() {
        int i;
+       SLPError err;
 
        if( slapd_srvurls == NULL ) return;
 
@@ -408,28 +419,41 @@ void slapd_slp_reg() {
                    strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
                                sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
                {
-                       SLPReg( slapd_hslp,
+                       err = SLPReg( slapd_hslp,
                                slapd_srvurls[i],
                                SLP_LIFETIME_MAXIMUM,
                                "ldap",
-                               "",
-                               1,
+                                       (slapd_slp_attrs) ? slapd_slp_attrs : "",
+                                       SLP_TRUE,
                                slapd_slp_regreport,
-                               NULL );
+                                       &slapd_slp_cookie );
+
+                       if (err != SLP_OK || slapd_slp_cookie != SLP_OK) {
+                               Debug( LDAP_DEBUG_CONNS,
+                                       "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
+                                       slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
+                       }       
                }
        }
 }
 
 void slapd_slp_dereg() {
        int i;
+       SLPError err;
 
        if( slapd_srvurls == NULL ) return;
 
        for( i=0; slapd_srvurls[i] != NULL; i++ ) {
-               SLPDereg( slapd_hslp,
+               err = SLPDereg( slapd_hslp,
                        slapd_srvurls[i],
                        slapd_slp_regreport,
-                       NULL );
+                               &slapd_slp_cookie );
+               
+               if (err != SLP_OK || slapd_slp_cookie != SLP_OK) {
+                       Debug( LDAP_DEBUG_CONNS,
+                               "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
+                               slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
+               }
        }
 }
 #endif /* HAVE_SLP */
@@ -1121,6 +1145,12 @@ int slapd_daemon_init( const char *urls )
 
        Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
                urls ? urls : "<null>", 0, 0 );
+
+       ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
+#ifdef HAVE_TCPD
+       ldap_pvt_thread_mutex_init( &slap_daemon.tcpd_mutex );
+#endif
+
        if( (rc = sockinit()) != 0 ) return rc;
 
 #ifdef HAVE_SYSCONF
@@ -1151,6 +1181,8 @@ int slapd_daemon_init( const char *urls )
        if( u == NULL || u[0] == NULL ) {
                Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
                        urls, 0, 0 );
+               if ( u )
+                       ldap_charray_free( u );
                return -1;
        }
 
@@ -1190,11 +1222,6 @@ int slapd_daemon_init( const char *urls )
 #endif
 
        ldap_charray_free( u );
-       ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
-
-#ifdef HAVE_TCPD
-       ldap_pvt_thread_mutex_init( &slap_daemon.tcpd_mutex );
-#endif
 
        return !i;
 }
@@ -1702,7 +1729,7 @@ slapd_daemon_task(
                struct timeval          tv;
                struct timeval          *tvp;
 
-               struct timeval          *cat;
+               struct timeval          cat;
                time_t                          tdelta = 1;
                struct re_s*            rtask;
                now = slap_get_time();
@@ -1783,7 +1810,7 @@ slapd_daemon_task(
 
                ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
                rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
-               while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
+               while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
                        if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
                                ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
                        } else {
@@ -1791,15 +1818,15 @@ slapd_daemon_task(
                                ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
                                ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
                                ldap_pvt_thread_pool_submit( &connection_pool,
-                                                                                       rtask->routine, (void *) rtask );
+                                       rtask->routine, (void *) rtask );
                                ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
                        }
                        rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
                }
                ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
 
-               if ( cat && cat->tv_sec ) {
-                       time_t diff = difftime( cat->tv_sec, now );
+               if ( rtask && cat.tv_sec ) {
+                       time_t diff = difftime( cat.tv_sec, now );
                        if ( diff == 0 ) diff = tdelta;
                        if ( tvp == NULL || diff < tv.tv_sec ) {
                                tv.tv_sec = diff;
@@ -1840,21 +1867,17 @@ slapd_daemon_task(
                case -1: {      /* failure - try again */
                                int err = sock_errno();
 
-                               if( err == EBADF
-#ifdef WSAENOTSOCK
-                                       /* you'd think this would be EBADF */
-                                       || err == WSAENOTSOCK
-#endif
-                               ) {
-                                       if (++ebadf < SLAPD_EBADF_LIMIT)
-                                               continue;
-                               }
-
                                if( err != EINTR ) {
-                                       Debug( LDAP_DEBUG_ANY,
-                                               "daemon: select failed (%d): %s\n",
-                                               err, sock_errstr(err), 0 );
-                                       slapd_shutdown = 2;
+                                       ebadf++;
+
+                                       /* Don't log unless we got it twice in a row */
+                                       if ( !( ebadf & 1 )) {
+                                               Debug( LDAP_DEBUG_ANY,
+                                                       "daemon: select failed count %d err (%d): %s\n",
+                                                       ebadf, err, sock_errstr(err) );
+                                       }
+                                       if ( ebadf >= SLAPD_EBADF_LIMIT )
+                                               slapd_shutdown = 2;
                                }
                        }
                        continue;
@@ -1980,9 +2003,6 @@ slapd_daemon_task(
                                "daemon: write active on %d\n",
                                wd, 0, 0 );
 
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-                       connection_write_activate( wd );
-#else
                        /*
                         * NOTE: it is possible that the connection was closed
                         * and that the stream is now inactive.
@@ -1998,7 +2018,6 @@ slapd_daemon_task(
                                        nrfds--;
                                }
                        }
-#endif
                }
 
                for ( i = 0; nrfds > 0; i++ ) {
@@ -2074,7 +2093,7 @@ slapd_daemon_task(
 #endif
 
                for (i=0; i<ns; i++) {
-                       int rc = 1, fd;
+                       int rc = 1, fd, waswrite = 0;
 
                        if ( SLAP_EVENT_IS_LISTENER(i) ) {
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
@@ -2104,9 +2123,8 @@ slapd_daemon_task(
                                                "daemon: write active on %d\n",
                                                fd, 0, 0 );
 
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-                                       connection_write_activate( fd );
-#else
+                                       waswrite = 1;
+
                                        /*
                                         * NOTE: it is possible that the connection was closed
                                         * and that the stream is now inactive.
@@ -2116,9 +2134,9 @@ slapd_daemon_task(
                                        if ( connection_write( fd ) < 0 ) {
                                                continue;
                                        }
-#endif
                                }
-                               if( SLAP_EVENT_IS_READ( i ) ) {
+                               /* If event is a read or an error */
+                               if( SLAP_EVENT_IS_READ( i ) || !waswrite ) {
                                        Debug( LDAP_DEBUG_CONNS,
                                                "daemon: read active on %d\n",
                                                fd, 0, 0 );