summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/FTRT_Event_Service/Event_Service/Crash_Injector.cpp
blob: 93d43bb38b8cc8e2b752e61b4cb15f03c5f20170 (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
// $Id$
#include "Crash_Injector.h"
#include "Crash_Injector_Initializer.h"
#include "ace/Get_Opt.h"

#ifndef WIN32
#include <sys/time.h>
#endif

extern "C" void crash_handler(int)
{
    ACE_DEBUG((LM_DEBUG, "FTRT_Event_Service crashed\n"));
    exit(1);
}


namespace {
  Crash_Injector* injector;

  const char* types[] = {
    "immediate",
      "after",
      "timeout:"
  };
}

Crash_Injector::Crash_Injector()
: crash_type_(CRASH_IMMEDIATELY)
, num_of_invocation_(0)
, done_(false)
{
  ACE_Service_Config::static_svcs ()->insert (&ace_svc_desc_Crash_Injector);

  PortableInterceptor::ORBInitializer_var orb_initializer = new Crash_Injector_Initializer;

  PortableInterceptor::register_orb_initializer (orb_initializer.in ());
  injector = this;
}

Crash_Injector::~Crash_Injector()
{
}

Crash_Injector* Crash_Injector::instance()
{
  return injector;
}


int Crash_Injector::init (int argc, ACE_TCHAR* argv[] )
{
  ACE_Get_Opt get_opt(argc, argv, ACE_LIB_TEXT("n::o::t::"), 0, 0, ACE_Get_Opt::PERMUTE_ARGS, 1);
  get_opt.long_option(ACE_LIB_TEXT("num_invocation"), 'n', ACE_Get_Opt::ARG_OPTIONAL);
  get_opt.long_option(ACE_LIB_TEXT("operation"), 'o', ACE_Get_Opt::ARG_OPTIONAL);
  get_opt.long_option(ACE_LIB_TEXT("type"), 't', ACE_Get_Opt::ARG_OPTIONAL);

  int opt;

  while ((opt = get_opt ()) != EOF)
  {
    switch (opt) {
      case 'n':
        if (get_opt.opt_arg ())
          crash_after_number_of_invocation_ = ACE_OS::atoi(get_opt.opt_arg ());
        break;
      case 'o':
        if (get_opt.opt_arg ())

          operation_ = get_opt.opt_arg ();
        break;
      case 't':
        if (get_opt.opt_arg ()) {
          if (ACE_OS::strcasecmp(get_opt.opt_arg (), "immediate") == 0)
            crash_type_ = CRASH_IMMEDIATELY;
          else if (ACE_OS::strcasecmp(get_opt.opt_arg (), "after_reply") == 0)
            crash_type_ = CRASH_AFTER_REPLY;
          else if (ACE_OS::strncasecmp("timeout:", get_opt.opt_arg (), 8)==0)

          {
            crash_type_ = CRASH_TIMEOUT;
            this->time_to_crash_in_ms_  = ACE_OS::atoi(get_opt.opt_arg ()+8);
            if (this->time_to_crash_in_ms_ ==0)
              ACE_ERROR_RETURN((LM_ERROR, "Crash_Injector : Invalid timeout value\n"), -1);
          }
        }
        break;
      default:
        return -1;
    }
  }
  return 0;
}

void Crash_Injector::check_on_receive_request(const char* operation)
{
  if (crash_type_ < CRASH_AFTER_REPLY && ACE_OS::strcmp(operation_.c_str() ,operation) == 0 )
    if (crash_after_number_of_invocation_ == ++num_of_invocation_)
    {
      switch (crash_type_) {
        case CRASH_IMMEDIATELY:
          exit(1);
        case CRASH_TIMEOUT:
          this->crash_timeout();
      }
    }
}

void Crash_Injector::check_on_sending_reply(const char* operation)
{
  if (crash_type_ == CRASH_AFTER_REPLY && ACE_OS::strcmp(operation_.c_str() ,operation) == 0 )
  {
    if (crash_after_number_of_invocation_ == ++num_of_invocation_)
      done_ = true;
  }
}



void Crash_Injector::crash_timeout()
{
#ifndef WIN32
    if (time_to_crash > 0) {
      signal(SIGALRM, &crash_handler);
      struct itimerval in, out;
      in.it_value.tv_sec = time_to_crash/1000;
      in.it_value.tv_usec = (time_to_crash%1000)*1000;
      setitimer(ITIMER_REAL, &in, &out);
    }
    else
      exit(1);
#endif

}


ACE_FACTORY_DEFINE (CRASH, Crash_Injector)


ACE_STATIC_SVC_DEFINE (Crash_Injector,
    ACE_TEXT("Crash_Injector"),
    ACE_SVC_OBJ_T,
    &ACE_SVC_NAME (Crash_Injector),
    ACE_Service_Type::DELETE_THIS
    | ACE_Service_Type::DELETE_OBJ,
    0)