00001
00002 #include <string.h>
00003 #include "irodsntutil.h"
00004 #include <io.h>
00005 #include <fcntl.h>
00006 #include <windows.h>
00007 #include <setjmp.h>
00008
00009 extern int irodsWinMain(int ac, char **av);
00010 int irodsNtRunService(int ac, char **av);
00011 char irods_service_name[1024];
00012
00013 int main(int argc, char **argv)
00014 {
00015 int LocalStatus;
00016
00017 iRODSNtServerCheckExecMode(argc, argv);
00018 iRODSNtSetServerHomeDir(argv[0]);
00019
00020 if(iRODSNtServerRunningConsoleMode())
00021 {
00022 LocalStatus = irodsWinMain(argc, argv);
00023 }
00024 else
00025 {
00026 if(iRODSNtGetServiceName(irods_service_name) < 0)
00027 {
00028 exit(0);
00029 }
00030 LocalStatus = irodsNtRunService(argc, argv);
00031 }
00032 return LocalStatus;
00033 }
00034
00035
00036 SERVICE_STATUS ssStatus;
00037 SERVICE_STATUS_HANDLE sshStatusHandle;
00038 DWORD dwErr = 0;
00039 BOOL bDebug = FALSE;
00040 int aArgc;
00041 char **aArgv;
00042
00043 BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
00044 DWORD dwWin32ExitCode,
00045 DWORD dwWaitHint)
00046 {
00047 static DWORD dwCheckPoint = 1;
00048
00049 if(bDebug)
00050 return TRUE;
00051
00052 if (dwCurrentState == SERVICE_START_PENDING)
00053 ssStatus.dwControlsAccepted = 0;
00054 else
00055 ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
00056
00057 ssStatus.dwCurrentState = dwCurrentState;
00058 ssStatus.dwWin32ExitCode = dwWin32ExitCode;
00059 ssStatus.dwWaitHint = dwWaitHint;
00060
00061 if ( ( dwCurrentState == SERVICE_RUNNING ) ||
00062 ( dwCurrentState == SERVICE_STOPPED ) )
00063 ssStatus.dwCheckPoint = 0;
00064 else
00065 ssStatus.dwCheckPoint = dwCheckPoint++;
00066
00067
00068 return SetServiceStatus( sshStatusHandle, &ssStatus);
00069 }
00070
00071 VOID ServiceStop()
00072 {
00073 (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
00074 }
00075
00076 VOID WINAPI service_ctrl(DWORD dwCtrlCode)
00077 {
00078
00079 switch(dwCtrlCode)
00080 {
00081
00082
00083
00084
00085
00086
00087
00088 case SERVICE_CONTROL_STOP:
00089 ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
00090 ServiceStop();
00091 return;
00092
00093
00094
00095 case SERVICE_CONTROL_INTERROGATE:
00096 break;
00097
00098
00099
00100 default:
00101 break;
00102
00103 }
00104
00105 ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
00106 }
00107
00108 int irodsNtStartService()
00109 {
00110 int LocalStatus;
00111
00112 if (!ReportStatusToSCMgr(SERVICE_START_PENDING,NO_ERROR,3000))
00113 {
00114 (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
00115 return -1;
00116 }
00117
00118
00119 if (!ReportStatusToSCMgr(SERVICE_RUNNING,NO_ERROR,0))
00120 {
00121 (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
00122 return -1;
00123 }
00124
00125
00126
00127
00128 LocalStatus = irodsWinMain(aArgc,aArgv);
00129
00130 fprintf(stderr,"irodsWinMain exit, status = %d\n", LocalStatus);
00131
00132 return 0;
00133 }
00134
00135 void irodsNtServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
00136 {
00137 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
00138 ssStatus.dwServiceSpecificExitCode = 0;
00139
00140 sshStatusHandle = RegisterServiceCtrlHandler(irods_service_name, service_ctrl);
00141
00142 if(!sshStatusHandle)
00143 return;
00144
00145 if (!ReportStatusToSCMgr(SERVICE_START_PENDING,NO_ERROR,3000))
00146 {
00147 (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
00148 return;
00149 }
00150
00151 (VOID)irodsNtStartService();
00152 }
00153
00154 int irodsNtRunService(int aargc, char **aargv)
00155 {
00156 SERVICE_TABLE_ENTRY dispatchTable[] =
00157 {
00158 { irods_service_name, (LPSERVICE_MAIN_FUNCTION)irodsNtServiceMain},
00159 { NULL, NULL }
00160 };
00161
00162 aArgc = aargc;
00163 aArgv = aargv;
00164
00165 if (!StartServiceCtrlDispatcher(dispatchTable))
00166 {
00167 fprintf(stderr,"Windows failed to dispatch the service, '%s'. Make sure the service is installed.\n", irods_service_name);
00168 return -1;
00169 }
00170
00171 return 0;
00172 }