]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/main.c
Per ITS#419, don't require SLAPD_RLOOKUPS when HAVE_TCPD
[openldap] / servers / slapd / main.c
index 559c1b5dde3ee3c6bbe86af5fca24b9e041a30bb..3176411796b5336c491291ba608aa7563b0afe16 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenLDAP$ */
 /*
  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
@@ -15,7 +16,6 @@
 #include <ac/signal.h>
 #include <ac/errno.h>
 
-#include "ldap_defaults.h"
 #include "slap.h"
 #include "lutil.h"
 
 static RETSIGTYPE wait4child( int sig );
 #endif
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_SERVICE_MANAGER
 #define MAIN_RETURN(x) return
 struct sockaddr_in     bind_addr;
 
 /* in nt_main.c */
-extern SERVICE_STATUS                  SLAPDServiceStatus;
-extern SERVICE_STATUS_HANDLE   hSLAPDServiceStatus;
+LIBLUTIL_F (SERVICE_STATUS)            SLAPDServiceStatus;
+LIBLUTIL_F (SERVICE_STATUS_HANDLE)     hSLAPDServiceStatus;
 extern ldap_pvt_thread_cond_t  started_event,          stopped_event;
 extern int       is_NT_Service;
 
-void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
-void LogSlapdStoppedEvent( char *svc );
-
 void CommenceStartupProcessing( LPCTSTR serviceName,
                                                           void(*stopper)(int));
 void ReportSlapdShutdownComplete( void );
 void *getRegParam( char *svc, char *value );
 
-#define SERVICE_EXIT( e, n ) \
-               if ( is_NT_Service ) \
-{ \
-                       SLAPDServiceStatus.dwWin32ExitCode                              = e; \
-                       SLAPDServiceStatus.dwServiceSpecificExitCode    = n; \
-} 
+#define SERVICE_EXIT( e, n )   do { \
+       if ( is_NT_Service ) { \
+               SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
+               SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
+       } \
+} while ( 0 )
+
 #else
 #define SERVICE_EXIT( e, n )
 #define MAIN_RETURN(x) return(x)
 #endif
 
