Back to index

openldap  2.4.31
nt_svc.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 #include <stdio.h>
00018 #include <ac/string.h>
00019 #include "slap.h"
00020 #include "lutil.h"
00021 
00022 #ifdef HAVE_NT_SERVICE_MANAGER
00023 
00024 /* in main.c */
00025 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
00026 
00027 /* in ntservice.c */
00028 int main( int argc, LPTSTR *argv )
00029 {
00030        int           length;
00031        char   filename[MAX_PATH], *fname_start;
00032 
00033        /*
00034         * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
00035         * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
00036         * ignored.
00037         */
00038 
00039        SERVICE_TABLE_ENTRY         DispatchTable[] = {
00040               {      "",    (LPSERVICE_MAIN_FUNCTION) ServiceMain     },
00041               {      NULL,                NULL   }
00042        };
00043 
00044        /*
00045         * set the service's current directory to the installation directory
00046         * for the service. this way we don't have to write absolute paths
00047         * in the configuration files
00048         */
00049        GetModuleFileName( NULL, filename, sizeof( filename ) );
00050        fname_start = strrchr( filename, *LDAP_DIRSEP );
00051 
00052        if ( argc > 1 ) {
00053               if ( _stricmp( "install", argv[1] ) == 0 ) 
00054               {
00055                      char *svcName = SERVICE_NAME;
00056                      char *displayName = "OpenLDAP Directory Service";
00057                      BOOL auto_start = FALSE;
00058 
00059                      if ( (argc > 2) && (argv[2] != NULL) )
00060                             svcName = argv[2];
00061 
00062                      if ( argc > 3 && argv[3])
00063                             displayName = argv[3];
00064 
00065                      if ( argc > 4 && stricmp(argv[4], "auto") == 0)
00066                             auto_start = TRUE;
00067 
00068                      strcat(filename, " service");
00069                      if ( !lutil_srv_install(svcName, displayName, filename, auto_start) ) 
00070                      {
00071                             fputs( "service failed installation ...\n", stderr  );
00072                             return EXIT_FAILURE;
00073                      }
00074                      fputs( "service has been installed ...\n", stderr  );
00075                      return EXIT_SUCCESS;
00076               }
00077 
00078               if ( _stricmp( "remove", argv[1] ) == 0 ) 
00079               {
00080                      char *svcName = SERVICE_NAME;
00081                      if ( (argc > 2) && (argv[2] != NULL) )
00082                             svcName = argv[2];
00083                      if ( !lutil_srv_remove(svcName, filename) ) 
00084                      {
00085                             fputs( "failed to remove the service ...\n", stderr  );
00086                             return EXIT_FAILURE;
00087                      }
00088                      fputs( "service has been removed ...\n", stderr );
00089                      return EXIT_SUCCESS;
00090               }
00091               if ( _stricmp( "service", argv[1] ) == 0 )
00092               {
00093                      is_NT_Service = 1;
00094                      *fname_start = '\0';
00095                      SetCurrentDirectory( filename );
00096               }
00097        }
00098 
00099        if (is_NT_Service)
00100        {
00101               StartServiceCtrlDispatcher(DispatchTable);
00102        } else
00103        {
00104               ServiceMain( argc, argv );
00105        }
00106 
00107        return EXIT_SUCCESS;
00108 }
00109 
00110 #endif