2013-08-07 08:32:22 +03:00
|
|
|
using System;
|
2013-08-14 07:40:34 +03:00
|
|
|
using System.Collections.Generic;
|
2013-08-07 08:32:22 +03:00
|
|
|
using System.ComponentModel;
|
2011-10-07 06:37:41 +03:00
|
|
|
using System.Diagnostics;
|
2013-08-07 08:32:22 +03:00
|
|
|
using System.IO;
|
2011-10-07 06:37:41 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-08-14 07:25:38 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-08-31 04:42:30 +03:00
|
|
|
using NzbDrone.Common.Instrumentation;
|
2011-10-23 08:26:43 +03:00
|
|
|
using NzbDrone.Common.Model;
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2011-10-23 08:26:43 +03:00
|
|
|
namespace NzbDrone.Common
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
public interface IProcessProvider
|
|
|
|
{
|
|
|
|
ProcessInfo GetCurrentProcess();
|
|
|
|
ProcessInfo GetProcessById(int id);
|
2013-08-14 08:20:24 +03:00
|
|
|
void OpenDefaultBrowser(string url);
|
2013-05-11 02:53:50 +03:00
|
|
|
void WaitForExit(Process process);
|
|
|
|
void SetPriority(int processId, ProcessPriorityClass priority);
|
|
|
|
void KillAll(string processName);
|
2013-07-30 23:19:09 +03:00
|
|
|
bool Exists(string processName);
|
|
|
|
ProcessPriorityClass GetCurrentProcessPriority();
|
2013-08-14 08:20:24 +03:00
|
|
|
Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null);
|
2013-08-21 01:12:35 +03:00
|
|
|
Process SpawnNewProcess(string path, string args = null);
|
2013-05-11 02:53:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ProcessProvider : IProcessProvider
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
2013-09-15 03:17:45 +03:00
|
|
|
private static readonly Logger Logger = NzbDroneLogger.GetLogger();
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2013-08-14 08:20:24 +03:00
|
|
|
public const string NZB_DRONE_PROCESS_NAME = "NzbDrone";
|
|
|
|
public const string NZB_DRONE_CONSOLE_PROCESS_NAME = "NzbDrone.Console";
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2013-08-14 07:40:34 +03:00
|
|
|
private static List<Process> GetProcessesByName(string name)
|
|
|
|
{
|
|
|
|
var monoProcesses = Process.GetProcessesByName("mono")
|
2013-08-14 08:02:25 +03:00
|
|
|
.Where(process => process.Modules.Cast<ProcessModule>().Any(module => module.ModuleName.ToLower() == name.ToLower() + ".exe"));
|
2013-08-14 07:40:34 +03:00
|
|
|
return Process.GetProcessesByName(name)
|
|
|
|
.Union(monoProcesses).ToList();
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:51:38 +03:00
|
|
|
public ProcessInfo GetCurrentProcess()
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
2011-10-07 09:36:04 +03:00
|
|
|
return ConvertToProcessInfo(Process.GetCurrentProcess());
|
|
|
|
}
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2013-07-30 23:19:09 +03:00
|
|
|
public bool Exists(string processName)
|
|
|
|
{
|
2013-08-14 07:40:34 +03:00
|
|
|
return GetProcessesByName(processName).Any();
|
2013-07-30 23:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public ProcessPriorityClass GetCurrentProcessPriority()
|
|
|
|
{
|
|
|
|
return Process.GetCurrentProcess().PriorityClass;
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:51:38 +03:00
|
|
|
public ProcessInfo GetProcessById(int id)
|
2011-10-07 09:36:04 +03:00
|
|
|
{
|
2011-12-12 09:52:58 +03:00
|
|
|
Logger.Trace("Finding process with Id:{0}", id);
|
|
|
|
|
2013-05-07 08:54:21 +03:00
|
|
|
var processInfo = ConvertToProcessInfo(Process.GetProcesses().FirstOrDefault(p => p.Id == id));
|
2011-12-12 09:52:58 +03:00
|
|
|
|
|
|
|
if (processInfo == null)
|
|
|
|
{
|
|
|
|
Logger.Warn("Unable to find process with ID {0}", id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Trace("Found process {0}", processInfo.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return processInfo;
|
2011-10-07 09:36:04 +03:00
|
|
|
}
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2013-08-14 08:20:24 +03:00
|
|
|
public void OpenDefaultBrowser(string url)
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
2013-08-14 08:20:24 +03:00
|
|
|
Logger.Info("Opening URL [{0}]", url);
|
|
|
|
|
|
|
|
var process = new Process
|
|
|
|
{
|
|
|
|
StartInfo = new ProcessStartInfo(url)
|
|
|
|
{
|
|
|
|
UseShellExecute = true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
process.Start();
|
2011-10-07 06:37:41 +03:00
|
|
|
}
|
|
|
|
|
2013-08-21 01:12:35 +03:00
|
|
|
|
|
|
|
|
2013-08-14 08:20:24 +03:00
|
|
|
public Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null)
|
2013-08-07 08:32:22 +03:00
|
|
|
{
|
2013-08-14 07:25:38 +03:00
|
|
|
|
|
|
|
if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
args = path + " " + args;
|
|
|
|
path = "mono";
|
|
|
|
}
|
|
|
|
|
2013-08-07 08:32:22 +03:00
|
|
|
var logger = LogManager.GetLogger(new FileInfo(path).Name);
|
|
|
|
|
|
|
|
var startInfo = new ProcessStartInfo(path, args)
|
|
|
|
{
|
|
|
|
CreateNoWindow = true,
|
|
|
|
UseShellExecute = false,
|
|
|
|
RedirectStandardError = true,
|
|
|
|
RedirectStandardOutput = true,
|
2013-08-14 06:22:28 +03:00
|
|
|
RedirectStandardInput = true
|
2013-08-07 08:32:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
logger.Info("Starting {0} {1}", path, args);
|
|
|
|
|
2013-08-13 08:08:37 +03:00
|
|
|
var process = new Process
|
|
|
|
{
|
|
|
|
StartInfo = startInfo
|
|
|
|
};
|
2013-08-07 08:32:22 +03:00
|
|
|
|
|
|
|
process.OutputDataReceived += (sender, eventArgs) =>
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(eventArgs.Data)) return;
|
|
|
|
|
|
|
|
logger.Debug(eventArgs.Data);
|
|
|
|
|
|
|
|
if (onOutputDataReceived != null)
|
|
|
|
{
|
|
|
|
onOutputDataReceived(eventArgs.Data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
process.ErrorDataReceived += (sender, eventArgs) =>
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(eventArgs.Data)) return;
|
|
|
|
|
|
|
|
logger.Error(eventArgs.Data);
|
|
|
|
|
|
|
|
if (onErrorDataReceived != null)
|
|
|
|
{
|
|
|
|
onErrorDataReceived(eventArgs.Data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-14 06:22:28 +03:00
|
|
|
process.Start();
|
|
|
|
|
2013-08-07 08:32:22 +03:00
|
|
|
process.BeginErrorReadLine();
|
|
|
|
process.BeginOutputReadLine();
|
|
|
|
|
2013-08-14 06:22:28 +03:00
|
|
|
|
2013-08-07 08:32:22 +03:00
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
2013-08-21 01:12:35 +03:00
|
|
|
public Process SpawnNewProcess(string path, string args = null)
|
|
|
|
{
|
|
|
|
if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
args = path + " " + args;
|
|
|
|
path = "mono";
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Info("Starting {0} {1}", path, args);
|
|
|
|
|
|
|
|
var startInfo = new ProcessStartInfo(path, args);
|
|
|
|
var process = new Process
|
|
|
|
{
|
|
|
|
StartInfo = startInfo
|
|
|
|
};
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:51:38 +03:00
|
|
|
public void WaitForExit(Process process)
|
2012-02-27 00:22:35 +03:00
|
|
|
{
|
|
|
|
Logger.Trace("Waiting for process {0} to exit.", process.ProcessName);
|
|
|
|
process.WaitForExit();
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:51:38 +03:00
|
|
|
public void SetPriority(int processId, ProcessPriorityClass priority)
|
2011-10-07 09:36:04 +03:00
|
|
|
{
|
|
|
|
var process = Process.GetProcessById(processId);
|
|
|
|
|
|
|
|
Logger.Info("Updating [{0}] process priority from {1} to {2}",
|
|
|
|
process.ProcessName,
|
|
|
|
process.PriorityClass,
|
|
|
|
priority);
|
|
|
|
|
|
|
|
process.PriorityClass = priority;
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:56:49 +03:00
|
|
|
public void KillAll(string processName)
|
|
|
|
{
|
2013-08-14 07:40:34 +03:00
|
|
|
var processToKill = GetProcessesByName(processName);
|
2013-07-27 03:56:49 +03:00
|
|
|
|
|
|
|
foreach (var processInfo in processToKill)
|
|
|
|
{
|
|
|
|
Kill(processInfo.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-07 09:36:04 +03:00
|
|
|
private static ProcessInfo ConvertToProcessInfo(Process process)
|
|
|
|
{
|
2013-07-30 23:19:09 +03:00
|
|
|
if (process == null) return null;
|
|
|
|
|
|
|
|
process.Refresh();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (process.Id <= 0 || process.HasExited) return null;
|
|
|
|
|
|
|
|
return new ProcessInfo
|
|
|
|
{
|
|
|
|
Id = process.Id,
|
2013-09-15 03:17:45 +03:00
|
|
|
StartPath = GetExeFileName(process),
|
2013-07-30 23:19:09 +03:00
|
|
|
Name = process.ProcessName
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch (Win32Exception)
|
|
|
|
{
|
|
|
|
Logger.Warn("Coudn't get process info for " + process.ProcessName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2011-10-07 06:37:41 +03:00
|
|
|
}
|
2013-05-05 00:09:25 +03:00
|
|
|
|
2013-09-15 03:17:45 +03:00
|
|
|
|
|
|
|
private static string GetExeFileName(Process process)
|
|
|
|
{
|
|
|
|
if (process.MainModule.FileName != "mono.exe")
|
|
|
|
{
|
|
|
|
return process.MainModule.FileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return process.Modules.Cast<ProcessModule>().FirstOrDefault(module => module.ModuleName.ToLower().EndsWith(".exe")).FileName;
|
|
|
|
}
|
|
|
|
|
2013-07-05 21:51:38 +03:00
|
|
|
private void Kill(int processId)
|
|
|
|
{
|
2013-08-14 06:22:28 +03:00
|
|
|
var process = Process.GetProcesses().FirstOrDefault(p => p.Id == processId);
|
|
|
|
|
|
|
|
if (process == null)
|
2013-07-05 21:51:38 +03:00
|
|
|
{
|
|
|
|
Logger.Warn("Cannot find process with id: {0}", processId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-14 06:22:28 +03:00
|
|
|
process.Refresh();
|
2013-07-05 21:51:38 +03:00
|
|
|
|
|
|
|
if (process.HasExited)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Info("[{0}]: Killing process", process.Id);
|
|
|
|
process.Kill();
|
|
|
|
Logger.Info("[{0}]: Waiting for exit", process.Id);
|
|
|
|
process.WaitForExit();
|
|
|
|
Logger.Info("[{0}]: Process terminated successfully", process.Id);
|
|
|
|
}
|
2011-10-07 06:37:41 +03:00
|
|
|
}
|
2013-08-13 17:23:47 +03:00
|
|
|
}
|