// Main.cs // // Copyright (C) 2008,2009 Bartek Jasicki // // This file is part of Grubng. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // /** * @namespace grubupload * @brief Main program namespace.*/ /** * @file Main.cs * Provide Main program class. */ using System; using System.Runtime.InteropServices; using System.Text; using System.IO; namespace grubupload { /**Provide Main program class.*/ static class MainClass { /**Main program function * @param args if array is empty, start server * if args[0] is "configure", create default configuration for server and exit.*/ public static void Main(string[] args) { SetProcessName("grub-upload"); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MainUnhandledException); int argslength = args.Length; if (argslength != 0) { //Configure server if (args[0] == "configure") { Config.CreateConfig(); Console.WriteLine("Default configuration file was created."); return; } //Create database if (args[0] == "createdb") { Database.CreateDB(); Console.WriteLine("Database created."); return; } //Update configuration file if (args[0] == "updateconf") { Config.UpdateConfig(); Console.WriteLine("Configuration file updated."); return; } //Update database if (args[0] == "updatedb") { Database.UpdateDB(); Console.WriteLine("Database updated."); return; } //Stop server if ((args[0] == "stop") || (args[0] == "restart")) { int pid = Convert.ToInt32(File.ReadAllText("grub.pid")); System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(pid); proc.Kill(); proc.Close(); File.Delete("grub.pid"); Console.WriteLine("Server stopped."); if (args[0] == "stop") { return; } } //Start server if ((args[0] == "start") || (args[0] == "restart")) { string arguments = "--server -O=all"; if (args.Length == 2) { if (args[1] == "debug") { arguments = "--debug"; } } System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo(); procinfo.FileName = "mono"; procinfo.Arguments = arguments + " grub-upload.exe &"; procinfo.UseShellExecute = false; System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procinfo); StreamWriter streamw = File.CreateText("grub.pid"); streamw.WriteLine(proc.Id.ToString()); streamw.Close(); proc.Close(); Console.Write("Server start"); if (arguments == "--debug") { Console.Write(" in debug mode"); } Console.WriteLine(); return; } } //Run server Server upserver = new Server(); upserver.Run(); } /**Function set process name on Unix system. * Code borrowed from http://abock.org/2006/02/09/changing-process-name-in-mono/ * @param name New name of process*/ public static void SetProcessName (string name) { try { if (NativeMethods.prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) { throw new ApplicationException ("Error setting process name: " + Mono.Unix.Native.Stdlib.GetLastError ()); } } catch (EntryPointNotFoundException) { NativeMethods.setproctitle (Encoding.ASCII.GetBytes ("%s\0"), Encoding.ASCII.GetBytes (name + "\0")); } } /**Main function for catch unhandled exceptions. * Write informations about exception to error.log. * @param sender Unused * @param args Provide informations about error (source, stacktrace, * general info about error)*/ static void MainUnhandledException(object sender, UnhandledExceptionEventArgs args) { if (sender != null) { sender = null; } Exception e = (Exception)args.ExceptionObject; string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss"); FileStream errorlog = File.Open("error.log", FileMode.Append); StreamWriter logstream = new StreamWriter(errorlog); logstream.WriteLine(tdate + " Caught: " + e.GetType().ToString() + " " + e.Message); logstream.WriteLine(" Source: " + e.Source); logstream.WriteLine(" StackTrace: " + e.StackTrace); logstream.WriteLine(" TargetSite: " + e.TargetSite); logstream.Close(); } } /**Provide p/invoke for native Unix methods.*/ internal static class NativeMethods { [DllImport ("libc")] // GNU/Linux public static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); [DllImport ("libc")] // BSD public static extern void setproctitle (byte [] fmt, byte [] str_arg); } }