Пример #1
0
        private static object DoBlock()
        {
            Signum[] quitSignals = new Signum[]
            {
                Signum.SIGABRT,
                //Signum.SIGBUS, Disabled because of mysterious MacOS behavior
                //Signum.SIGCHLD, Disabled because child processes stopping shouldn't kill the parent process
                Signum.SIGHUP,
                Signum.SIGILL,
                Signum.SIGINT,
                Signum.SIGQUIT,
                Signum.SIGTERM,
                Signum.SIGTSTP,
                Signum.SIGUSR1,
                Signum.SIGUSR2
            };

            List<UnixSignal> signals = new List<UnixSignal>();
            foreach (Signum quitSignal in quitSignals)
                signals.Add(new UnixSignal(quitSignal));

            // Wait for a signal to be delivered
                int index = UnixSignal.WaitAny(signals.ToArray(), -1);

            UnixSignal signal = signals[index];

            return signal.Signum;
        }
Пример #2
0
		public UnixSignal (Signum signum)
		{
			this.signum = NativeConvert.FromSignum (signum);
			this.signal_info = install (this.signum);
			if (this.signal_info == IntPtr.Zero) {
				throw new ArgumentException ("Unable to handle signal", "signum");
			}
		}
Пример #3
0
		public SignalWatcher(Context context, Signum signum, Action callback)
			: this(context, new Signum[] { signum }, (num) => {
				if (callback != null) {
					callback();
				}
			})
		{
		}
Пример #4
0
		public static int FromSignum(Signum value)
		{
			int result;
			if (UnsafeNativeMethods.FromSignum(value, out result) == -1)
			{
				throw new ArgumentException("value " + value + " is not an acceptable signum");
			}
			return result;
		}
Пример #5
0
        public void Subscribe(Signum signal, Action handler)
        {
            List<Action> subscribers;

            if (!_subscriptions.TryGetValue(signal, out subscribers))
            {
                subscribers = new List<Action>();

                _subscriptions.Add(signal, subscribers);
            }

            subscribers.Add(handler);
        }
Пример #6
0
        private void Handle(Signum signal)
        {
            List<Action> subscribers;

            if (_subscriptions.TryGetValue(signal, out subscribers))
            {
                foreach (var subscriber in subscribers)
                {
                    try
                    {
                        subscriber();
                    }
                    catch
                    {
                    }
                }
            }
        }
Пример #7
0
		public SignalWatcher(Context context, Signum[] signals, Action<Signum> callback)
		{
			Signals = signals;
			unixSignals = new UnixSignal[signals.Length];
			for (int i = 0; i < signals.Length; i++) {
				unixSignals[i] = new UnixSignal(signals[i]);
			}

			watcher = new AsyncWatcher<Signum>(context, (key) => {
				if (callback != null) {
					callback(key);
				}
			});

			thread = new Thread((o) => {
				while (true) {
					var index = UnixSignal.WaitAny(unixSignals);
					watcher.Send(Signals[index]);
				}
			});
		}
Пример #8
0
		private static extern int ToSignum (Int32 value, out Signum rval);
Пример #9
0
		public static Int32 FromSignum (Signum value)
		{
			Int32 rval;
			if (FromSignum (value, out rval) == -1)
				ThrowArgumentException (value);
			return rval;
		}
Пример #10
0
		public static bool TryFromSignum (Signum value, out Int32 rval)
		{
			return FromSignum (value, out rval) == 0;
		}
Пример #11
0
		public static int kill (int pid, Signum sig)
		{
			int _sig = UnixConvert.FromSignum (sig);
			return sys_kill (pid, _sig);
		}
Пример #12
0
		static UnixSignal[] CreateSignals (Signum[] signals)
		{
			UnixSignal[] s = new UnixSignal [signals.Length];
			for (int i = 0; i < signals.Length; ++i)
				s [i] = new UnixSignal (signals [i]);
			return s;
		}
