summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/Application.cpp
blob: adcd5ddfb69f275ee9524fe60847a86c66f9ff01 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/Application.h>
#include <IceUtil/StaticMutex.h>
#include <IceUtil/CtrlCHandler.h>
#include <IceUtil/Cond.h>
#include <Ice/GC.h>
#include <memory>

using namespace std;
using namespace Ice;
using namespace IceUtil;

//
// _mutex and _condVar are used to synchronize the main thread and
// the CtrlCHandler thread
//
static StaticMutex _mutex = ICE_STATIC_MUTEX_INITIALIZER;
static auto_ptr<Cond> _condVar;

//
// Variables than can change while run() and communicator->destroy() are running!
//
static bool _callbackInProgress = false;
static bool _destroyed = false;
static bool _interrupted = false;
static bool _released = false;
static CtrlCHandlerCallback _previousCallback = 0;

//
// Variables that are immutable during run() and until communicator->destroy() has returned;
// before and after run(), and once communicator->destroy() has returned, we assume that 
// only the main thread and CtrlCHandler threads are running.
//
static const char* _appName = 0;
static Application* _application;
static CommunicatorPtr _communicator;
static CtrlCHandler* _ctrlCHandler = 0;
static bool _nohup = false;

#ifdef _WIN32
const DWORD SIGHUP = CTRL_LOGOFF_EVENT;
#else
#   include <csignal>
#endif

//
// Compaq C++ defines signal() as a macro, causing problems with the _condVar->signal()
// statement, which the compiler for some reason replaces by the macro.
//
#if defined (__digital__) && defined (__unix__)
#   undef signal
#endif

//
// CtrlCHandler callbacks.
//

static void
holdInterruptCallback(int signal)
{
    CtrlCHandlerCallback callback = 0;
    {
        StaticMutex::Lock lock(_mutex);
        while(!_released)
        {
            _condVar->wait(lock);
        }
        
        if(_destroyed)
        {
            //
            // Being destroyed by main thread
            //
            return;
        }
        assert(_ctrlCHandler != 0);
        callback = _ctrlCHandler->getCallback();
    }
    
    if(callback != 0)
    {
        callback(signal);
    }
}

