blob: c09b6b2127f9f0d447af6d07b76838d01814f4ab [file] [log] [blame]
Mark Salyzyn100658c2015-05-26 14:57:13 -07001#! /system/bin/sh
Mark Salyzyn2e210e42016-04-12 14:35:27 -07002# logpersist cat, start and stop handlers
Mark Salyzyn1b9456a2015-09-21 10:45:10 -07003progname="${0##*/}"
Mark Salyzynd1f41d62016-02-09 15:49:29 -08004case `getprop ro.debuggable` in
51) ;;
Mark Salyzyn1b9456a2015-09-21 10:45:10 -07006*) echo "${progname} - Permission denied"
7 exit 1
8 ;;
9esac
Mark Salyzyn2e210e42016-04-12 14:35:27 -070010
Mark Salyzyn100658c2015-05-26 14:57:13 -070011property=persist.logd.logpersistd
Mark Salyzyn2591d482016-07-07 09:24:51 -070012
13case `getprop ${property#persist.}.enable` in
14true) ;;
15*) echo "${progname} - Disabled"
16 exit 1
17 ;;
18esac
19
Mark Salyzyn2e5b7182016-08-03 15:13:51 -070020log_uid=logd
Mark Salyzyn6a7bfd82016-07-21 14:00:30 -070021log_tag_property=persist.log.tag
Mark Salyzyn2e5b7182016-08-03 15:13:51 -070022data=/data/misc/logd/logcat
Mark Salyzyn100658c2015-05-26 14:57:13 -070023service=logcatd
Mark Salyzyn2e210e42016-04-12 14:35:27 -070024size_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 '[ -~]' ' '`"
Mark Salyzyn1d945112016-06-01 15:35:41 -070041 echo "${progname%.*}.cat - dump current ${service} logs"
Mark Salyzyn2e210e42016-04-12 14:35:27 -070042 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
Mark Salyzyn100658c2015-05-26 14:57:13 -070056fi
Mark Salyzyn2e210e42016-04-12 14:35:27 -070057if [ -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
Mark Salyzyn6a7bfd82016-07-21 14:00:30 -070070log_tag="`getprop ${log_tag_property}`"
71logd_logpersistd="`getprop ${property}`"
72
Mark Salyzyn100658c2015-05-26 14:57:13 -070073case ${progname} in
74*.cat)
Mark Salyzyn2e210e42016-04-12 14:35:27 -070075 if [ -n "${size}${buffer}" -o "true" = "${clear}" ]; then
76 echo WARNING: Can not use --clear, --size or --buffer with ${progname%.*}.cat >&2
77 fi
Mark Salyzyn2e5b7182016-08-03 15:13:51 -070078 su ${log_uid} ls "${data%/*}" |
Mark Salyzyn100658c2015-05-26 14:57:13 -070079 tr -d '\r' |
80 sort -ru |
Mark Salyzyn2e5b7182016-08-03 15:13:51 -070081 sed "s#^#${data%/*}/#" |
82 grep "${data}[.]*[0-9]*\$" |
83 su ${log_uid} xargs cat
Mark Salyzyn100658c2015-05-26 14:57:13 -070084 ;;
85*.start)
Mark Salyzyn2591d482016-07-07 09:24:51 -070086 current_buffer="`getprop ${property#persist.}.buffer`"
87 current_size="`getprop ${property#persist.}.size`"
88 if [ "${service}" = "`getprop ${property#persist.}`" ]; then
Mark Salyzyn2e210e42016-04-12 14:35:27 -070089 if [ "true" = "${clear}" ]; then
Mark Salyzyn2591d482016-07-07 09:24:51 -070090 setprop ${property#persist.} "clear"
Mark Salyzyn2e210e42016-04-12 14:35:27 -070091 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
Mark Salyzyncd5118b2016-06-06 11:22:13 -0700112 elif [ "true" = "${clear}" ]; then
Mark Salyzyn2591d482016-07-07 09:24:51 -0700113 setprop ${property#persist.} "clear"
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700114 fi
115 if [ -n "${buffer}${current_buffer}" ]; then
Mark Salyzyn8d2a6552016-06-06 11:22:13 -0700116 setprop ${property}.buffer "${buffer}"
Mark Salyzyn2591d482016-07-07 09:24:51 -0700117 if [ -z "${buffer}" ]; then
118 # deal with trampoline for empty properties
119 setprop ${property#persist.}.buffer ""
120 fi
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700121 fi
122 if [ -n "${size}${current_size}" ]; then
Mark Salyzyn8d2a6552016-06-06 11:22:13 -0700123 setprop ${property}.size "${size}"
Mark Salyzyn2591d482016-07-07 09:24:51 -0700124 if [ -z "${size}" ]; then
125 # deal with trampoline for empty properties
126 setprop ${property#persist.}.size ""
127 fi
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700128 fi
Mark Salyzyn2591d482016-07-07 09:24:51 -0700129 while [ "clear" = "`getprop ${property#persist.}`" ]; do
Mark Salyzyncd5118b2016-06-06 11:22:13 -0700130 continue
131 done
Mark Salyzyn6a7bfd82016-07-21 14:00:30 -0700132 # 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
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700138 # ${service}.rc does the heavy lifting with the following trigger
Mark Salyzyn8d2a6552016-06-06 11:22:13 -0700139 setprop ${property} ${service}
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700140 # 20ms done, to permit process feedback check
Mark Salyzyn100658c2015-05-26 14:57:13 -0700141 sleep 1
Mark Salyzyn2591d482016-07-07 09:24:51 -0700142 getprop ${property#persist.}
Mark Salyzyn1d945112016-06-01 15:35:41 -0700143 # also generate an error return code if not found running
Mark Salyzyn2e5b7182016-08-03 15:13:51 -0700144 pgrep -u ${log_uid} ${service%d}
Mark Salyzyn100658c2015-05-26 14:57:13 -0700145 ;;
146*.stop)
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700147 if [ -n "${size}${buffer}" ]; then
148 echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2
149 fi
Mark Salyzyncd5118b2016-06-06 11:22:13 -0700150 if [ "true" = "${clear}" ]; then
151 setprop ${property} "clear"
152 else
153 setprop ${property} "stop"
154 fi
Mark Salyzyn2591d482016-07-07 09:24:51 -0700155 if [ -n "`getprop ${property#persist.}.buffer`" ]; then
Mark Salyzyn8d2a6552016-06-06 11:22:13 -0700156 setprop ${property}.buffer ""
Mark Salyzyn2591d482016-07-07 09:24:51 -0700157 # deal with trampoline for empty properties
158 setprop ${property#persist.}.buffer ""
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700159 fi
Mark Salyzyn2591d482016-07-07 09:24:51 -0700160 if [ -n "`getprop ${property#persist.}.size`" ]; then
Mark Salyzyn8d2a6552016-06-06 11:22:13 -0700161 setprop ${property}.size ""
Mark Salyzyn2591d482016-07-07 09:24:51 -0700162 # deal with trampoline for empty properties
163 setprop ${property#persist.}.size ""
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700164 fi
Mark Salyzyn2591d482016-07-07 09:24:51 -0700165 while [ "clear" = "`getprop ${property#persist.}`" ]; do
Mark Salyzyncd5118b2016-06-06 11:22:13 -0700166 continue
167 done
Mark Salyzyn100658c2015-05-26 14:57:13 -0700168 ;;
169*)
Mark Salyzyn2e210e42016-04-12 14:35:27 -0700170 echo "ERROR: Unexpected command ${0##*/} ${args}" >&2
Mark Salyzyn100658c2015-05-26 14:57:13 -0700171 exit 1
172esac
Mark Salyzyn6a7bfd82016-07-21 14:00:30 -0700173
174if [ X"${log_tag}" != X"`getprop ${log_tag_property}`" ] ||
175 [ X"${logd_logpersistd}" != X"`getprop ${property}`" ]; then
Mark Salyzyn452eaf72016-08-04 08:39:52 -0700176 echo "WARNING: killing Settings application to pull in new values" >&2
Mark Salyzyn6a7bfd82016-07-21 14:00:30 -0700177 am force-stop com.android.settings
178fi