Пример #13
0
        static void UnixSignalTrapper()
        {
            Signum[] quitSignals = new Signum[]
            {
                Signum.SIGABRT,
                //Signum.SIGBUS, Disabled because of mysterious MacOS behavior
                //Signum.SIGCHLD, Disabled because child processes stopping shouldn't kill the parent process
                Signum.SIGHUP,
                Signum.SIGILL,
                Signum.SIGINT,
                Signum.SIGQUIT,
                Signum.SIGTERM,
                Signum.SIGTSTP,
                Signum.SIGUSR1,
                Signum.SIGUSR2
            };

            List<UnixSignal> signals = new List<UnixSignal>();
            foreach (Signum quitSignal in quitSignals)
                signals.Add(new UnixSignal(quitSignal));

            // Wait for a signal to be delivered
            while (StayOpen)
                    //Console.WriteLine(UnixSignal.WaitAny(signals.ToArray(), -1).ToString());
                    UnixSignal.WaitAny(signals.ToArray(), -1);

            Process.GetCurrentProcess().Kill();
        }
Пример #14
0
        public static void Main(string[] args)
        {
            #if !__MonoCS__
            handler = new ConsoleEventDelegate(ConsoleEventCallback);
            SetConsoleCtrlHandler(handler, true);
            #else
            // http://stackoverflow.com/questions/6546509/detect-when-console-application-is-closing-killed
            var signums = new Signum []
            {
                Signum.SIGABRT,
                Signum.SIGINT,
                Signum.SIGKILL,
                Signum.SIGQUIT,
                Signum.SIGTERM,
                Signum.SIGSTOP,
                Signum.SIGTSTP
            };

            List<UnixSignal> signals = new List<UnixSignal>();
            foreach (var signum in signums)
            {
                try
                {
                    signals.Add(new UnixSignal(signum));
                }
                catch(Exception) {}
            }

            new Thread (delegate ()
            {
                // Wait for a signal to be delivered
                UnixSignal.WaitAny(signals.ToArray(), -1);
                app.Shutdown("UnixSignal");
            }).Start();
            #endif

            if (string.IsNullOrWhiteSpace(Settings.Default.TempPath))
            {
                Settings.Default.TempPath = Settings.Default.GetAppDataPath() + "tmp";
            }
            if (!Settings.Default.TempPath.EndsWith("" + Path.DirectorySeparatorChar))
            {
                Settings.Default.TempPath += Path.DirectorySeparatorChar;
            }
            new DirectoryInfo(Settings.Default.TempPath).Create();

            if (string.IsNullOrWhiteSpace(Settings.Default.ReadyPath))
            {
                Settings.Default.ReadyPath = Settings.Default.GetAppDataPath() + "dl";
            }
            if (!Settings.Default.ReadyPath.EndsWith("" + Path.DirectorySeparatorChar))
            {
                Settings.Default.ReadyPath += Path.DirectorySeparatorChar;
            }
            new DirectoryInfo(Settings.Default.ReadyPath).Create();

            Settings.Default.Save();

            if (File.Exists(Settings.Default.GetAppDataPath() + "log4net.xml"))
            {
                // load settings from file
                XmlConfigurator.Configure(new FileInfo(Settings.Default.GetAppDataPath() + "log4net.xml"));
            }
            else
            {
                // build our own, who logs only fatals to console
                Logger root = ((Hierarchy)LogManager.GetRepository()).Root;

                var lAppender = new ConsoleAppender
                {
                    Name = "Console",
                    Layout = new PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %line:%logger.%message%n"),
            #if DEBUG
                    Threshold = Level.Info
            #else
                    Threshold = Level.Fatal
            #endif
                };
                lAppender.ActivateOptions();

                root.AddAppender(lAppender);
                root.Repository.Configured = true;
            }

            #if __MonoCS__
            PlatformID id = Environment.OSVersion.Platform;
            // Don't allow running as root on Linux or Mac
            try
            {
                if ((id == PlatformID.Unix || id == PlatformID.MacOSX) && new UnixUserInfo(UnixEnvironment.UserName).UserId == 0)
                {
                    LogManager.GetLogger(typeof(Programm)).Fatal("Sorry, you can't run XG with these permissions. Safety first!");
                    Environment.Exit(-1);
                }
            }
            catch (ArgumentException)
            {
                // arch linux fix
                // https://github.com/lformella/xdcc-grabscher/issues/36
            }
            #endif

            app = new App();

            app.AddPlugin(new Plugin.Irc.Plugin());
            if (Settings.Default.UseJabberClient)
            {
                app.AddPlugin(new Plugin.Jabber.Plugin());
            }
            if (Settings.Default.UseElasticSearch)
            {
                app.AddPlugin(new Plugin.ElasticSearch.Plugin());
            }
            if (Settings.Default.UseWebserver)
            {
                var webServer = new Plugin.Webserver.Plugin { RrdDB = app.RrdDb };
                webServer.OnShutdown += delegate { app.Shutdown(webServer); };
                app.AddPlugin(webServer);
            }

            app.OnShutdownComplete += delegate { Environment.Exit(0); };
            app.Start(typeof(App).ToString());
        }

        #endregion Methods
    }
