Program.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using MediaBrowser.Common.Implementations.Logging;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Server.Implementations;
  4. using MediaBrowser.Server.Mono.Native;
  5. using MediaBrowser.Server.Startup.Common;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Security;
  13. using System.Reflection;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Threading.Tasks;
  16. using Emby.Common.Implementations.IO;
  17. namespace MediaBrowser.Server.Mono
  18. {
  19. public class MainClass
  20. {
  21. private static ApplicationHost _appHost;
  22. private static ILogger _logger;
  23. public static void Main(string[] args)
  24. {
  25. var applicationPath = Assembly.GetEntryAssembly().Location;
  26. var options = new StartupOptions();
  27. // Allow this to be specified on the command line.
  28. var customProgramDataPath = options.GetOption("-programdata");
  29. var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
  30. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  31. logManager.ReloadLogger(LogSeverity.Info);
  32. logManager.AddConsoleOutput();
  33. var logger = _logger = logManager.GetLogger("Main");
  34. ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
  35. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  36. try
  37. {
  38. RunApplication(appPaths, logManager, options);
  39. }
  40. finally
  41. {
  42. logger.Info("Shutting down");
  43. _appHost.Dispose();
  44. }
  45. }
  46. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  47. {
  48. if (string.IsNullOrEmpty(programDataPath))
  49. {
  50. programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
  51. }
  52. return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath));
  53. }
  54. private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
  55. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
  56. {
  57. Microsoft.Win32.SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  58. // Allow all https requests
  59. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  60. var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), false, false);
  61. fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
  62. var nativeApp = new NativeApp(options, logManager.GetLogger("App"));
  63. _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "emby.mono.zip", nativeApp);
  64. if (options.ContainsOption("-v"))
  65. {
  66. Console.WriteLine(_appHost.ApplicationVersion.ToString());
  67. return;
  68. }
  69. Console.WriteLine("appHost.Init");
  70. var initProgress = new Progress<double>();
  71. var task = _appHost.Init(initProgress);
  72. Task.WaitAll(task);
  73. Console.WriteLine("Running startup tasks");
  74. task = _appHost.RunStartupTasks();
  75. Task.WaitAll(task);
  76. task = ApplicationTaskCompletionSource.Task;
  77. Task.WaitAll(task);
  78. }
  79. /// <summary>
  80. /// Handles the SessionEnding event of the SystemEvents control.
  81. /// </summary>
  82. /// <param name="sender">The source of the event.</param>
  83. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  84. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  85. {
  86. if (e.Reason == SessionEndReasons.SystemShutdown)
  87. {
  88. Shutdown();
  89. }
  90. }
  91. /// <summary>
  92. /// Handles the UnhandledException event of the CurrentDomain control.
  93. /// </summary>
  94. /// <param name="sender">The source of the event.</param>
  95. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  96. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  97. {
  98. var exception = (Exception)e.ExceptionObject;
  99. new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
  100. if (!Debugger.IsAttached)
  101. {
  102. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  103. }
  104. }
  105. public static void Shutdown()
  106. {
  107. ApplicationTaskCompletionSource.SetResult(true);
  108. }
  109. public static void Restart(StartupOptions startupOptions)
  110. {
  111. _logger.Info("Disposing app host");
  112. _appHost.Dispose();
  113. _logger.Info("Starting new instance");
  114. string module = startupOptions.GetOption("-restartpath");
  115. string commandLineArgsString = startupOptions.GetOption("-restartargs") ?? string.Empty;
  116. if (string.IsNullOrWhiteSpace(module))
  117. {
  118. module = Environment.GetCommandLineArgs().First();
  119. }
  120. if (!startupOptions.ContainsOption("-restartargs"))
  121. {
  122. var args = Environment.GetCommandLineArgs()
  123. .Skip(1)
  124. .Select(NormalizeCommandLineArgument);
  125. commandLineArgsString = string.Join(" ", args.ToArray());
  126. }
  127. _logger.Info("Executable: {0}", module);
  128. _logger.Info("Arguments: {0}", commandLineArgsString);
  129. Process.Start(module, commandLineArgsString);
  130. _logger.Info("Calling Environment.Exit");
  131. Environment.Exit(0);
  132. }
  133. private static string NormalizeCommandLineArgument(string arg)
  134. {
  135. if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
  136. {
  137. return arg;
  138. }
  139. return "\"" + arg + "\"";
  140. }
  141. }
  142. class NoCheckCertificatePolicy : ICertificatePolicy
  143. {
  144. public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  145. {
  146. return true;
  147. }
  148. }
  149. }