summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/Options.cpp
blob: 3e4b9bfbf9d3d600072d5592b26b2b3aae08f487 (plain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//=============================================================================
/**
 *  @file    Options.cpp
 *
 *  $Id$
 *
 *  @author Darrell Brunsch <brunsch@cs.wustl.edu>
 */
//=============================================================================

#include "Options.h"
#include "NT_Service.h"
#include "ace/Arg_Shifter.h"
#include "ace/ARGV.h"
#include "ace/Configuration.h"

#if defined (ACE_WIN32)
const HKEY SERVICE_REG_ROOT = HKEY_LOCAL_MACHINE;
const ACE_TCHAR *SERVICE_REG_PATH =
  ACE_TEXT ("SYSTEM\\CurrentControlSet\\Services\\TAOImplRepo\\Parameters");
const ACE_TCHAR *SERVICE_REG_VALUE_NAME = ACE_TEXT ("ORBOptions");
#endif /* ACE_WIN32 */


/**
 * Default Constructor.  Assigns default values to all the member variables.
 */
Options::Options ()
  : config_ (0)
  , debug_ (1)
  , ior_output_file_ (0)
  , multicast_ (0)
  , ping_interval_ (0, 200)
  , service_ (0)
  , startup_timeout_ (5)
  , readonly_ (0)
{
}


/**
 * Destructor.  Just deletes this->config_.
 */
Options::~Options ()
{
  delete this->config_;
}


/**
 * parse_args uses an ACE_Arg_Shifter to grab all the options that are
 * specific to the ImR.
 *
 * @retval  0 Success
 * @retval -1 Error parsing args
 * @retval  1 Success but we should exit.
 */
int
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)
        {
          // Run the service command
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -c option needs a command\n"));
              return -1;
            }

          if (this->run_service_command (shifter.get_current ()) != 0)
            return -1;

          // Since we just ran a command, we will exit right away.
          return 1;
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-d")) == 0)
        {
          // Set the debug level
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -d option needs a debuglevel\n"));
              return -1;
            }

          this->debug_ = ACE_OS::atoi (shifter.get_current ());
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-l")) == 0)
        {
          // Lock the database
          this->readonly_ = 1;
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-m")) == 0)
        {
          // multicast?
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -m option requires 1/0\n"));
              return -1;
            }

          this->multicast_ = ACE_OS::atoi (shifter.get_current ());
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-o")) == 0)
        {
          // Output the IOR to a file.
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -o option needs a filename\n"));
              return -1;
            }

          this->ior_output_file_ = ACE_OS::fopen (shifter.get_current (), "w");
          if (this->ior_output_file_ == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Error: Unable to open %s for writing: %p\n",
                               shifter.get_current ()), -1);
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-p")) == 0)
        {
          // Initialize file persistence
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -p option needs a filename\n"));
              return -1;
            }

          if (this->initialize_file_persistence (shifter.get_current ()) != 0)
            return -1;
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-r")) == 0)
        {
          // win32 registry implementation
          if (this->initialize_registry_persistence () != 0)
            return -1;
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-s")) == 0)
        {
          // Run as a service
          this->service_ = 1;
        }
      else if (ACE_OS::strcasecmp (shifter.get_current (),
                                   ACE_TEXT ("-t")) == 0)
        {
          // Set timeout value
          shifter.consume_arg ();

          if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
            {
              ACE_ERROR ((LM_ERROR, "Error: -t option needs a value\n"));
              return -1;
            }

          this->startup_timeout_ =
            ACE_Time_Value (ACE_OS::atoi (shifter.get_current ()));
        }
      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
        {
          shifter.ignore_arg ();
          continue;
        }

      shifter.consume_arg ();
    }

  return 0;
}

/**
 * @retval  0 Success
 * @retval -1 Error parsing args
 * @retval  1 Success but we should exit.
 */
