jellyfin/MediaBrowser.Server.Mono/Program.cs

324 lines
11 KiB
C#
Raw Normal View History

2013-09-26 23:20:26 +02:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Mono.Native;
using MediaBrowser.Server.Startup.Common;
2013-09-24 23:06:21 +02:00
using System;
2013-09-26 23:20:26 +02:00
using System.Diagnostics;
2016-11-13 22:04:21 +01:00
using System.Globalization;
2015-05-24 00:01:13 +02:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
2014-10-07 01:58:46 +02:00
using System.Reflection;
2016-11-11 09:13:11 +01:00
using System.Text.RegularExpressions;
2013-09-26 23:20:26 +02:00
using System.Threading.Tasks;
2016-11-11 09:13:11 +01:00
using Emby.Common.Implementations.EnvironmentInfo;
2016-10-29 22:13:23 +02:00
using Emby.Common.Implementations.Logging;
2016-11-11 20:55:12 +01:00
using Emby.Common.Implementations.Networking;
using Emby.Common.Implementations.Security;
2016-11-11 06:23:15 +01:00
using Emby.Server.Core;
2016-11-18 22:06:00 +01:00
using Emby.Server.Implementations;
2016-11-11 08:24:36 +01:00
using Emby.Server.Implementations.IO;
2016-11-11 09:13:11 +01:00
using MediaBrowser.Model.System;
2016-11-11 20:55:12 +01:00
using MediaBrowser.Server.Startup.Common.IO;
2016-11-11 09:13:11 +01:00
using Mono.Unix.Native;
using NLog;
using ILogger = MediaBrowser.Model.Logging.ILogger;
2016-11-11 20:55:12 +01:00
using X509Certificate = System.Security.Cryptography.X509Certificates.X509Certificate;
2013-09-24 23:06:21 +02:00
namespace MediaBrowser.Server.Mono
{
2015-05-24 00:28:26 +02:00
public class MainClass
{
private static ApplicationHost _appHost;
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
private static ILogger _logger;
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
public static void Main(string[] args)
{
2014-11-14 07:27:10 +01:00
var applicationPath = Assembly.GetEntryAssembly().Location;
2016-11-21 18:25:42 +01:00
var appFolderPath = Path.GetDirectoryName(applicationPath);
TryCopySqliteConfigFile(appFolderPath);
SetSqliteProvider();
2014-09-14 17:26:33 +02:00
2016-11-18 22:06:00 +01:00
var options = new StartupOptions(Environment.GetCommandLineArgs());
2015-05-24 00:28:26 +02:00
// Allow this to be specified on the command line.
var customProgramDataPath = options.GetOption("-programdata");
2015-05-24 00:28:26 +02:00
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Info);
logManager.AddConsoleOutput();
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
var logger = _logger = logManager.GetLogger("Main");
2013-09-26 23:20:26 +02:00
2014-11-14 07:27:10 +01:00
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
2016-12-10 02:58:52 +01:00
2015-05-24 00:28:26 +02:00
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
try
{
RunApplication(appPaths, logManager, options);
}
finally
{
logger.Info("Shutting down");
_appHost.Dispose();
}
}
2013-09-26 23:20:26 +02:00
2016-11-21 18:25:42 +01:00
private static void TryCopySqliteConfigFile(string appFolderPath)
{
try
{
File.Copy(Path.Combine(appFolderPath, "System.Data.SQLite.dll.config"),
Path.Combine(appFolderPath, "SQLitePCLRaw.provider.sqlite3.dll.config"),
true);
}
catch
{
}
}
private static void SetSqliteProvider()
{
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
}
2015-05-24 00:28:26 +02:00
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
{
if (string.IsNullOrEmpty(programDataPath))
{
programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
}
2013-09-26 23:20:26 +02:00
2016-11-13 22:04:21 +01:00
var appFolderPath = Path.GetDirectoryName(applicationPath);
return new ServerApplicationPaths(programDataPath, appFolderPath, Path.GetDirectoryName(applicationPath));
2015-05-24 00:28:26 +02:00
}
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
2015-05-24 00:28:26 +02:00
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
{
// Allow all https requests
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
2016-11-11 08:24:36 +01:00
var fileSystem = new MonoFileSystem(logManager.GetLogger("FileSystem"), false, false);
2015-10-04 06:23:11 +02:00
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
2014-10-07 01:58:46 +02:00
2016-11-11 09:13:11 +01:00
var environmentInfo = GetEnvironmentInfo();
2016-11-11 20:55:12 +01:00
var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths);
2016-11-13 05:33:51 +01:00
_appHost = new MonoAppHost(appPaths,
2016-11-11 20:55:12 +01:00
logManager,
options,
fileSystem,
new PowerManagement(),
"emby.mono.zip",
environmentInfo,
imageEncoder,
new Startup.Common.SystemEvents(logManager.GetLogger("SystemEvents")),
new MemoryStreamProvider(),
new NetworkManager(logManager.GetLogger("NetworkManager")),
GenerateCertificate,
2016-11-14 07:44:21 +01:00
() => Environment.UserName);
2015-05-24 00:28:26 +02:00
if (options.ContainsOption("-v"))
{
Console.WriteLine(_appHost.ApplicationVersion.ToString());
return;
}
Console.WriteLine("appHost.Init");
var initProgress = new Progress<double>();
var task = _appHost.Init(initProgress);
Task.WaitAll(task);
Console.WriteLine("Running startup tasks");
task = _appHost.RunStartupTasks();
Task.WaitAll(task);
task = ApplicationTaskCompletionSource.Task;
Task.WaitAll(task);
}
2016-11-11 20:55:12 +01:00
private static void GenerateCertificate(string certPath, string certHost)
{
CertificateGenerator.CreateSelfSignCertificatePfx(certPath, certHost, _logger);
}
2016-11-11 09:13:11 +01:00
private static MonoEnvironmentInfo GetEnvironmentInfo()
{
var info = new MonoEnvironmentInfo();
var uname = GetUnixName();
var sysName = uname.sysname ?? string.Empty;
if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
{
//info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
}
else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
{
//info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
}
else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
{
//info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
info.IsBsd = true;
}
var archX86 = new Regex("(i|I)[3-6]86");
if (archX86.IsMatch(uname.machine))
{
info.CustomArchitecture = Architecture.X86;
}
else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
{
info.CustomArchitecture = Architecture.X64;
}
else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
{
info.CustomArchitecture = Architecture.Arm;
}
else if (System.Environment.Is64BitOperatingSystem)
{
info.CustomArchitecture = Architecture.X64;
}
else
{
info.CustomArchitecture = Architecture.X86;
}
return info;
}
private static Uname _unixName;
private static Uname GetUnixName()
{
if (_unixName == null)
{
var uname = new Uname();
try
{
Utsname utsname;
var callResult = Syscall.uname(out utsname);
if (callResult == 0)
{
uname.sysname = utsname.sysname ?? string.Empty;
uname.machine = utsname.machine ?? string.Empty;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error getting unix name", ex);
}
_unixName = uname;
}
return _unixName;
}
public class Uname
{
public string sysname = string.Empty;
public string machine = string.Empty;
}
2015-05-24 00:28:26 +02:00
/// <summary>
/// Handles the UnhandledException event of the CurrentDomain control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
2013-09-26 23:20:26 +02:00
2014-11-23 23:36:40 +01:00
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
if (!Debugger.IsAttached)
{
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
}
}
2013-09-26 23:20:26 +02:00
2015-05-24 00:28:26 +02:00
public static void Shutdown()
{
ApplicationTaskCompletionSource.SetResult(true);
}
2015-05-24 00:01:13 +02:00
2015-06-05 16:27:01 +02:00
public static void Restart(StartupOptions startupOptions)
2015-05-24 00:01:13 +02:00
{
_logger.Info("Disposing app host");
_appHost.Dispose();
_logger.Info("Starting new instance");
2015-06-05 16:27:01 +02:00
string module = startupOptions.GetOption("-restartpath");
string commandLineArgsString = startupOptions.GetOption("-restartargs") ?? string.Empty;
2015-05-24 00:01:13 +02:00
2015-06-05 16:27:01 +02:00
if (string.IsNullOrWhiteSpace(module))
{
module = Environment.GetCommandLineArgs().First();
}
if (!startupOptions.ContainsOption("-restartargs"))
{
var args = Environment.GetCommandLineArgs()
.Skip(1)
.Select(NormalizeCommandLineArgument);
commandLineArgsString = string.Join(" ", args.ToArray());
}
2015-05-24 00:28:26 +02:00
_logger.Info("Executable: {0}", module);
_logger.Info("Arguments: {0}", commandLineArgsString);
2015-05-24 00:01:13 +02:00
2015-05-24 00:28:26 +02:00
Process.Start(module, commandLineArgsString);
2015-05-24 00:01:13 +02:00
_logger.Info("Calling Environment.Exit");
Environment.Exit(0);
}
2015-05-24 00:28:26 +02:00
private static string NormalizeCommandLineArgument(string arg)
{
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
{
2015-05-24 00:01:13 +02:00
return arg;
2015-05-24 00:28:26 +02:00
}
2015-05-24 00:01:13 +02:00
return "\"" + arg + "\"";
2015-05-24 00:28:26 +02:00
}
}
class NoCheckCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
}
2016-11-11 09:13:11 +01:00
public class MonoEnvironmentInfo : EnvironmentInfo
{
public bool IsBsd { get; set; }
2016-11-13 22:04:21 +01:00
2016-11-14 21:05:38 +01:00
public override string GetUserId()
2016-11-13 22:04:21 +01:00
{
return Syscall.getuid().ToString(CultureInfo.InvariantCulture);
}
2016-11-11 09:13:11 +01:00
}
2013-09-24 23:06:21 +02:00
}