Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87eae3fd318228ae752b8b106c5194e11cc44dee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.eclipse.cdt.internal.core.qnx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.eclipse.cdt.core.IProcessInfo;
import org.eclipse.cdt.core.IProcessList;
import org.eclipse.cdt.utils.spawner.ProcessFactory;

/**
 * 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()  {
		Process pidin;
		BufferedReader pidinOutput;
		String[] args = {"pidin", "-fan" };

		try {
			pidin = ProcessFactory.getFactory().exec(args);
			pidinOutput = new BufferedReader(new InputStreamReader(pidin.getInputStream()));
		} catch(Exception e) {
			return getProcessListPureJava();
		}
		
		//Read the output and parse it into an array list
		ArrayList procInfo = new ArrayList();

		String pidStr, nameStr, lastline;
		try {
			while((lastline = pidinOutput.readLine()) != null) {
				//The format of the output should be 
				//PID a/slash/delimited/name
		
				StringTokenizer tok = new StringTokenizer(lastline.trim());
				pidStr = tok.nextToken();
				if(pidStr == null || pidStr.charAt(0) < '0' || pidStr.charAt(0) > '9') {
					continue;
				}

				nameStr = tok.nextToken();

				int index = nameStr.lastIndexOf('/');			
				if(index != -1) {
					nameStr = nameStr.substring(index + 1);
				}

				procInfo.add(new ProcessInfo(pidStr, nameStr));
			}
		
			pidin.destroy();
		} catch(Exception e) {
			/* Ignore */
		} finally {
			pidin.destroy();
		}
		
		return (IProcessInfo [])procInfo.toArray(new IProcessInfo[procInfo.size()]);
	}

	/**
	 * This is our current backup strategy for getting the pid list 
	 * (reading /proc directly).  Currently the exename is not implemented
	 * so the names will all show up as unknown, but at least you get a
	 * pid list.
	 */
	private IProcessInfo [] getProcessListPureJava() {
		File proc = new File("/proc");
		File[] pidFiles = 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 {
						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], "exename");
				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;		
	}
}

Back to the top