+#ifdef HAVE_NT_EVENT_LOG
+void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
+void LogSlapdStoppedEvent( char *svc );
+#endif
+
 /*
  * when more than one slapd is running on one machine, each one might have
  * it's own LOCAL for syslogging and must have its own pid/args files
@@ -75,15 +78,15 @@ typedef struct _str2intDispatch {
 
 /* table to compute syslog-options to integer */
 static STRDISP  syslog_types[] = {
-    { "LOCAL0",         6, LOG_LOCAL0 },
-    { "LOCAL1",         6, LOG_LOCAL1 },
-    { "LOCAL2",         6, LOG_LOCAL2 },
-    { "LOCAL3",         6, LOG_LOCAL3 },
-    { "LOCAL4",         6, LOG_LOCAL4 },
-    { "LOCAL5",         6, LOG_LOCAL5 },
-    { "LOCAL6",         6, LOG_LOCAL6 },
-    { "LOCAL7",         6, LOG_LOCAL7 },
-    { NULL }
+       { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
+       { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
+       { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
+       { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
+       { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
+       { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
+       { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
+       { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
+       { NULL }
 };
 
 static int   cnvt_str2int( char *, STRDISP_P, int );
@@ -105,18 +108,14 @@ usage( char *name )
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
                "\t-g group\tGroup (id or name) to ran as\n"
 #endif
-               "\t-h URLs\tList of URLs to serve"
+               "\t-h URLs\tList of URLs to serve\n"
 #ifdef LOG_LOCAL4
                "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
 #endif
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_EVENT_LOG
                "\t-n NTserviceName\tNT service name\n"
 #endif
 
-               "\t-p port\tLDAP Port\n"
-#ifdef HAVE_TLS
-               "\t-P port\tLDAP over TLS Port\n"
-#endif
                "\t-s level\tSyslog Level\n"
 #ifdef SLAPD_BDB2
                "\t-t\t\tEnable BDB2 timing\n"
@@ -127,13 +126,13 @@ usage( char *name )
     );
 }
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_SERVICE_MANAGER
 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
 #else
 int main( int argc, char **argv )
 #endif
 {
-       int             i;
+       int             i, no_detach = 0;
        int             rc;
        char *urls = NULL;
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
@@ -143,8 +142,11 @@ int main( int argc, char **argv )
 #ifdef LOG_LOCAL4
     int     syslogUser = DEFAULT_SYSLOG_USER;
 #endif
-#ifdef HAVE_WINSOCK
+
+#ifdef HAVE_NT_EVENT_LOG
        char        *NTservice  = SERVICE_NAME;
+#endif
+#ifdef HAVE_NT_SERVICE_MANAGER
        char            *configfile = ".\\slapd.conf";
 #else
        char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
@@ -152,35 +154,25 @@ int main( int argc, char **argv )
        char        *serverName;
        int         serverMode = SLAP_SERVER_MODE;
 
-       int port = LDAP_PORT;
-#ifdef HAVE_TLS
-       int tls_port = LDAPS_PORT;
-#else
-       int tls_port = 0;
+#ifdef CSRIMALLOC
+       FILE *leakfile;
+       if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
+               leakfile = stderr;
+       }
 #endif
 
        g_argc = argc;
        g_argv = argv;
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_SERVICE_MANAGER
        {
                int *i;
                char *newConfigFile;
-               if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
-               i = (int*)getRegParam( NULL, "Port" );
-               if ( i != NULL )
-               {
-                       port = *i;
-                       Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
-               }
-#ifdef HAVE_TLS
-               i = (int*)getRegParam( NULL, "TLSPort" );
-               if ( i != NULL )
-               {
-                       tls_port = *i;
-                       Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
+
+               if ( is_NT_Service ) {
+                       CommenceStartupProcessing( NTservice, slap_sig_shutdown );
                }
-#endif
+
                i = (int*)getRegParam( NULL, "DebugLevel" );
                if ( i != NULL ) 
                {
@@ -197,7 +189,7 @@ int main( int argc, char **argv )
 #endif
 
        while ( (i = getopt( argc, argv,
-                            "d:f:h:p:s:u"
+                            "d:f:h:s:"
 #ifdef LOG_LOCAL4
                             "l:"
 #endif
@@ -210,57 +202,31 @@ int main( int argc, char **argv )
 #ifdef LDAP_CONNECTIONLESS
                                 "c"
 #endif
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_EVENT_LOG
                                 "n:"
-#endif
-#ifdef HAVE_TLS
-                            "P:"
 #endif
                             )) != EOF ) {
                switch ( i ) {
                case 'h':       /* listen URLs */
+                       if ( urls != NULL ) free( urls );
                        urls = ch_strdup( optarg );
             break;
 
+               case 'd':       /* set debug level and 'do not detach' flag */
+                       no_detach = 1;
 #ifdef LDAP_DEBUG
-               case 'd':       /* turn on debugging */
                        slap_debug |= atoi( optarg );
-                       break;
 #else
-               case 'd':       /* turn on debugging */
-                       fprintf( stderr,
-                           "must compile with LDAP_DEBUG for debugging\n" );
-                       break;
+                       if ( atoi( optarg ) != 0 )
+                               fputs( "must compile with LDAP_DEBUG for debugging\n",
+                                      stderr );
 #endif
+                       break;
 
                case 'f':       /* read config file */
                        configfile = ch_strdup( optarg );
                        break;
 
-               case 'p': {     /* port on which to listen */
-                               int p = atoi( optarg );
-                               if(! p ) {
-                                       fprintf(stderr, "-p %s must be numeric\n", optarg);
-                               } else if( p < 0 || p >= 1<<16) {
-                                       fprintf(stderr, "-p %s invalid\n", optarg);
-                               } else {
-                                       port = p;
-                               }
-                       } break;
-
-#ifdef HAVE_TLS
-               case 'P': {     /* port on which to listen for TLS */
-                               int p = atoi( optarg );
-                               if(! p ) {
-                                       fprintf(stderr, "-P %s must be numeric\n", optarg);
-                               } else if( p < 0 || p >= 1<<16) {
-                                       fprintf(stderr, "-P %s invalid\n", optarg);
-                               } else {
-                                       tls_port = p;
-                               }
-                       } break;
-#endif
-
                case 's':       /* set syslog level */
                        ldap_syslog = atoi( optarg );
                        break;
@@ -282,7 +248,7 @@ int main( int argc, char **argv )
 
 #ifdef SLAPD_BDB2
                case 't':  /* timed server */
-                       serverMode = SLAP_TIMEDSERVER_MODE;
+                       serverMode |= SLAP_TIMED_MODE;
                        break;
 #endif
 
@@ -298,7 +264,7 @@ int main( int argc, char **argv )
                        break;
 #endif /* SETUID && GETUID */
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_EVENT_LOG
                case 'n':  /* NT service name */
                        NTservice = ch_strdup( optarg );
                        break;
@@ -325,11 +291,11 @@ int main( int argc, char **argv )
 
 #ifdef LOG_LOCAL4
        openlog( serverName, OPENLOG_OPTIONS, syslogUser );
-#else
+#elif LOG_DEBUG
        openlog( serverName, OPENLOG_OPTIONS );
 #endif
 
-       if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
+       if( slapd_daemon_init( urls ) != 0 ) {
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
                goto stop;
@@ -341,6 +307,16 @@ int main( int argc, char **argv )
        }
 #endif
 
+       extops_init();
+
+#ifdef SLAPD_MODULES
+       if ( module_init() != 0 ) {
+               rc = 1;
+               SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
+               goto destroy;
+       }
+#endif
+
        if ( slap_init( serverMode, serverName ) != 0 ) {
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
@@ -358,33 +334,34 @@ int main( int argc, char **argv )
        ldap_pvt_tls_init_def_ctx();
 #endif
 
-       (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
-       (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
+       (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
+       (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
+
 #ifdef SIGPIPE
        (void) SIGNAL( SIGPIPE, SIG_IGN );
 #endif
 #ifdef SIGHUP
-       (void) SIGNAL( SIGHUP, slap_set_shutdown );
+       (void) SIGNAL( SIGHUP, slap_sig_shutdown );
 #endif
-       (void) SIGNAL( SIGINT, slap_set_shutdown );
-       (void) SIGNAL( SIGTERM, slap_set_shutdown );
+       (void) SIGNAL( SIGINT, slap_sig_shutdown );
+       (void) SIGNAL( SIGTERM, slap_sig_shutdown );
 #ifdef LDAP_SIGCHLD
        (void) SIGNAL( LDAP_SIGCHLD, wait4child );
 #endif
 #ifdef SIGBREAK
        /* SIGBREAK is generated when Ctrl-Break is pressed. */
-       (void) SIGNAL( SIGBREAK, slap_set_shutdown );
+       (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
 #endif
 
 #ifndef HAVE_WINSOCK
-#ifdef LDAP_DEBUG
-               lutil_detach( ldap_debug, 0 );
-#else
-               lutil_detach( 0, 0 );
-#endif
+       lutil_detach( no_detach, 0 );
 #endif /* HAVE_WINSOCK */
 
-       if ( slap_startup(-1)  != 0 ) {
+#ifdef CSRIMALLOC
+       mal_leaktrace(1);
+#endif
+
+       if ( slap_startup( NULL )  != 0 ) {
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
                goto shutdown;
@@ -413,13 +390,13 @@ int main( int argc, char **argv )
                }
        }
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_EVENT_LOG
        LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
 #endif
 
        rc = slapd_daemon();
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_SERVICE_MANAGER
        /* Throw away the event that we used during the startup process. */
        if ( is_NT_Service )
                ldap_pvt_thread_cond_destroy( &started_event );
@@ -427,25 +404,38 @@ int main( int argc, char **argv )
 
 shutdown:
        /* remember an error during shutdown */
-       rc |= slap_shutdown(-1);
+       rc |= slap_shutdown( NULL );
+
 destroy:
        /* remember an error during destroy */
        rc |= slap_destroy();
 
+#ifdef SLAPD_MODULES
+       module_kill();
+#endif
+
+       extops_kill();
+
 stop:
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_EVENT_LOG
        LogSlapdStoppedEvent( NTservice );
 #endif
 
        Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
 
-#ifdef HAVE_WINSOCK
+#ifdef HAVE_NT_SERVICE_MANAGER
        ReportSlapdShutdownComplete();
 #endif
 
+#ifdef LOG_DEBUG
     closelog();
+#endif
        slapd_daemon_destroy();
 
+#ifdef CSRIMALLOC
+       mal_dumpleaktrace( leakfile );
+#endif
+
        MAIN_RETURN(rc);
 }