00001
00002
00003 #include "ImR_Locator_i.h"
00004 #include "Locator_NT_Service.h"
00005 #include "Locator_Options.h"
00006 #include "orbsvcs/Shutdown_Utilities.h"
00007
00008 class ImR_Locator_Shutdown : public Shutdown_Functor
00009 {
00010 public:
00011 ImR_Locator_Shutdown (ImR_Locator_i& imr);
00012
00013 void operator() (int which_signal);
00014 private:
00015 ImR_Locator_i& imr_;
00016 };
00017
00018 ImR_Locator_Shutdown::ImR_Locator_Shutdown (ImR_Locator_i &imr)
00019 : imr_(imr)
00020 {
00021 }
00022
00023 void
00024 ImR_Locator_Shutdown::operator () (int )
00025 {
00026 ACE_DECLARE_NEW_CORBA_ENV;
00027 ACE_TRY
00028 {
00029 this->imr_.shutdown (true ACE_ENV_ARG_PARAMETER);
00030 ACE_TRY_CHECK;
00031 }
00032 ACE_CATCHANY
00033 {
00034 ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "ImR: ");
00035 }
00036 ACE_ENDTRY;
00037 }
00038
00039 int
00040 run_standalone (Options& opts)
00041 {
00042 ImR_Locator_i server;
00043
00044 ImR_Locator_Shutdown killer (server);
00045 Service_Shutdown kill_contractor(killer);
00046
00047 ACE_DECLARE_NEW_CORBA_ENV;
00048 ACE_TRY
00049 {
00050 int status = server.init (opts ACE_ENV_ARG_PARAMETER);
00051 ACE_TRY_CHECK;
00052 if (status == -1)
00053 {
00054 return 1;
00055 }
00056 else
00057 {
00058
00059 server.run (ACE_ENV_SINGLE_ARG_PARAMETER);
00060 ACE_TRY_CHECK;
00061
00062
00063 status = server.fini (ACE_ENV_SINGLE_ARG_PARAMETER);
00064 ACE_TRY_CHECK;
00065
00066 if (status == -1)
00067 return 1;
00068 }
00069 return 0;
00070 }
00071 ACE_CATCH (CORBA::SystemException, sysex)
00072 {
00073 ACE_PRINT_EXCEPTION (sysex, "System Exception");
00074 }
00075 ACE_CATCH (CORBA::UserException, userex)
00076 {
00077 ACE_PRINT_EXCEPTION (userex, "User Exception");
00078 }
00079 ACE_CATCHANY
00080 {
00081 ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Unknown Exception");
00082 }
00083 ACE_ENDTRY;
00084
00085 return 1;
00086 }
00087
00088 #if defined (ACE_WIN32)
00089 ACE_NT_SERVICE_DEFINE (service, Locator_NT_Service, IMR_LOCATOR_SERVICE_NAME);
00090 #endif
00091
00092 int
00093 run_service (void)
00094 {
00095 #if defined (ACE_WIN32)
00096
00097 SERVICE::instance ()->name (IMR_LOCATOR_SERVICE_NAME, IMR_LOCATOR_DISPLAY_NAME);
00098
00099 ACE_NT_SERVICE_RUN (service, SERVICE::instance (), ret);
00100
00101 if (ret == 0)
00102 ACE_ERROR ((LM_ERROR, "%p\n", "Couldn't start service"));
00103
00104 return ret;
00105 #else
00106 return 1;
00107 #endif
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 static int
00117 run_service_command (Options& opts)
00118 {
00119 if (opts.service_command () == Options::SC_NONE)
00120 return 0;
00121
00122 #if defined (ACE_WIN32)
00123 SERVICE::instance()->name (IMR_LOCATOR_SERVICE_NAME, IMR_LOCATOR_DISPLAY_NAME);
00124
00125 if (opts.service_command () == Options::SC_INSTALL)
00126 {
00127 const DWORD MAX_PATH_LENGTH = 4096;
00128 char pathname[MAX_PATH_LENGTH];
00129
00130 DWORD length = ACE_TEXT_GetModuleFileName (NULL, pathname, MAX_PATH_LENGTH);
00131 if (length == 0 || length >= MAX_PATH_LENGTH - sizeof(" -s"))
00132 {
00133 ACE_ERROR ((LM_ERROR, "Error: Could not get module file name\n"));
00134 return -1;
00135 }
00136
00137
00138 ACE_OS::strcat (pathname, ACE_TEXT (" -s"));
00139
00140 int ret = SERVICE::instance ()->insert (SERVICE_DEMAND_START,
00141 SERVICE_ERROR_NORMAL,
00142 pathname);
00143 if (ret != -1)
00144 {
00145 ACE_DEBUG ((LM_DEBUG, "ImR: Service installed.\n"));
00146 opts.save_registry_options ();
00147 }
00148 else
00149 {
00150 ACE_ERROR ((LM_ERROR, "Error: Failed to install service. error:%d\n", errno));
00151 }
00152 if (ret == 0)
00153 return 1;
00154 }
00155 else if (opts.service_command () == Options::SC_REMOVE)
00156 {
00157 int ret = SERVICE::instance ()->remove ();
00158 ACE_DEBUG ((LM_DEBUG, "ImR: Service removed.\n"));
00159 if (ret == 0)
00160 return 1;
00161 }
00162 else
00163 {
00164 ACE_ERROR ((LM_ERROR, "Error: Unknown service command :%d \n",
00165 opts.service_command ()));
00166 return -1;
00167 }
00168
00169 return -1;
00170
00171 #else
00172 ACE_ERROR ((LM_ERROR, "NT Service not supported on this platform"));
00173 return -1;
00174 #endif
00175 }
00176
00177 int
00178 main (int argc, char *argv[])
00179 {
00180 Options opts;
00181
00182 int result = opts.init (argc, argv);
00183 if (result < 0)
00184 return 1;
00185 else if (result > 0)
00186 return 0;
00187
00188 result = run_service_command(opts);
00189 if (result < 0)
00190 return 1;
00191 else if (result > 0)
00192 return 0;
00193
00194 if (opts.service ())
00195 {
00196 return run_service ();
00197 }
00198
00199 return run_standalone (opts);
00200 }
00201