From da6466e2476aa31873c23eed9f8138c6912118c6 Mon Sep 17 00:00:00 2001 From: noisyfox Date: Tue, 8 Nov 2016 18:49:09 +1100 Subject: [PATCH] Improve exception handling --- shadowsocks-csharp/Controller/Logging.cs | 11 +++++ .../Controller/Service/PrivoxyRunner.cs | 41 +++++++------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/shadowsocks-csharp/Controller/Logging.cs b/shadowsocks-csharp/Controller/Logging.cs index 39452b64..4ab82980 100755 --- a/shadowsocks-csharp/Controller/Logging.cs +++ b/shadowsocks-csharp/Controller/Logging.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.IO; using System.Net.Sockets; using System.Net; @@ -123,6 +124,16 @@ namespace Shadowsocks.Controller else if (e is ObjectDisposedException) { } + else if (e is Win32Exception) + { + var ex = (Win32Exception) e; + + // Win32Exception (0x80004005): A 32 bit processes cannot access modules of a 64 bit process. + if ((uint) ex.ErrorCode != 0x80004005) + { + Info(e); + } + } else { Info(e); diff --git a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs index 631a65f4..895b659f 100644 --- a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs +++ b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs @@ -125,43 +125,32 @@ namespace Shadowsocks.Controller private static bool IsChildProcess(Process process) { - if (Utils.IsPortableMode()) + try { - /* - * Under PortableMode, we could identify it by the path of ss_privoxy.exe. - */ - try + if (Utils.IsPortableMode()) { /* - * Sometimes Process.GetProcessesByName will return some processes that - * are already dead, and that will cause exceptions here. - * We could simply ignore those exceptions. + * Under PortableMode, we could identify it by the path of ss_privoxy.exe. */ - string path = process.MainModule.FileName; + var path = process.MainModule.FileName; + return Utils.GetTempPath("ss_privoxy.exe").Equals(path); } - catch (Exception ex) - { - Logging.LogUsefulException(ex); - return false; - } - } - else - { - try + else { var cmd = process.GetCommandLine(); return cmd.Contains(UniqueConfigFile); } - catch (Win32Exception ex) - { - if ((uint) ex.ErrorCode != 0x80004005) - { - throw; - } - } - + } + catch (Exception ex) + { + /* + * Sometimes Process.GetProcessesByName will return some processes that + * are already dead, and that will cause exceptions here. + * We could simply ignore those exceptions. + */ + Logging.LogUsefulException(ex); return false; } }