Пример #15
0
		public void Signal (Signum signal)
		{
			int r = Syscall.kill (pid, signal);
			UnixMarshal.ThrowExceptionForLastErrorIf (r);
		}
Пример #16
0
 private static int kill(int pid, Signum sig)
 {
     var _sig = FromSignum(sig);
     return sys_kill(pid, _sig);
 }
Пример #17
0
        public static int SendSignal(int pid, Signum sig)
        {
            switch (PlatformIdentifier)
            {
                case PlatformID.Unix:
                    return kill(pid, sig);

                case PlatformID.Win32NT:
                    switch (sig)
                    {
                        case Signum.SIGINT:
                            return SendCtrlC();

                        default:
                            throw new NotImplementedException();
                    }

                default:
                    throw new NotImplementedException();
            }
        }
Пример #18
0
 private static int FromSignum(Signum value)
 {
     int rval;
     if (FromSignum(value, out rval) == -1)
         throw new ArgumentException();
     return rval;
 }
Пример #19
0
 private static bool TryFromSignum(Signum value, out int rval)
 {
     return FromSignum(value, out rval) == 0;
 }
Пример #20
0
		public static string strsignal (Signum sig)
		{
			int s = UnixConvert.FromSignum (sig);
			lock (signal_lock) {
				IntPtr r = sys_strsignal (s);
				return UnixMarshal.PtrToString (r);
			}
		}
Пример #21
0
		public static bool TryToSignum (Int32 value, out Signum rval)
		{
			return ToSignum (value, out rval) == 0;
		}
Пример #22
0
        public static void RegisterHandler(Signum signal,
                                            SignalHandler handler)
        {
            handlers[signal] = handler;

            SigActionData data = new SigActionData (new SignalHandler (InternalHandler));
            sigaction ((int) signal, ref data, IntPtr.Zero);
        }
Пример #23
0
		public static int raise (Signum sig)
		{
			int _sig = UnixConvert.FromSignum (sig);
			return sys_raise (_sig);
		}
		private static int ToSignum (Int32 value, out Signum rval)
		{
			throw new System.NotImplementedException();
		}
Пример #25
0
		public static SignalHandler signal (Signum signum, SignalHandler handler)
		{
			int _sig = UnixConvert.FromSignum (signum);
			IntPtr r;
			if (handler == SIG_DFL)
				r = sys_signal (_sig, _SIG_DFL);
			else if (handler == SIG_ERR)
				r = sys_signal (_sig, _SIG_ERR);
			else if (handler == SIG_IGN)
				r = sys_signal (_sig, _SIG_IGN);
			else
				r = sys_signal (_sig, handler);
			return TranslateHandler (r);
		}
Пример #26
0
 private static void OnSignal(Signum sig)
 {
     if (sig == Signum.SIGTERM || sig == Signum.SIGINT) {
         Stop ();
     }
 }
Пример #27
0
 public void Kill(Signum signum)
 {
     Kill((int)signum);
 }
Пример #28
0
		private static extern int FromSignum (Signum value, out Int32 rval);
		private static int FromSignum (Signum value, out Int32 rval)
		{
			throw new System.NotImplementedException();
		}
Пример #30
0
		public static void psignal (Signum sig, string s)
		{
			int signum = UnixConvert.FromSignum (sig);
			psignal (signum, s);
		}