Home | History | Annotate | Download | only in awk
      1 # Copyright (C) 2010 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 #
     15 
     16 # Extract the pid of a given package name. This assumes that the
     17 # input is the product of 'adb shell ps' with all \r\n line endings
     18 # converted to \n, and that the PACKAGE variable has been initialized
     19 # to the package's name. In other words, this should be used as:
     20 #
     21 #   adb shell ps | awk -f <this-script> -v PACKAGE=<name>
     22 #
     23 # The printed value will be 0 if the package is not found.
     24 #
     25 
     26 BEGIN {
     27     FS=" "
     28 
     29     # A default package name, used _only_ for unit-testing
     30     # com.google.android.apps.maps is interesting because
     31     # in our unit test input files, 'ps' lists several sub-processes
     32     # that implement services (e.g. com.google.android.apps.maps:<something>)
     33     # and we explicitely don't want to match them.
     34     #
     35     if (PACKAGE == "") {
     36         PACKAGE="com.google.android.apps.maps"
     37     }
     38 
     39     PID=0
     40 
     41     # The default column where we expect the PID to appear, this
     42     # matches the default Android toolbox 'ps', but some devices seem
     43     # to have a different version installed (e.g. Busybox) that place
     44     # it somewhere else. We will probe the output to detect this, but
     45     # this is a good fallback value.
     46     PID_COLUMN=2
     47 }
     48 
     49 {
     50     # First, remove any trailing \r from the input line. This is important
     51     # because the output of "adb shell <cmd>" seems to use \r\n line ending.
     52     gsub("\r","",$NF)
     53 
     54     if (NR == 1) {
     55         # The first line of the 'ps' output should list the columns, so we're going
     56         # to parse it to try to update PID_COLUMN
     57         for (n = 1; n <= NF; n++) {
     58             if ($n == "PID") {
     59                 PID_COLUMN=n;
     60             }
     61         }
     62     } else {
     63         # Not the first line, compare the package name, which shall always
     64         # be the last field.
     65     if ($NF == PACKAGE) {
     66         PID=$PID_COLUMN
     67         }
     68     }
     69 }
     70 
     71 END {
     72     print PID
     73 }
     74