Ejemplo n.º 1
0
        private async Task <List <ProcessListEntry> > GetByProcessAsync(IProcess process)
        {
            var  results = new SortedDictionary <uint, ProcessListEntry>();
            uint psPid   = 0;

            TextReceivedEventHandler outHandler =
                delegate(object sender, TextReceivedEventArgs data)
            {
                string line = data.Text;
                if (line == null)
                {
                    return;
                }

                string[] words = line.Split(
                    default(string[]), StringSplitOptions.RemoveEmptyEntries);
                if (words.Length < 4)
                {
                    return;
                }
                uint pid;
                if (!UInt32.TryParse(words[0], out pid))
                {
                    return;
                }
                uint ppid;
                if (!UInt32.TryParse(words[1], out ppid))
                {
                    return;
                }
                // Grab the pid of the "ps" command so we can filter out parent processes.
                if (words[2] == "ps")
                {
                    psPid = pid;
                }
                results.Add(pid, new ProcessListEntry
                {
                    Pid     = pid,
                    Ppid    = ppid,
                    Title   = words[2],
                    Command = String.Join(" ", words.Skip(3)),
                });
            };

            process.OutputDataReceived += outHandler;

            await process.RunToExitWithSuccessAsync();

            // Remove the chain of processes from the "ps" command.
            ProcessListEntry psResult;

            while (results.TryGetValue(psPid, out psResult))
            {
                psPid = psResult.Ppid;
                results.Remove(psResult.Pid);
            }

            return(results.Values.ToList());
        }
Ejemplo n.º 2
0
        private async Task <List <CoreListEntry> > GetCoreListFromProcessStartInfoAsync(
            ProcessStartInfo processStartInfo)
        {
            var results = new List <CoreListEntry>();

            using (var process = managedProcessFactory.Create(processStartInfo))
            {
                TextReceivedEventHandler outHandler =
                    delegate(object sender, TextReceivedEventArgs data)
                {
                    string line = data.Text;
                    if (line == null)
                    {
                        return;
                    }

                    // Split the line into a maximum of 7 columns. This preserves spaces in the
                    // 7th column, which contains the filename.
                    // Sample output line:
                    // "-rw-r--r-- 1 cloudcast cloudcast 421088 1568730925 hello world.core.dmp"
                    string[] words = line.Split(
                        default(string[]), 7, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length < 7)
                    {
                        return;
                    }
                    // Only show cores owned by 'cloudcast'.
                    if (words[2] != "cloudcast")
                    {
                        return;
                    }
                    results.Add(new CoreListEntry()
                    {
                        Name = words[6],
                        Date =
                            DateTimeOffset.FromUnixTimeSeconds(
                                long.Parse(words[5])).DateTime.ToLocalTime()
                    });
                };
                process.OutputDataReceived += outHandler;
                await process.RunToExitWithSuccessAsync();

                return(results);
            }
        }