// // InterProcess.cs // // Author: // Carsten Sonne Larsen // // Copyright (c) 2016 Carsten Sonne Larsen // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using Ntp.Analyzer.Log; #if __MonoCS__ using Mono.Unix; using Mono.Unix.Native; #else using System.Threading; #endif namespace Ntp.System { public static class InterProcess { public enum Signal { Error = 0, Exit, Refresh, Reload } #if __MonoCS__ public static Signal Wait (string name, LogBase log) { UnixSignal[] signals = new UnixSignal[] { new UnixSignal (Signum.SIGTERM), new UnixSignal (Signum.SIGQUIT), new UnixSignal (Signum.SIGINT), new UnixSignal (Signum.SIGHUP), new UnixSignal (Signum.SIGUSR1) }; int sig = 0; while (true) { try { sig = UnixSignal.WaitAny (signals, -1); } catch (Exception e) { log.WriteLine ("Error in Signum handling.", Severity.Error); log.WriteLine (e, Severity.Trace); return Signal.Error; } if (sig >= 0 && sig < signals.Length) { Signum signal = signals [sig].Signum; switch (signal) { case Signum.SIGINT: log.WriteLine ("Interrupted.", Severity.Notice); return Signal.Exit; case Signum.SIGTERM: log.WriteLine ("Received kill signal.", Severity.Notice); return Signal.Exit; case Signum.SIGQUIT: log.WriteLine ("Received quit signal.", Severity.Notice); return Signal.Exit; case Signum.SIGHUP: log.WriteLine ("Received log restart signal.", Severity.Debug); return Signal.Refresh; case Signum.SIGUSR1: log.WriteLine ("Received reload config signal.", Severity.Debug); return Signal.Reload; default: log.WriteLine ("Unknown signal received.", Severity.Warn); break; } } else { log.WriteLine ("Unknown signal received.", Severity.Warn); } } } #else public static bool Release (string name, LogBase log) { try { EventWaitHandle handle = EventWaitHandle.OpenExisting (name); handle.Set (); return Signal.Exit; } catch (Exception e) { log.WriteLine ("Error in mutex handling.", Severity.Error); log.WriteLine (e, Severity.Trace); return Signal.Error; } } public static Signal Wait (string name, LogBase log) { try { EventWaitHandle handle = new EventWaitHandle ( false, EventResetMode.ManualReset, name); handle.WaitOne (); return true; } catch (Exception e) { log.WriteLine ("Error in mutex handling.", Severity.Error); log.WriteLine (e, Severity.Trace); return false; } } #endif } }