3 //=============================================================================
5 * @file NT_Notify_Server.cpp
7 * Driver program that runs the TAO Notification Service as a Windows NT
10 * @author John Tucker <jtucker@infoglide.com>
11 * @author Mike Vitalo <mvitalo@infoglide.com>
12 * @author and David Robison <drrobison@openroadsconsulting.com>
14 //=============================================================================
17 #include "orbsvcs/Log_Macros.h"
19 #if !defined (ACE_WIN32) || defined (ACE_LACKS_WIN32_SERVICES)
22 ACE_TMAIN(int, ACE_TCHAR
*[])
24 ORBSVCS_ERROR ((LM_INFO
,
25 "This program is only supported "
26 "on Win32 platforms\n"));
32 #include "ace/Get_Opt.h"
33 #include "ace/Arg_Shifter.h"
35 #include "ace/Configuration.h"
36 #include "ace/Init_ACE.h"
39 #include "NT_Notify_Service.h"
41 // Default for the -i (install) option
42 #define DEFAULT_SERVICE_INIT_STARTUP SERVICE_DEMAND_START
47 * @brief Keeps track of the command-line options for this program.
55 int run (int argc
, ACE_TCHAR
*argv
[]);
58 void parse_args (int argc
, ACE_TCHAR
*argv
[]);
59 void print_usage_and_die ();
62 ACE_TCHAR progname
[128];
65 const ACE_TCHAR
*opt_args
;
76 typedef ACE_Singleton
<Options
, ACE_Mutex
> OPTIONS
;
89 ACE_OS::strcpy (progname
,
90 ACE_TEXT ("service"));
100 Options::print_usage_and_die ()
102 ORBSVCS_DEBUG ((LM_INFO
,
103 ACE_TEXT ("Usage: %s")
104 ACE_TEXT (" -c [<args>] -in -r -s -k -tn -d\n")
105 ACE_TEXT (" -c: set or retrieve command line arguments for NT service\n")
106 ACE_TEXT (" -i: Install this program as an NT service, with specified startup\n")
107 ACE_TEXT (" -r: Remove this program from the Service Manager\n")
108 ACE_TEXT (" -s: Start the service\n")
109 ACE_TEXT (" -k: Kill the service\n")
110 ACE_TEXT (" -t: Set startup for an existing service\n")
111 ACE_TEXT (" -d: Debug; run as a regular application\n"),
118 Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
120 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("c:i:rskt:d"));
123 while ((c
= get_opt ()) != -1)
128 opt_args
= get_opt
.opt_arg ();
132 opt_startup
= ACE_OS::atoi (get_opt
.optarg
);
133 if (opt_startup
<= 0)
134 print_usage_and_die ();
147 opt_startup
= ACE_OS::atoi (get_opt
.optarg
);
148 if (opt_startup
<= 0)
149 print_usage_and_die ();
155 // -c and -i can also be given without a value - if so, it defaults
157 const ACE_TCHAR
*lastarg
= get_opt
.argv ()[get_opt
.opt_ind () - 1];
158 if (ACE_OS::strcmp (get_opt
.argv_
[get_opt
.optind
-1], ACE_TEXT ("-i")) == 0)
161 opt_startup
= DEFAULT_SERVICE_INIT_STARTUP
;
163 else if (ACE_OS::strcmp (lastarg
, ACE_TEXT ("-c")) == 0)
169 this->print_usage_and_die ();
174 // Define a function to handle Ctrl+C to cleanly shut this down.
176 ConsoleHandler (DWORD
/* ctrlType */)
178 SERVICE::instance ()->handle_control (SERVICE_CONTROL_STOP
);
182 ACE_NT_SERVICE_DEFINE (service
,
183 TAO_NT_Notify_Service
,
184 ACE_TEXT ("TAO NT Notify Service"));
187 Options::run (int argc
, ACE_TCHAR
* argv
[])
189 SERVICE::instance ()->name (ACE_TEXT ("TAO_NT_Notify_Service"),
190 ACE_TEXT ("TAO NT Notify Service"));
192 this->parse_args (argc
, argv
);
194 if (opt_install
&& !opt_remove
)
195 return SERVICE::instance ()->insert (opt_startup
);
197 if (opt_remove
&& !opt_install
)
198 return SERVICE::instance ()->remove ();
200 if (opt_start
&& opt_kill
)
201 print_usage_and_die ();
204 return SERVICE::instance ()->set_args (opt_args
);
207 return SERVICE::instance ()->start_svc ();
210 return SERVICE::instance ()->stop_svc ();
213 return SERVICE::instance ()->startup (opt_startup
);
215 // If we get here, we either run the app in debug mode (-d) or are
216 // being called from the service manager to start the service.
220 SetConsoleCtrlHandler (&ConsoleHandler
, 1);
221 SERVICE::instance ()->svc ();
225 ACE_NT_SERVICE_RUN (service
,
226 SERVICE::instance (),
229 ORBSVCS_ERROR ((LM_ERROR
,
231 ACE_TEXT ("Couldn't start service")));
238 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
240 return OPTIONS::instance ()->run (argc
, argv
);
243 #endif /* !ACE_WIN32 || ACE_LACKS_WIN32_SERVICES */