Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 1 | #! /system/bin/sh |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 2 | # logpersist cat, start and stop handlers |
Mark Salyzyn | 1b9456a | 2015-09-21 10:45:10 -0700 | [diff] [blame] | 3 | progname="${0##*/}" |
Mark Salyzyn | d1f41d6 | 2016-02-09 15:49:29 -0800 | [diff] [blame] | 4 | case `getprop ro.debuggable` in |
| 5 | 1) ;; |
Mark Salyzyn | 1b9456a | 2015-09-21 10:45:10 -0700 | [diff] [blame] | 6 | *) echo "${progname} - Permission denied" |
| 7 | exit 1 |
| 8 | ;; |
| 9 | esac |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 10 | |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 11 | property=persist.logd.logpersistd |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 12 | |
| 13 | case `getprop ${property#persist.}.enable` in |
| 14 | true) ;; |
| 15 | *) echo "${progname} - Disabled" |
| 16 | exit 1 |
| 17 | ;; |
| 18 | esac |
| 19 | |
Mark Salyzyn | 2e5b718 | 2016-08-03 15:13:51 -0700 | [diff] [blame] | 20 | log_uid=logd |
Mark Salyzyn | 6a7bfd8 | 2016-07-21 14:00:30 -0700 | [diff] [blame] | 21 | log_tag_property=persist.log.tag |
Mark Salyzyn | 2e5b718 | 2016-08-03 15:13:51 -0700 | [diff] [blame] | 22 | data=/data/misc/logd/logcat |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 23 | service=logcatd |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 24 | size_default=256 |
| 25 | buffer_default=all |
| 26 | args="${@}" |
| 27 | |
| 28 | size=${size_default} |
| 29 | buffer=${buffer_default} |
| 30 | clear=false |
| 31 | while [ ${#} -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 Salyzyn | 1d94511 | 2016-06-01 15:35:41 -0700 | [diff] [blame] | 41 | echo "${progname%.*}.cat - dump current ${service} logs" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 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 |
| 52 | done |
| 53 | |
| 54 | if [ -z "${size}" -o "${size_default}" = "${size}" ]; then |
| 55 | unset size |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 56 | fi |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 57 | if [ -n "${size}" ] && |
| 58 | ! ( [ 0 -lt "${size}" ] && [ 2048 -ge "${size}" ] ) >/dev/null 2>&1; then |
| 59 | echo ERROR: Invalid --size ${size} >&2 |
| 60 | exit 1 |
| 61 | fi |
| 62 | if [ -z "${buffer}" -o "${buffer_default}" = "${buffer}" ]; then |
| 63 | unset buffer |
| 64 | fi |
| 65 | if [ -n "${buffer}" ] && ! logcat -b ${buffer} -g >/dev/null 2>&1; then |
| 66 | echo ERROR: Invalid --buffer ${buffer} >&2 |
| 67 | exit 1 |
| 68 | fi |
| 69 | |
Mark Salyzyn | 6a7bfd8 | 2016-07-21 14:00:30 -0700 | [diff] [blame] | 70 | log_tag="`getprop ${log_tag_property}`" |
| 71 | logd_logpersistd="`getprop ${property}`" |
| 72 | |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 73 | case ${progname} in |
| 74 | *.cat) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 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 |
Mark Salyzyn | 2e5b718 | 2016-08-03 15:13:51 -0700 | [diff] [blame] | 78 | su ${log_uid} ls "${data%/*}" | |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 79 | tr -d '\r' | |
| 80 | sort -ru | |
Mark Salyzyn | 2e5b718 | 2016-08-03 15:13:51 -0700 | [diff] [blame] | 81 | sed "s#^#${data%/*}/#" | |
| 82 | grep "${data}[.]*[0-9]*\$" | |
| 83 | su ${log_uid} xargs cat |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 84 | ;; |
| 85 | *.start) |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 86 | current_buffer="`getprop ${property#persist.}.buffer`" |
| 87 | current_size="`getprop ${property#persist.}.size`" |
| 88 | if [ "${service}" = "`getprop ${property#persist.}`" ]; then |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 89 | if [ "true" = "${clear}" ]; then |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 90 | setprop ${property#persist.} "clear" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 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 |
Mark Salyzyn | cd5118b | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 112 | elif [ "true" = "${clear}" ]; then |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 113 | setprop ${property#persist.} "clear" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 114 | fi |
| 115 | if [ -n "${buffer}${current_buffer}" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 116 | setprop ${property}.buffer "${buffer}" |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 117 | if [ -z "${buffer}" ]; then |
| 118 | # deal with trampoline for empty properties |
| 119 | setprop ${property#persist.}.buffer "" |
| 120 | fi |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 121 | fi |
| 122 | if [ -n "${size}${current_size}" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 123 | setprop ${property}.size "${size}" |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 124 | if [ -z "${size}" ]; then |
| 125 | # deal with trampoline for empty properties |
| 126 | setprop ${property#persist.}.size "" |
| 127 | fi |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 128 | fi |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 129 | while [ "clear" = "`getprop ${property#persist.}`" ]; do |
Mark Salyzyn | cd5118b | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 130 | continue |
| 131 | done |
Mark Salyzyn | 6a7bfd8 | 2016-07-21 14:00:30 -0700 | [diff] [blame] | 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 |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 138 | # ${service}.rc does the heavy lifting with the following trigger |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 139 | setprop ${property} ${service} |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 140 | # 20ms done, to permit process feedback check |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 141 | sleep 1 |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 142 | getprop ${property#persist.} |
Mark Salyzyn | 1d94511 | 2016-06-01 15:35:41 -0700 | [diff] [blame] | 143 | # also generate an error return code if not found running |
Mark Salyzyn | 2e5b718 | 2016-08-03 15:13:51 -0700 | [diff] [blame] | 144 | pgrep -u ${log_uid} ${service%d} |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 145 | ;; |
| 146 | *.stop) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 147 | if [ -n "${size}${buffer}" ]; then |
| 148 | echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2 |
| 149 | fi |
Mark Salyzyn | cd5118b | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 150 | if [ "true" = "${clear}" ]; then |
| 151 | setprop ${property} "clear" |
| 152 | else |
| 153 | setprop ${property} "stop" |
| 154 | fi |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 155 | if [ -n "`getprop ${property#persist.}.buffer`" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 156 | setprop ${property}.buffer "" |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 157 | # deal with trampoline for empty properties |
| 158 | setprop ${property#persist.}.buffer "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 159 | fi |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 160 | if [ -n "`getprop ${property#persist.}.size`" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 161 | setprop ${property}.size "" |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 162 | # deal with trampoline for empty properties |
| 163 | setprop ${property#persist.}.size "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 164 | fi |
Mark Salyzyn | 2591d48 | 2016-07-07 09:24:51 -0700 | [diff] [blame] | 165 | while [ "clear" = "`getprop ${property#persist.}`" ]; do |
Mark Salyzyn | cd5118b | 2016-06-06 11:22:13 -0700 | [diff] [blame] | 166 | continue |
| 167 | done |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 168 | ;; |
| 169 | *) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 170 | echo "ERROR: Unexpected command ${0##*/} ${args}" >&2 |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 171 | exit 1 |
| 172 | esac |
Mark Salyzyn | 6a7bfd8 | 2016-07-21 14:00:30 -0700 | [diff] [blame] | 173 | |
| 174 | if [ X"${log_tag}" != X"`getprop ${log_tag_property}`" ] || |
| 175 | [ X"${logd_logpersistd}" != X"`getprop ${property}`" ]; then |
Mark Salyzyn | 452eaf7 | 2016-08-04 08:39:52 -0700 | [diff] [blame] | 176 | echo "WARNING: killing Settings application to pull in new values" >&2 |
Mark Salyzyn | 6a7bfd8 | 2016-07-21 14:00:30 -0700 | [diff] [blame] | 177 | am force-stop com.android.settings |
| 178 | fi |