Esempio n. 1
0
        public static int Main(string[] args)
        {
            ShowHeader();

            using (var procgov = new ProcessGovernor()) {
                List <string>     procargs = null;
                bool              showhelp = false, nogui = false, debug = false;
                int               pid               = 0;
                RegistryOperation registryOperation = RegistryOperation.NONE;

                var p = new OptionSet()
                {
                    { "m|maxmem=", "Max committed memory usage in bytes (accepted suffixes: K, M or G).",
                      v => { procgov.MaxProcessMemory = ParseMemoryString(v); } },
                    { "env=", "A text file with environment variables (each line in form: VAR=VAL). Applies only to newly created processes.",
                      v => LoadCustomEnvironmentVariables(procgov, v) },
                    { "c|cpu=", "If in hex (starts with 0x) it is treated as an affinity mask, otherwise it is a number of CPU cores assigned to your app.",
                      v => {
                          if (v.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                          {
                              procgov.CpuAffinityMask = long.Parse(v.Substring(2), NumberStyles.HexNumber);
                          }
                          else
                          {
                              procgov.CpuAffinityMask = CalculateAffinityMaskFromCpuCount(int.Parse(v));
                          }
                      } },
                    { "r|recursive", "Apply limits to child processes too (will wait for all processes to finish).", v => { procgov.PropagateOnChildProcesses = v != null; } },
                    { "newconsole", "Start the process in a new console window.", v => { procgov.SpawnNewConsoleWindow = v != null; } },
                    { "nogui", "Hide Process Governor console window (set always when installed as debugger).",
                      v => { nogui = v != null; } },
                    { "p|pid=", "Attach to an already running process", (int v) => pid = v },
                    { "install", "Install procgov as a debugger for a specific process using Image File Executions. " +
                      "DO NOT USE this option if the process you want to control starts child instances of itself (for example, Chrome).",
                      v => { registryOperation = RegistryOperation.INSTALL; } },
                    { "t|timeout=", "Kill the process (with -r, also all its children) if it does not finish within the specified time. " +
                      "Add suffix to define the time unit. Valid suffixes are: ms, s, m, h.",
                      (string v) => procgov.ClockTimeLimitInMilliseconds = ParseTimeStringToMilliseconds(v) },
                    { "process-utime=", "Kill the process (with -r, also applies to its children) if it exceeds the given " +
                      "user-mode execution time. Add suffix to define the time unit. Valid suffixes are: ms, s, m, h.",
                      (string v) => procgov.ProcessUserTimeLimitInMilliseconds = ParseTimeStringToMilliseconds(v) },
                    { "job-utime=", "Kill the process (with -r, also all its children) if the total user-mode execution " +
                      "time exceed the specified value. Add suffix to define the time unit. Valid suffixes are: ms, s, m, h.",
                      (string v) => procgov.JobUserTimeLimitInMilliseconds = ParseTimeStringToMilliseconds(v) },
                    { "uninstall", "Uninstall procgov for a specific process.", v => { registryOperation = RegistryOperation.UNINSTALL; } },
                    { "debugger", "Internal - do not use.",
                      v => debug = v != null },
                    { "v|verbose", "Show verbose messages in the console.", v => procgov.ShowTraceMessages = v != null },
                    { "h|help", "Show this message and exit", v => showhelp = v != null },
                    { "?", "Show this message and exit", v => showhelp = v != null }
                };

                try {
                    procargs = p.Parse(args);
                } catch (OptionException ex) {
                    Console.Error.Write("ERROR: invalid argument");
                    Console.Error.WriteLine(ex.Message);
                    Console.WriteLine();
                    showhelp = true;
                } catch (FormatException) {
                    Console.Error.WriteLine("ERROR: invalid number in one of the constraints");
                    Console.WriteLine();
                    showhelp = true;
                } catch (ArgumentException ex) {
                    Console.Error.WriteLine("ERROR: {0}", ex.Message);
                    Console.WriteLine();
                    showhelp = true;
                }

                if (!showhelp && registryOperation != RegistryOperation.NONE)
                {
                    if (procargs.Count == 0)
                    {
                        Console.Error.WriteLine("ERROR: please provide an image name for a process you would like to intercept.");
                        return(1);
                    }
                    SetupRegistryForProcessGovernor(procgov, procargs[0], registryOperation);
                    return(0);
                }

                if (!showhelp && (procargs.Count == 0 && pid == 0) || (pid > 0 && procargs.Count > 0))
                {
                    Console.Error.WriteLine("ERROR: please provide either process name or PID of the already running process");
                    Console.WriteLine();
                    showhelp = true;
                }

                if (showhelp)
                {
                    ShowHelp(p);
                    return(0);
                }

                if (nogui)
                {
                    WinWindows.NativeMethods.ShowWindow(WinWindows.NativeMethods.GetConsoleWindow(),
                                                        WinWindows.NativeMethods.SW_HIDE);
                }

                try {
                    ShowLimits(procgov);

                    if (debug)
                    {
                        return(procgov.StartProcessUnderDebuggerAndDetach(procargs));
                    }
                    if (pid > 0)
                    {
                        return(procgov.AttachToProcess(pid));
                    }
                    return(procgov.StartProcess(procargs));
                } catch (Win32Exception ex) {
                    Console.Error.WriteLine("ERROR: {0} (0x{1:X})", ex.Message, ex.ErrorCode);
                    return(1);
                } catch (Exception ex) {
                    Console.Error.WriteLine("ERROR: {0}", ex.Message);
                    return(1);
                }
            }
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            using (var procgov = new ProcessGovernor()) {
                List <string>     procargs = null;
                bool              showhelp = false, nogui = false, debug = false;
                int               pid               = 0;
                RegistryOperation registryOperation = RegistryOperation.NONE;

                var p = new OptionSet()
                {
                    { "m|maxmem=", "Max committed memory usage in bytes (accepted suffixes: K, M or G).",
                      v => { procgov.MaxProcessMemory = ParseMemoryString(v); } },
                    { "env=", "A text file with environment variables (each line in form: VAR=VAL). Applies only to newly created processes.",
                      v => LoadCustomEnvironmentVariables(procgov, v) },
                    { "c|cpu=", "If in hex (starts with 0x) it is treated as an affinity mask, otherwise it is a number of CPU cores assigned to your app.",
                      v => {
                          if (v.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                          {
                              procgov.CpuAffinityMask = long.Parse(v.Substring(2), NumberStyles.HexNumber);
                          }
                          else
                          {
                              procgov.CpuAffinityMask = CalculateAffinityMaskFromCpuCount(int.Parse(v));
                          }
                      } },
                    { "newconsole", "Start the process in a new console window.", v => { procgov.SpawnNewConsoleWindow = v != null; } },
                    { "nogui", "Hide Process Governor console window (set always when installed as debugger).",
                      v => { nogui = v != null; } },
                    { "p|pid=", "Attach to an already running process", (int v) => pid = v },
                    { "install", "Installs procgov as a debugger for a specific process using Image File Executions.",
                      v => { registryOperation = RegistryOperation.INSTALL; } },
                    { "uninstall", "Uninstalls procgov for a specific process.",
                      v => { registryOperation = RegistryOperation.UNINSTALL; } },
                    { "debugger", "Internal - do not use.",
                      v => { debug = v != null; } },
                    { "h|help", "Show this message and exit", v => showhelp = v != null },
                    { "?", "Show this message and exit", v => showhelp = v != null }
                };

                try {
                    procargs = p.Parse(args);
                } catch (OptionException ex) {
                    Console.Write("ERROR: invalid argument");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine();
                    showhelp = true;
                } catch (FormatException) {
                    Console.WriteLine("ERROR: invalid number in one of the constraints");
                    Console.WriteLine();
                    showhelp = true;
                } catch (ArgumentException ex) {
                    Console.WriteLine("ERROR: {0}", ex.Message);
                    Console.WriteLine();
                    showhelp = true;
                }

                if (!showhelp && registryOperation != RegistryOperation.NONE)
                {
                    if (procargs.Count == 0)
                    {
                        Console.WriteLine("ERROR: please provide an image name for a process you would like to intercept.");
                        return;
                    }
                    SetupRegistryForProcessGovernor(procgov, procargs[0], registryOperation);
                    return;
                }

                if (!showhelp && (procargs.Count == 0 && pid == 0) || (pid > 0 && procargs.Count > 0))
                {
                    Console.WriteLine("ERROR: please provide either process name or PID of the already running process");
                    Console.WriteLine();
                    showhelp = true;
                }

                if (showhelp)
                {
                    ShowHelp(p);
                    return;
                }

                if (nogui)
                {
                    WinWindows.NativeMethods.ShowWindow(WinWindows.NativeMethods.GetConsoleWindow(),
                                                        WinWindows.NativeMethods.SW_HIDE);
                }

                try {
                    if (debug)
                    {
                        procgov.StartProcessUnderDebuggerAndDetach(procargs);
                    }
                    else if (pid > 0)
                    {
                        procgov.AttachToProcess(pid);
                    }
                    else
                    {
                        procgov.StartProcess(procargs);
                    }
                } catch (Win32Exception ex) {
                    Console.WriteLine("ERROR: {0} (0x{1:X})", ex.Message, ex.ErrorCode);
                } catch (Exception ex) {
                    Console.WriteLine("ERROR: {0}", ex.Message);
                }
            }
        }