mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 18:05:33 +02:00
Implements a Java version to extract pid from Linux.
This commit is contained in:
parent
7c0bb86e38
commit
4477c66df6
4 changed files with 126 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="/org.eclipse.core.resources"/>
|
||||
<classpathentry kind="src" path="/org.eclipse.ui"/>
|
||||
<classpathentry kind="src" path="/org.eclipse.cdt.core"/>
|
||||
|
@ -11,5 +12,5 @@
|
|||
<classpathentry kind="src" path="/org.eclipse.debug.core"/>
|
||||
<classpathentry kind="src" path="/org.eclipse.core.boot"/>
|
||||
<classpathentry kind="var" path="JRE_LIB" rootpath="JRE_SRCROOT" sourcepath="JRE_SRC"/>
|
||||
<classpathentry kind="output" path=""/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
@ -17,4 +17,10 @@
|
|||
</requires>
|
||||
|
||||
|
||||
<extension
|
||||
point="org.eclipse.cdt.core.ProcessList">
|
||||
<processList
|
||||
class="org.eclipse.cdt.internal.core.linux.ProcessList">
|
||||
</processList>
|
||||
</extension>
|
||||
</fragment>
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package org.eclipse.cdt.internal.core.linux;
|
||||
|
||||
import org.eclipse.cdt.core.IProcessInfo;
|
||||
|
||||
/**
|
||||
* @author alain
|
||||
*
|
||||
* To change this generated comment edit the template variable "typecomment":
|
||||
* Window>Preferences>Java>Templates.
|
||||
* To enable and disable the creation of type comments go to
|
||||
* Window>Preferences>Java>Code Generation.
|
||||
*/
|
||||
public class ProcessInfo implements IProcessInfo {
|
||||
|
||||
int pid;
|
||||
String name;
|
||||
|
||||
public ProcessInfo(String pidString, String name) {
|
||||
try {
|
||||
pid = Integer.parseInt(pidString);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ProcessInfo(int pid, String name) {
|
||||
this.pid = pid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.eclipse.cdt.core.IProcessInfo#getName()
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.eclipse.cdt.core.IProcessInfo#getPid()
|
||||
*/
|
||||
public int getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package org.eclipse.cdt.internal.core.linux;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.cdt.core.IProcessInfo;
|
||||
import org.eclipse.cdt.core.IProcessList;
|
||||
|
||||
/**
|
||||
* Insert the type's description here.
|
||||
* @see IProcessList
|
||||
*/
|
||||
public class ProcessList implements IProcessList {
|
||||
|
||||
ProcessInfo[] empty = new ProcessInfo[0];
|
||||
|
||||
public ProcessList() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the method's description here.
|
||||
* @see IProcessList#getProcessList
|
||||
*/
|
||||
public IProcessInfo [] getProcessList() {
|
||||
File proc = new File("/proc");
|
||||
File[] pidFiles = null;
|
||||
String[] pidNames = null;
|
||||
|
||||
// We are only interrested in the pid so filter the rest out.
|
||||
try {
|
||||
FilenameFilter filter = new FilenameFilter() {
|
||||
public boolean accept(File dir, String name) {
|
||||
boolean isPID = false;
|
||||
try {
|
||||
int pid = Integer.parseInt(name);
|
||||
isPID = true;
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
return isPID;
|
||||
}
|
||||
};
|
||||
pidFiles = proc.listFiles(filter);
|
||||
} catch (SecurityException e) {
|
||||
}
|
||||
|
||||
ProcessInfo[] processInfo = empty;
|
||||
if (pidFiles != null) {
|
||||
processInfo = new ProcessInfo[pidFiles.length];
|
||||
for (int i = 0; i < pidFiles.length; i++) {
|
||||
File cmdLine = new File(pidFiles[i], "cmdline");
|
||||
StringBuffer line = new StringBuffer();
|
||||
try {
|
||||
FileReader reader = new FileReader(cmdLine);
|
||||
int c;
|
||||
while ((c = reader.read()) > 0) {
|
||||
line.append((char)c);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
String name = line.toString();
|
||||
if (name.length() == 0) {
|
||||
name = "Unknown";
|
||||
}
|
||||
processInfo[i] = new ProcessInfo(pidFiles[i].getName(), name);
|
||||
}
|
||||
} else {
|
||||
pidFiles = new File[0];
|
||||
}
|
||||
return processInfo;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue