Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / ImplRepo_Service / ImR_Activator.cpp
blob679908d3e351f972cf94a198493ca6604ecc70e3
1 /* -*- C++ -*- */
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
11 public:
12 ImR_Activator_Shutdown (ImR_Activator_i& act);
14 void operator() (int which_signal);
15 private:
16 ImR_Activator_i& act_;
19 ImR_Activator_Shutdown::ImR_Activator_Shutdown (ImR_Activator_i &act)
20 : act_(act)
24 void
25 ImR_Activator_Shutdown::operator() (int /*which_signal*/)
27 try
29 this->act_.shutdown (true);
31 catch (const CORBA::Exception& ex)
33 ex._tao_print_exception ("ImR Activator: ");
37 int
38 run_standalone (Activator_Options& opts)
40 ImR_Activator_i server;
42 ImR_Activator_Shutdown killer (server);
43 Service_Shutdown kill_contractor (killer);
45 try
47 int status = server.init (opts);
49 if (status == -1)
51 return 1;
53 else
55 // Run the server if it is initialized correctly.
56 server.run ();
58 // End the server after its work is done.
59 status = server.fini ();
61 if (status == -1)
62 return 1;
64 return 0;
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");
79 return 1;
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 */
86 int
87 run_service ()
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);
93 if (ret == 0)
94 ORBSVCS_ERROR ((LM_ERROR, "%p\n", "Couldn't start service"));
96 return ret;
97 #else /* ACE_WIN32 */
98 return 1;
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
106 * these commands.
108 static int
109 run_service_command (Activator_Options& opts)
111 if (opts.service_command () == Activator_Options::SC_NONE)
112 return 0;
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"));
127 return -1;
130 // Append the command used for running the implrepo as a service
131 ACE_OS::strcat (pathname, ACE_TEXT (" -s"));
132 int ret = -1;
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,
140 pathname,
141 0, // group
142 0, // tag
143 DEPENDS_ON);
145 else
147 ret = SERVICE::instance ()->insert (SERVICE_DEMAND_START,
148 SERVICE_ERROR_NORMAL,
149 pathname);
151 if (ret != -1)
153 ORBSVCS_DEBUG ((LM_DEBUG, "ImR Activator: Service installed\n"));
154 opts.save_registry_options ();
156 else
158 ORBSVCS_ERROR ((LM_ERROR, "Error: Failed to install service. errno %d, error <%p>\n", errno, "install"));
160 if (ret == 0)
161 return 1;
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"));
167 if (ret == 0)
168 return 1; // If successful, then we don't want to continue.
170 else
172 ORBSVCS_ERROR ((LM_ERROR, "Error: Unknown service command :%d\n",
173 opts.service_command ()));
174 return -1;
177 return -1;
179 #else /* ACE_WIN32 */
180 ORBSVCS_ERROR ((LM_ERROR, "NT Service not supported on this platform"));
181 return -1;
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);
191 if (result < 0)
192 return 1; // Error
193 else if (result > 0)
194 return 0; // No error, but we should exit anyway.
196 result = run_service_command (opts);
197 if (result < 0)
198 return 1; // Error
199 else if (result > 0)
200 return 0; // No error, but we should exit anyway.
202 if (opts.service ())
203 return run_service ();
205 return run_standalone (opts);