1 #include "orbsvcs/Log_Macros.h"
2 #include "ImR_Locator_i.h"
3 #include "Locator_NT_Service.h"
4 #include "Locator_Options.h"
5 #include "orbsvcs/Shutdown_Utilities.h"
7 class ImR_Locator_Shutdown
: public Shutdown_Functor
10 ImR_Locator_Shutdown (ImR_Locator_i
& imr
);
12 void operator() (int which_signal
);
17 ImR_Locator_Shutdown::ImR_Locator_Shutdown (ImR_Locator_i
&imr
)
23 ImR_Locator_Shutdown::operator () (int /*sig*/)
27 this->imr_
.signal_shutdown ();
29 catch (const CORBA::Exception
& ex
)
31 ex
._tao_print_exception ("ImR: ");
36 run_standalone (Options
& opts
)
40 ImR_Locator_Shutdown
killer (server
);
41 Service_Shutdown
kill_contractor(killer
);
45 int status
= server
.init (opts
);
53 // Run the server if it is initialized correctly.
56 // End the server after its work is done.
57 status
= server
.fini ();
64 catch (const CORBA::SystemException
& sysex
)
66 sysex
._tao_print_exception ("System Exception");
68 catch (const CORBA::UserException
& userex
)
70 userex
._tao_print_exception ("User Exception");
72 catch (const CORBA::Exception
& ex
)
74 ex
._tao_print_exception ("Unknown Exception");
80 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
81 ACE_NT_SERVICE_DEFINE (service
, Locator_NT_Service
, IMR_LOCATOR_SERVICE_NAME
);
82 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
87 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
89 SERVICE::instance ()->name (IMR_LOCATOR_SERVICE_NAME
, IMR_LOCATOR_DISPLAY_NAME
);
91 ACE_NT_SERVICE_RUN (service
, SERVICE::instance (), ret
);
94 ORBSVCS_ERROR ((LM_ERROR
, "%p\n", "Couldn't start service"));
99 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
103 * Executes the various commands that are useful for a NT service. Right
104 * now these include 'install' and 'remove'. Others, such as 'start' and
105 * 'stop' can be added, but the 'net' program in Windows already handles
109 run_service_command (Options
& opts
)
111 if (opts
.service_command () == Options::SC_NONE
)
114 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
115 SERVICE::instance()->name (IMR_LOCATOR_SERVICE_NAME
, IMR_LOCATOR_DISPLAY_NAME
);
117 if (opts
.service_command () == Options::SC_INSTALL
)
119 const DWORD MAX_PATH_LENGTH
= 4096;
120 ACE_TCHAR pathname
[MAX_PATH_LENGTH
];
122 DWORD length
= ACE_TEXT_GetModuleFileName (0, pathname
, MAX_PATH_LENGTH
);
123 if (length
== 0 || length
>= MAX_PATH_LENGTH
- sizeof(" -s"))
125 ORBSVCS_ERROR ((LM_ERROR
, "Error: Could not get module file name\n"));
129 // Append the command used for running the implrepo as a service
130 ACE_OS::strcat (pathname
, ACE_TEXT (" -s"));
132 int ret
= SERVICE::instance ()->insert (SERVICE_DEMAND_START
,
133 SERVICE_ERROR_NORMAL
,
137 ORBSVCS_DEBUG ((LM_DEBUG
, "ImR: Service installed.\n"));
138 opts
.save_registry_options ();
142 ORBSVCS_ERROR ((LM_ERROR
, "Error: Failed to install service. errno %d, error <%p>\n", errno
, "install"));
147 else if (opts
.service_command () == Options::SC_REMOVE
)
149 int ret
= SERVICE::instance ()->remove ();
150 ORBSVCS_DEBUG ((LM_DEBUG
, "ImR: Service removed.\n"));
152 return 1; // If successful, then we don't want to continue.
156 ORBSVCS_ERROR ((LM_ERROR
, "Error: Unknown service command :%d\n",
157 opts
.service_command ()));
163 #else /* ACE_WIN32 */
164 ORBSVCS_ERROR ((LM_ERROR
, "NT Service not supported on this platform"));
166 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
170 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
174 int result
= opts
.init (argc
, argv
);
178 return 0; // No error, but we should exit anyway.
180 result
= run_service_command(opts
);
184 return 0; // No error, but we should exit anyway.
188 return run_service ();
191 return run_standalone (opts
);