Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / Notify_Service / NT_Notify_Server.cpp
blob54391be46d0b47bf9084386e534b5b2c6ed2dd6d
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file NT_Notify_Server.cpp
7 * Driver program that runs the TAO Notification Service as a Windows NT
8 * Service.
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)
21 int
22 ACE_TMAIN(int, ACE_TCHAR *[])
24 ORBSVCS_ERROR ((LM_INFO,
25 "This program is only supported "
26 "on Win32 platforms\n"));
27 return 1;
30 #else
32 #include "ace/Get_Opt.h"
33 #include "ace/Arg_Shifter.h"
34 #include "ace/ARGV.h"
35 #include "ace/Configuration.h"
36 #include "ace/Init_ACE.h"
38 #include "winreg.h"
39 #include "NT_Notify_Service.h"
41 // Default for the -i (install) option
42 #define DEFAULT_SERVICE_INIT_STARTUP SERVICE_DEMAND_START
44 /**
45 * @class Options
47 * @brief Keeps track of the command-line options for this program.
49 class Options
51 public:
52 Options (void);
53 ~Options (void);
55 int run (int argc, ACE_TCHAR *argv[]);
57 private:
58 void parse_args (int argc, ACE_TCHAR *argv[]);
59 void print_usage_and_die (void);
61 private:
62 ACE_TCHAR progname[128];
64 int opt_setargs;
65 const ACE_TCHAR *opt_args;
66 int opt_install;
67 int opt_remove;
68 int opt_start;
69 int opt_kill;
70 int opt_type;
71 int opt_debug;
73 int opt_startup;
76 typedef ACE_Singleton<Options, ACE_Mutex> OPTIONS;
78 Options::Options (void)
79 : opt_setargs (0),
80 opt_args (0),
81 opt_install (0),
82 opt_remove (0),
83 opt_start (0),
84 opt_kill (0),
85 opt_type (0),
86 opt_debug (0),
87 opt_startup (0)
89 ACE_OS::strcpy (progname,
90 ACE_TEXT ("service"));
91 ACE::init ();
94 Options::~Options (void)
96 ACE::fini ();
99 void
100 Options::print_usage_and_die (void)
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"),
112 progname,
113 0));
114 ACE_OS::exit (1);
117 void
118 Options::parse_args (int argc, ACE_TCHAR *argv[])
120 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("c:i:rskt:d"));
121 int c;
123 while ((c = get_opt ()) != -1)
124 switch (c)
126 case 'c':
127 opt_setargs = 1;
128 opt_args = get_opt.opt_arg ();
129 break;
130 case 'i':
131 opt_install = 1;
132 opt_startup = ACE_OS::atoi (get_opt.optarg);
133 if (opt_startup <= 0)
134 print_usage_and_die ();
135 break;
136 case 'r':
137 opt_remove = 1;
138 break;
139 case 's':
140 opt_start = 1;
141 break;
142 case 'k':
143 opt_kill = 1;
144 break;
145 case 't':
146 opt_type = 1;
147 opt_startup = ACE_OS::atoi (get_opt.optarg);
148 if (opt_startup <= 0)
149 print_usage_and_die ();
150 break;
151 case 'd':
152 //opt_debug = 1;
153 break;
154 default:
155 // -c and -i can also be given without a value - if so, it defaults
156 // to defined value.
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)
160 opt_install = 1;
161 opt_startup = DEFAULT_SERVICE_INIT_STARTUP;
163 else if (ACE_OS::strcmp (lastarg, ACE_TEXT ("-c")) == 0)
165 opt_setargs = 1;
166 opt_args = 0;
168 else
169 this->print_usage_and_die ();
170 break;
174 // Define a function to handle Ctrl+C to cleanly shut this down.
175 static BOOL WINAPI
176 ConsoleHandler (DWORD /* ctrlType */)
178 SERVICE::instance ()->handle_control (SERVICE_CONTROL_STOP);
179 return TRUE;
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 ();
203 if (opt_setargs)
204 return SERVICE::instance ()->set_args (opt_args);
206 if (opt_start)
207 return SERVICE::instance ()->start_svc ();
209 if (opt_kill)
210 return SERVICE::instance ()->stop_svc ();
212 if (opt_type)
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.
218 if (opt_debug)
220 SetConsoleCtrlHandler (&ConsoleHandler, 1);
221 SERVICE::instance ()->svc ();
223 else
225 ACE_NT_SERVICE_RUN (service,
226 SERVICE::instance (),
227 ret);
228 if (ret == 0)
229 ORBSVCS_ERROR ((LM_ERROR,
230 ACE_TEXT ("%p\n"),
231 ACE_TEXT ("Couldn't start service")));
234 return 0;
238 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
240 return OPTIONS::instance ()->run (argc, argv);
243 #endif /* !ACE_WIN32 || ACE_LACKS_WIN32_SERVICES */