3 //=============================================================================
5 * @file NT_Naming_Server.cpp
7 * Driver program that runs the TAO Naming Service as a Windows NT
10 * @author John Tucker <jtucker@infoglide.com> and Mike Vitalo <mvitalo@infoglide.com>
12 //=============================================================================
14 #include "orbsvcs/Log_Macros.h"
16 #if !defined (ACE_WIN32) || defined (ACE_LACKS_WIN32_SERVICES)
19 ACE_TMAIN(int, ACE_TCHAR
*[])
21 ORBSVCS_ERROR ((LM_INFO
,
22 "This program is only supported "
23 "on Win32 platforms\n"));
29 #include "ace/Get_Opt.h"
30 #include "ace/Init_ACE.h"
33 #include "NT_Naming_Service.h"
35 // Default for the -i (install) option
36 #define DEFAULT_SERVICE_INIT_STARTUP SERVICE_DEMAND_START
41 * @brief Keeps track of the command-line options for this program.
49 int run (int argc
, ACE_TCHAR
*argv
[]);
52 void parse_args (int argc
,
54 void print_usage_and_die ();
57 ACE_TCHAR progname
[128];
60 const ACE_TCHAR
*opt_args
;
71 typedef ACE_Singleton
<Options
, ACE_Mutex
> OPTIONS
;
84 ACE_OS::strcpy (progname
,
95 Options::print_usage_and_die ()
97 ORBSVCS_DEBUG ((LM_INFO
,
99 ACE_TEXT(" -c [<args>] -in -r -s -k -tn -d\n")
100 ACE_TEXT(" -c: set or retrieve command line arguments for NT service\n")
101 ACE_TEXT(" -i: Install this program as an NT service, with specified startup\n")
102 ACE_TEXT(" -r: Remove this program from the Service Manager\n")
103 ACE_TEXT(" -s: Start the service\n")
104 ACE_TEXT(" -k: Kill the service\n")
105 ACE_TEXT(" -t: Set startup for an existing service\n")
106 ACE_TEXT(" -d: Debug; run as a regular application\n"),
113 Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
115 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("c:i:rskt:d"));
118 while ((c
= get_opt ()) != -1)
123 opt_args
= get_opt
.opt_arg ();
127 opt_startup
= ACE_OS::atoi (get_opt
.opt_arg ());
128 if (opt_startup
<= 0)
129 print_usage_and_die ();
142 opt_startup
= ACE_OS::atoi (get_opt
.opt_arg ());
143 if (opt_startup
<= 0)
144 print_usage_and_die ();
150 // -c and -i can also be given without a value - if so, it defaults
152 const ACE_TCHAR
*lastarg
= get_opt
.argv ()[get_opt
.opt_ind () - 1];
153 if (ACE_OS::strcmp (lastarg
, ACE_TEXT ("-i")) == 0)
156 opt_startup
= DEFAULT_SERVICE_INIT_STARTUP
;
158 else if (ACE_OS::strcmp (lastarg
, ACE_TEXT ("-c")) == 0)
164 this->print_usage_and_die ();
169 // Define a function to handle Ctrl+C to cleanly shut this down.
172 ConsoleHandler (DWORD
/* ctrlType */)
174 SERVICE::instance ()->handle_control (SERVICE_CONTROL_STOP
);
178 ACE_NT_SERVICE_DEFINE (service
,
179 TAO_NT_Naming_Service
,
180 ACE_TEXT ("TAO NT Naming Service"));
183 Options::run (int argc
, ACE_TCHAR
* argv
[])
185 SERVICE::instance ()->name (ACE_TEXT ("TAO_NT_Naming_Service"),
186 ACE_TEXT ("TAO NT Naming Service"));
188 this->parse_args (argc
, argv
);
190 if (opt_install
&& !opt_remove
)
191 return SERVICE::instance ()->insert (opt_startup
);
193 if (opt_remove
&& !opt_install
)
194 return SERVICE::instance ()->remove ();
196 if (opt_start
&& opt_kill
)
197 print_usage_and_die ();
200 return SERVICE::instance ()->set_args (opt_args
);
203 return SERVICE::instance ()->start_svc ();
206 return SERVICE::instance ()->stop_svc ();
209 return SERVICE::instance ()->startup (opt_startup
);
211 // If we get here, we either run the app in debug mode (-d) or are
212 // being called from the service manager to start the service.
216 SetConsoleCtrlHandler (&ConsoleHandler
, 1);
217 SERVICE::instance ()->svc ();
221 ACE_NT_SERVICE_RUN (service
,
222 SERVICE::instance (),
225 ORBSVCS_ERROR ((LM_ERROR
,
227 "Couldn't start service"));
234 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
236 return OPTIONS::instance ()->run (argc
, argv
);
239 #endif /* !ACE_WIN32 || ACE_LACKS_WIN32_SERVICES */