1 //=============================================================================
3 * @file Activator_Options.cpp
5 * @author Darrell Brunsch <brunsch@cs.wustl.edu>
7 //=============================================================================
8 #include "Activator_Options.h"
10 #include "ace/Arg_Shifter.h"
11 #include "ace/OS_NS_strings.h"
12 #include "orbsvcs/Log_Macros.h"
14 #if defined (ACE_WIN32)
15 static const HKEY SERVICE_REG_ROOT
= HKEY_LOCAL_MACHINE
;
16 // This string must agree with the one used in Activator_NT_Service.h
17 static const ACE_TCHAR
*SERVICE_REG_PATH
=
18 ACE_TEXT ("SYSTEM\\CurrentControlSet\\Services\\TAOImRActivator\\Parameters");
19 #endif /* ACE_WIN32 */
21 Activator_Options::Activator_Options ()
26 , service_command_(SC_NONE
)
27 , env_buf_len_ (Activator_Options::ENVIRONMENT_BUFFER
)
28 , max_env_vars_ (Activator_Options::ENVIRONMENT_MAX_VARS
)
30 #if defined IMR_DETACH_CHILD_DEF
40 Activator_Options::parse_args (int &argc
, ACE_TCHAR
*argv
[])
42 ACE_Arg_Shifter
shifter (argc
, argv
);
44 while (shifter
.is_anything_left ())
46 if (ACE_OS::strcasecmp (shifter
.get_current (),
47 ACE_TEXT ("-c")) == 0)
49 shifter
.consume_arg ();
51 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
53 ORBSVCS_ERROR ((LM_ERROR
, "Error: -c option needs a command\n"));
58 if (ACE_OS::strcasecmp (shifter
.get_current (),
59 ACE_TEXT ("install")) == 0)
61 this->service_command_
= SC_INSTALL
;
63 else if (ACE_OS::strcasecmp (shifter
.get_current (),
64 ACE_TEXT ("remove")) == 0)
66 this->service_command_
= SC_REMOVE
;
68 else if (ACE_OS::strcasecmp (shifter
.get_current (),
69 ACE_TEXT ("install_no_imr")) == 0)
71 this->service_command_
= SC_INSTALL_NO_LOCATOR
;
75 ORBSVCS_ERROR((LM_ERROR
, "Error: Unknown service command : %s\n", shifter
.get_current()));
80 else if (ACE_OS::strcasecmp (shifter
.get_current (),
81 ACE_TEXT ("-d")) == 0)
83 shifter
.consume_arg ();
85 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
87 ORBSVCS_ERROR ((LM_ERROR
, "Error: -d option needs a debuglevel\n"));
92 this->debug_
= ACE_OS::atoi (shifter
.get_current ());
94 else if (ACE_OS::strcasecmp (shifter
.get_current (),
95 ACE_TEXT ("-e")) == 0)
97 shifter
.consume_arg ();
99 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
101 ORBSVCS_ERROR ((LM_ERROR
, "Error: -e option needs "
102 "an environment buffer length\n"));
103 this->print_usage ();
107 this->env_buf_len_
= ACE_OS::atoi (shifter
.get_current ());
109 else if (ACE_OS::strcasecmp (shifter
.get_current (),
110 ACE_TEXT ("-m")) == 0)
112 shifter
.consume_arg ();
114 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
116 ORBSVCS_ERROR ((LM_ERROR
, "Error: -m option needs "
117 "a maximum number of environment vars\n"));
118 this->print_usage ();
122 this->max_env_vars_
= ACE_OS::atoi (shifter
.get_current ());
124 else if (ACE_OS::strcasecmp (shifter
.get_current (),
125 ACE_TEXT ("-o")) == 0)
127 shifter
.consume_arg ();
129 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
131 ORBSVCS_ERROR ((LM_ERROR
, "Error: -o option needs a filename\n"));
132 this->print_usage ();
135 this->ior_output_file_
= shifter
.get_current ();
137 else if (ACE_OS::strcasecmp (shifter
.get_current (),
138 ACE_TEXT ("-s")) == 0)
140 this->service_
= true;
142 else if ((ACE_OS::strcasecmp (shifter
.get_current (),
143 ACE_TEXT ("-?")) == 0)
144 || (ACE_OS::strcasecmp (shifter
.get_current (),
145 ACE_TEXT ("-h")) == 0))
147 this->print_usage ();
150 else if (ACE_OS::strcasecmp (shifter
.get_current (),
151 ACE_TEXT ("-n")) == 0)
153 shifter
.consume_arg ();
155 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
157 ORBSVCS_ERROR ((LM_ERROR
, "Error: -n option needs a name\n"));
158 this->print_usage ();
161 this->name_
= ACE_TEXT_ALWAYS_CHAR(shifter
.get_current ());
163 else if (ACE_OS::strcasecmp (shifter
.get_current (),
164 ACE_TEXT ("-l")) == 0)
166 this->notify_imr_
= true;
168 else if (ACE_OS::strcasecmp (shifter
.get_current (),
169 ACE_TEXT ("-delay")) == 0)
171 shifter
.consume_arg ();
173 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
175 ORBSVCS_ERROR ((LM_ERROR
, "Error: -delay option needs a value\n"));
176 this->print_usage ();
179 this->induce_delay_
= ACE_OS::atoi (shifter
.get_current ());
181 else if (ACE_OS::strcasecmp (shifter
.get_current (),
182 ACE_TEXT ("-detach")) == 0)
184 shifter
.consume_arg ();
186 if (!shifter
.is_anything_left () || shifter
.get_current ()[0] == '-')
188 ORBSVCS_ERROR ((LM_ERROR
, "Error: -detach option needs a value\n"));
189 this->print_usage ();
192 this->detach_child_
= ACE_OS::atoi (shifter
.get_current ()) != 0;
197 shifter
.ignore_arg ();
201 shifter
.consume_arg ();
207 Activator_Options::init (int argc
, ACE_TCHAR
*argv
[])
209 // Make an initial pass through and grab the arguments that we recognize.
210 // This may also run the commands to install or remove the nt service.
211 int result
= this->parse_args (argc
, argv
);
217 for (int i
= 0; i
< argc
; ++i
)
219 this->cmdline_
+= ACE_CString (ACE_TEXT_ALWAYS_CHAR(argv
[i
])) + ACE_CString (" ");
226 Activator_Options::init_from_registry ()
228 this->load_registry_options();
233 Activator_Options::print_usage () const
235 ORBSVCS_ERROR ((LM_ERROR
,
238 "ImR_Activator [-c cmd] [-d 0|1|2] [-e buflen] [-o file] [-l] [-n name] [-m maxenv]\n"
240 " -c command Runs service commands\n"
241 " ('install' or 'remove' or 'install_no_imr')\n"
242 " -d level Sets the debug level\n"
243 " -e buflen Set the environment buffer length in bytes for activated servants\n"
244 " -o file Outputs the ImR's IOR to a file\n"
245 " -l Notify the ImR when a process exits\n"
246 " -n name Specify a name for the Activator\n"
247 " -delay ms When using -l to notify, induce a delay of ms before notifying\n")
252 Activator_Options::save_registry_options()
254 #if defined (ACE_WIN32)
256 // Create or open the parameters key
257 LONG err
= ACE_TEXT_RegCreateKeyEx (SERVICE_REG_ROOT
,
260 const_cast<ACE_TCHAR
*> (ACE_TEXT("")), // class
261 REG_OPTION_NON_VOLATILE
,
267 if (err
!= ERROR_SUCCESS
)
271 err
= ACE_TEXT_RegSetValueEx (key
, ACE_TEXT("ORBInitOptions"), 0, REG_SZ
,
272 (LPBYTE
) this->cmdline_
.c_str (), (DWORD
) this->cmdline_
.length () + 1);
273 ACE_ASSERT (err
== ERROR_SUCCESS
);
275 err
= ACE_TEXT_RegSetValueEx (key
, ACE_TEXT("IORFile"), 0, REG_SZ
,
276 (LPBYTE
) this->ior_output_file_
.c_str (), (DWORD
) this->ior_output_file_
.length () + 1);
277 ACE_ASSERT (err
== ERROR_SUCCESS
);
279 err
= ACE_TEXT_RegSetValueEx (key
, ACE_TEXT("DebugLevel"), 0, REG_DWORD
,
280 (LPBYTE
) &this->debug_
, sizeof (this->debug_
));
281 ACE_ASSERT (err
== ERROR_SUCCESS
);
283 err
= ACE_TEXT_RegSetValueEx( key
, ACE_TEXT("Name"), 0, REG_SZ
,
284 (LPBYTE
) this->name_
.c_str (), (DWORD
) this->name_
.length () + 1);
285 ACE_ASSERT (err
== ERROR_SUCCESS
);
287 DWORD tmpint
= this->notify_imr_
;
288 err
= ACE_TEXT_RegSetValueEx (key
, ACE_TEXT("NotifyImR"), 0, REG_DWORD
,
289 (LPBYTE
) &tmpint
, sizeof (tmpint
));
290 ACE_ASSERT (err
== ERROR_SUCCESS
);
292 tmpint
= this->env_buf_len_
;
293 err
= ACE_TEXT_RegSetValueEx(key
, ACE_TEXT("EnvBufLen"), 0, REG_DWORD
,
294 (LPBYTE
) &tmpint
, sizeof(tmpint
));
295 ACE_ASSERT(err
== ERROR_SUCCESS
);
297 tmpint
= this->max_env_vars_
;
298 err
= ACE_TEXT_RegSetValueEx(key
, ACE_TEXT("MaxEnvVars"), 0, REG_DWORD
,
299 (LPBYTE
) &tmpint
, sizeof(tmpint
));
300 ACE_ASSERT(err
== ERROR_SUCCESS
);
302 err
= ::RegCloseKey (key
);
303 ACE_ASSERT (err
== ERROR_SUCCESS
);
309 Activator_Options::load_registry_options ()
311 #if defined (ACE_WIN32)
313 // Create or open the parameters key
314 LONG err
= ACE_TEXT_RegOpenKeyEx (SERVICE_REG_ROOT
,
320 if (err
!= ERROR_SUCCESS
)
322 // If there aren't any saved parameters, then that's ok.
325 ACE_TCHAR tmpstr
[4096];
326 DWORD sz
= sizeof (tmpstr
);
328 err
= ACE_TEXT_RegQueryValueEx (key
, ACE_TEXT("ORBInitOptions"), 0, &type
,
329 (LPBYTE
) tmpstr
, &sz
);
330 if (err
== ERROR_SUCCESS
)
332 ACE_ASSERT (type
== REG_SZ
);
333 tmpstr
[sz
- 1] = '\0';
334 this->cmdline_
= ACE_TEXT_ALWAYS_CHAR(tmpstr
);
338 err
= ACE_TEXT_RegQueryValueEx (key
, ACE_TEXT("IORFile"), 0, &type
,
339 (LPBYTE
) tmpstr
, &sz
);
340 if (err
== ERROR_SUCCESS
)
342 ACE_ASSERT (type
== REG_SZ
);
343 tmpstr
[sz
- 1] = '\0';
344 this->ior_output_file_
= tmpstr
;
348 err
= ACE_TEXT_RegQueryValueEx (key
, ACE_TEXT("DebugLevel"), 0, &type
,
349 (LPBYTE
) &this->debug_
, &sz
);
350 if (err
== ERROR_SUCCESS
)
352 ACE_ASSERT (type
== REG_DWORD
);
356 err
= ACE_TEXT_RegQueryValueEx (key
, ACE_TEXT("Name"), 0, &type
,
357 (LPBYTE
) tmpstr
, &sz
);
358 if (err
== ERROR_SUCCESS
)
360 ACE_ASSERT (type
== REG_SZ
);
361 tmpstr
[sz
- 1] = '\0';
362 this->name_
= ACE_TEXT_ALWAYS_CHAR(tmpstr
);
367 err
= ACE_TEXT_RegQueryValueEx (key
, ACE_TEXT("NotifyImR"), 0, &type
,
368 (LPBYTE
) &tmpint
, &sz
);
369 if (err
== ERROR_SUCCESS
)
371 ACE_ASSERT (type
== REG_DWORD
);
373 this->notify_imr_
= tmpint
!= 0;
375 err
= ACE_TEXT_RegQueryValueEx(key
, ACE_TEXT("EnvBufLen"), 0, &type
,
376 (LPBYTE
) &tmpint
, &sz
);
377 if (err
== ERROR_SUCCESS
) {
378 ACE_ASSERT(type
== REG_DWORD
);
380 this->env_buf_len_
= tmpint
;
382 err
= ACE_TEXT_RegQueryValueEx(key
, ACE_TEXT("MaxEnvArgs"), 0, &type
,
383 (LPBYTE
) &tmpint
, &sz
);
384 if (err
== ERROR_SUCCESS
) {
385 ACE_ASSERT(type
== REG_DWORD
);
387 this->max_env_vars_
= tmpint
;
389 err
= ::RegCloseKey (key
);
390 ACE_ASSERT(err
== ERROR_SUCCESS
);
391 #endif /* ACE_WIN32 */
396 Activator_Options::service () const
398 return this->service_
;
402 Activator_Options::notify_imr () const
404 return this->notify_imr_
;
408 Activator_Options::induce_delay () const
410 return this->induce_delay_
;
414 Activator_Options::debug () const
420 Activator_Options::ior_filename () const
422 return this->ior_output_file_
;
425 Activator_Options::SERVICE_COMMAND
426 Activator_Options::service_command () const
428 return this->service_command_
;
432 Activator_Options::cmdline () const
434 return this->cmdline_
.c_str ();
438 Activator_Options::name () const
444 Activator_Options::env_buf_len () const
446 return this->env_buf_len_
;
450 Activator_Options::max_env_vars () const
452 return this->max_env_vars_
;
456 Activator_Options::detach_child () const
458 return this->detach_child_
;