• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /system/bin/sh
2# logpersist cat, start and stop handlers
3progname="${0##*/}"
4case `getprop ro.build.type` in
5userdebug|eng) ;;
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
20data=/data/misc/logd
21service=logcatd
22size_default=256
23buffer_default=all
24args="${@}"
25
26size=${size_default}
27buffer=${buffer_default}
28clear=false
29while [ ${#} -gt 0 ]; do
30  case ${1} in
31    -c|--clear) clear=true ;;
32    --size=*) size="${1#--size=}" ;;
33    --rotate-count=*) size="${1#--rotate-count=}" ;;
34    -n|--size|--rotate-count) size="${2}" ; shift ;;
35    --buffer=*) buffer="${1#--buffer=}" ;;
36    -b|--buffer) buffer="${2}" ; shift ;;
37    -h|--help|*)
38      LEAD_SPACE_="`echo ${progname%.*} | tr '[ -~]' ' '`"
39      echo "${progname%.*}.cat             - dump current ${service%d} logs"
40      echo "${progname%.*}.start [--size=<size_in_kb>] [--buffer=<buffers>] [--clear]"
41      echo "${LEAD_SPACE_}                 - start ${service} service"
42      echo "${progname%.*}.stop [--clear]  - stop ${service} service"
43      case ${1} in
44        -h|--help) exit 0 ;;
45        *) echo ERROR: bad argument ${@} >&2 ; exit 1 ;;
46      esac
47      ;;
48  esac
49  shift
50done
51
52if [ -z "${size}" -o "${size_default}" = "${size}" ]; then
53  unset size
54fi
55if [ -n "${size}" ] &&
56  ! ( [ 0 -lt "${size}" ] && [ 2048 -ge "${size}" ] ) >/dev/null 2>&1; then
57  echo ERROR: Invalid --size ${size} >&2
58  exit 1
59fi
60if [ -z "${buffer}" -o "${buffer_default}" = "${buffer}" ]; then
61  unset buffer
62fi
63if [ -n "${buffer}" ] && ! logcat -b ${buffer} -g >/dev/null 2>&1; then
64  echo ERROR: Invalid --buffer ${buffer} >&2
65  exit 1
66fi
67
68case ${progname} in
69*.cat)
70  if [ -n "${size}${buffer}" -o "true" = "${clear}" ]; then
71    echo WARNING: Can not use --clear, --size or --buffer with ${progname%.*}.cat >&2
72  fi
73  su logd ls "${data}" |
74  tr -d '\r' |
75  sort -ru |
76  sed "s#^#${data}/#" |
77  su logd xargs cat
78  ;;
79*.start)
80  current_buffer="`getprop ${property#persist.}.buffer`"
81  current_size="`getprop ${property#persist.}.size`"
82  if [ "${service}" = "`getprop ${property#persist.}`" ]; then
83    if [ "true" = "${clear}" ]; then
84      setprop ${property#persist.} "clear"
85    elif [ "${buffer}|${size}" != "${current_buffer}|${current_size}" ]; then
86      echo   "ERROR: Changing existing collection parameters from" >&2
87      if [ "${buffer}" != "${current_buffer}" ]; then
88        a=${current_buffer}
89        b=${buffer}
90        if [ -z "${a}" ]; then a="${default_buffer}"; fi
91        if [ -z "${b}" ]; then b="${default_buffer}"; fi
92        echo "           --buffer ${a} to ${b}" >&2
93      fi
94      if [ "${size}" != "${current_size}" ]; then
95        a=${current_size}
96        b=${size}
97        if [ -z "${a}" ]; then a="${default_size}"; fi
98        if [ -z "${b}" ]; then b="${default_size}"; fi
99        echo "           --size ${a} to ${b}" >&2
100      fi
101      echo   "       Are you sure you want to do this?" >&2
102      echo   "       Suggest add --clear to erase data and restart with new settings." >&2
103      echo   "       To blindly override and retain data, ${progname%.*}.stop first." >&2
104      exit 1
105    fi
106  elif [ "true" = "${clear}" ]; then
107    setprop ${property#persist.} "clear"
108  fi
109  if [ -n "${buffer}${current_buffer}" ]; then
110    setprop ${property}.buffer "${buffer}"
111    if [ -z "${buffer}" ]; then
112      # deal with trampoline for empty properties
113      setprop ${property#persist.}.buffer ""
114    fi
115  fi
116  if [ -n "${size}${current_size}" ]; then
117    setprop ${property}.size "${size}"
118    if [ -z "${size}" ]; then
119      # deal with trampoline for empty properties
120      setprop ${property#persist.}.size ""
121    fi
122  fi
123  while [ "clear" = "`getprop ${property#persist.}`" ]; do
124    continue
125  done
126  # ${service}.rc does the heavy lifting with the following trigger
127  setprop ${property} ${service}
128  # 20ms done, to permit process feedback check
129  sleep 1
130  getprop ${property#persist.}
131  # also generate an error return code if not found running, bonus
132  ps -t | grep "${data##*/}.*${service%d}"
133  ;;
134*.stop)
135  if [ -n "${size}${buffer}" ]; then
136    echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2
137  fi
138  if [ "true" = "${clear}" ]; then
139    setprop ${property} "clear"
140  else
141    setprop ${property} "stop"
142  fi
143  if [ -n "`getprop ${property#persist.}.buffer`" ]; then
144    setprop ${property}.buffer ""
145    # deal with trampoline for empty properties
146    setprop ${property#persist.}.buffer ""
147  fi
148  if [ -n "`getprop ${property#persist.}.size`" ]; then
149    setprop ${property}.size ""
150    # deal with trampoline for empty properties
151    setprop ${property#persist.}.size ""
152  fi
153  while [ "clear" = "`getprop ${property#persist.}`" ]; do
154    continue
155  done
156  ;;
157*)
158  echo "ERROR: Unexpected command ${0##*/} ${args}" >&2
159  exit 1
160esac
161