int
Options::init (int argc, ACE_TCHAR *argv[])
{
  ACE_ARGV orb_args;
  int i = 0;
  int result = 0;

  // Make an initial pass through and grab the arguments that we recognize.

  result = this->parse_args (argc, argv);

  if (result != 0)
    return result;

  // Save the leftovers to a ACE_ARGV class

  for (i = 1; i < argc; ++i)
    {
      if (orb_args.add (argv[i]) == -1)
        {
          ACE_ERROR ((LM_ERROR, "Error: Could not save argument"));
          return -1;
        }
    }

#if defined (ACE_WIN32)
  // Load any arguments from the registry

  if (this->load_registry_options (orb_args) != 0)
    return -1;
#endif /* ACE_WIN32 */

  // Make another pass to catch any ImR arguments that may be in the
  // registry

  int orb_argc = orb_args.argc ();

  result = this->parse_args (orb_argc, orb_args.argv ());

  if (result != 0)
    return result;

  // Now initialize the orb and pass it the leftover arguments

  ACE_TRY_NEW_ENV
    {

      this->orb_ = CORBA::ORB_init (orb_argc,
                                    orb_args.argv (),
                                    0
                                    TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_ERROR ((LM_ERROR, "Error: Cannot initialize ORB\n"));
      return -1;
    }
  ACE_ENDTRY;

  // If there are any arguments left (besides the executable filename)
  // then they were not picked up by us or the orb and must be
  // unrecognized.

  if (orb_argc > 1)
    {
      // Just print out the first option as an error
      ACE_ERROR ((LM_ERROR,
                  "Unrecognized option: %s\n",
                  orb_args.argv ()[1]));
      return -1;
    }

  // If no persistent implementation specified, use a simple heap.
  if (this->config_ == 0)
    if (this->initialize_non_persistence () != 0)
      return -1;

  // Indicates successful parsing of command line.
  return 0;
}


/**
 * Just print out the usage message to STDERR
 */
void
Options::print_usage (void) const
{
  ACE_ERROR ((LM_ERROR,
              "Usage:\n"
              "\n"
              "ImplRepo_Service [-c cmd] [-d lvl] [-l] [-m 0/1] [-o file]"
              " [-r|-p file] [-r] [-s] [-t secs]\n"
              "\n"
              "  -c command  Runs service commands ('install' or 'remove')\n"
              "  -d level    Sets the debug level\n"
              "  -l          Lock the database\n"
              "  -m [0/1]    Turn on(1)/off(0) multicast (default: 1)\n"
              "  -o file     Outputs the ImR's IOR to a file\n"
              "  -p file     Use file for storing/loading settings\n"
              "  -r          Use the registry for storing/loading settings\n"
              "  -s          Runs as a service (NT Only)\n"
              "  -t secs     Timeout used for killing unresponsive servers\n")
             );
}


/**
 * The most portable form of persistence is file persistence.  Here
 * we assign an ACE_Configuration_Heap object using @param filename
 * as the file.
 *
 * @retval  0 Success
 * @retval -1 Failure
 */
int
Options::initialize_file_persistence (const ACE_TCHAR *filename)
{
  if (this->config_ != 0)
  {
    ACE_ERROR ((LM_ERROR,
                "Error: initialize_file_persistence (): "
                "Configuration already defined. \n"
                "Did you pass multiple persistence options?\n"));
    return -1;
  }

  ACE_Configuration_Heap *heap = 0;
  ACE_NEW_RETURN (heap, ACE_Configuration_Heap, -1);

  if (heap->open (filename) == 0)
    {
      this->config_ = heap;
      return 0;
    }

  delete heap;
  heap = 0;

  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("Error: Opening persistent heap file '%s'\n"),
              filename));

  return -1;
}


/**
 * On Windows, we have the option of using the Registry to store the
 * server data.  Assigns a ACE_Configuration_Win32Registry to
 * this->config_.  On non-Win32 systems, just returns an error.
 *
 * @todo Where in the registry should this be stored?
 *
 * @retval  0 Success
 * @retval -1 Failure
 */
