Esempio n. 1
0
        public static void SetupSignalHandlers(SignalHandler signal_handler)
        {
            UnixSignal[] signals = new UnixSignal [] {
                new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
                new UnixSignal(Mono.Unix.Native.Signum.SIGUSR1),
                new UnixSignal(Mono.Unix.Native.Signum.SIGUSR2)
            };

            // Ignore SIGPIPE
            // FIXME: Shouldn't this be done in every thread ?
            Mono.Unix.Native.Stdlib.SetSignalAction(Mono.Unix.Native.Signum.SIGPIPE, Mono.Unix.Native.SignalAction.Ignore);

            // Work around a mono feature/bug
            // https://bugzilla.novell.com/show_bug.cgi?id=381928
            // When beagle crashes, mono will try to print a stack trace and then call abort()
            // The abort somehow calls back into beagle and causes a deadlock
            if (Environment.GetEnvironmentVariable("BEAGLE_MONO_DEBUG_FLAG_IS_SET") == null)
            {
                Mono.Unix.Native.Stdlib.SetSignalAction(Mono.Unix.Native.Signum.SIGABRT, Mono.Unix.Native.SignalAction.Default);
            }

            Thread signal_thread = new Thread(delegate() {
                Log.Debug("Starting signal handler thread");
                int signal_handler_timeout = -1;
                while (!MainloopFinished)
                {
                    int index = UnixSignal.WaitAny(signals, signal_handler_timeout);

                    if (index > 3)
                    {
                        continue;
                    }
                    Mono.Unix.Native.Signum signal = signals [index].Signum;

                    // Set shutdown flag to true so that other threads can stop initializing
                    if (signal == Mono.Unix.Native.Signum.SIGINT ||
                        signal == Mono.Unix.Native.Signum.SIGTERM)
                    {
                        ShutdownRequested      = true;
                        signal_handler_timeout = 200;                         // 200 ms
                    }

                    if (signal_handler == null)
                    {
                        continue;
                    }

                    // Do all signal handling work in the main loop and not in the signal handler.
                    GLib.Idle.Add(new GLib.IdleHandler(delegate() { signal_handler((int)signal); return(false); }));
                }
                Log.Debug("Exiting signal handler thread");
            });

            signal_thread.Start();
        }
Esempio n. 2
0
        static public void SignalRemoteIndexer(Mono.Unix.Native.Signum signal)
        {
            // No helper running right now
            if (helper_pid == -1 || !CheckHelper())
            {
                helper_pid = -1;
                return;
            }

            Log.Debug("Forwarding signal {0} ({1}) to index helper (pid {2})", (int)signal, signal, helper_pid);
            Mono.Unix.Native.Syscall.kill(helper_pid, signal);
        }
Esempio n. 3
0
        /// <summary>
        /// Launches a background thread to listen for POSIX signals.
        /// Exit on two signal 2's or one signal 15.
        /// </summary>
        static void InstallSignalHandler(RCRunner runner)
        {
#if __MonoCS__
            UnixSignal[] signals =
            {
                new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
                new UnixSignal(Mono.Unix.Native.Signum.SIGINT)
            };
            Thread signalThread = new Thread(delegate()
            {
                while (true)
                {
                    int index = UnixSignal.WaitAny(signals, -1);
                    if (index < 0)
                    {
                        continue;
                    }
                    Mono.Unix.Native.Signum signal = signals[index].Signum;
                    if (signal == Mono.Unix.Native.Signum.SIGTERM)
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object state)
                        {
                            RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGTERM");
                            runner.Abort(15);
                        });
                    }
                    else if (signal == Mono.Unix.Native.Signum.SIGINT)
                    {
                        if (!_firstSigint)
                        {
                            _firstSigint = true;
                            ThreadPool.QueueUserWorkItem(delegate(object state)
                            {
                                RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGINT");
                                runner.Interupt();
                            });
                        }
                        else
                        {
                            ThreadPool.QueueUserWorkItem(delegate(object state)
                            {
                                RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGINT (exiting)");
                                runner.Abort(2);
                            });
                        }
                    }
                }
            });
            signalThread.IsBackground = true;
            signalThread.Start();
#endif
        }
 public static void UnixSignalTrap()
 {
     // Catch SIGINT and SIGTERM
     UnixSignal[] signals = new UnixSignal [] {
         new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
         new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
     };
     while (true)
     {
         // Wait for a signal to be delivered
         int index = UnixSignal.WaitAny(signals, -1);
         // invoke the clean-up routine before exiting
         Mono.Unix.Native.Signum signal = signals[index].Signum;
         OnConsoleCtrlCheck(signal == Mono.Unix.Native.Signum.SIGINT ? CtrlTypes.CTRL_C_EVENT : CtrlTypes.CTRL_CLOSE_EVENT);
         break;
     }
 }