static void
destroyOnInterruptCallback(int signal)
{
    {
        StaticMutex::Lock lock(_mutex);
        if(_destroyed)
        {
            //
            // Being destroyed by main thread
            //
            return;
        }
        if(_nohup && signal == SIGHUP)
        {
            return;
        }

        assert(!_callbackInProgress);
        _callbackInProgress = true;
        _interrupted = true;
        _destroyed = true;
    }
        
    assert(_communicator != 0);
    
    try
    {
        _communicator->destroy();
    }
    catch(const IceUtil::Exception& ex)
    {
        cerr << _appName << " (while destroying in response to signal " << signal 
             << "): " << ex << endl;
    }
    catch(const std::exception& ex)
    {
        cerr << _appName << " (while destroying in response to signal " << signal 
             << "): std::exception: " << ex.what() << endl;
    }
    catch(const std::string& msg)
    {
        cerr << _appName << " (while destroying in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(const char * msg)
    {
        cerr << _appName << " (while destroying in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(...)
    {
        cerr << _appName << " (while destroying in response to signal " << signal 
             << "): unknown exception" << endl;
    }

    {
        StaticMutex::Lock lock(_mutex);
        _callbackInProgress = false;
    }
    _condVar->signal();
}


static void
shutdownOnInterruptCallback(int signal)
{
    {
        StaticMutex::Lock lock(_mutex);
        if(_destroyed)
        {
            //
            // Being destroyed by main thread
            //
            return;
        }
        if(_nohup && signal == SIGHUP)
        {
            return;
        }
        assert(!_callbackInProgress);
        _callbackInProgress = true;
        _interrupted = true;
    }

    assert(_communicator != 0);
    try
    {
        _communicator->shutdown();
    }
    catch(const IceUtil::Exception& ex)
    {
        cerr << _appName << " (while shutting down in response to signal " << signal 
             << "): " << ex << endl;
    }
    catch(const std::exception& ex)
    {
        cerr << _appName << " (while shutting down in response to signal " << signal 
             << "): std::exception: " << ex.what() << endl;
    }
    catch(const std::string& msg)
    {
        cerr << _appName << " (while shutting down in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(const char * msg)
    {
        cerr << _appName << " (while shutting down in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(...)
    {
        cerr << _appName << " (while shutting down in response to signal " << signal 
             << "): unknown exception" << endl;
    }

    {
        StaticMutex::Lock lock(_mutex);
        _callbackInProgress = false;
    }
    _condVar->signal();
}

static void
callbackOnInterruptCallback(int signal)
{
    {
        StaticMutex::Lock lock(_mutex);
        if(_destroyed)
        {
            //
            // Being destroyed by main thread
            //
            return;
        }
        // For SIGHUP the user callback is always called. It can
        // decide what to do.
        assert(!_callbackInProgress);
        _callbackInProgress = true;
        _interrupted = true;
    }

    assert(_application != 0);
    try
    {
        _application->interruptCallback(signal);
    }
    catch(const IceUtil::Exception& ex)
    {
        cerr << _appName << " (while interrupting in response to signal " << signal 
             << "): " << ex << endl;
    }
    catch(const std::exception& ex)
    {
        cerr << _appName << " (while interrupting in response to signal " << signal 
             << "): std::exception: " << ex.what() << endl;
    }
    catch(const std::string& msg)
    {
        cerr << _appName << " (while interrupting in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(const char * msg)
    {
        cerr << _appName << " (while interrupting in response to signal " << signal
             << "): " << msg << endl;
    }
    catch(...)
    {
        cerr << _appName << " (while interrupting in response to signal " << signal 
             << "): unknown exception" << endl;
    }

    {
        StaticMutex::Lock lock(_mutex);
        _callbackInProgress = false;
    }
    _condVar->signal();
}


Ice::Application::Application()
{
}

Ice::Application::~Application()
{
}

int
Ice::Application::main(int argc, char* argv[])
{
    return main(argc, argv, InitializationData());
}

int
Ice::Application::main(int argc, char* argv[], const char* configFile)
{
    //
    // We don't call the main below to avoid a deprecated warning
    //

    InitializationData initData;
    if(configFile)
    {
        try
        {
            initData.properties = createProperties();
            initData.properties->load(configFile);
        }
        catch(const IceUtil::Exception& ex)
        {
            cerr << argv[0] << ": " << ex << endl;
            return EXIT_FAILURE;
        }
        catch(const std::exception& ex)
        {
            cerr << argv[0] << ": std::exception: " << ex.what() << endl;
            return EXIT_FAILURE;
        }
        catch(...)
        {
            cerr << argv[0] << ": unknown exception" << endl;
            return EXIT_FAILURE;
        }
    }
    return main(argc, argv, initData);
}


int
Ice::Application::main(int argc, char* argv[], const char* configFile, const Ice::LoggerPtr& logger)
{
    InitializationData initData;
    if(configFile)
    {
        try
        {
            initData.properties = createProperties();
            initData.properties->load(configFile);
        }
        catch(const IceUtil::Exception& ex)
        {
            cerr << argv[0] << ": " << ex << endl;
            return EXIT_FAILURE;
        }
        catch(const std::exception& ex)
        {
            cerr << argv[0] << ": std::exception: " << ex.what() << endl;
            return EXIT_FAILURE;
        }
        catch(...)
        {
            cerr << argv[0] << ": unknown exception" << endl;
            return EXIT_FAILURE;
        }
    }
    initData.logger = logger;
    return main(argc, argv, initData);
}

int
Ice::Application::main(int argc, char* argv[], const InitializationData& initData)
{
    if(_communicator != 0)
    {
        cerr << argv[0] << ": only one instance of the Application class can be used" << endl;
        return EXIT_FAILURE;
    }
    int status;

    try
    {
        //
        // The ctrlCHandler must be created before starting any thread, in particular
        // before initializing the communicator.
        //
        CtrlCHandler ctrCHandler;
        _ctrlCHandler = &ctrCHandler;

        try
        {
            if(_condVar.get() == 0)
            {
                _condVar.reset(new Cond);
            }

            _interrupted = false;
            _appName = argv[0];
                
            _application = this;
            _communicator = initialize(argc, argv, initData);
            _destroyed = false;

            //
            // Used by destroyOnInterruptCallback and shutdownOnInterruptCallback.
            //
            _nohup = (_communicator->getProperties()->getPropertyAsInt("Ice.Nohup") > 0);
        
            //
            // The default is to destroy when a signal is received.
            //
            destroyOnInterrupt();
            status = run(argc, argv);
        }
        catch(const IceUtil::Exception& ex)
        {
            cerr << _appName << ": " << ex << endl;
            status = EXIT_FAILURE;
        }
        catch(const std::exception& ex)
        {
            cerr << _appName << ": std::exception: " << ex.what() << endl;
            status = EXIT_FAILURE;
        }
        catch(const std::string& msg)
        {
            cerr << _appName << ": " << msg << endl;
            status = EXIT_FAILURE;
        }
        catch(const char* msg)
        {
            cerr << _appName << ": " << msg << endl;
            status = EXIT_FAILURE;
        }
        catch(...)
        {
            cerr << _appName << ": unknown exception" << endl;
            status = EXIT_FAILURE;
        }

        //
        // Don't want any new interrupt and at this point (post-run),
        // it would not make sense to release a held signal to run
        // shutdown or destroy.
        //
        ignoreInterrupt();

        {
            StaticMutex::Lock lock(_mutex);
            while(_callbackInProgress)
            {
                _condVar->wait(lock);
            }
            if(_destroyed)
            {
                _communicator = 0;
            }
            else
            {
                _destroyed = true;
                //
                // And _communicator != 0, meaning will be destroyed
                // next, _destroyed = true also ensures that any
                // remaining callback won't do anything
                //
            }
            _application = 0;
        }

        if(_communicator != 0)
        {  
            try
            {
                _communicator->destroy();
            }
            catch(const IceUtil::Exception& ex)
            {
                cerr << _appName << ": " << ex << endl;
                status = EXIT_FAILURE;
            }
            catch(const std::exception& ex)
            {
                cerr << _appName << ": std::exception: " << ex.what() << endl;
                status = EXIT_FAILURE;
            }
            catch(...)
            {
                cerr << _appName << ": unknown exception" << endl;
                status = EXIT_FAILURE;
            }
            _communicator = 0;
        }

        //
        // Set _ctrlCHandler to 0 only once communicator->destroy() has completed.
        // 
        _ctrlCHandler = 0;
    }
    catch(const CtrlCHandlerException&)
    {
        cerr << argv[0] << ": only one instance of the Application class can be used" << endl;
        status = EXIT_FAILURE;
    }
   
    return status;
}

void
Ice::Application::interruptCallback(int)
{
}

const char*
Ice::Application::appName()
{
    return _appName;
}

CommunicatorPtr
Ice::Application::communicator()
{
    return _communicator;
}

void
Ice::Application::destroyOnInterrupt()
{
    //
    // if _ctrlCHandler == 0, it's really a bug in the caller
    //
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() == holdInterruptCallback)
        {
            _released = true;
            _condVar->signal();
        }
        _ctrlCHandler->setCallback(destroyOnInterruptCallback);
    }
}

void
Ice::Application::shutdownOnInterrupt()
{
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() == holdInterruptCallback)
        {
            _released = true;
            _condVar->signal();
        }
        _ctrlCHandler->setCallback(shutdownOnInterruptCallback);
    }
}

void
Ice::Application::ignoreInterrupt()
{
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() == holdInterruptCallback)
        {
            _released = true;
            _condVar->signal();
        }
        _ctrlCHandler->setCallback(0);
    }
}

void
Ice::Application::callbackOnInterrupt()
{
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() == holdInterruptCallback)
        {
            _released = true;
            _condVar->signal();
        }
        _ctrlCHandler->setCallback(callbackOnInterruptCallback);
    }
}

void
Ice::Application::holdInterrupt()
{
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() != holdInterruptCallback)
        {
            _previousCallback = _ctrlCHandler->getCallback();
            _released = false;
            _ctrlCHandler->setCallback(holdInterruptCallback);
        }
        // else, we were already holding signals
    }
}

void
Ice::Application::releaseInterrupt()
{
    if(_ctrlCHandler != 0)
    {
        StaticMutex::Lock lock(_mutex); // we serialize all the interrupt-setting
        if(_ctrlCHandler->getCallback() == holdInterruptCallback)
        {
            //
            // Note that it's very possible no signal is held;
            // in this case the callback is just replaced and
            // setting _released to true and signalling _condVar
            // do no harm.
            //
            
            _released = true;
            _ctrlCHandler->setCallback(_previousCallback);
            _condVar->signal();
        }
        // Else nothing to release.
    }
}

bool
Ice::Application::interrupted()
{
    StaticMutex::Lock lock(_mutex);
    return _interrupted;
}