1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
//=============================================================================
/**
* @file Activator_Options.cpp
*
* @author Darrell Brunsch <brunsch@cs.wustl.edu>
*/
//=============================================================================
#include "Activator_Options.h"
#include "ace/Arg_Shifter.h"
#include "ace/OS_NS_strings.h"
#include "orbsvcs/Log_Macros.h"
#if defined (ACE_WIN32)
static const HKEY SERVICE_REG_ROOT = HKEY_LOCAL_MACHINE;
// This string must agree with the one used in Activator_NT_Service.h
static const ACE_TCHAR *SERVICE_REG_PATH =
ACE_TEXT ("SYSTEM\\CurrentControlSet\\Services\\TAOImRActivator\\Parameters");
#endif /* ACE_WIN32 */
Activator_Options::Activator_Options ()
: debug_ (0)
, service_ (false)
, notify_imr_ (false)
, induce_delay_ (0)
, service_command_(SC_NONE)
, env_buf_len_ (Activator_Options::ENVIRONMENT_BUFFER)
, max_env_vars_ (Activator_Options::ENVIRONMENT_MAX_VARS)
, detach_child_ (
#if defined IMR_DETACH_CHILD_DEF
true
#else
false
#endif
)
{
}
int
Activator_Options::parse_args (int &argc, ACE_TCHAR *argv[])
{
ACE_Arg_Shifter shifter (argc, argv);
while (shifter.is_anything_left ())
{
if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-c")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -c option needs a command\n"));
this->print_usage ();
return -1;
}
if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("install")) == 0)
{
this->service_command_ = SC_INSTALL;
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("remove")) == 0)
{
this->service_command_ = SC_REMOVE;
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("install_no_imr")) == 0)
{
this->service_command_ = SC_INSTALL_NO_LOCATOR;
}
else
{
ORBSVCS_ERROR((LM_ERROR, "Error: Unknown service command : %s\n", shifter.get_current()));
this->print_usage ();
return -1;
}
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-d")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -d option needs a debuglevel\n"));
this->print_usage ();
return -1;
}
this->debug_ = ACE_OS::atoi (shifter.get_current ());
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-e")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -e option needs "
"an environment buffer length\n"));
this->print_usage ();
return -1;
}
this->env_buf_len_ = ACE_OS::atoi (shifter.get_current ());
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-m")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -m option needs "
"a maximum number of environment vars\n"));
this->print_usage ();
return -1;
}
this->max_env_vars_ = ACE_OS::atoi (shifter.get_current ());
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-o")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -o option needs a filename\n"));
this->print_usage ();
return -1;
}
this->ior_output_file_ = shifter.get_current ();
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-s")) == 0)
{
this->service_ = true;
}
else if ((ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-?")) == 0)
|| (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-h")) == 0))
{
this->print_usage ();
return 1;
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-n")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -n option needs a name\n"));
this->print_usage ();
return -1;
}
this->name_ = ACE_TEXT_ALWAYS_CHAR(shifter.get_current ());
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-l")) == 0)
{
this->notify_imr_ = true;
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-delay")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -delay option needs a value\n"));
this->print_usage ();
return -1;
}
this->induce_delay_ = ACE_OS::atoi (shifter.get_current ());
}
else if (ACE_OS::strcasecmp (shifter.get_current (),
ACE_TEXT ("-detach")) == 0)
{
shifter.consume_arg ();
if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
{
ORBSVCS_ERROR ((LM_ERROR, "Error: -detach option needs a value\n"));
this->print_usage ();
return -1;
}
this->detach_child_ = ACE_OS::atoi (shifter.get_current ()) != 0;
}
else
{
shifter.ignore_arg ();
continue;
}
shifter.consume_arg ();
}
return 0;
}
int
Activator_Options::init (int argc, ACE_TCHAR *argv[])
{
// Make an initial pass through and grab the arguments that we recognize.
// This may also run the commands to install or remove the nt service.
int result = this->parse_args (argc, argv);
if (result != 0)
{
return result;
}
for (int i = 0; i < argc; ++i)
{
this->cmdline_ += ACE_CString (ACE_TEXT_ALWAYS_CHAR(argv[i])) + ACE_CString (" ");
}
return 0;
}
int
Activator_Options::init_from_registry (void)
{
this->load_registry_options();
return 0;
}
void
Activator_Options::print_usage (void) const
{
ORBSVCS_ERROR ((LM_ERROR,
"Usage:\n"
"\n"
"ImR_Activator [-c cmd] [-d 0|1|2] [-e buflen] [-o file] [-l] [-n name] [-m maxenv]\n"
"\n"
" -c command Runs service commands\n"
" ('install' or 'remove' or 'install_no_imr')\n"
" -d level Sets the debug level\n"
" -e buflen Set the environment buffer length in bytes for activated servants\n"
" -o file Outputs the ImR's IOR to a file\n"
" -l Notify the ImR when a process exits\n"
" -n name Specify a name for the Activator\n"
" -delay ms When using -l to notify, induce a delay of ms before notifying\n")
);
}
int
Activator_Options::save_registry_options()
{
#if defined (ACE_WIN32)
HKEY key = 0;
// Create or open the parameters key
LONG err = ACE_TEXT_RegCreateKeyEx (SERVICE_REG_ROOT,
SERVICE_REG_PATH,
0,
const_cast<ACE_TCHAR *> (ACE_TEXT("")), // class
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
0,
&key,
0
);
if (err != ERROR_SUCCESS)
{
return -1;
}
err = ACE_TEXT_RegSetValueEx (key, ACE_TEXT("ORBInitOptions"), 0, REG_SZ,
(LPBYTE) this->cmdline_.c_str (), (DWORD) this->cmdline_.length () + 1);
ACE_ASSERT (err == ERROR_SUCCESS);
err = ACE_TEXT_RegSetValueEx (key, ACE_TEXT("IORFile"), 0, REG_SZ,
(LPBYTE) this->ior_output_file_.c_str (), (DWORD) this->ior_output_file_.length () + 1);
ACE_ASSERT (err == ERROR_SUCCESS);
err = ACE_TEXT_RegSetValueEx (key, ACE_TEXT("DebugLevel"), 0, REG_DWORD,
(LPBYTE) &this->debug_ , sizeof (this->debug_));
ACE_ASSERT (err == ERROR_SUCCESS);
err = ACE_TEXT_RegSetValueEx( key, ACE_TEXT("Name"), 0, REG_SZ,
(LPBYTE) this->name_.c_str (), (DWORD) this->name_.length () + 1);
ACE_ASSERT (err == ERROR_SUCCESS);
DWORD tmpint = this->notify_imr_;
err = ACE_TEXT_RegSetValueEx (key, ACE_TEXT("NotifyImR"), 0, REG_DWORD,
(LPBYTE) &tmpint , sizeof (tmpint));
ACE_ASSERT (err == ERROR_SUCCESS);
tmpint = this->env_buf_len_;
err = ACE_TEXT_RegSetValueEx(key, ACE_TEXT("EnvBufLen"), 0, REG_DWORD,
(LPBYTE) &tmpint , sizeof(tmpint));
ACE_ASSERT(err == ERROR_SUCCESS);
tmpint = this->max_env_vars_;
err = ACE_TEXT_RegSetValueEx(key, ACE_TEXT("MaxEnvVars"), 0, REG_DWORD,
(LPBYTE) &tmpint , sizeof(tmpint));
ACE_ASSERT(err == ERROR_SUCCESS);
err = ::RegCloseKey (key);
ACE_ASSERT (err == ERROR_SUCCESS);
#endif
return 0;
}
int
Activator_Options::load_registry_options ()
{
#if defined (ACE_WIN32)
HKEY key = 0;
// Create or open the parameters key
LONG err = ACE_TEXT_RegOpenKeyEx (SERVICE_REG_ROOT,
SERVICE_REG_PATH,
0,
KEY_READ,
&key
);
if (err != ERROR_SUCCESS)
{
// If there aren't any saved parameters, then that's ok.
return 0;
}
ACE_TCHAR tmpstr[4096];
DWORD sz = sizeof (tmpstr);
DWORD type = 0;
err = ACE_TEXT_RegQueryValueEx (key, ACE_TEXT("ORBInitOptions"), 0, &type,
(LPBYTE) tmpstr, &sz);
if (err == ERROR_SUCCESS)
{
ACE_ASSERT (type == REG_SZ);
tmpstr[sz - 1] = '\0';
this->cmdline_ = ACE_TEXT_ALWAYS_CHAR(tmpstr);
}
sz = sizeof(tmpstr);
err = ACE_TEXT_RegQueryValueEx (key, ACE_TEXT("IORFile"), 0, &type,
(LPBYTE) tmpstr, &sz);
if (err == ERROR_SUCCESS)
{
ACE_ASSERT (type == REG_SZ);
tmpstr[sz - 1] = '\0';
this->ior_output_file_ = tmpstr;
}
sz = sizeof(debug_);
err = ACE_TEXT_RegQueryValueEx (key, ACE_TEXT("DebugLevel"), 0, &type,
(LPBYTE) &this->debug_ , &sz);
if (err == ERROR_SUCCESS)
{
ACE_ASSERT (type == REG_DWORD);
}
sz = sizeof(tmpstr);
err = ACE_TEXT_RegQueryValueEx (key, ACE_TEXT("Name"), 0, &type,
(LPBYTE) tmpstr, &sz);
if (err == ERROR_SUCCESS)
{
ACE_ASSERT (type == REG_SZ);
tmpstr[sz - 1] = '\0';
this->name_ = ACE_TEXT_ALWAYS_CHAR(tmpstr);
}
DWORD tmpint = 0;
sz = sizeof(tmpint);
err = ACE_TEXT_RegQueryValueEx (key, ACE_TEXT("NotifyImR"), 0, &type,
(LPBYTE) &tmpint , &sz);
if (err == ERROR_SUCCESS)
{
ACE_ASSERT (type == REG_DWORD);
}
this->notify_imr_ = tmpint != 0;
err = ACE_TEXT_RegQueryValueEx(key, ACE_TEXT("EnvBufLen"), 0, &type,
(LPBYTE) &tmpint , &sz);
if (err == ERROR_SUCCESS) {
ACE_ASSERT(type == REG_DWORD);
}
this->env_buf_len_ = tmpint;
err = ACE_TEXT_RegQueryValueEx(key, ACE_TEXT("MaxEnvArgs"), 0, &type,
(LPBYTE) &tmpint , &sz);
if (err == ERROR_SUCCESS) {
ACE_ASSERT(type == REG_DWORD);
}
this->max_env_vars_ = tmpint;
err = ::RegCloseKey (key);
ACE_ASSERT(err == ERROR_SUCCESS);
#endif /* ACE_WIN32 */
return 0;
}
bool
Activator_Options::service (void) const
{
return this->service_;
}
bool
Activator_Options::notify_imr (void) const
{
return this->notify_imr_;
}
unsigned int
Activator_Options::induce_delay (void) const
{
return this->induce_delay_;
}
unsigned int
Activator_Options::debug (void) const
{
return this->debug_;
}
const ACE_TString&
Activator_Options::ior_filename (void) const
{
return this->ior_output_file_;
}
Activator_Options::SERVICE_COMMAND
Activator_Options::service_command (void) const
{
return this->service_command_;
}
const char*
Activator_Options::cmdline (void) const
{
return this->cmdline_.c_str ();
}
const ACE_CString&
Activator_Options::name (void) const
{
return this->name_;
}
int
Activator_Options::env_buf_len (void) const
{
return this->env_buf_len_;
}
int
Activator_Options::max_env_vars (void) const
{
return this->max_env_vars_;
}
bool
Activator_Options::detach_child (void) const
{
return this->detach_child_;
}
|