Changeset 231 for Evidence/Evidence.cc


Ignore:
Timestamp:
06/24/10 14:25:18 (14 years ago)
Author:
ogrimm
Message:
Bug fixes following previous update
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Evidence.cc

    r229 r231  
    1010    this severity, the call to Message() is guranteed not to return).
    1111  - Configuration data can be requested by GetConfig() and non-blocking by GetConfigNB().
    12   - If the configuration file changes the signal SIGUSR1 is emitted which is caught by the standard
     12  - If the configuration file changes a signal can be emitted which is caught by the standard
    1313    signal handler. The handler invokes ConfigChanged() which can be redefined by the user application.
    1414        The signal is delivered only to the main thread (where the constructor is executed) and thus
    15         blocking rpc can be made from it.
     15        blocking rpc can be made from it. The signal is set using ActivateSignal().
    1616  - Signal handlers to ignore common signals are installed.
    1717    These signals will then cause pause() to return which can be used
     
    4545  ConfigTimeStamp = 0;
    4646 
    47   // SIGUSR2 delivered to this thread if configuration file changes
     47  // Signal delivered to this thread if configuration file changes
    4848  ThreadID = pthread_self();
    4949
     
    7070  Unlock();
    7171
    72   if (pthread_kill(ThreadID, SIGUSR2) != 0) {
    73         ThisServer->Message(ThisServer->WARN, "Could not send signal SIGUSR2 to main thread");
     72  if (pthread_kill(ThreadID, ThisServer->ConfigSignal) != 0) {
     73        ThisServer->Message(ThisServer->WARN, "Could not send signal to main thread");
    7474  }
    7575}
     
    111111        }
    112112        else {
    113           if (Config.getSize() == 0) Result = Default;
     113          if (Config.getSize() <= 1) Result = Default;
    114114          else Result = string(Config.getString(), Config.getSize()-1); // Retrieve string safely
    115115    }
     
    143143}
    144144
    145 // Locking and unlocking for list access. Signal SIGUSR2 is also blocked.
     145// Locking and unlocking for list access
     146// Signal blocked before locking to avaoid dead-lock by calling GetConfig() from ConfigChanged().
    146147void EvidenceServer::Config::Lock() {
    147148
    148149  int Ret = 0;
    149150  sigset_t Set;
    150   //printf("Locking %u\n", pthread_self());
    151   Ret += abs(sigemptyset(&Set));
    152   Ret += abs(sigaddset(&Set, SIGUSR2));
    153   Ret += abs(pthread_sigmask(SIG_BLOCK, &Set, NULL));
     151
     152  if (ThisServer->ConfigSignal != 0) {
     153        Ret += abs(sigemptyset(&Set));
     154        Ret += abs(sigaddset(&Set, ThisServer->ConfigSignal));
     155        Ret += abs(pthread_sigmask(SIG_BLOCK, &Set, NULL));
     156  }
    154157  Ret += abs(pthread_mutex_lock(&Mutex));
    155158 
     
    163166  int Ret = 0;
    164167  sigset_t Set;
    165   //printf("  Unlocking %u\n", pthread_self());
    166168
    167169  Ret += abs(pthread_mutex_unlock(&Mutex));
    168   Ret += abs(sigemptyset(&Set));
    169   Ret += abs(sigaddset(&Set, SIGUSR2));
    170   Ret += abs(pthread_sigmask(SIG_UNBLOCK, &Set, NULL));
     170  if (ThisServer->ConfigSignal != 0) {
     171        Ret += abs(sigemptyset(&Set));
     172        Ret += abs(sigaddset(&Set, ThisServer->ConfigSignal));
     173        Ret += abs(pthread_sigmask(SIG_UNBLOCK, &Set, NULL));
     174  }
    171175
    172176  if (Ret != 0) {
     
    179183// EvidenceServer Class //
    180184//////////////////////////
     185
     186int EvidenceServer::ConfigSignal = 0;
    181187
    182188// Constructor starts server with given name
     
    195201  signal(SIGHUP, &SignalHandler);   // Terminal closed
    196202 
    197   struct sigaction S;
    198   S.sa_handler = &SignalHandler;
    199   S.sa_flags = SA_RESTART;
    200   sigaction(SIGUSR2, &S, NULL);
    201 
    202203  // Catch C++ unhandled exceptions
    203204  set_terminate(Terminate);
    204205
    205   // Configuration class (instantiate after signal handling for SIGUSR2 installed)
     206  // Configuration class (must be instantiate after signal handling installed)
    206207  ConfClass = new class Config(Name);
    207208
     
    310311}
    311312
     313// Signal emitted when configuraton file changes, signal handler calls ConfigChanged()
     314void EvidenceServer::ActivateSignal(int Signal) {
     315
     316  struct sigaction S;
     317
     318  ConfigSignal = Signal;
     319  S.sa_handler = &SignalHandler;
     320  S.sa_flags = SA_RESTART;
     321  sigaction(Signal, &S, NULL);
     322}
     323
     324
    312325// ====== Static methods ======
    313326
     
    317330  static bool Called = false;
    318331
    319   // If SIGUSR2, invoke call-back for configuraton change
    320   if (Signal == SIGUSR2) {
     332  // If signal indicates configuration change, invoke call-back
     333  if (Signal == EvidenceServer::ConfigSignal) {
    321334    ThisServer->ConfigChanged();
    322335        return;
     
    338351void EvidenceServer::Terminate() {
    339352
    340   static char Msg[STATUS_SIZE];
     353  ostringstream Msg;
    341354  static bool Terminating = false;
    342355
    343356  if (Terminating) {
    344         snprintf(Msg, sizeof(Msg), "%s: Terminate() called recursively, calling abort()", ThisServer->MessageService->getName());
    345         printf("%s\n", Msg);
    346         DimClient::sendCommandNB("DColl/Log", Msg);
     357        Msg << ThisServer->Name << ": Terminate() called recursively, calling abort()";
     358        printf("%s\n", Msg.str().c_str());
     359        ThisServer->SendToLog(Msg.str().c_str());
    347360        abort();
    348361  }
     
    358371
    359372        Demangled = abi::__cxa_demangle(Type->name(), 0, 0, &Status);
    360         snprintf(Msg, sizeof(Msg), "Terminate() called after throwing an instance of '%s'", Status==0 ? Demangled : Type->name());
     373        Msg << "Terminate() called after throwing an instance of '" << (Status==0 ? Demangled : Type->name()) << "'";
    361374        free(Demangled);
    362375
     
    364377        try { __throw_exception_again; }
    365378        catch (exception &E) {
    366           snprintf(Msg+strlen(Msg), sizeof(Msg)-strlen(Msg), " (what(): %s)", E.what());         
     379          Msg << " (what(): " << E.what() << ")";         
    367380        }
    368381        catch (...) { }
    369382  }
    370   else snprintf(Msg, sizeof(Msg), "Terminate() called without an active exception");
    371 
    372   ThisServer->Message(FATAL, Msg);
     383  else Msg << "Terminate() called without an active exception";
     384
     385  ThisServer->Message(FATAL, Msg.str().c_str());
    373386}
    374387
Note: See TracChangeset for help on using the changeset viewer.