summaryrefslogtreecommitdiff
path: root/cs/src/Ice/Application.cs
blob: 9ce47f6fb050d36190cf6faab8e12de3e49d6549 (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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// **********************************************************************
//
// Copyright (c) 2003-2009 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.
//
// **********************************************************************

namespace Ice
{

    using System;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;

    public enum SignalPolicy { HandleSignals, NoSignalHandling }

    public abstract class Application
    {
        public abstract int run(string[] args);

        //
        // Override this method to provide a custom application interrupt
        // hook. You must call callbackOnInterrupt for this method
        // to be called. Note that the interruptCallback can be called
        // concurrently with any other thread (including main) in your
        // application--take appropriate concurrency precautions.
        //
        public virtual void interruptCallback(int sig)
        {
        }

        public Application()
        {
        }

        public Application(SignalPolicy signalPolicy)
        {
            _signalPolicy = signalPolicy;
        }

        //
        // This main() must be called by the global Main(). main()
        // initializes the Communicator, calls run(), and destroys
        // the Communicator upon return from run(). It thereby handles
        // all exceptions properly, i.e., error messages are printed
        // if exceptions propagate to main(), and the Communicator is
        // always destroyed, regardless of exceptions.
        //
        public int main(string[] args)
        {
            return main(args, new InitializationData());
        }

        public int main(string[] args, string configFile)
        {
            InitializationData initData = new InitializationData();
            if(configFile != null)
            {
                try
                {
                    initData.properties = Util.createProperties();
                    initData.properties.load(configFile);
                }
                catch(Ice.Exception ex)
                {
                    Console.Error.WriteLine(_appName + ":\n" + ex);
                    return 1;
                }
                catch(System.Exception ex)
                {
                    Console.Error.WriteLine(_appName + ": unknown exception:\n" + ex);
                    return 1;
                }
            }
            return main(args, initData);
        }

        public int main(string[] args, InitializationData initData)
        {
            if(_communicator != null)
            {
                Console.Error.WriteLine(_appName + ": only one instance of the Application class can be used");
                return 1;
            }

            int status;

            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                if(IceInternal.AssemblyUtil.platform_ == IceInternal.AssemblyUtil.Platform.Windows)
                {
                    _signals = new WindowsSignals();
                }
                else
                {
                    _signals = new MonoSignals();
                }
                _signals.register(_handler);

                status = mainInternal(args, initData);

                _signals = null;
            }
            else
            {
                status = mainInternal(args, initData);
            }

            return status;
        }

        //
        // Return the application name.
        //
        public static string appName()
        {
            return _appName;
        }

        //
        // One limitation of this class is that there can only be one
        // Application instance, with one global Communicator, accessible
        // with this communicator() operation. This limitiation is due to
        // how the signal handling functions below operate. If you require
        // multiple Communicators, then you cannot use this Application
        // framework class.
        //
        public static Communicator communicator()
        {
            return _communicator;
        }

        public static void destroyOnInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback == _holdCallback)
                    {
                        _released = true;
                        Monitor.Pulse(_mutex);
                    }
                    _callback = _destroyCallback;
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static void shutdownOnInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback == _holdCallback)
                    {
                        _released = true;
                        Monitor.Pulse(_mutex);
                    }
                    _callback = _shutdownCallback;
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static void ignoreInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback == _holdCallback)
                    {
                        _released = true;
                        Monitor.Pulse(_mutex);
                    }
                    _callback = null;
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static void callbackOnInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback == _holdCallback)
                    {
                        _released = true;
                        Monitor.Pulse(_mutex);
                    }
                    _callback = _userCallback;
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static void holdInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback != _holdCallback)
                    {
                        _previousCallback = _callback;
                        _released = false;
                        _callback = _holdCallback;
                    }
                    // else, we were already holding signals
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static void releaseInterrupt()
        {
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                lock(_mutex)
                {
                    if(_callback == _holdCallback)
                    {
                        //
                        // 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 _mutex
                        // do no harm.
                        //

                        _released = true;
                        _callback = _previousCallback;
                        Monitor.Pulse(_mutex);
                    }
                    // Else nothing to release.
                }
            }
            else
            {
                Console.Error.WriteLine(_appName +
                            ": warning: interrupt method called on Application configured to not handle interrupts.");
            }
        }

        public static bool interrupted()
        {
            lock(_mutex)
            {
                return _interrupted;
            }
        }

        private int mainInternal(string[] args, InitializationData initializationData)
        {
            int status = 0;

            try
            {
                //
                // We parse the properties here to extract Ice.ProgramName.
                //
                InitializationData initData;
                if(initializationData != null)
                {
                    initData = (InitializationData)initializationData.Clone();
                }
                else
                {
                    initData = new InitializationData();
                }
                initData.properties = Util.createProperties(ref args, initData.properties);

                //
                // If the process logger is the default logger, we replace it with a
                // a logger which is using the program name for the prefix.
                //
                if(Util.getProcessLogger() is LoggerI)
                {
                    Util.setProcessLogger(new LoggerI(initData.properties.getProperty("Ice.ProgramName")));
                }

                _application = this;
                _communicator = Util.initialize(ref args, initData);
                _destroyed = false;

                Properties props = _communicator.getProperties();
                _nohup = props.getPropertyAsInt("Ice.Nohup") > 0;
                _appName = props.getPropertyWithDefault("Ice.ProgramName", _appName);

                //
                // The default is to destroy when a signal is received.
                //
                if(_signalPolicy == SignalPolicy.HandleSignals)
                {
                    destroyOnInterrupt();
                }

                status = run(args);
            }
            catch(Ice.Exception ex)
            {
                Console.Error.WriteLine(_appName + ":\n" + ex);
                status = 1;
            }
            catch(System.Exception ex)
            {
                Console.Error.WriteLine(_appName + ": unknown exception:\n" + ex);
                status = 1;
            }

            //
            // 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.
            //
            if(_signalPolicy == SignalPolicy.HandleSignals)
            {
                ignoreInterrupt();
            }

            lock(_mutex)
            {
                while(_callbackInProgress)
                {
                    Monitor.Wait(_mutex);
                }
                if(_destroyed)
                {
                    _communicator = null;
                }
                else
                {
                    _destroyed = true;
                    //
                    // _communicator != null means that it will be destroyed
                    // next; _destroyed == true ensures that any
                    // remaining callback won't do anything
                    //
                }
                _application = null;
            }

            if(_communicator != null)
            {
                try
                {
                    _communicator.destroy();
                }
                catch(Ice.Exception ex)
                {
                    Console.Error.WriteLine(_appName + ":\n" + ex);
                    status = 1;
                }
                catch(System.Exception ex)
                {
                    Console.Error.WriteLine(_appName + ": unknown exception:\n" + ex);
                    status = 1;
                }
                _communicator = null;
            }

            return status;
        }

        //
        // First-level handler.
        //
        private static void signalHandler(int sig)
        {
            Callback callback;
            lock(_mutex)
            {
                callback = _callback;
            }
            if(callback != null)
            {
                try
                {
                    callback(sig);
                }
                catch(System.Exception)
                {
                    Debug.Assert(false);
                }
            }
        }

        //
        // The callbacks to be invoked from the handler.
        //
        private static void holdInterruptCallback(int sig)
        {
            Callback callback = null;
            lock(_mutex)
            {
                while(!_released)
                {
                    Monitor.Wait(_mutex);
                }

                if(_destroyed)
                {
                    //
                    // Being destroyed by main thread
                    //
                    return;
                }

                callback = _callback;
            }

            if(callback != null)
            {
                callback(sig);
            }
        }

        //
        // The callbacks to be invoked from the handler.
        //
        private static void destroyOnInterruptCallback(int sig)
        {
            lock(_mutex)
            {
                if(_destroyed)
                {
                    //
                    // Being destroyed by main thread
                    //
                    return;
                }
                if(_nohup && sig == SIGHUP)
                {
                    return;
                }

                Debug.Assert(!_callbackInProgress);
                _callbackInProgress = true;
                _interrupted = true;
                _destroyed = true;
            }

            try
            {
                Debug.Assert(_communicator != null);
                _communicator.destroy();
            }
            catch(System.Exception ex)
            {
                Console.Error.WriteLine(_appName + " (while destroying in response to signal " + sig + "):\n" + ex);
            }

            lock(_mutex)
            {
                _callbackInProgress = false;
                Monitor.Pulse(_mutex);
            }
        }

        private static void shutdownOnInterruptCallback(int sig)
        {
            lock(_mutex)
            {
                if(_destroyed)
                {
                    //
                    // Being destroyed by main thread
                    //
                    return;
                }
                if(_nohup && sig == SIGHUP)
                {
                    return;
                }

                Debug.Assert(!_callbackInProgress);
                _callbackInProgress = true;
                _interrupted = true;
            }

            try
            {
                Debug.Assert(_communicator != null);
                _communicator.shutdown();
            }
            catch(System.Exception ex)
            {
                Console.Error.WriteLine(_appName + " (while shutting down in response to signal " + sig + "):\n" + ex);
            }

            lock(_mutex)
            {
                _callbackInProgress = false;
                Monitor.Pulse(_mutex);
            }
        }

        private static void userCallbackOnInterruptCallback(int sig)
        {
            lock(_mutex)
            {
                if(_destroyed)
                {
                    //
                    // Being destroyed by main thread
                    //
                    return;
                }
                // For SIGHUP the user callback is always called. It can
                // decide what to do.
                Debug.Assert(!_callbackInProgress);
                _callbackInProgress = true;
                _interrupted = true;
            }

            try
            {
                Debug.Assert(_application != null);
                _application.interruptCallback(sig);
            }
            catch(System.Exception ex)
            {
                Console.Error.WriteLine(_appName + " (while interrupting in response to signal " + sig + "):\n" + ex);
            }

            lock(_mutex)
            {
                _callbackInProgress = false;
                Monitor.Pulse(_mutex);
            }
        }

        private static readonly object _mutex = new object();

        private static bool _callbackInProgress = false;
        private static bool _destroyed = false;
        private static bool _interrupted = false;
        private static bool _released = false;
        private static bool _nohup = false;
        private static SignalPolicy _signalPolicy = SignalPolicy.HandleSignals;

        private delegate void Callback(int sig);
        private static readonly Callback _destroyCallback = new Callback(destroyOnInterruptCallback);
        private static readonly Callback _shutdownCallback = new Callback(shutdownOnInterruptCallback);
        private static readonly Callback _holdCallback = new Callback(holdInterruptCallback);
        private static readonly Callback _userCallback = new Callback(userCallbackOnInterruptCallback);

        private static Callback _callback = null; // Current callback
        private static Callback _previousCallback; // Remembers prev. callback when signals are held

        //
        // We use FriendlyName instead of Process.GetCurrentProcess().ProcessName because the latter
        // is terribly slow. (It takes around 1 second!)
        //
        private static string _appName = AppDomain.CurrentDomain.FriendlyName;
        private static Communicator _communicator;
        private static Application _application;

        private static int SIGHUP;
        static Application()
        {
            if(IceInternal.AssemblyUtil.platform_ == IceInternal.AssemblyUtil.Platform.Windows)
            {
                SIGHUP = 5; // CTRL_LOGOFF_EVENT, from wincon.h
            }
            else
            {
                SIGHUP = 1;
            }
        }

        private delegate void SignalHandler(int sig);
        private static readonly SignalHandler _handler = new SignalHandler(signalHandler);
        private Signals _signals;

        private interface Signals
        {
            void register(SignalHandler handler);
        }

        private class MonoSignals : Signals
        {
            public void register(SignalHandler handler)
            {
                try
                {
                    //
                    // Signal handling in Mono is provided in the Mono.Unix.Native namespace.
                    // We use reflection to do the equivalent of the following:
                    //
                    // Stdlib.signal(Signum.SIGHUP, delegate);
                    // Stdlib.signal(Signum.SIGINT, delegate);
                    // Stdlib.signal(Signum.SIGTERM, delegate);
                    //
                    // We don't use conditional compilation so that the Ice assembly can be
                    // used without change on Windows and Mono.
                    //
                    Assembly a = Assembly.Load(
                        "Mono.Posix, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
                    Type sigs = a.GetType("Mono.Unix.Native.Signum");
                    object SIGHUP = Enum.Parse(sigs, "SIGHUP");
                    object SIGINT = Enum.Parse(sigs, "SIGINT");
                    object SIGTERM = Enum.Parse(sigs, "SIGTERM");
                    Type stdlib = a.GetType("Mono.Unix.Native.Stdlib");
                    MethodInfo method = stdlib.GetMethod("signal", BindingFlags.Static | BindingFlags.Public);
                    Type del = a.GetType("Mono.Unix.Native.SignalHandler");
                    _delegate = Delegate.CreateDelegate(del, handler.Target, handler.Method);
                    object[] args = new object[2];
                    args[0] = SIGHUP;
                    args[1] = _delegate;
                    method.Invoke(null, args);
                    args[0] = SIGINT;
                    args[1] = _delegate;
                    method.Invoke(null, args);
                    args[0] = SIGTERM;
                    args[1] = _delegate;
                    method.Invoke(null, args);
                }
                catch(System.DllNotFoundException)
                {
                    //
                    // The class Mono.Unix.Native.Stdlib requires libMonoPosixHelper.so. Mono raises
                    // DllNotFoundException if it cannot be found in the shared library search path.
                    //
                    Console.Error.WriteLine("Ice.Application: warning: unable to initialize signals");
                }
                catch(System.Exception)
                {
                    Debug.Assert(false);
                }
            }

            private Delegate _delegate;
        }

        private class WindowsSignals : Signals
        {
#if MANAGED
            public void register(SignalHandler handler)
            {
                //
                // Signals aren't supported in managed code on Windows.
                //
            }
#else
            public void register(SignalHandler handler)
            {
                _handler = handler;
                _callback = new EventHandler(callback);

                bool rc = SetConsoleCtrlHandler(_callback, true);
                Debug.Assert(rc);
            }

            private delegate bool EventHandler(int sig);
            private EventHandler _callback;
            private SignalHandler _handler;

            private bool callback(int sig)
            {
                _handler(sig);
                return true;
            }

            //
            // It's not necessary to wrap DllImport in conditional compilation. The binding occurs
            // at run time, and it will never be executed on Mono.
            //
            [DllImport("kernel32.dll")]
            private static extern bool SetConsoleCtrlHandler(EventHandler eh, bool add);
#endif
        }
    }
}