int
Options::initialize_registry_persistence (void)
{
#if defined (ACE_WIN32)
  if (this->config_ != 0)
  {
    ACE_ERROR ((LM_ERROR,
                "Error: initialize_registry_persistence (): "
                "Configuration already defined. \n"
                "Did you pass multiple persistence options?\n"));
    return -1;
  }

  HKEY root =
    ACE_Configuration_Win32Registry::resolve_key(HKEY_LOCAL_MACHINE,
                                                 "Software\\TAO\\IR");
  ACE_NEW_RETURN (this->config_,
                  ACE_Configuration_Win32Registry(root),
                  -1);
  return 0;
#else /* ACE_WIN32 */
  ACE_ERROR_RETURN ((LM_ERROR, "Registry not supported on this platform"), -1);
#endif /* ACE_WIN32 */
}


/**
 * In cases where persistence isn't needed, create an object of
 * the ACE_Configuration_Heap class to be used.  Initializes
 * this->config_ to an opened ACE_Configuration_Heap.
 *
 * @retval 0  Success
 * @retval -1 Failure
 */
int
Options::initialize_non_persistence (void)
{
  ACE_Configuration_Heap *heap = 0;
  ACE_NEW_RETURN (heap, ACE_Configuration_Heap, -1);

  if (heap->open () == 0)
    {
      this->config_ = heap;
      return 0;
    }

  delete heap;
  heap = 0;

  ACE_ERROR ((LM_ERROR,
             ACE_TEXT ("Error: Opening ACE_Configuration heap\n")));

  return -1;
}


/**
 * Executes the various commands that are useful for a NT service.  Right
 * now these include 'install' and 'remove'.  Others, such as 'start' and
 * 'stop' can be added, but the 'net' program in Windows already handles
 * these commands.
 *
 * @todo Finish implementing Options::run_service_command
 * @todo Update to unicode
 */
int
Options::run_service_command (const ACE_TCHAR *command)
{
#if defined (ACE_WIN32)
  SERVICE::instance ()->name (IMR_SERVICE_NAME, IMR_DISPLAY_NAME);

  if (ACE_OS::strcmp (command, ACE_TEXT ("install")) == 0)
    {
      ACE_TCHAR pathname[_MAX_PATH * 2 + 3];  // +3 for the ' -s' at the end

      if (ACE_TEXT_GetModuleFileName(NULL, pathname, _MAX_PATH * 2) == 0)
        {
          ACE_ERROR ((LM_ERROR, "Error: Could not get module file name.\n"));
          return -1;
        }

      // Append the command used for running the implrepo as
      ACE_OS::strcat (pathname, ACE_TEXT (" -s"));

      return SERVICE::instance ()->insert (SERVICE_AUTO_START,
                                           SERVICE_ERROR_IGNORE,
                                           pathname);
    }
  else if (ACE_OS::strcmp (command, ACE_TEXT ("remove")) == 0)
    {
      return SERVICE::instance ()->remove ();
    }

  ACE_ERROR ((LM_ERROR, "Error: Unrecognized command: %s\n", command));
  return -1;

#else /* ACE_WIN32 */
  ACE_UNUSED_ARG (command);
  ACE_ERROR ((LM_ERROR, "Service not supported on this platform"));

  return -1;
#endif /* ACE_WIN32 */
}

/**
 *  We will only load from the registry if we are a service.
 *  The location we store options in is HKEY_LOCAL_MACHINE:
 *  SYSTEM\CurrentControlSet\Services\TAOImplRepo\Parameters
 *
 *  The only key currently supported is ORBOptions
 *
 *  @todo Is there a better way to handle the hKey? (as in a auto_ptr)
 */
