summaryrefslogtreecommitdiff
path: root/application/agent/src/processManager.cpp
blob: 965e7b260a2382afe91dc0d1a35c75d347f6392f (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
/*
 ****************************************************************************
 *                            PROGRAM MODULE
 *
 *     $Workfile:   agent.cpp  $
 *     $Author: poullain $
 *     $Date: 2005/07/18 13:26:53 $
 *
 *       Copyright (C) 2004 by SPiDCOM Technologies All rights reserved.
 *
 ****************************************************************************
 */

#ifndef _eventManager_cpp
#define _eventManager_cpp

#include "macro.h"

#include "processManager.h"

//#include "eventListener.h"
//#include "requestManager.h"

#include "agent_pp/system_group.h"
#include <mib-interface.h>

#ifndef WIN32
#include <signal.h>
#endif

#include "mib-common.h"
#include "systemmanager.h"
#include "AlarmListManager.h"
#include "eventListener.h"


#include "agent_pp/agent++.h"
#include "agent_pp/system_group.h"


// quick fix for SNMP bug when master crashes with many slaves
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_cond  = PTHREAD_COND_INITIALIZER;

//using namespace threads_namespace;
using namespace agentsnmp;

//Singleton instance
processManager* processManager::m_instance_p = 0;

//Table of threads
pthread_t processManager::m_threadsTable[NB_THREADS];





/**
 *  Handler for Interruption signal in main process => kill all threads and release memory
 */
void killAllThreads(int sigNb)
{ 
    int status = 0;
     switch (sigNb)
     {
        case SIGINT:
        case SIGTERM:
           {
              cout<<"user abort"<<endl;
              processManager::instance()->killThreads(sigNb);
              processManager::instance()->freeMemory();
              SystemManager::instance()->freeMemory();
              AlarmListManager::instance()->freeMemory();
              //exit(0);
              break;
           }
        case SIGSEGV:
           {
              pthread_t interruptedThread = pthread_self();
              cout<<"Segmentation fault in Thread id: "<< interruptedThread<<endl;
              processManager::instance()->killThreads(interruptedThread, sigNb);
              //pthread_kill(interruptedThread, SIGUSR1);
              processManager::instance()->freeMemory();
              SystemManager::instance()->freeMemory();
              AlarmListManager::instance()->freeMemory();
             // exit(1);
              status = 1;
              break;
           }
        default:
            {
              cout<<"Unhandled signal"<<endl;
              status = 1; 
              //exit(1);
              break;
             }
     }

    // processManager::instance()->freeMemory();
    // SystemManager::instance()->freeMemory();
    // AlarmListManager::instance()->freeMemory();
     cout<<"Aborting Program"<<endl;
     exit(status);
    // processManager::instance()->killThreads(interruptedThread, sigNb);
}




/**
 *  Manager the incommong SNMP requests
 */
void* requestManagerRun(void* varg)
{
     //u_short* port = (u_short*) varg;
     u_short* port = static_cast<u_short*> (varg);
     DEBUG_IF((port==0), "port is null ponteur<<endl;");
     if (0 != port)
        processManager::instance()->startRequestManager(*port);
     else //port chosen by default: 161
        processManager::instance()->startRequestManager(161);
     return 0;
}


#ifdef TRAP_SUPPORT
/**
 *  Manager the incommong events from kernel
 */
void* eventListenerRun(void* varg)
{
    processManager::instance()->startEventListener();
    return 0;
}
#endif


/**
 *  Manager the polling of the alarm OIDs
 */
void* alarmManagerRun(void* varg)
{
    processManager::instance()->startAlarmManager();
    return 0;
}


void processManager::killThreads(pthread_t interruptedThread, int sigNb)
{
     DEBUG("kill threads in process manager"<<endl);

     for (int i=0;i<NB_THREADS;++i)
     {
         if (!(pthread_equal(m_threadsTable[i], interruptedThread)))
         {
            DEBUG("kill thread nb: "<<i<<endl);
            pthread_kill(m_threadsTable[i], SIGUSR1);
         }
     }

}



void processManager::killThreads(int sigNb)
{
     DEBUG(cout<<"kill all threads in process manager"<<endl);

     for (int i=0;i<NB_THREADS;++i)
     {
            pthread_kill(m_threadsTable[i], SIGUSR1);
     }

}



processManager::processManager() 
{
    m_mib = 0;
    m_reqList = 0;
    m_port = 0;	  ;
}


#ifdef TRAP_SUPPORT
void processManager::startEventListener()
{
     eventListener::instance()->run();
}
#endif

void processManager::startAlarmManager()
{
    while (true)
    {
        sleep(15);
        SystemManager::instance()->checkAllAlarms();
    }

    return;
}

void processManager::startRequestManager(u_short port) 
{
#ifndef WIN32
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = killAllThreads;
    sigemptyset(&(action.sa_mask));

    if ( sigaction(SIGSEGV, &action, NULL) < 0 )
    {
      DEBUG("Cannot set SIGSEGV signal!" << endl);
    }
#endif

    int status;
    m_port = port;
    Snmp::socket_startup();  // Initialize socket subsystem
    Snmpx snmp(status, m_port );

    if (status != SNMP_CLASS_SUCCESS) 
    {
        cerr << "main: SNMP port [" << port << "] init failed." << endl;
        cerr << "Please check that it is not already used by another application." << endl;
        exit(1);
    }

    m_mib	  = new Mib();
    m_reqList = new RequestList();


#ifndef _NO_LOGGING
    // set the DefaultLog used for log feature 
#ifndef REMOTE_LOG
    char* user = getenv("USER");
    char location[100] = "/tmp";	
    if ( NULL != user ) {
        strcat(location,"/");
        strcat(location,user);
        mkdir(location,0777);
    }
    strcat(location,"/sm-agent.log");
    AgentLog* agentLog = new AgentLogImpl(location);
#else
    //printf("remote logger: before creation\n");
    AgentLog* agentLog = new AgentRemoteLog("192.168.6.9",5501);
    //printf("remote logger: after creation\n");
#endif // REMOTE_LOG
    DefaultLog::init(agentLog);
    //printf("DefaultLog initialized\n");
#endif // _NO_LOGGING



    // register requestList for outgoing requests
    m_mib->set_request_list(m_reqList);	

#ifdef __LINUX__
    init_socket();
#endif //__LINUX__

    // get agent config from file
    init_agent_config();    //---- FD01: SNMP community

    // mib initialisation
    SystemManager::instance()->Init( *m_mib );
    SystemManager::instance()->setMib(m_mib);
    m_mib->init();

    // request list initialisation
    m_reqList->set_snmp(&snmp);

    char snmp_GET_community_name[ASN_MAX_NAME_LEN];
    char snmp_SET_community_name[ASN_MAX_NAME_LEN];

    SystemManager::instance()->getSNMPCommunity( snmp_GET_community_name, snmp_SET_community_name );

    m_reqList->set_read_community( OctetStr(snmp_GET_community_name));
    m_reqList->set_write_community( OctetStr(snmp_SET_community_name));

    Request* req = 0;

    LOG_BEGIN(DEBUG_LOG);
    LOG("agent has been started");
    LOG_END;

    SnmpTarget::set_default_timeout(1000);

    //generate a segmentation fault in thread
    int segfault = 0;
    // bool mibCleaned = false;

    // signal the condition variable to allow eventListener thread to continue
    pthread_mutex_lock(&condition_mutex);
    pthread_cond_signal(&condition_cond);
    pthread_mutex_unlock(&condition_mutex);
    while (true)
    {
        req = m_reqList->receive(120);

        /* if (segfault>5)
        {
           cout<<"Manual segmentation fault generated for test"<<endl; 
           char* atoto=0;
           strncpy (atoto, "titi",5);
          //killAllThreads(SIGTERM);
        }*/

        if (req)
        {
            // cout<<"request received"<<endl;
            m_mib->process_request(req);
            //  mibCleaned = false;
            //  ++segfault;
        }
        else
        {
            //if (!mibCleaned)
            //{
                m_mib->cleanup();
                //  mibCleaned = true;
            //}
        }
    }
    delete m_reqList;
    delete m_mib;
#ifdef __LINUX__
    close_socket();
#endif //__LINUX__
}





/**
 * To get the only Singleton instance
 */
processManager* processManager::instance()
{
    if ( m_instance_p == 0 )
    {
        m_instance_p = new processManager();
    }

    return m_instance_p;
}








/**
 * To avoid singleton  memory leaks
 */
void processManager::freeMemory()
{

    if (0 != m_reqList) 
        delete m_reqList;

    if (0 != m_mib)
        delete m_mib;

    m_threadsTable[0]=0;
    m_threadsTable[1]=0;

    if ( m_instance_p != 0 )
    {
        delete m_instance_p;
        m_instance_p = 0;
    }
}



processManager::~processManager()
{

}







/*
commonThread* processManager::getNthThread(int i)
{
    if (i>NB_THREADS)
      return 0;
    else
	  return m_threadsTable[i];
}
*/


RequestList* processManager::getReqList()
{
    return m_reqList;
}


Mib* processManager::getMib()
{
    return m_mib;
}




void processManager::initThreads(u_short* port)
{
    // Alarm Initialisation
    //cout<<"instanciation of AlamrListManager in main"<<endl;
    //AlarmListManager::instance();

#ifndef WIN32
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = killAllThreads;
    sigemptyset(&(action.sa_mask));

    if ((sigaction(SIGINT, &action, NULL) < 0) || (sigaction(SIGTERM, &action, NULL) < 0)) // || (sigaction(SIGSEGV, &action, NULL)))
    {
      DEBUG("Cannot set SIGINT/SIGTERM signal!" << endl);
    }
#endif

   DEBUG("init Threads"<<endl); 


    int retcode = 0;

    //processManager::instance()->startRequestManager(*port);

    retcode = pthread_create(&m_threadsTable[1], 0, requestManagerRun, port);
    if (retcode != 0) {
      cerr << "create_thread: " << m_threadsTable[1] << " failed!" << endl;
    }
    DEBUG("SNMP Request Manager Thread"<<endl);

#ifdef TRAP_SUPPORT
    pthread_mutex_lock(&condition_mutex);
    pthread_cond_wait(&condition_cond, &condition_mutex);
    pthread_mutex_unlock(&condition_mutex);

    retcode = pthread_create(&m_threadsTable[0], 0, eventListenerRun, 0);
    if (retcode != 0) {
        cerr << "create_thread failed!" << endl;
     }
     DEBUG("Kernel Event Listener Thread"<<endl);
#endif

    retcode = pthread_create(&m_threadsTable[0], 0, alarmManagerRun, 0);
    if (retcode != 0) {
        cerr << "create_thread failed!" << endl;
     }
     DEBUG("Kernel Alarm Manager Thread"<<endl);


    for (int i=0; i <NB_THREADS ; ++i)  
    {
        pthread_join(m_threadsTable[i], 0);
        DEBUG("thread: " << m_threadsTable[i] <<" has joined"<<endl); 
    }
}





#endif