2013-06-28 00:04:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2013-07-26 06:11:55 +00:00
|
|
|
|
using System.IO;
|
2014-02-10 09:49:06 +00:00
|
|
|
|
using System.Reflection;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
using System.Security.Principal;
|
2013-11-26 06:53:36 +00:00
|
|
|
|
using System.ServiceProcess;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
using NLog;
|
2014-01-14 01:35:16 +00:00
|
|
|
|
using NzbDrone.Common.Processes;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.EnvironmentInfo
|
|
|
|
|
{
|
|
|
|
|
public interface IRuntimeInfo
|
|
|
|
|
{
|
|
|
|
|
bool IsUserInteractive { get; }
|
|
|
|
|
bool IsAdmin { get; }
|
2013-11-26 06:53:36 +00:00
|
|
|
|
bool IsWindowsService { get; }
|
2014-01-13 01:14:57 +00:00
|
|
|
|
bool IsConsole { get; }
|
|
|
|
|
bool IsRunning { get; set; }
|
2014-03-10 05:35:50 +00:00
|
|
|
|
bool RestartPending { get; set; }
|
2014-02-10 09:49:06 +00:00
|
|
|
|
string ExecutingApplication { get; }
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RuntimeInfo : IRuntimeInfo
|
|
|
|
|
{
|
|
|
|
|
private readonly Logger _logger;
|
2014-01-13 01:14:57 +00:00
|
|
|
|
private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
|
public RuntimeInfo(Logger logger, IServiceProvider serviceProvider)
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2013-11-26 06:53:36 +00:00
|
|
|
|
|
|
|
|
|
IsWindowsService = !IsUserInteractive &&
|
2014-01-13 01:14:57 +00:00
|
|
|
|
OsInfo.IsWindows &&
|
|
|
|
|
serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) &&
|
|
|
|
|
serviceProvider.GetStatus(ServiceProvider.NZBDRONE_SERVICE_NAME) == ServiceControllerStatus.StartPending;
|
2014-02-10 09:49:06 +00:00
|
|
|
|
|
2014-02-10 20:24:59 +00:00
|
|
|
|
//Guarded to avoid issues when running in a non-managed process
|
2014-02-10 19:40:13 +00:00
|
|
|
|
var entry = Assembly.GetEntryAssembly();
|
|
|
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
|
{
|
|
|
|
|
ExecutingApplication = entry.Location;
|
|
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-13 01:14:57 +00:00
|
|
|
|
static RuntimeInfo()
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
2014-01-13 01:14:57 +00:00
|
|
|
|
IsProduction = InternalIsProduction();
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-13 01:14:57 +00:00
|
|
|
|
public bool IsUserInteractive
|
2013-07-26 06:11:55 +00:00
|
|
|
|
{
|
2014-01-13 01:14:57 +00:00
|
|
|
|
get { return Environment.UserInteractive; }
|
2013-07-26 06:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-28 00:04:52 +00:00
|
|
|
|
public bool IsAdmin
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
|
|
|
|
|
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Error checking if the current user is an administrator.", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
|
public bool IsWindowsService { get; private set; }
|
|
|
|
|
|
2014-01-13 01:14:57 +00:00
|
|
|
|
public bool IsConsole
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (OsInfo.IsWindows &&
|
|
|
|
|
IsUserInteractive &&
|
2014-01-14 01:35:16 +00:00
|
|
|
|
ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase)) ||
|
2014-03-09 05:28:40 +00:00
|
|
|
|
OsInfo.IsMono;
|
2014-01-13 01:14:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsRunning { get; set; }
|
2014-03-10 05:35:50 +00:00
|
|
|
|
public bool RestartPending { get; set; }
|
2014-02-10 09:49:06 +00:00
|
|
|
|
public string ExecutingApplication { get; private set; }
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2013-07-26 06:11:55 +00:00
|
|
|
|
public static bool IsProduction { get; private set; }
|
|
|
|
|
|
|
|
|
|
private static bool InternalIsProduction()
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
2013-07-26 06:11:55 +00:00
|
|
|
|
if (BuildInfo.IsDebug || Debugger.IsAttached) return false;
|
|
|
|
|
if (BuildInfo.Version.Revision > 10000) return false; //Official builds will never have such a high revision
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2013-07-26 06:11:55 +00:00
|
|
|
|
var lowerProcessName = ProcessName.ToLower();
|
|
|
|
|
if (lowerProcessName.Contains("vshost")) return false;
|
|
|
|
|
if (lowerProcessName.Contains("nunit")) return false;
|
|
|
|
|
if (lowerProcessName.Contains("jetbrain")) return false;
|
|
|
|
|
if (lowerProcessName.Contains("resharper")) return false;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2013-09-15 00:23:58 +00:00
|
|
|
|
string lowerCurrentDir = Directory.GetCurrentDirectory().ToLower();
|
|
|
|
|
if (lowerCurrentDir.Contains("teamcity")) return false;
|
|
|
|
|
if (lowerCurrentDir.StartsWith("/run/")) return false;
|
2013-07-26 06:11:55 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|