2 #include "orbsvcs/Log_Macros.h"
3 #include "Activator_Options.h"
4 #include "ImR_Activator_i.h"
5 #include "Activator_NT_Service.h"
7 #include "orbsvcs/Shutdown_Utilities.h"
9 class ImR_Activator_Shutdown
: public Shutdown_Functor
12 ImR_Activator_Shutdown (ImR_Activator_i
& act
);
14 void operator() (int which_signal
);
16 ImR_Activator_i
& act_
;
19 ImR_Activator_Shutdown::ImR_Activator_Shutdown (ImR_Activator_i
&act
)
25 ImR_Activator_Shutdown::operator() (int /*which_signal*/)
29 this->act_
.shutdown (true);
31 catch (const CORBA::Exception
& ex
)
33 ex
._tao_print_exception ("ImR Activator: ");
38 run_standalone (Activator_Options
& opts
)
40 ImR_Activator_i server
;
42 ImR_Activator_Shutdown
killer (server
);
43 Service_Shutdown
kill_contractor (killer
);
47 int status
= server
.init (opts
);
55 // Run the server if it is initialized correctly.
58 // End the server after its work is done.
59 status
= server
.fini ();
66 catch (const CORBA::SystemException
& sysex
)
68 sysex
._tao_print_exception ("System Exception");
70 catch (const CORBA::UserException
& userex
)
72 userex
._tao_print_exception ("User Exception");
74 catch (const CORBA::Exception
& ex
)
76 ex
._tao_print_exception ("Unknown Exception");
82 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
83 ACE_NT_SERVICE_DEFINE (service
, Activator_NT_Service
, IMR_ACTIVATOR_SERVICE_NAME
);
84 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
89 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
90 SERVICE::instance()->name (IMR_ACTIVATOR_SERVICE_NAME
, IMR_ACTIVATOR_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 (Activator_Options
& opts
)
111 if (opts
.service_command () == Activator_Options::SC_NONE
)
114 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
115 SERVICE::instance()->name (IMR_ACTIVATOR_SERVICE_NAME
, IMR_ACTIVATOR_DISPLAY_NAME
);
117 if (opts
.service_command () == Activator_Options::SC_INSTALL
||
118 opts
.service_command () == Activator_Options::SC_INSTALL_NO_LOCATOR
)
120 const DWORD MAX_PATH_LENGTH
= 4096;
121 ACE_TCHAR pathname
[MAX_PATH_LENGTH
];
123 DWORD length
= ACE_TEXT_GetModuleFileName(0, pathname
, MAX_PATH_LENGTH
);
124 if (length
== 0 || length
>= MAX_PATH_LENGTH
- sizeof(" -s"))
126 ORBSVCS_ERROR ((LM_ERROR
, "Error: Could not get module file name\n"));
130 // Append the command used for running the implrepo as a service
131 ACE_OS::strcat (pathname
, ACE_TEXT (" -s"));
133 if (opts
.service_command () == Activator_Options::SC_INSTALL
)
135 // Must match Locator_NT_Service.h
136 const ACE_TCHAR
* DEPENDS_ON
= ACE_TEXT("TAOImR");
138 ret
= SERVICE::instance ()->insert (SERVICE_DEMAND_START
,
139 SERVICE_ERROR_NORMAL
,
147 ret
= SERVICE::instance ()->insert (SERVICE_DEMAND_START
,
148 SERVICE_ERROR_NORMAL
,
153 ORBSVCS_DEBUG ((LM_DEBUG
, "ImR Activator: Service installed\n"));
154 opts
.save_registry_options ();
158 ORBSVCS_ERROR ((LM_ERROR
, "Error: Failed to install service. errno %d, error <%p>\n", errno
, "install"));
163 else if (opts
.service_command () == Activator_Options::SC_REMOVE
)
165 int ret
= SERVICE::instance ()->remove ();
166 ORBSVCS_DEBUG ((LM_DEBUG
, "ImR Activator: Service removed\n"));
168 return 1; // If successful, then we don't want to continue.
172 ORBSVCS_ERROR ((LM_ERROR
, "Error: Unknown service command :%d\n",
173 opts
.service_command ()));
179 #else /* ACE_WIN32 */
180 ORBSVCS_ERROR ((LM_ERROR
, "NT Service not supported on this platform"));
182 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
186 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
188 Activator_Options opts
;
190 int result
= opts
.init (argc
, argv
);
194 return 0; // No error, but we should exit anyway.
196 result
= run_service_command (opts
);
200 return 0; // No error, but we should exit anyway.
203 return run_service ();
205 return run_standalone (opts
);