Esempio n. 5
0
        private void signalThread()
        {
            while (Running)
            {
                // Wait for a signal to be delivered
                int index = UnixSignal.WaitAny(signals, -1);

                Mono.Unix.Native.Signum signal = signals [index].Signum;

                log.Info("signal: " + signal.ToString());
                // Notify the main thread that a signal was received,
                // you can use things like:
                //    Application.Invoke () for Gtk#
                //    Control.Invoke on Windows.Forms
                //    Write to a pipe created with UnixPipes for server apps.
                //    Use an AutoResetEvent

                // For example, this works with Gtk#
                //Application.Invoke (delegate () { ReceivedSignal (signal);
                stop();
            }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            //Set up IO - (TODO, allow to be set in config file)
            var RingerPower      = new Raspberry.IO.GeneralPurpose.OutputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin17);
            var RingerOscillator = new Raspberry.IO.GeneralPurpose.OutputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin18);
            var HookSwitch       = new Raspberry.IO.GeneralPurpose.InputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin22)
            {
                Reversed = true
            };
            var DialPulseSwitch = new Raspberry.IO.GeneralPurpose.InputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin27);


            //Start Ringer and DialListener GPIO classes
            using (var ringer = new clsRinger(RingerPower, RingerOscillator))
            {
                using (var dialListener = new clsDialHookListener(HookSwitch, DialPulseSwitch))
                {
                    //Simple bell test function. 0 = UK ring, 1 = USA Ring
                    dialListener.NumberDialed += (uint NumberDialed) =>
                    {
                        Console.WriteLine("Number Dialed:{0}", NumberDialed);

                        if (NumberDialed == 0)
                        {
                            ringer.SetRingPattern(clsRinger.ringPattern_UK);
                            ringer.StartRing();
                        }
                        else if (NumberDialed == 9)
                        {
                            ringer.SetRingPattern(clsRinger.ringPattern_USA);
                            ringer.StartRing();
                        }
                    };

                    //Cancel the Ringer
                    dialListener.HookSwitchChange += (bool OnHook, uint Pulse) =>
                    {
                        if (!OnHook)
                        {
                            ringer.StopRing();
                        }
                    };


                    UnixSignal[] signals = new UnixSignal [] {
                        new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                        new UnixSignal(Mono.Unix.Native.Signum.SIGUSR1),
                    };

                    while (true)
                    {
                        int index = UnixSignal.WaitAny(signals, -1);                          //Wait for any Unix Signals

                        Mono.Unix.Native.Signum signal = signals [index].Signum;
                        Console.Write("SIGNAL:{0}", signal.ToString());
                        break;
                    }
                    ;
                }
            }
            Console.WriteLine("**end**");
        }
