Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / ImplRepo_Service / ImR_Locator.cpp
blob770eba4371275ba6aee60c799d085cda2fb8e51e
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
9 public:
10 ImR_Locator_Shutdown (ImR_Locator_i& imr);
12 void operator() (int which_signal);
13 private:
14 ImR_Locator_i& imr_;
17 ImR_Locator_Shutdown::ImR_Locator_Shutdown (ImR_Locator_i &imr)
18 : imr_(imr)
22 void
23 ImR_Locator_Shutdown::operator () (int /*sig*/)
25 try
27 this->imr_.signal_shutdown ();
29 catch (const CORBA::Exception& ex)
31 ex._tao_print_exception ("ImR: ");
35 int
36 run_standalone (Options& opts)
38 ImR_Locator_i server;
40 ImR_Locator_Shutdown killer (server);
41 Service_Shutdown kill_contractor(killer);
43 try
45 int status = server.init (opts);
46 if (status == -1)
48 server.fini ();
49 return 1;
51 else
53 // Run the server if it is initialized correctly.
54 server.run ();
56 // End the server after its work is done.
57 status = server.fini ();
59 if (status == -1)
60 return 1;
62 return 0;
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");
77 return 1;
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 */
84 int
85 run_service (void)
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);
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 (Options& opts)
111 if (opts.service_command () == Options::SC_NONE)
112 return 0;
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"));
126 return -1;
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,
134 pathname);
135 if (ret != -1)
137 ORBSVCS_DEBUG ((LM_DEBUG, "ImR: Service installed.\n"));
138 opts.save_registry_options ();
140 else
142 ORBSVCS_ERROR ((LM_ERROR, "Error: Failed to install service. errno %d, error <%p>\n", errno, "install"));
144 if (ret == 0)
145 return 1;
147 else if (opts.service_command () == Options::SC_REMOVE)
149 int ret = SERVICE::instance ()->remove ();
150 ORBSVCS_DEBUG ((LM_DEBUG, "ImR: Service removed.\n"));
151 if (ret == 0)
152 return 1; // If successful, then we don't want to continue.
154 else
156 ORBSVCS_ERROR ((LM_ERROR, "Error: Unknown service command :%d\n",
157 opts.service_command ()));
158 return -1;
161 return -1;
163 #else /* ACE_WIN32 */
164 ORBSVCS_ERROR ((LM_ERROR, "NT Service not supported on this platform"));
165 return -1;
166 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
170 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
172 Options opts;
174 int result = opts.init (argc, argv);
175 if (result < 0)
176 return 1; // Error
177 else if (result > 0)
178 return 0; // No error, but we should exit anyway.
180 result = run_service_command(opts);
181 if (result < 0)
182 return 1; // Error
183 else if (result > 0)
184 return 0; // No error, but we should exit anyway.
186 if (opts.service ())
188 return run_service ();
191 return run_standalone (opts);