1 /* 2 * Copyright 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.security.cts; 18 19 import java.io.BufferedReader; 20 import java.io.File; 21 import java.io.FileReader; 22 import java.io.IOException; 23 24 /** 25 * Utilities for accessing /proc filesystem information. 26 */ 27 public class Proc { findPidFor(String executable)28 public static int findPidFor(String executable) throws IOException { 29 File f = new File("/proc"); 30 for (File d : f.listFiles()) { 31 String cmdLineString = d.getAbsolutePath() + "/cmdline"; 32 File cmdLine = new File(cmdLineString); 33 if (cmdLine.exists()) { 34 BufferedReader in = null; 35 try { 36 in = new BufferedReader(new FileReader(cmdLine)); 37 String line = in.readLine(); 38 if ((line != null) && line.startsWith(executable)) { 39 return Integer.decode(d.getName()); 40 } 41 } finally { 42 if (in != null) { 43 in.close(); 44 } 45 } 46 } 47 } 48 throw new RuntimeException("should never get here"); 49 } 50 } 51