int
Options::load_registry_options (ACE_ARGV &orb_options)
{
#if defined (ACE_WIN32)
  // Only if we are running as a service
  if (!this->service ())
  {
    if (this->debug () > 1)
      ACE_DEBUG ((LM_DEBUG,
                  "Not running as a service, will not load data from "
                  "registry\n"));
    return 0;
  }

  HKEY hKey = 0;
  BYTE buffer[ACE_DEFAULT_ARGV_BUFSIZ] = {0};

  if (ACE_TEXT_RegOpenKeyEx (SERVICE_REG_ROOT,
                             SERVICE_REG_PATH,
                             0,
                             KEY_READ,
                             &hKey) != ERROR_SUCCESS)
    {
      if (this->debug () > 1)
        {
          ACE_DEBUG ((LM_DEBUG, "Could not open Registry Key, skipping\n"));
        }

      return 0;
    }

  DWORD dwType = 0;
  DWORD dwSize = sizeof (buffer);

  if (ACE_TEXT_RegQueryValueEx (hKey,
                                SERVICE_REG_VALUE_NAME,
                                NULL,
                                &dwType,
                                buffer,
                                &dwSize) != ERROR_SUCCESS)
    {
      if (this->debug () > 1)
        {
          ACE_DEBUG ((LM_DEBUG, "Could not read Registry Key, skipping\n"));
          ::RegCloseKey (hKey);
          return 0;
        }
    }

  if (dwType != REG_MULTI_SZ)
    {
      ACE_ERROR ((LM_ERROR, "Error: ORB Options registry key not MULTI_SZ\n"));
     ::RegCloseKey (hKey);
      return -1;
    }

  // Skip the case where there are no arguments
  if (dwSize > 1)
    {
      // Create an argv array
      ACE_TCHAR **orb_argv = 0;

      ACE_NEW_RETURN (orb_argv, ACE_TCHAR *[dwSize], -1);

      ACE_TCHAR *tchar_buffer = ACE_reinterpret_cast (ACE_TCHAR *, buffer);
      orb_argv[0] = tchar_buffer;

      if (this->debug () > 1)
        ACE_DEBUG ((LM_DEBUG,
                    "Registry Argument Added: %s\n",
                    orb_argv[0]));

      int orb_argv_pos = 1;

      for (unsigned int buffer_pos = 0;
           buffer_pos < dwSize / sizeof (ACE_TCHAR) - 2;
           ++buffer_pos)
        {
          if (tchar_buffer[buffer_pos] == 0)
            {
              if (this->debug () > 1)
                ACE_DEBUG ((LM_DEBUG,
                            "Registry Argument Added: %s\n",
                            &tchar_buffer[buffer_pos] + 1));

              orb_argv[orb_argv_pos++] = &tchar_buffer[buffer_pos] + 1;
            }
        }

      orb_argv[orb_argv_pos] = 0;

      orb_options.add (orb_argv);

      delete [] orb_argv;
    }

  if (::RegCloseKey (hKey) != ERROR_SUCCESS)
    {
      ACE_ERROR ((LM_ERROR, "Error: Could close Registry Key\n"));
      return -1;
    }

  return 0;
#else /* ACE_WIN32 */
  ACE_UNUSED_ARG (orb_options);
  ACE_ERROR ((LM_ERROR, "Service not supported on this platform"));

  return -1;
#endif /* ACE_WIN32 */
}

/**
 * Standalone Mode
 *
 * @retval 0 Run as standalone service
 * @retval 1 Run as a service (only on NT right now)
 */
int
Options::service (void) const
{
  return this->service_;
}


/**
 * Debug level for the IR.
 *
 * @retval 0 Quiet
 * @retval 1 Trace messages
 * @retval 2 Detailed messages
 */
unsigned int
Options::debug (void) const
{
  return this->debug_;
}


/**
 * @return The file where the IOR will be stored.
 */
FILE *
Options::output_file (void) const
{
  return this->ior_output_file_;
}


/**
 * @return The amount of time to wait for a server to start.
 */
const ACE_Time_Value &
Options::startup_timeout (void) const
{
  return this->startup_timeout_;
}


/**
 * @return The amount of time to wait between pings
 */
const ACE_Time_Value &
Options::ping_interval (void) const
{
  return this->ping_interval_;
}

/**
 * @return The ACE_Configuration object that is used to store data.
 */
ACE_Configuration *
Options::config (void) const
{
  return this->config_;
}


/**
 * @return A pointer to the ORB.
 */
CORBA::ORB_ptr
Options::orb (void) const
{
  return CORBA::ORB::_duplicate (this->orb_.in ());
}


/**
 * @retval 0 Do not listen for multicast location requests.
 * @retval 1 Do Listen.
 */
int
Options::multicast (void) const
{
  return this->multicast_;
}

/**
 * @retval 0 Normal operation.
 * @retval 1 Do not let server info be modified.
 */
int
Options::readonly (void) const
{
  return this->readonly_;
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Singleton <Options, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Singleton <Options, ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */