]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/main.c
Recommit NT service changes (untested) with changes to resolve
[openldap] / servers / slapd / main.c
index 6a96d847c0ef62c34284b30db813cbd1fa9110f7..8a42a012eab5362c3c1e2e8300cb42b875fd3dff 100644 (file)
 
 #include "ldap_defaults.h"
 #include "slap.h"
+#ifndef HAVE_WINSOCK
 #include "lutil.h"                     /* Get lutil_detach() */
+#endif
 
 #ifdef LDAP_SIGCHLD
 static RETSIGTYPE wait4child( int sig );
 #endif
 
+#ifdef HAVE_WINSOCK
+#define SERVICE_NAME "OpenLDAP"
+
+struct sockaddr_in     bind_addr;
+
+// in nt_main.c
+extern SERVICE_STATUS                  SLAPDServiceStatus;
+extern 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, short port, int udp );
+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; \
+} 
+#else
+#define SERVICE_EXIT( e, n )
+#endif
+
+short port = LDAP_PORT;
 /*
  * 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
@@ -62,7 +94,7 @@ static void
 usage( char *name )
 {
        fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
-    fprintf( stderr, "\n        [-a bind-address] [-i]" );
+    fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
 #if LDAP_CONNECTIONLESS
        fprintf( stderr, " [-c]" );
 #endif
@@ -80,39 +112,77 @@ usage( char *name )
 
 time_t starttime;
 struct sockaddr_in     bind_addr;
+int tcps;
 
-int
-main( int argc, char **argv )
+#ifdef HAVE_WINSOCK
+void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
+{
+#else
+int main( int argc, char **argv )
 {
+#endif
+
        int             i;
        int             inetd = 0;
        int             rc;
-       int             tcps;
-#ifdef LDAP_CONNECTIONLESS
+       struct slapd_args args;
        int             udp;
+#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
+       char *username = NULL;
+       char *groupname = NULL;
 #endif
 #ifdef LOG_LOCAL4
     int     syslogUser = DEFAULT_SYSLOG_USER;
 #endif
-#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
-       char            *username = NULL, *groupname = NULL;
+#ifdef HAVE_WINSOCK
+       char            *configfile = ".\\slapd.conf";
+#else
+       char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
 #endif
-       char            *configfile;
        char        *serverName;
        int         serverMode = SLAP_SERVER_MODE;
 
-       configfile = SLAPD_DEFAULT_CONFIGFILE;
-
        (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
        bind_addr.sin_family = AF_INET;
        bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-       bind_addr.sin_port = htons(LDAP_PORT);
+       bind_addr.sin_port = htons(port);
 
        g_argc = argc;
        g_argv = argv;
 
+#ifdef HAVE_WINSOCK
+       //if ( is_NT_Service )
+       {
+               int *newPort;
+               int *newDebugLevel;
+               char *newConfigFile;
+               ldap_debug = 0xffff;
+               if ( is_NT_Service ) CommenceStartupProcessing( SERVICE_NAME, slap_set_shutdown );
+               newPort = (int*)getRegParam( NULL, "Port" );
+               if ( newPort != NULL )
+               {
+                       port = *newPort;
+                       bind_addr.sin_port = htons(port);
+                       Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
+               }
+               newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
+               if ( newDebugLevel != NULL ) 
+               {
+                       slap_debug = *newDebugLevel;
+                       Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
+               }
+               newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
+               if ( newConfigFile != NULL ) 
+               {
+                       configfile = newConfigFile;
+                       Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
+               }
+       }
+               
+#endif
+
        while ( (i = getopt( argc, argv,
-                            "d:f:ia:p:s:"
+                            "d:f:ia:p:s:u"
 #ifdef LOG_LOCAL4
                             "l:"
 #endif
@@ -189,7 +259,7 @@ main( int argc, char **argv )
                        break;
 
                case 'p': {     /* port on which to listen */
-                               short port = (short)atoi( optarg );
+                               port = (short)atoi( optarg );
                                if(! port ) {
                                        fprintf(stderr, "-p %s must be numeric\n", optarg);
                                } else {
@@ -234,7 +304,9 @@ main( int argc, char **argv )
 
                default:
                        usage( argv[0] );
-                       exit( 1 );
+                       rc = 1;
+                       SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
+                       goto stop;
                }
        }
 
@@ -256,8 +328,6 @@ main( int argc, char **argv )
        openlog( serverName, OPENLOG_OPTIONS );
 #endif
 
-       tcps = set_socket( inetd ? NULL : &bind_addr );
-
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
        if ( username != NULL || groupname != NULL )
                slap_init_user( username, groupname );
@@ -265,14 +335,19 @@ main( int argc, char **argv )
 
        if ( slap_init( serverMode, serverName ) != 0 ) {
                rc = 1;
+               SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
                goto destroy;
        }
 
        if ( read_config( configfile ) != 0 ) {
                rc = 1;
+               SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
                goto destroy;
        }
 
+
+       tcps = set_socket( inetd ? NULL : &bind_addr );
+
        (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
        (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
 #ifdef SIGPIPE
@@ -286,8 +361,12 @@ main( int argc, char **argv )
 #ifdef LDAP_SIGCHLD
        (void) SIGNAL( LDAP_SIGCHLD, wait4child );
 #endif
+#ifdef HAVE_WINSOCK
+       // SIGBREAK is generated when Ctrl-Break is pressed.
+       (void) SIGNAL( SIGBREAK, slap_set_shutdown );
+#endif
 
-#ifndef WIN32
+#ifndef HAVE_WINSOCK
        if(!inetd) {
 #ifdef LDAP_DEBUG
                lutil_detach( ldap_debug, 0 );
@@ -295,16 +374,19 @@ main( int argc, char **argv )
                lutil_detach( 0, 0 );
 #endif
        }
-#endif /* WIN32 */
+#endif /* HAVE_WINSOC */
 
        if ( slap_startup(-1)  != 0 ) {
                rc = 1;
+               SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
                goto shutdown;
        }
 
        if(!inetd) {
                FILE *fp;
 
+               args.addr = &bind_addr;
+
                Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
 
                if (( slapd_pid_file != NULL ) &&
@@ -323,11 +405,25 @@ main( int argc, char **argv )
                        fprintf( fp, "\n" );
                        fclose( fp );
                }
+
+       } else {
+               args.addr = NULL;
        }
+       args.tcps = tcps;
 
        time( &starttime );
+#ifdef HAVE_WINSOCK
+       LogSlapdStartedEvent( SERVICE_NAME, slap_debug, configfile, port, udp );
+#endif
+
+       rc = slapd_daemon( &args );
+
+#ifdef HAVE_WINSOCK
+       // Throw away the event that we used during the startup process.
+       if ( is_NT_Service )
+               ldap_pvt_thread_cond_destroy( &started_event );
+#endif
 
-       rc = slapd_daemon( inetd, tcps );
 
 shutdown:
        /* remember an error during shutdown */
@@ -336,9 +432,16 @@ destroy:
        /* remember an error during destroy */
        rc |= slap_destroy();
 
+stop:
+#ifdef HAVE_WINSOCK
+       LogSlapdStoppedEvent( SERVICE_NAME );
+#endif
        Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
+#ifdef HAVE_WINSOCK
+       ReportSlapdShutdownComplete();
+#endif
 
-       closelog();
+    closelog();
 
        return rc;
 }