Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / orbsvcs / Naming_Service / NT_Naming_Server.cpp
blob7cf4bbb158363e48367664637985d0a3118767ba
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file NT_Naming_Server.cpp
7 * Driver program that runs the TAO Naming Service as a Windows NT
8 * Service.
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)
18 int
19 ACE_TMAIN(int, ACE_TCHAR *[])
21 ORBSVCS_ERROR ((LM_INFO,
22 "This program is only supported "
23 "on Win32 platforms\n"));
24 return 1;
27 #else
29 #include "ace/Get_Opt.h"
30 #include "ace/Init_ACE.h"
32 #include "winreg.h"
33 #include "NT_Naming_Service.h"
35 // Default for the -i (install) option
36 #define DEFAULT_SERVICE_INIT_STARTUP SERVICE_DEMAND_START
38 /**
39 * @class Options
41 * @brief Keeps track of the command-line options for this program.
43 class Options
45 public:
46 Options ();
47 ~Options ();
49 int run (int argc, ACE_TCHAR *argv[]);
51 private:
52 void parse_args (int argc,
53 ACE_TCHAR *argv[]);
54 void print_usage_and_die ();
56 private:
57 ACE_TCHAR progname[128];
59 int opt_setargs;
60 const ACE_TCHAR *opt_args;
61 int opt_install;
62 int opt_remove;
63 int opt_start;
64 int opt_kill;
65 int opt_type;
66 int opt_debug;
68 int opt_startup;
71 typedef ACE_Singleton<Options, ACE_Mutex> OPTIONS;
73 Options::Options ()
74 : opt_setargs (0),
75 opt_args (0),
76 opt_install (0),
77 opt_remove (0),
78 opt_start (0),
79 opt_kill (0),
80 opt_type (0),
81 opt_debug (0),
82 opt_startup (0)
84 ACE_OS::strcpy (progname,
85 ACE_TEXT("service"));
86 ACE::init ();
89 Options::~Options ()
91 ACE::fini ();
94 void
95 Options::print_usage_and_die ()
97 ORBSVCS_DEBUG ((LM_INFO,
98 ACE_TEXT("Usage: %s")
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"),
107 progname,
108 0));
109 ACE_OS::exit (1);
112 void
113 Options::parse_args (int argc, ACE_TCHAR *argv[])
115 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("c:i:rskt:d"));
116 int c;
118 while ((c = get_opt ()) != -1)
119 switch (c)
121 case 'c':
122 opt_setargs = 1;
123 opt_args = get_opt.opt_arg ();
124 break;
125 case 'i':
126 opt_install = 1;
127 opt_startup = ACE_OS::atoi (get_opt.opt_arg ());
128 if (opt_startup <= 0)
129 print_usage_and_die ();
130 break;
131 case 'r':
132 opt_remove = 1;
133 break;
134 case 's':
135 opt_start = 1;
136 break;
137 case 'k':
138 opt_kill = 1;
139 break;
140 case 't':
141 opt_type = 1;
142 opt_startup = ACE_OS::atoi (get_opt.opt_arg ());
143 if (opt_startup <= 0)
144 print_usage_and_die ();
145 break;
146 case 'd':
147 //opt_debug = 1;
148 break;
149 default:
150 // -c and -i can also be given without a value - if so, it defaults
151 // to defined value.
152 const ACE_TCHAR *lastarg = get_opt.argv ()[get_opt.opt_ind () - 1];
153 if (ACE_OS::strcmp (lastarg, ACE_TEXT ("-i")) == 0)
155 opt_install = 1;
156 opt_startup = DEFAULT_SERVICE_INIT_STARTUP;
158 else if (ACE_OS::strcmp (lastarg, ACE_TEXT ("-c")) == 0)
160 opt_setargs = 1;
161 opt_args = 0;
163 else
164 this->print_usage_and_die ();
165 break;
169 // Define a function to handle Ctrl+C to cleanly shut this down.
171 static BOOL WINAPI
172 ConsoleHandler (DWORD /* ctrlType */)
174 SERVICE::instance ()->handle_control (SERVICE_CONTROL_STOP);
175 return TRUE;
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 ();
199 if (opt_setargs)
200 return SERVICE::instance ()->set_args (opt_args);
202 if (opt_start)
203 return SERVICE::instance ()->start_svc ();
205 if (opt_kill)
206 return SERVICE::instance ()->stop_svc ();
208 if (opt_type)
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.
214 if (opt_debug)
216 SetConsoleCtrlHandler (&ConsoleHandler, 1);
217 SERVICE::instance ()->svc ();
219 else
221 ACE_NT_SERVICE_RUN (service,
222 SERVICE::instance (),
223 ret);
224 if (ret == 0)
225 ORBSVCS_ERROR ((LM_ERROR,
226 "%p\n",
227 "Couldn't start service"));
230 return 0;
234 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
236 return OPTIONS::instance ()->run (argc, argv);
239 #endif /* !ACE_WIN32 || ACE_LACKS_WIN32_SERVICES */