Esempio n. 7
0
 private static extern int sigaction(Mono.Unix.Native.Signum sig, IntPtr act, IntPtr oact);
        public static void Main(string[] args)
        {
            bool   _shutDown = false;
            bool   _isMono;
            Thread signal_thread = null;

            _isMono = Type.GetType("Mono.Runtime") != null;

            if (_isMono)
            {
                //http://unixhelp.ed.ac.uk/CGI/man-cgi?signal+7
                UnixSignal[] signals;
                signals = new UnixSignal[] {
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGHUP),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGQUIT),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGABRT),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGKILL),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGSTOP),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGTSTP)
                };



                signal_thread = new Thread(delegate()
                {
                    while (!_shutDown)
                    {
                        // Wait for a signal to be delivered
                        int index = UnixSignal.WaitAny(signals, -1);
                        Mono.Unix.Native.Signum signal = signals[index].Signum;
                        Console.WriteLine("shutdown signal recieved {0}" + signal.ToString());
                        _shutDown = true;
                    }
                });
            }



            Uri elasticSearchUrl;
            int daysToKeep = 30;

            if (args.Length == 0)
            {
                elasticSearchUrl = new Uri(Environment.GetEnvironmentVariable("elasticSearchUrl"));
                string environmentVariable = Environment.GetEnvironmentVariable("daysToKeep");
                if (environmentVariable != null)
                {
                    daysToKeep = int.Parse(environmentVariable);
                }
            }
            else
            {
                elasticSearchUrl = new Uri(args[0]);
                if (args.Length > 1)
                {
                    daysToKeep = int.Parse(args[1]);
                }
            }

            IPAddress ip = GetExternalIP();


            TimeSpan wait;

            while (!_shutDown)
            {
                Console.WriteLine("\n\nstarting purge from IP {0} at {1}", ip, DateTime.UtcNow);
                try
                {
                    var mapping      = GetMappings(elasticSearchUrl);
                    var toDate       = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day).Subtract(TimeSpan.FromDays(daysToKeep));
                    var toDateString = GetDayOnlyString(toDate);


                    mapping.Indices.Sort();
                    foreach (var i in mapping.Indices)
                    {
                        Console.WriteLine("checking index {0}", i);
                        if (IndexExists(elasticSearchUrl, i))
                        {
                            var count = GetCount(elasticSearchUrl, i, toDateString);
                            if (count != 0)
                            {
                                Console.WriteLine("deleting {0} records", count);
                                Uri uri = new Uri(elasticSearchUrl, i + "/_query");

                                string range      = GetRangeString(toDateString);
                                var    queryBytes = Encoding.UTF8.GetBytes(range);
                                /*string responseText = */
                                Console.WriteLine("delete query: {0}", range);
                                GetResponseText(uri, "DELETE", queryBytes);

                                //{"ok":true,"_indices":{"logstash-2013.07.21":{"_shards":{"total":4,"successful":4,"failed":0}}}}
                            }
                            else
                            {
                                Console.WriteLine("index {0} has no matching records", i);
                            }

                            // then clean it up if empty
                            if (DeleteIndexIfEmpty(elasticSearchUrl, i))
                            {
                                Console.WriteLine("deleted emtpy index {0}", i);
                            }
                        }
                        else
                        {
                            Console.WriteLine("index {0} does not exist", i);
                        }
                    }
                    wait = TimeSpan.FromDays(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR:" + ex.ToString());
                    wait = TimeSpan.FromMinutes(1);
                }
                Console.WriteLine("\n\nwaiting {0} minutes until next purge", wait.TotalMinutes);
                Thread.Sleep(wait);
            }
            Console.WriteLine("shutting down");

            if (_isMono)
            {
                signal_thread.Join();
            }

            Console.WriteLine("shut down complete.");
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
#if UNIX
            Console.WriteLine("Hello unix");
#else
            Console.WriteLine("Hello WIN");
#endif
            Program p = new Program();
            parseArgs(p, args);
            if (p.config == null)
            {
                Console.WriteLine("Failed to read config file");
                return;
            }
            if (p.config.key == null || p.config.iv == null)
            {
                Console.WriteLine("Key or IV not supplied. Can't start");
                return;
            }
            //Read key and iv

            /*Console.WriteLine("Enter Encryption key (base64)");
             * key = Console.ReadLine();
             * Console.WriteLine("Enter Encryption IV (base64)");
             * iv = Console.ReadLine();*/
            Console.WriteLine();
            p.securityMan = new SecurityManager(p.config.keyFileLocation, p.config.key, p.config.iv, p.config.userFileLocation, p.config.emailUserFileLocation);

            Debug.WriteLine("Creating logger");
            WebLogger logger = new WebLogger();
            //TODO Properly initialize
            Debug.WriteLine("Initalizing ticketStorage");
            if (p.config.ticketDirectory != null)
            {
                p.ticketStorage = new Tickets.TicketStorage(p.config.ticketDirectory);
            }
            else
            {
                p.ticketStorage = new Tickets.TicketStorage();
            }
            //TODO Properly initalize
            Debug.WriteLine("Creating Scheduler");
            p.scheduler = new Scheduler(p.ticketStorage, p.securityMan);
            Debug.WriteLine("Creating Method Manager");
            p.methodMan = new MethodMan(p.ticketStorage, p.scheduler);
            Debug.WriteLine("Creating Service Factory");
            ServiceFactory service = new ServiceFactory(logger, p.methodMan);
            Debug.WriteLine("Creating Web Server");
            p.server = new WebServer(service, logger);
            p.server.Listen(p.config.port);
            Debug.WriteLine("Main() is listening");
            p.securityMan.sendStartup();
            p.serviceServer = new ServiceServer(p.securityMan);
#if UNIX
            // Catch SIGINT and SIGUSR1
            UnixSignal[] signals = new UnixSignal[] {
                new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                new UnixSignal(Mono.Unix.Native.Signum.SIGUSR1),
                new UnixSignal(Mono.Unix.Native.Signum.SIGQUIT),
                new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
            };
            while (true)
            {
                int index = UnixSignal.WaitAny(signals, -1);
                p.server.Dispose();
                Mono.Unix.Native.Signum signal = signals[index].Signum;
                //Stop listening and close threads
                p.securityMan.sendShutdown("Recieved kill signal");
                Environment.Exit(0);
            }
#else
            Console.ReadKey();
#endif
        }