Esempio n. 1
0
        /// <summary>
        /// Get the Unix Process by searching the /proc files. The /proc directory is a virtual file system,
        /// sometimes reffered to as the process information pseudo file system.It doesn't contain 'real' files but runtime system information.
        /// Each of the numbered directories corresponds to an actual process ID. Details of this process can
        /// be obtained by looking at the associated files in the directory for this process, for example /proc/460. Inside this folder we can find more directories:
        /// - /proc/PID/cmdline for command line arguments that were used to lanch the process
        /// - /proc/PID/stat for process status
        /// </summary>
        /// <returns></returns>
        public static LinuxProcess[] GetProcesses()
        {
            const string PROC_DIR = "/proc";

            IList <LinuxProcess> processList = new List <LinuxProcess>();

            foreach (var directory in Directory.GetDirectories(PROC_DIR))
            {
                var directoryName = Path.GetFileName(directory);

                //We want to search only through the files that are associated with a PID, that have
                //filenames represented as numbers. So, if we can not convert it to an int, then we skip the
                //directory
                int  pid;
                bool isPidDirectory = int.TryParse(directoryName, out pid);
                if (!isPidDirectory)
                {
                    continue;
                }

                //Parse the process status file in order to get the filename of that lanched the process. If no stat
                //file is present, than we skip and continue with the next directory
                LinuxProcessStatusFile processStatusFile = ParseLinuxProcessStatusFile(pid);
                if (processStatusFile == null)
                {
                    continue;
                }

                //Build Linux process and add it to our result set
                LinuxProcess linuxProcess = BuildProcess(processStatusFile);
                processList.Add(linuxProcess);
            }

            return(processList.ToArray());
        }
Esempio n. 2
0
        private static string GetProcessName(LinuxProcessStatusFile processStatusFile)
        {
            //We verify if the process is a mono app. They are present in the stat file if the (mono) text is there. If
            //this is the case, then we gho deeper and anlyze the cmdline file in order to get the arguments to the mono command.
            //Those will tell us what was the .exe applicaiton that was executed.
            if (processStatusFile.FileName == "(mono)")
            {
                // We can find the command line text, including arguments for each process. We find it in /proc/{pid}/cmdline. It is here where we
                // can seacrh for the name of the .exe file that was executed under Mono, as it is an argument for the mono command used to execute
                // our .NET code under Mono
                IList <string> cmdLineArgs;
                const string   CMDLINE_PATTERN = "/proc/{0}/cmdline";
                using (var fileReader = File.OpenText(string.Format(CMDLINE_PATTERN, processStatusFile.Pid)))
                {
                    string contents = fileReader.ReadToEnd();
                    cmdLineArgs = contents.Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                }

                //the first element is the mono command
                //the second element is the program we executed (.exe)
                return(Path.GetFileName(cmdLineArgs.ElementAt(1)));
            }
            //normal process, we just trim the paranthesis
            return(processStatusFile.FileName.Trim('(', ')'));
        }
Esempio n. 3
0
        private static LinuxProcess BuildProcess(LinuxProcessStatusFile processStatusFile)
        {
            string       processName  = GetProcessName(processStatusFile);
            LinuxProcess linuxProcess = new LinuxProcess(processStatusFile.Pid, processName);

            return(linuxProcess);
        }
Esempio n. 4
0
        private static string GetProcessName(LinuxProcessStatusFile processStatusFile)
        {
            //We verify if the process is a mono app. They are present in the stat file if the (mono) text is there. If
            //this is the case, then we gho deeper and anlyze the cmdline file in order to get the arguments to the mono command.
            //Those will tell us what was the .exe applicaiton that was executed.
            if (processStatusFile.FileName == "(mono)")
            {
                // We can find the command line text, including arguments for each process. We find it in /proc/{pid}/cmdline. It is here where we
                // can seacrh for the name of the .exe file that was executed under Mono, as it is an argument for the mono command used to execute
                // our .NET code under Mono
                IList<string> cmdLineArgs;
                const string CMDLINE_PATTERN = "/proc/{0}/cmdline";
                using (var fileReader = File.OpenText(string.Format(CMDLINE_PATTERN, processStatusFile.Pid)))
                {
                    string contents = fileReader.ReadToEnd();
                    cmdLineArgs = contents.Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                }

                //the first element is the mono command
                //the second element is the program we executed (.exe)
                return Path.GetFileName(cmdLineArgs.ElementAt(1));
            }
            //normal process, we just trim the paranthesis
            return processStatusFile.FileName.Trim('(', ')');
        }
Esempio n. 5
0
 private static LinuxProcess BuildProcess(LinuxProcessStatusFile processStatusFile)
 {
     string processName = GetProcessName(processStatusFile);
     LinuxProcess linuxProcess = new LinuxProcess(processStatusFile.Pid, processName);
     return linuxProcess;
 }