Skip to content

Commit

Permalink
Cleanup properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Nov 16, 2020
1 parent 3817299 commit 6635474
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions IPBanCore/Core/IPBan/IPBanService_Private.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ private void ExecuteExternalProcessForBannedIPAddresses(IReadOnlyCollection<IPAd
WorkingDirectory = Path.GetDirectoryName(program),
Arguments = arguments
};
Process.Start(psi);
using Process p = Process.Start(psi);
}
else
{
Expand Down Expand Up @@ -768,7 +768,7 @@ protected virtual async Task<bool> GetUrl(UrlType urlType)
// pass -c to tell the update executable to delete itself when done
// pass -d for a directory which tells the .exe where this service lives
string args = "-c \"-d=" + AppContext.BaseDirectory + "\"";
Process.Start(tempFile, args);
ProcessUtility.CreateDetachedProcess(tempFile, args);
}
}
else if (urlType == UrlType.Config && bytes.Length != 0)
Expand Down
5 changes: 3 additions & 2 deletions IPBanCore/Core/Utility/OSUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ private static void LoadVersionFromLinux()
Name = FriendlyName = OSUtility.Linux;
isLinux = true;
string tempFile = GetTempFileName();
Process.Start("/bin/bash", "-c \"cat /etc/*release* > " + tempFile + "\"").WaitForExit();
using Process p = Process.Start("/bin/bash", "-c \"cat /etc/*release* > " + tempFile + "\"");
p.WaitForExit();
System.Threading.Tasks.Task.Delay(100); // wait a small bit for file to really be closed
string versionText = File.ReadAllText(tempFile).Trim();
ExtensionMethods.FileDeleteWithRetry(tempFile);
Expand Down Expand Up @@ -289,7 +290,7 @@ public static string StartProcessAndWait(int timeoutMilliseconds, string program
{
Logger.Info($"Executing process {program} {args}...");

var process = new Process
using var process = new Process
{
StartInfo = new ProcessStartInfo(program, args)
{
Expand Down
1 change: 1 addition & 0 deletions IPBanCore/Core/Utility/ProcessUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public static void CreateDetachedProcess(string fileName, string arguments)

Logger.Warn("Running detached process {0} {1}", info.FileName, info.Arguments);

// do not get a reference and do not dispose, this process is orphaned from this process
Process.Start(info);
}
}
Expand Down
3 changes: 2 additions & 1 deletion IPBanCore/IPBanPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public static void IPBanLoginSucceeded(string source, string userName, string re
// Linux
else if (Directory.Exists(@"/var/log"))
{
string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
using Process currentProcess = Process.GetCurrentProcess();
string processName = currentProcess.ProcessName;
File.AppendAllText($"/var/log/ipbancustom_{ProcessName}.log", $"{DateTime.UtcNow:u}, ipban success login, ip address: {remoteIpAddress}, source: {source}, user: {userName}\n");
}
}
Expand Down

0 comments on commit 6635474

Please sign in to comment.