2016-11-12 04:03:07 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Server.Implementations;
|
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2016-10-26 02:54:12 +00:00
|
|
|
|
using System.Linq;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2016-10-26 02:54:12 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
using Emby.Common.Implementations.EnvironmentInfo;
|
|
|
|
|
using Emby.Common.Implementations.IO;
|
|
|
|
|
using Emby.Common.Implementations.Logging;
|
|
|
|
|
using Emby.Common.Implementations.Networking;
|
2016-11-13 04:33:51 +00:00
|
|
|
|
using Emby.Drawing;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
using Emby.Server.Core;
|
2016-11-20 21:02:32 +00:00
|
|
|
|
using Emby.Server.Implementations.Browser;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
using Emby.Server.Implementations.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-11-13 04:33:51 +00:00
|
|
|
|
using Emby.Server.IO;
|
2016-11-20 21:02:32 +00:00
|
|
|
|
using Emby.Server.Implementations;
|
2016-10-26 02:54:12 +00:00
|
|
|
|
|
|
|
|
|
namespace Emby.Server
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2016-11-12 04:03:07 +00:00
|
|
|
|
private static ApplicationHost _appHost;
|
|
|
|
|
|
|
|
|
|
private static ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private static bool _appHostDisposed;
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
static extern bool SetDllDirectory(string lpPathName);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines the entry point of the application.
|
|
|
|
|
/// </summary>
|
2016-10-26 02:54:12 +00:00
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
2016-11-12 04:03:07 +00:00
|
|
|
|
var options = new StartupOptions();
|
|
|
|
|
|
|
|
|
|
var currentProcess = Process.GetCurrentProcess();
|
2016-11-13 21:04:21 +00:00
|
|
|
|
|
|
|
|
|
var baseDirectory = System.AppContext.BaseDirectory;
|
2016-11-13 04:33:51 +00:00
|
|
|
|
//var architecturePath = Path.Combine(Path.GetDirectoryName(applicationPath), Environment.Is64BitProcess ? "x64" : "x86");
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
//Wand.SetMagickCoderModulePath(architecturePath);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
//var success = SetDllDirectory(architecturePath);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
var appPaths = CreateApplicationPaths(baseDirectory);
|
2016-11-18 08:39:20 +00:00
|
|
|
|
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
|
|
|
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
|
|
|
|
logManager.ReloadLogger(LogSeverity.Debug);
|
|
|
|
|
logManager.AddConsoleOutput();
|
|
|
|
|
|
|
|
|
|
var logger = _logger = logManager.GetLogger("Main");
|
2016-11-13 21:04:21 +00:00
|
|
|
|
|
2016-11-12 04:03:07 +00:00
|
|
|
|
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
//if (IsAlreadyRunning(applicationPath, currentProcess))
|
|
|
|
|
//{
|
|
|
|
|
// logger.Info("Shutting down because another instance of Emby Server is already running.");
|
|
|
|
|
// return;
|
|
|
|
|
//}
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
|
|
|
|
if (PerformUpdateIfNeeded(appPaths, logger))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Exiting to perform application update.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
RunApplication(appPaths, logManager, options);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is already running] [the specified current process].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="applicationPath">The application path.</param>
|
|
|
|
|
/// <param name="currentProcess">The current process.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is already running] [the specified current process]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
private static bool IsAlreadyRunning(string applicationPath, Process currentProcess)
|
|
|
|
|
{
|
|
|
|
|
var duplicate = Process.GetProcesses().FirstOrDefault(i =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (currentProcess.Id == i.Id)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//_logger.Info("Module: {0}", i.MainModule.FileName);
|
|
|
|
|
if (string.Equals(applicationPath, i.MainModule.FileName, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (duplicate != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Found a duplicate process. Giving it time to exit.");
|
|
|
|
|
|
|
|
|
|
if (!duplicate.WaitForExit(30000))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("The duplicate process did not exit.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the application paths.
|
|
|
|
|
/// </summary>
|
2016-11-13 21:04:21 +00:00
|
|
|
|
private static ServerApplicationPaths CreateApplicationPaths(string appDirectory)
|
2016-11-12 04:03:07 +00:00
|
|
|
|
{
|
2016-11-13 21:04:21 +00:00
|
|
|
|
var resourcesPath = appDirectory;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
return new ServerApplicationPaths(ApplicationPathHelper.GetProgramDataPath(appDirectory), appDirectory, resourcesPath);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance can self restart.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
|
|
|
|
|
public static bool CanSelfRestart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-13 21:04:21 +00:00
|
|
|
|
return true;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance can self update.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
|
|
|
|
|
public static bool CanSelfUpdate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-13 21:04:21 +00:00
|
|
|
|
return false;
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="options">The options.</param>
|
2016-11-13 21:04:21 +00:00
|
|
|
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
|
2016-11-12 04:03:07 +00:00
|
|
|
|
{
|
2016-11-13 04:33:51 +00:00
|
|
|
|
var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), true, true, true);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
var imageEncoder = new NullImageEncoder();
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
_appHost = new CoreAppHost(appPaths,
|
2016-11-12 04:03:07 +00:00
|
|
|
|
logManager,
|
|
|
|
|
options,
|
|
|
|
|
fileSystem,
|
|
|
|
|
new PowerManagement(),
|
|
|
|
|
"emby.windows.zip",
|
|
|
|
|
new EnvironmentInfo(),
|
|
|
|
|
imageEncoder,
|
2016-11-13 04:33:51 +00:00
|
|
|
|
new CoreSystemEvents(),
|
|
|
|
|
new MemoryStreamFactory(),
|
2016-11-12 04:03:07 +00:00
|
|
|
|
new NetworkManager(logManager.GetLogger("NetworkManager")),
|
|
|
|
|
GenerateCertificate,
|
2016-11-13 04:33:51 +00:00
|
|
|
|
() => "EmbyUser");
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
|
|
|
|
var initProgress = new Progress<double>();
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
// Not crazy about this but it's the only way to suppress ffmpeg crash dialog boxes
|
|
|
|
|
SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT |
|
|
|
|
|
ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
|
|
|
|
var task = _appHost.Init(initProgress);
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
|
|
|
|
|
task = task.ContinueWith(new Action<Task>(a => _appHost.RunStartupTasks()), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
Task.WaitAll(task);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
task = ApplicationTaskCompletionSource.Task;
|
|
|
|
|
Task.WaitAll(task);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void GenerateCertificate(string certPath, string certHost)
|
|
|
|
|
{
|
2016-11-13 04:33:51 +00:00
|
|
|
|
//CertificateGenerator.CreateSelfSignCertificatePfx(certPath, certHost, _logger);
|
2016-11-12 04:03:07 +00: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;
|
|
|
|
|
|
|
|
|
|
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
ShowMessageBox("Unhandled exception: " + exception.Message);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
|
{
|
|
|
|
|
Environment.Exit(Marshal.GetHRForException(exception));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs the update if needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 04:33:51 +00:00
|
|
|
|
private static void ShowMessageBox(string msg)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-12 04:03:07 +00:00
|
|
|
|
public static void Shutdown()
|
|
|
|
|
{
|
2016-11-13 21:04:21 +00:00
|
|
|
|
DisposeAppHost();
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
//_logger.Info("Calling Application.Exit");
|
|
|
|
|
//Application.Exit();
|
|
|
|
|
|
|
|
|
|
_logger.Info("Calling Environment.Exit");
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
|
|
|
|
|
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult");
|
|
|
|
|
ApplicationTaskCompletionSource.SetResult(true);
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Restart()
|
|
|
|
|
{
|
|
|
|
|
DisposeAppHost();
|
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
// todo: start new instance
|
2016-11-12 04:03:07 +00:00
|
|
|
|
|
2016-11-13 21:04:21 +00:00
|
|
|
|
Shutdown();
|
2016-11-12 04:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DisposeAppHost()
|
|
|
|
|
{
|
|
|
|
|
if (!_appHostDisposed)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Disposing app host");
|
|
|
|
|
|
|
|
|
|
_appHostDisposed = true;
|
|
|
|
|
_appHost.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the error mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uMode">The u mode.</param>
|
|
|
|
|
/// <returns>ErrorModes.</returns>
|
|
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
|
static extern ErrorModes SetErrorMode(ErrorModes uMode);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enum ErrorModes
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum ErrorModes : uint
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SYSTE m_ DEFAULT
|
|
|
|
|
/// </summary>
|
|
|
|
|
SYSTEM_DEFAULT = 0x0,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ FAILCRITICALERRORS
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_FAILCRITICALERRORS = 0x0001,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOALIGNMENTFAULTEXCEPT
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOGPFAULTERRORBOX
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOGPFAULTERRORBOX = 0x0002,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOOPENFILEERRORBOX
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOOPENFILEERRORBOX = 0x8000
|
2016-10-26 02:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|