using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations; using MediaBrowser.Server.Startup.Common; using MediaBrowser.ServerApplication.Native; using MediaBrowser.ServerApplication.Splash; using MediaBrowser.ServerApplication.Updates; using Microsoft.Win32; using System; using System.Configuration.Install; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; using System.Runtime.InteropServices; using System.ServiceProcess; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Emby.Common.Implementations.EnvironmentInfo; using Emby.Common.Implementations.IO; using Emby.Common.Implementations.Logging; using Emby.Common.Implementations.Networking; using Emby.Server.Core.Cryptography; using Emby.Drawing; using Emby.Server.Core; using Emby.Server.Core.IO; using Emby.Server.Core.Logging; using Emby.Server.Implementations; using Emby.Server.Implementations.Browser; using Emby.Server.Implementations.IO; using Emby.Server.Implementations.Logging; using MediaBrowser.Common.Net; using MediaBrowser.Model.IO; namespace MediaBrowser.ServerApplication { public class MainStartup { private static ApplicationHost _appHost; private static ILogger _logger; public static bool IsRunningAsService = false; private static bool _canRestartService = false; private static bool _appHostDisposed; [DllImport("kernel32.dll", SetLastError = true)] static extern bool SetDllDirectory(string lpPathName); public static string ApplicationPath; private static IFileSystem FileSystem; /// /// Defines the entry point of the application. /// [STAThread] public static void Main() { var options = new StartupOptions(Environment.GetCommandLineArgs()); IsRunningAsService = options.ContainsOption("-service"); if (IsRunningAsService) { //_canRestartService = CanRestartWindowsService(); } var currentProcess = Process.GetCurrentProcess(); ApplicationPath = currentProcess.MainModule.FileName; var architecturePath = Path.Combine(Path.GetDirectoryName(ApplicationPath), Environment.Is64BitProcess ? "x64" : "x86"); var success = SetDllDirectory(architecturePath); SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3()); var appPaths = CreateApplicationPaths(ApplicationPath, IsRunningAsService); var logManager = new NlogManager(appPaths.LogDirectoryPath, "server"); logManager.ReloadLogger(LogSeverity.Debug); logManager.AddConsoleOutput(); var logger = _logger = logManager.GetLogger("Main"); ApplicationHost.LogEnvironmentInfo(logger, appPaths, true); // Install directly if (options.ContainsOption("-installservice")) { logger.Info("Performing service installation"); InstallService(ApplicationPath, logger); return; } // Restart with admin rights, then install if (options.ContainsOption("-installserviceasadmin")) { logger.Info("Performing service installation"); RunServiceInstallation(ApplicationPath); return; } // Uninstall directly if (options.ContainsOption("-uninstallservice")) { logger.Info("Performing service uninstallation"); UninstallService(ApplicationPath, logger); return; } // Restart with admin rights, then uninstall if (options.ContainsOption("-uninstallserviceasadmin")) { logger.Info("Performing service uninstallation"); RunServiceUninstallation(ApplicationPath); return; } AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; RunServiceInstallationIfNeeded(ApplicationPath); if (IsAlreadyRunning(ApplicationPath, currentProcess)) { logger.Info("Shutting down because another instance of Emby Server is already running."); return; } if (PerformUpdateIfNeeded(appPaths, logger)) { logger.Info("Exiting to perform application update."); return; } try { RunApplication(appPaths, logManager, IsRunningAsService, options); } finally { OnServiceShutdown(); } } /// /// Determines whether [is already running] [the specified current process]. /// /// The application path. /// The current process. /// true if [is already running] [the specified current process]; otherwise, false. 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(40000)) { _logger.Info("The duplicate process did not exit."); return true; } } if (!IsRunningAsService) { return IsAlreadyRunningAsService(applicationPath); } return false; } private static bool IsAlreadyRunningAsService(string applicationPath) { var serviceName = BackgroundService.GetExistingServiceName(); WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName)); ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery); ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); foreach (ManagementObject managementObject in managementObjectCollection) { var obj = managementObject.GetPropertyValue("PathName"); if (obj == null) { continue; } var path = obj.ToString(); _logger.Info("Service path: {0}", path); // Need to use indexOf instead of equality because the path will have the full service command line if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) { _logger.Info("The windows service is already running"); MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service."); return true; } } return false; } /// /// Creates the application paths. /// /// The application path. /// if set to true [run as service]. /// ServerApplicationPaths. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, bool runAsService) { var appFolderPath = Path.GetDirectoryName(applicationPath); var resourcesPath = Path.GetDirectoryName(applicationPath); Action createDirectoryFn = s => Directory.CreateDirectory(s); if (runAsService) { var systemPath = Path.GetDirectoryName(applicationPath); var programDataPath = Path.GetDirectoryName(systemPath); return new ServerApplicationPaths(programDataPath, appFolderPath, resourcesPath, createDirectoryFn); } return new ServerApplicationPaths(ApplicationPathHelper.GetProgramDataPath(applicationPath), appFolderPath, resourcesPath, createDirectoryFn); } /// /// Gets a value indicating whether this instance can self restart. /// /// true if this instance can self restart; otherwise, false. public static bool CanSelfRestart { get { if (IsRunningAsService) { return _canRestartService; } else { return true; } } } /// /// Gets a value indicating whether this instance can self update. /// /// true if this instance can self update; otherwise, false. public static bool CanSelfUpdate { get { #if DEBUG return false; #endif if (IsRunningAsService) { return _canRestartService; } else { return true; } } } /// /// Runs the application. /// /// The app paths. /// The log manager. /// if set to true [run service]. /// The options. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, bool runService, StartupOptions options) { var environmentInfo = new EnvironmentInfo(); var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), environmentInfo, appPaths.TempDirectory); FileSystem = fileSystem; _appHost = new WindowsAppHost(appPaths, logManager, options, fileSystem, new PowerManagement(), "emby.windows.zip", environmentInfo, new NullImageEncoder(), new Server.Startup.Common.SystemEvents(logManager.GetLogger("SystemEvents")), new RecyclableMemoryStreamProvider(), new Networking.NetworkManager(logManager.GetLogger("NetworkManager")), GenerateCertificate, () => Environment.UserDomainName); var initProgress = new Progress(); if (!runService) { if (!options.ContainsOption("-nosplash")) ShowSplashScreen(_appHost.ApplicationVersion, initProgress, logManager.GetLogger("Splash")); // 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); } var task = _appHost.Init(initProgress); Task.WaitAll(task); if (!runService) { task = InstallVcredist2013IfNeeded(_appHost, _logger); Task.WaitAll(task); // needed by skia task = InstallVcredist2015IfNeeded(_appHost, _logger); Task.WaitAll(task); } // set image encoder here _appHost.ImageProcessor.ImageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths); task = task.ContinueWith(new Action(a => _appHost.RunStartupTasks()), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent); if (runService) { StartService(logManager); } else { Task.WaitAll(task); Microsoft.Win32.SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; HideSplashScreen(); ShowTrayIcon(); } } private static void GenerateCertificate(string certPath, string certHost, string certPassword) { CertificateGenerator.CreateSelfSignCertificatePfx(certPath, certHost, certPassword, _logger); } private static ServerNotifyIcon _serverNotifyIcon; private static TaskScheduler _mainTaskScheduler; private static void ShowTrayIcon() { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); _serverNotifyIcon = new ServerNotifyIcon(_appHost.LogManager, _appHost, _appHost.ServerConfigurationManager, _appHost.LocalizationManager); _mainTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Application.Run(); } internal static SplashForm _splash; private static Thread _splashThread; private static void ShowSplashScreen(Version appVersion, Progress progress, ILogger logger) { var thread = new Thread(() => { _splash = new SplashForm(appVersion, progress); _splash.ShowDialog(); }); thread.SetApartmentState(ApartmentState.STA); thread.IsBackground = true; thread.Start(); _splashThread = thread; } private static void HideSplashScreen() { if (_splash != null) { Action act = () => { _splash.Close(); _splashThread = null; }; _splash.Invoke(act); } } static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) { if (e.Reason == SessionSwitchReason.SessionLogon) { BrowserLauncher.OpenDashboard(_appHost); } } public static void Invoke(Action action) { if (IsRunningAsService) { action(); } else { Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, _mainTaskScheduler ?? TaskScheduler.Current); } } /// /// Starts the service. /// private static void StartService(ILogManager logManager) { var service = new BackgroundService(logManager.GetLogger("Service")); service.Disposed += service_Disposed; ServiceBase.Run(service); } /// /// Handles the Disposed event of the service control. /// /// The source of the event. /// The instance containing the event data. static void service_Disposed(object sender, EventArgs e) { OnServiceShutdown(); } private static void OnServiceShutdown() { _logger.Info("Shutting down"); DisposeAppHost(); } /// /// Installs the service. /// private static void InstallService(string applicationPath, ILogger logger) { try { ManagedInstallerClass.InstallHelper(new[] { applicationPath }); logger.Info("Service installation succeeded"); } catch (Exception ex) { logger.ErrorException("Uninstall failed", ex); } } /// /// Uninstalls the service. /// private static void UninstallService(string applicationPath, ILogger logger) { try { ManagedInstallerClass.InstallHelper(new[] { "/u", applicationPath }); logger.Info("Service uninstallation succeeded"); } catch (Exception ex) { logger.ErrorException("Uninstall failed", ex); } } private static void RunServiceInstallationIfNeeded(string applicationPath) { var serviceName = BackgroundService.GetExistingServiceName(); var ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName); if (ctl == null) { RunServiceInstallation(applicationPath); } } /// /// Runs the service installation. /// private static void RunServiceInstallation(string applicationPath) { var startInfo = new ProcessStartInfo { FileName = applicationPath, Arguments = "-installservice", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Verb = "runas", ErrorDialog = false }; using (var process = Process.Start(startInfo)) { process.WaitForExit(); } } /// /// Runs the service uninstallation. /// private static void RunServiceUninstallation(string applicationPath) { var startInfo = new ProcessStartInfo { FileName = applicationPath, Arguments = "-uninstallservice", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Verb = "runas", ErrorDialog = false }; using (var process = Process.Start(startInfo)) { process.WaitForExit(); } } /// /// Handles the UnhandledException event of the CurrentDomain control. /// /// The source of the event. /// The instance containing the event data. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var exception = (Exception)e.ExceptionObject; new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager, FileSystem, new ConsoleLogger()).Log(exception); if (!IsRunningAsService) { MessageBox.Show("Unhandled exception: " + exception.Message); } if (!Debugger.IsAttached) { Environment.Exit(Marshal.GetHRForException(exception)); } } /// /// Performs the update if needed. /// /// The app paths. /// The logger. /// true if XXXX, false otherwise private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger) { // Not supported if (IsRunningAsService) { return false; } // Look for the existence of an update archive var updateArchive = Path.Combine(appPaths.TempUpdatePath, "MBServer" + ".zip"); if (File.Exists(updateArchive)) { logger.Info("An update is available from {0}", updateArchive); // Update is there - execute update try { var serviceName = IsRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty; new ApplicationUpdater().UpdateApplication(appPaths, updateArchive, logger, serviceName); // And just let the app exit so it can update return true; } catch (Exception e) { logger.ErrorException("Error starting updater.", e); MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message)); } } return false; } public static void Shutdown() { if (IsRunningAsService) { ShutdownWindowsService(); } else { DisposeAppHost(); ShutdownWindowsApplication(); } } public static void Restart() { DisposeAppHost(); if (IsRunningAsService) { RestartWindowsService(); } else { //_logger.Info("Hiding server notify icon"); //_serverNotifyIcon.Visible = false; _logger.Info("Starting new instance"); //Application.Restart(); Process.Start(ApplicationPath); ShutdownWindowsApplication(); } } private static void DisposeAppHost() { if (!_appHostDisposed) { _logger.Info("Disposing app host"); _appHostDisposed = true; _appHost.Dispose(); _logger.Info("App host dispose complete"); } } private static void ShutdownWindowsApplication() { if (_serverNotifyIcon != null) { _serverNotifyIcon.Dispose(); _serverNotifyIcon = null; } _logger.Info("Calling Application.Exit"); //Application.Exit(); Environment.Exit(0); } private static void ShutdownWindowsService() { _logger.Info("Stopping background service"); var service = new ServiceController(BackgroundService.GetExistingServiceName()); service.Refresh(); if (service.Status == ServiceControllerStatus.Running) { service.Stop(); } } private static void RestartWindowsService() { _logger.Info("Restarting background service"); var startInfo = new ProcessStartInfo { FileName = "cmd.exe", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Verb = "runas", ErrorDialog = false, Arguments = String.Format("/c sc stop {0} & sc start {0} & sc start {0}", BackgroundService.GetExistingServiceName()) }; Process.Start(startInfo); } private static async Task InstallVcredist2013IfNeeded(ApplicationHost appHost, ILogger logger) { // Reference // http://stackoverflow.com/questions/12206314/detect-if-visual-c-redistributable-for-visual-studio-2012-is-installed try { var subkey = Environment.Is64BitProcess ? "SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\12.0\\VC\\Runtimes\\x64" : "SOFTWARE\\Microsoft\\VisualStudio\\12.0\\VC\\Runtimes\\x86"; using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default) .OpenSubKey(subkey)) { if (ndpKey != null && ndpKey.GetValue("Version") != null) { var installedVersion = ((string)ndpKey.GetValue("Version")).TrimStart('v'); if (installedVersion.StartsWith("12", StringComparison.OrdinalIgnoreCase)) { return; } } } } catch (Exception ex) { logger.ErrorException("Error getting .NET Framework version", ex); return; } try { await InstallVcredist(GetVcredist2013Url()).ConfigureAwait(false); } catch (Exception ex) { logger.ErrorException("Error installing Visual Studio C++ runtime", ex); } } private static string GetVcredist2013Url() { if (Environment.Is64BitProcess) { return "https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2013/vcredist_x64.exe"; } // TODO: ARM url - https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2013/vcredist_arm.exe return "https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2013/vcredist_x86.exe"; } private static async Task InstallVcredist2015IfNeeded(ApplicationHost appHost, ILogger logger) { // Reference // http://stackoverflow.com/questions/12206314/detect-if-visual-c-redistributable-for-visual-studio-2012-is-installed try { var subkey = Environment.Is64BitProcess ? "SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\14.0\\VC\\Runtimes\\x64" : "SOFTWARE\\Microsoft\\VisualStudio\\14.0\\VC\\Runtimes\\x86"; using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default) .OpenSubKey(subkey)) { if (ndpKey != null && ndpKey.GetValue("Version") != null) { var installedVersion = ((string)ndpKey.GetValue("Version")).TrimStart('v'); if (installedVersion.StartsWith("14", StringComparison.OrdinalIgnoreCase)) { return; } } } } catch (Exception ex) { logger.ErrorException("Error getting .NET Framework version", ex); return; } try { await InstallVcredist(GetVcredist2015Url()).ConfigureAwait(false); } catch (Exception ex) { logger.ErrorException("Error installing Visual Studio C++ runtime", ex); } } private static string GetVcredist2015Url() { if (Environment.Is64BitProcess) { return "https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2015/vc_redist.x64.exe"; } // TODO: ARM url - https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2015/vcredist_arm.exe return "https://github.com/MediaBrowser/Emby.Resources/raw/master/vcredist2015/vc_redist.x86.exe"; } private async static Task InstallVcredist(string url) { var httpClient = _appHost.HttpClient; var tmp = await httpClient.GetTempFile(new HttpRequestOptions { Url = url, Progress = new Progress() }).ConfigureAwait(false); var exePath = Path.ChangeExtension(tmp, ".exe"); File.Copy(tmp, exePath); var startInfo = new ProcessStartInfo { FileName = exePath, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Verb = "runas", ErrorDialog = false }; _logger.Info("Running {0}", startInfo.FileName); using (var process = Process.Start(startInfo)) { process.WaitForExit(); } } /// /// Sets the error mode. /// /// The u mode. /// ErrorModes. [DllImport("kernel32.dll")] static extern ErrorModes SetErrorMode(ErrorModes uMode); /// /// Enum ErrorModes /// [Flags] public enum ErrorModes : uint { /// /// The SYSTE m_ DEFAULT /// SYSTEM_DEFAULT = 0x0, /// /// The SE m_ FAILCRITICALERRORS /// SEM_FAILCRITICALERRORS = 0x0001, /// /// The SE m_ NOALIGNMENTFAULTEXCEPT /// SEM_NOALIGNMENTFAULTEXCEPT = 0x0004, /// /// The SE m_ NOGPFAULTERRORBOX /// SEM_NOGPFAULTERRORBOX = 0x0002, /// /// The SE m_ NOOPENFILEERRORBOX /// SEM_NOOPENFILEERRORBOX = 0x8000 } } }