• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /system/bin/sh
2# logpersist cat, start and stop handlers
3progname="${0##*/}"
4case `getprop ro.debuggable` in
51) ;;
6*) echo "${progname} - Permission denied"
7   exit 1
8   ;;
9esac
10
11property=persist.logd.logpersistd
12
13case `getprop ${property#persist.}.enable` in
14true) ;;
15*) echo "${progname} - Disabled"
16   exit 1
17   ;;
18esac
19
20log_uid=logd
21log_tag_property=persist.log.tag
22data=/data/misc/logd/logcat
23service=logcatd
24size_default=256
25buffer_default=all
26args="${@}"
27
28size=${size_default}
29buffer=${buffer_default}
30clear=false
31while [ ${#} -gt 0 ]; do
32  case ${1} in
33    -c|--clear) clear=true ;;
34    --size=*) size="${1#--size=}" ;;
35    --rotate-count=*) size="${1#--rotate-count=}" ;;
36    -n|--size|--rotate-count) size="${2}" ; shift ;;
37    --buffer=*) buffer="${1#--buffer=}" ;;
38    -b|--buffer) buffer="${2}" ; shift ;;
39    -h|--help|*)
40      LEAD_SPACE_="`echo ${progname%.*} | tr '[ -~]' ' '`"
41      echo "${progname%.*}.cat             - dump current ${service} logs"
42      echo "${progname%.*}.start [--size=<size_in_kb>] [--buffer=<buffers>] [--clear]"
43      echo "${LEAD_SPACE_}                 - start ${service} service"
44      echo "${progname%.*}.stop [--clear]  - stop ${service} service"
45      case ${1} in
46        -h|--help) exit 0 ;;
47        *) echo ERROR: bad argument ${@} >&2 ; exit 1 ;;
48      esac
49      ;;
50  esac
51  shift
52done
53
54if [ -z "${size}" -o "${size_default}" = "${size}" ]; then
55  unset size
56fi
57if [ -n "${size}" ] &&
58  ! ( [ 0 -lt "${size}" ] && [ 2048 -ge "${size}" ] ) >/dev/null 2>&1; then
59  echo ERROR: Invalid --size ${size} >&2
60  exit 1
61fi
62if [ -z "${buffer}" -o "${buffer_default}" = "${buffer}" ]; then
63  unset buffer
64fi
65if [ -n "${buffer}" ] && ! logcat -b ${buffer} -g >/dev/null 2>&1; then
66  echo ERROR: Invalid --buffer ${buffer} >&2
67  exit 1
68fi
69
70log_tag="`getprop ${log_tag_property}`"
71logd_logpersistd="`getprop ${property}`"
72
73case ${progname} in
74*.cat)
75  if [ -n "${size}${buffer}" -o "true" = "${clear}" ]; then
76    echo WARNING: Can not use --clear, --size or --buffer with ${progname%.*}.cat >&2
77  fi
78  su ${log_uid} ls "${data%/*}" |
79  tr -d '\r' |
80  sort -ru |
81  sed "s#^#${data%/*}/#" |
82  grep "${data}[.]*[0-9]*\$" |
83  su ${log_uid} xargs cat
84  ;;
85*.start)
86  current_buffer="`getprop ${property#persist.}.buffer`"
87  current_size="`getprop ${property#persist.}.size`"
88  if [ "${service}" = "`getprop ${property#persist.}`" ]; then
89    if [ "true" = "${clear}" ]; then
90      setprop ${property#persist.} "clear"
91    elif [ "${buffer}|${size}" != "${current_buffer}|${current_size}" ]; then
92      echo   "ERROR: Changing existing collection parameters from" >&2
93      if [ "${buffer}" != "${current_buffer}" ]; then
94        a=${current_buffer}
95        b=${buffer}
96        if [ -z "${a}" ]; then a="${default_buffer}"; fi
97        if [ -z "${b}" ]; then b="${default_buffer}"; fi
98        echo "           --buffer ${a} to ${b}" >&2
99      fi
100      if [ "${size}" != "${current_size}" ]; then
101        a=${current_size}
102        b=${size}
103        if [ -z "${a}" ]; then a="${default_size}"; fi
104        if [ -z "${b}" ]; then b="${default_size}"; fi
105        echo "           --size ${a} to ${b}" >&2
106      fi
107      echo   "       Are you sure you want to do this?" >&2
108      echo   "       Suggest add --clear to erase data and restart with new settings." >&2
109      echo   "       To blindly override and retain data, ${progname%.*}.stop first." >&2
110      exit 1
111    fi
112  elif [ "true" = "${clear}" ]; then
113    setprop ${property#persist.} "clear"
114  fi
115  if [ -n "${buffer}${current_buffer}" ]; then
116    setprop ${property}.buffer "${buffer}"
117    if [ -z "${buffer}" ]; then
118      # deal with trampoline for empty properties
119      setprop ${property#persist.}.buffer ""
120    fi
121  fi
122  if [ -n "${size}${current_size}" ]; then
123    setprop ${property}.size "${size}"
124    if [ -z "${size}" ]; then
125      # deal with trampoline for empty properties
126      setprop ${property#persist.}.size ""
127    fi
128  fi
129  while [ "clear" = "`getprop ${property#persist.}`" ]; do
130    continue
131  done
132  # Tell Settings that we are back on again if we turned logging off
133  tag="${log_tag#Settings}"
134  if [ X"${log_tag}" != X"${tag}" ]; then
135    echo "WARNING: enabling logd service" >&2
136    setprop ${log_tag_property} "${tag#,}"
137  fi
138  # ${service}.rc does the heavy lifting with the following trigger
139  setprop ${property} ${service}
140  # 20ms done, to permit process feedback check
141  sleep 1
142  getprop ${property#persist.}
143  # also generate an error return code if not found running
144  pgrep -u ${log_uid} ${service%d}
145  ;;
146*.stop)
147  if [ -n "${size}${buffer}" ]; then
148    echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2
149  fi
150  if [ "true" = "${clear}" ]; then
151    setprop ${property#persist.} "clear"
152  else
153    setprop ${property#persist.} "stop"
154  fi
155  if [ -n "`getprop ${property#persist.}.buffer`" ]; then
156    setprop ${property}.buffer ""
157    # deal with trampoline for empty properties
158    setprop ${property#persist.}.buffer ""
159  fi
160  if [ -n "`getprop ${property#persist.}.size`" ]; then
161    setprop ${property}.size ""
162    # deal with trampoline for empty properties
163    setprop ${property#persist.}.size ""
164  fi
165  while [ "clear" = "`getprop ${property#persist.}`" ]; do
166    continue
167  done
168  ;;
169*)
170  echo "ERROR: Unexpected command ${0##*/} ${args}" >&2
171  exit 1
172esac
173
174if [ X"${log_tag}" != X"`getprop ${log_tag_property}`" ] ||
175   [ X"${logd_logpersistd}" != X"`getprop ${property}`" ]; then
176  if command -v am >/dev/null; then
177    echo "WARNING: killing Settings application to pull in new values" >&2
178    am force-stop com.android.settings
179  else
180    echo "WARNING: could not kill Settings application to pull in new values: am command not found" >&2
181  fi
182fi
183