// MaverickReaper.exe — encerra o nvstreamer em loop e registra TUDO em log. // Standalone: nao depende de nada. Compile com csc.exe. // csc /nologo /optimize+ /out:MaverickReaper.exe MaverickReaper.cs using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Threading; internal static class MaverickReaper { const uint PROCESS_TERMINATE = 0x0001; const uint TOKEN_ADJUST_PRIVILEGES = 0x0020; const uint TOKEN_QUERY = 0x0008; const uint SE_PRIVILEGE_ENABLED = 0x0002; [StructLayout(LayoutKind.Sequential)] struct LUID { public uint LowPart; public int HighPart; } [StructLayout(LayoutKind.Sequential)] struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; } [DllImport("advapi32.dll", SetLastError = true)] static extern bool OpenProcessToken(IntPtr process, uint access, out IntPtr token); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] static extern bool LookupPrivilegeValue(string system, string name, out LUID luid); [DllImport("advapi32.dll", SetLastError = true)] static extern bool AdjustTokenPrivileges(IntPtr token, bool disableAll, ref TOKEN_PRIVILEGES newState, uint bufferLength, IntPtr previousState, IntPtr returnLength); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr OpenProcess(uint access, bool inherit, int pid); [DllImport("kernel32.dll", SetLastError = true)] static extern bool TerminateProcess(IntPtr handle, uint exitCode); [DllImport("kernel32.dll", SetLastError = true)] static extern bool CloseHandle(IntPtr handle); static readonly string LogPath = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "MaverickReaper.log"); static void Log(string message) { string line = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + message; try { File.AppendAllText(LogPath, line + Environment.NewLine); } catch { } Console.WriteLine(line); } static bool EnablePrivilege(string name) { IntPtr token; if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out token)) { Log("OpenProcessToken falhou err=" + Marshal.GetLastWin32Error()); return false; } try { LUID luid; if (!LookupPrivilegeValue(null, name, out luid)) { Log("LookupPrivilegeValue falhou para " + name); return false; } var tp = new TOKEN_PRIVILEGES { PrivilegeCount = 1, Luid = luid, Attributes = SE_PRIVILEGE_ENABLED }; bool ok = AdjustTokenPrivileges(token, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); int err = Marshal.GetLastWin32Error(); Log(string.Format("privilege {0}: ok={1} err={2}", name, ok, err)); return ok && err == 0; } finally { CloseHandle(token); } } static int KillAll(string name) { int killed = 0; Process[] processes; try { processes = Process.GetProcessesByName(name); } catch (Exception ex) { Log("enumerar " + name + " falhou: " + ex.Message); return 0; } foreach (Process p in processes) { IntPtr handle = OpenProcess(PROCESS_TERMINATE, false, p.Id); if (handle == IntPtr.Zero) { Log(string.Format("{0}:{1} OpenProcess NEGADO err={2}", name, p.Id, Marshal.GetLastWin32Error())); continue; } try { if (TerminateProcess(handle, 0)) { killed++; Log(string.Format("{0}:{1} TERMINADO", name, p.Id)); } else { Log(string.Format("{0}:{1} TerminateProcess falhou err={2}", name, p.Id, Marshal.GetLastWin32Error())); } } finally { CloseHandle(handle); p.Dispose(); } } return killed; } static void DumpState(string label) { string[] watch = { "nvstreamer", "gs2-agent", "nvcontainer", "nvmon", "SystemRuntime", "Maverick" }; var parts = new List(); foreach (string name in watch) { int n = 0; try { n = Process.GetProcessesByName(name).Length; } catch { } parts.Add(name + "=" + n); } Log(label + " " + string.Join(" ", parts.ToArray())); } static int Main(string[] args) { int cycles = 0; int seconds = 600; int interval = 3000; bool once = false; for (int i = 0; i < args.Length; i++) { string a = args[i]; if (a == "--once") { once = true; } else if (a == "--seconds" && i + 1 < args.Length) { int.TryParse(args[++i], out seconds); } else if (a == "--interval" && i + 1 < args.Length) { int.TryParse(args[++i], out interval); } } Log("=== MaverickReaper inicio (seconds=" + seconds + " interval=" + interval + " once=" + once + ")"); Log("usuario: " + Environment.UserName + " sessao: " + Process.GetCurrentProcess().SessionId); EnablePrivilege("SeDebugPrivilege"); DumpState("estado inicial:"); DateTime deadline = DateTime.UtcNow.AddSeconds(seconds); do { int killed = KillAll("nvstreamer"); if (killed > 0) { cycles++; Log("ciclo " + cycles + ": nvstreamer encerrado x" + killed); } if (once) break; Thread.Sleep(interval); } while (DateTime.UtcNow < deadline); DumpState("estado final:"); Log("=== fim: " + cycles + " ciclos com kill"); return cycles; } }