Program.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using MediaBrowser.Common.Constants;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Common.Implementations.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Implementations;
  6. using MediaBrowser.ServerApplication;
  7. using MediaBrowser.ServerApplication.Native;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Windows;
  14. using System.Net;
  15. using System.Net.Security;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Threading.Tasks;
  18. using System.Reflection;
  19. // MONOMKBUNDLE: For the embedded version, mkbundle tool
  20. #if MONOMKBUNDLE
  21. using Mono.Unix;
  22. using Mono.Unix.Native;
  23. using System.Text;
  24. #endif
  25. namespace MediaBrowser.Server.Mono
  26. {
  27. public class MainClass
  28. {
  29. private static ApplicationHost _appHost;
  30. private static ILogger _logger;
  31. public static void Main (string[] args)
  32. {
  33. //GetEntryAssembly is empty when running from a mkbundle package
  34. #if MONOMKBUNDLE
  35. var applicationPath = GetExecutablePath();
  36. #else
  37. var applicationPath = Assembly.GetEntryAssembly ().Location;
  38. #endif
  39. var appPaths = CreateApplicationPaths(applicationPath);
  40. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  41. logManager.ReloadLogger(LogSeverity.Info);
  42. logManager.AddConsoleOutput();
  43. var logger = _logger = logManager.GetLogger("Main");
  44. BeginLog(logger);
  45. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  46. if (PerformUpdateIfNeeded(appPaths, logger))
  47. {
  48. logger.Info("Exiting to perform application update.");
  49. return;
  50. }
  51. try
  52. {
  53. RunApplication(appPaths, logManager);
  54. }
  55. finally
  56. {
  57. logger.Info("Shutting down");
  58. _appHost.Dispose();
  59. }
  60. }
  61. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath)
  62. {
  63. return new ServerApplicationPaths(applicationPath);
  64. }
  65. /// <summary>
  66. /// Determines whether this instance [can self restart].
  67. /// </summary>
  68. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  69. public static bool CanSelfRestart
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets a value indicating whether this instance can self update.
  78. /// </summary>
  79. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  80. public static bool CanSelfUpdate
  81. {
  82. get
  83. {
  84. return false;
  85. }
  86. }
  87. private static RemoteCertificateValidationCallback _ignoreCertificates = new RemoteCertificateValidationCallback(delegate { return true; });
  88. private static TaskCompletionSource<bool> _applicationTaskCompletionSource = new TaskCompletionSource<bool>();
  89. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  90. {
  91. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  92. // Allow all https requests
  93. ServicePointManager.ServerCertificateValidationCallback = _ignoreCertificates;
  94. _appHost = new ApplicationHost(appPaths, logManager, false);
  95. Console.WriteLine ("appHost.Init");
  96. var initProgress = new Progress<double>();
  97. var task = _appHost.Init(initProgress);
  98. Task.WaitAll (task);
  99. Console.WriteLine ("Running startup tasks");
  100. task = _appHost.RunStartupTasks();
  101. Task.WaitAll (task);
  102. task = _applicationTaskCompletionSource.Task;
  103. Task.WaitAll (task);
  104. }
  105. /// <summary>
  106. /// Handles the SessionEnding event of the SystemEvents control.
  107. /// </summary>
  108. /// <param name="sender">The source of the event.</param>
  109. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  110. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  111. {
  112. if (e.Reason == SessionEndReasons.SystemShutdown)
  113. {
  114. Shutdown();
  115. }
  116. }
  117. /// <summary>
  118. /// Begins the log.
  119. /// </summary>
  120. /// <param name="logger">The logger.</param>
  121. private static void BeginLog(ILogger logger)
  122. {
  123. logger.Info("Media Browser Server started");
  124. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  125. logger.Info("Server: {0}", Environment.MachineName);
  126. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  127. }
  128. /// <summary>
  129. /// Handles the UnhandledException event of the CurrentDomain control.
  130. /// </summary>
  131. /// <param name="sender">The source of the event.</param>
  132. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  133. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  134. {
  135. var exception = (Exception)e.ExceptionObject;
  136. LogUnhandledException(exception);
  137. if (!Debugger.IsAttached)
  138. {
  139. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  140. }
  141. }
  142. private static void LogUnhandledException(Exception ex)
  143. {
  144. _logger.ErrorException("UnhandledException", ex);
  145. _appHost.LogManager.Flush ();
  146. var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt");
  147. var builder = LogHelper.GetLogMessage(ex);
  148. Console.WriteLine ("UnhandledException");
  149. Console.WriteLine (builder.ToString());
  150. File.WriteAllText(path, builder.ToString());
  151. }
  152. /// <summary>
  153. /// Performs the update if needed.
  154. /// </summary>
  155. /// <param name="appPaths">The app paths.</param>
  156. /// <param name="logger">The logger.</param>
  157. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  158. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  159. {
  160. return false;
  161. }
  162. public static void Shutdown()
  163. {
  164. _applicationTaskCompletionSource.SetResult (true);
  165. }
  166. public static void Restart()
  167. {
  168. // Second instance will start first, so dispose so that the http ports will be available to the new instance
  169. _appHost.Dispose();
  170. // Right now this method will just shutdown, but not restart
  171. Shutdown ();
  172. }
  173. // Return the running process path
  174. #if MONOMKBUNDLE
  175. public static string GetExecutablePath()
  176. {
  177. var builder = new StringBuilder (8192);
  178. if (Syscall.readlink("/proc/self/exe", builder) >= 0)
  179. return builder.ToString ();
  180. else
  181. return null;
  182. }
  183. #endif
  184. }
  185. class NoCheckCertificatePolicy : ICertificatePolicy
  186. {
  187. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  188. {
  189. return true;
  190. }
  191. }
  192. }