public void TestNestedInvocation() { UnixSignal s = new UnixSignal(Signum.SIGINT); Thread a = new Thread(delegate() { bool r = s.WaitOne(1000, false); }); Thread b = new Thread(delegate() { bool r = s.WaitOne(500, false); }); a.Start(); b.Start(); a.Join(); b.Join(); }
/// <summary> /// Set a handler for SIGINT, so they system can exit gracefully. /// </summary> public static void setSignalHandler <MeasurerT, PoseT, MeasurementT>( Manipulator <MeasurerT, PoseT, MeasurementT> manipulator) where PoseT : IPose <PoseT>, new() where MeasurementT : IMeasurement <MeasurementT>, new() where MeasurerT : IMeasurer <MeasurerT, PoseT, MeasurementT>, new() { UnixSignal sigint = new UnixSignal(Mono.Unix.Native.Signum.SIGINT); signalthread = new Thread(() => { while (true) { sigint.WaitOne(); if (manipulator != null && !manipulator.Abort) { manipulator.Abort = true; } else { Environment.Exit(1); } } }); signalthread.Start(); }
public void TestRaise() { Thread t1 = new Thread(delegate() { using (UnixSignal a = new UnixSignal(Signum.SIGINT)) { DateTime start = DateTime.Now; bool r = a.WaitOne(5000, false); DateTime end = DateTime.Now; Assert.AreEqual(a.Count, 1); Assert.AreEqual(r, true); if ((end - start) > new TimeSpan(0, 0, 5)) { throw new InvalidOperationException("Signal slept too long"); } } }); Thread t2 = new Thread(delegate() { Thread.Sleep(1000); Stdlib.raise(Signum.SIGINT); }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); }
public void TestRaise() { Thread t1 = new Thread(delegate() { using (UnixSignal a = new UnixSignal(Signum.SIGINT)) { var sw = Stopwatch.StartNew(); bool r = a.WaitOne(5000, false); sw.Stop(); Assert.AreEqual(a.Count, 1); Assert.AreEqual(r, true); if (sw.Elapsed > new TimeSpan(0, 0, 5)) { throw new InvalidOperationException("Signal slept too long"); } } }); Thread t2 = new Thread(delegate() { Thread.Sleep(1000); Stdlib.raise(Signum.SIGINT); }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); }
public void TestNoEmit() { using (UnixSignal u = new UnixSignal(Signum.SIGINT)) { DateTime start = DateTime.Now; bool r = u.WaitOne(5100, false); Assert.AreEqual(r, false); DateTime end = DateTime.Now; if ((end - start) < new TimeSpan(0, 0, 5)) { throw new InvalidOperationException("Signal didn't block for 5s; blocked for " + (end - start).ToString()); } } }
public void TestNoEmit() { using (UnixSignal u = new UnixSignal(Signum.SIGINT)) { var sw = Stopwatch.StartNew(); bool r = u.WaitOne(5100, false); Assert.AreEqual(r, false); sw.Stop(); if (sw.Elapsed < new TimeSpan(0, 0, 5)) { throw new InvalidOperationException("Signal didn't block for 5s; blocked for " + sw.Elapsed.TotalSeconds.ToString()); } } }
void TerminateHandler(object obj) { HostControl hc = obj as HostControl; log.Debug("Initializing Handler for SIGINT"); UnixSignal signal = new UnixSignal(Signum.SIGINT); if (signal.WaitOne()) { log.Debug("Control-C Pressed!"); } log.Debug("handler Terminated"); hc.Stop(); }
// helper method to create a thread waiting on a UnixSignal static Thread CreateWaitSignalThread(UnixSignal signal, int timeout) { Thread t1 = new Thread(delegate() { DateTime start = DateTime.Now; bool r = signal.WaitOne(timeout, false); DateTime end = DateTime.Now; Assert.AreEqual(signal.Count, 1); Assert.AreEqual(r, true); if ((end - start) > new TimeSpan(0, 0, timeout / 1000)) { throw new InvalidOperationException("Signal slept too long"); } }); return(t1); }
// helper method to create a thread waiting on a UnixSignal static Thread CreateWaitSignalThread(UnixSignal signal, int timeout) { Thread t1 = new Thread(delegate() { var sw = Stopwatch.StartNew(); bool r = signal.WaitOne(timeout, false); sw.Stop(); Assert.AreEqual(signal.Count, 1); Assert.AreEqual(r, true); if (sw.Elapsed > new TimeSpan(0, 0, timeout / 1000)) { throw new InvalidOperationException("Signal slept too long"); } }); return(t1); }
static void Main() { Program.Games = new Dictionary <int, Game>(); dump_fname = Environment.GetEnvironmentVariable("OPENSHIFT_DATA_DIR") + "games.dump"; log_fname = Environment.GetEnvironmentVariable("OPENSHIFT_DIY_LOG_DIR") + "errors.log"; string data_dir = Environment.GetEnvironmentVariable("OPENSHIFT_DATA_DIR"); if (data_dir != null) { System.IO.TextReader file = new System.IO.StreamReader(data_dir + "secret_token.txt", true); SECRET = file.ReadToEnd(); file.Close(); } if (System.IO.File.Exists(dump_fname)) { System.IO.TextReader file = new System.IO.StreamReader(dump_fname, true); string[] f = file.ReadToEnd().Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string el in f) { string[] s = el.Split('@'); Program.Games.Add(int.Parse(s[0]), JsonConvert.DeserializeObject <Game>(s[1])); } file.Close(); } try { WebSocket.Start(); } catch (Exception e) { Log("Startup fatal error: " + e.ToString()); System.Environment.Exit(0); } Log("Start"); UnixSignal term = new UnixSignal(Signum.SIGTERM); term.WaitOne(); Log("Terminate"); SaveGame(); System.Environment.Exit(0); }
internal static void SetControlCHandler() { if (Utilities.IsWindows) { if (!_windowsConsoleHandlerSet) { Console.CancelKeyPress += ConsoleControlCHandler; } _windowsConsoleHandlerSet = true; } else if (_signalThread == null) { Stdlib.SetSignalAction(Signum.SIGINT, SignalAction.Default); _signalThread = new Thread(() => { try { using (var sig = new UnixSignal(Signum.SIGINT)) { while (true) { sig.WaitOne(); ControlCHandler(); } } } catch (ThreadAbortException) { } }); _signalThread.Start(); } }
private static void MainStage2(Logger logger, string booruPath) { Console.Write("\x1b]0;SharpBooru Server\x07"); // Console.Title = "SharpBooru Server"; Console.TreatControlCAsInput = true; if (Environment.OSVersion.Platform != PlatformID.Unix) { throw new PlatformNotSupportedException("Only Linux is supported"); } if (Type.GetType("Mono.Runtime") == null) { throw new PlatformNotSupportedException("Only Mono is supported"); } logger.LogLine("Loading configuration..."); Config config = new Config("config.xml"); X509Certificate2 cert = null; if (config.CertificateNeeded) { logger.LogLine("Loading certificate..."); cert = new X509Certificate2(config.Certificate); } logger.LogLine("Loading booru..."); ServerBooru booru = new ServerBooru(booruPath); Server server = null; SocketListener[] sockListeners = null; MailNotificator mn = null; try { logger.LogLine("Binding sockets..."); // if (File.Exists(unixSocketPath)) // { // logger.LogLine("Socket exists, removing it..."); // File.Delete(unixSocketPath); // } Socket[] sockets = new Socket[config.SocketConfigs.Count]; sockListeners = new SocketListener[sockets.Length]; for (byte i = 0; i < sockets.Length; i++) { var sockConf = config.SocketConfigs[i]; sockets[i] = sockConf.Socket; sockets[i].Bind(sockConf.EndPoint); if (sockConf.UnixSocketPath != null) { SyscallEx.chmod(sockConf.UnixSocketPath, sockConf.UnixSocketPerms); SyscallEx.chown(sockConf.UnixSocketPath, config.User); _UnixSocketPaths.Add(sockConf.UnixSocketPath); } } logger.LogLine("Changing UID to {0}...", config.User); SyscallEx.setuid(config.User); logger.LogLine("Starting server..."); if (config.EnableMailNotificator) { mn = new MailNotificator(logger, config.MailNotificatorServer, config.MailNotificatorPort, config.MailNotificatorUsername, config.MailNotificatorPassword, config.MailNotificatorSender, config.MailNotificatorReceiver); } server = new Server(booru, logger, mn, 2); for (int i = 0; i < sockets.Length; i++) { bool useTLS = config.SocketConfigs[i].UseTLS; sockListeners[i] = new SocketListener(sockets[i]); sockListeners[i].SocketAccepted += socket => { logger.LogLine("Client connected"); NetworkStream ns = new NetworkStream(socket, true); server.AddConnectedClient(ns, useTLS ? cert : null); }; sockListeners[i].Start(); } // Ctrl+C is not supported due to heavy crashes of Mono using (UnixSignal sigtermSignal = new UnixSignal(Signum.SIGTERM)) { logger.LogLine("Startup finished, waiting for SIGTERM..."); sigtermSignal.WaitOne(); } } catch (Exception ex) { logger.LogException("MainStage3", ex); } finally { logger.LogLine("Stopping server and closing sockets..."); server.Dispose(); foreach (var listener in sockListeners) { listener.Dispose(); } } logger.LogLine("Closing booru..."); booru.Dispose(); }