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 | data=/data/misc/logd |
| 12 | property=persist.logd.logpersistd |
| 13 | service=logcatd |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 14 | size_default=256 |
| 15 | buffer_default=all |
| 16 | args="${@}" |
| 17 | |
| 18 | size=${size_default} |
| 19 | buffer=${buffer_default} |
| 20 | clear=false |
| 21 | while [ ${#} -gt 0 ]; do |
| 22 | case ${1} in |
| 23 | -c|--clear) clear=true ;; |
| 24 | --size=*) size="${1#--size=}" ;; |
| 25 | --rotate-count=*) size="${1#--rotate-count=}" ;; |
| 26 | -n|--size|--rotate-count) size="${2}" ; shift ;; |
| 27 | --buffer=*) buffer="${1#--buffer=}" ;; |
| 28 | -b|--buffer) buffer="${2}" ; shift ;; |
| 29 | -h|--help|*) |
| 30 | LEAD_SPACE_="`echo ${progname%.*} | tr '[ -~]' ' '`" |
Mark Salyzyn | 1d94511 | 2016-06-01 15:35:41 -0700 | [diff] [blame] | 31 | echo "${progname%.*}.cat - dump current ${service} logs" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 32 | echo "${progname%.*}.start [--size=<size_in_kb>] [--buffer=<buffers>] [--clear]" |
| 33 | echo "${LEAD_SPACE_} - start ${service} service" |
| 34 | echo "${progname%.*}.stop [--clear] - stop ${service} service" |
| 35 | case ${1} in |
| 36 | -h|--help) exit 0 ;; |
| 37 | *) echo ERROR: bad argument ${@} >&2 ; exit 1 ;; |
| 38 | esac |
| 39 | ;; |
| 40 | esac |
| 41 | shift |
| 42 | done |
| 43 | |
| 44 | if [ -z "${size}" -o "${size_default}" = "${size}" ]; then |
| 45 | unset size |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 46 | fi |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 47 | if [ -n "${size}" ] && |
| 48 | ! ( [ 0 -lt "${size}" ] && [ 2048 -ge "${size}" ] ) >/dev/null 2>&1; then |
| 49 | echo ERROR: Invalid --size ${size} >&2 |
| 50 | exit 1 |
| 51 | fi |
| 52 | if [ -z "${buffer}" -o "${buffer_default}" = "${buffer}" ]; then |
| 53 | unset buffer |
| 54 | fi |
| 55 | if [ -n "${buffer}" ] && ! logcat -b ${buffer} -g >/dev/null 2>&1; then |
| 56 | echo ERROR: Invalid --buffer ${buffer} >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 60 | case ${progname} in |
| 61 | *.cat) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 62 | if [ -n "${size}${buffer}" -o "true" = "${clear}" ]; then |
| 63 | echo WARNING: Can not use --clear, --size or --buffer with ${progname%.*}.cat >&2 |
| 64 | fi |
| 65 | su logd ls "${data}" | |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 66 | tr -d '\r' | |
| 67 | sort -ru | |
| 68 | sed "s#^#${data}/#" | |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 69 | su logd xargs cat |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 70 | ;; |
| 71 | *.start) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 72 | current_buffer="`getprop ${property}.buffer`" |
| 73 | current_size="`getprop ${property}.size`" |
| 74 | if [ "${service}" = "`getprop ${property}`" ]; then |
| 75 | if [ "true" = "${clear}" ]; then |
| 76 | su root stop ${service} |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 77 | setprop ${property} "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 78 | # 20ms done, guarantees content stop before rm |
| 79 | sleep 1 |
| 80 | elif [ "${buffer}|${size}" != "${current_buffer}|${current_size}" ]; then |
| 81 | echo "ERROR: Changing existing collection parameters from" >&2 |
| 82 | if [ "${buffer}" != "${current_buffer}" ]; then |
| 83 | a=${current_buffer} |
| 84 | b=${buffer} |
| 85 | if [ -z "${a}" ]; then a="${default_buffer}"; fi |
| 86 | if [ -z "${b}" ]; then b="${default_buffer}"; fi |
| 87 | echo " --buffer ${a} to ${b}" >&2 |
| 88 | fi |
| 89 | if [ "${size}" != "${current_size}" ]; then |
| 90 | a=${current_size} |
| 91 | b=${size} |
| 92 | if [ -z "${a}" ]; then a="${default_size}"; fi |
| 93 | if [ -z "${b}" ]; then b="${default_size}"; fi |
| 94 | echo " --size ${a} to ${b}" >&2 |
| 95 | fi |
| 96 | echo " Are you sure you want to do this?" >&2 |
| 97 | echo " Suggest add --clear to erase data and restart with new settings." >&2 |
| 98 | echo " To blindly override and retain data, ${progname%.*}.stop first." >&2 |
| 99 | exit 1 |
| 100 | fi |
| 101 | fi |
| 102 | if [ "true" = "${clear}" ]; then |
| 103 | su logd,misc rm -rf "${data}" |
| 104 | fi |
| 105 | if [ -n "${buffer}${current_buffer}" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 106 | setprop ${property}.buffer "${buffer}" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 107 | fi |
| 108 | if [ -n "${size}${current_size}" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 109 | setprop ${property}.size "${size}" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 110 | fi |
| 111 | # ${service}.rc does the heavy lifting with the following trigger |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 112 | setprop ${property} ${service} |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 113 | getprop ${property} |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 114 | # 20ms done, to permit process feedback check |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 115 | sleep 1 |
Mark Salyzyn | 1d94511 | 2016-06-01 15:35:41 -0700 | [diff] [blame] | 116 | # also generate an error return code if not found running |
| 117 | pgrep -u ${data##*/} ${service%d} |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 118 | ;; |
| 119 | *.stop) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 120 | if [ -n "${size}${buffer}" ]; then |
| 121 | echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2 |
| 122 | fi |
| 123 | su root stop ${service} |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 124 | setprop ${property} "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 125 | if [ -n "`getprop ${property}.buffer`" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 126 | setprop ${property}.buffer "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 127 | fi |
| 128 | if [ -n "`getprop ${property}.size`" ]; then |
Mark Salyzyn | 8d2a655 | 2016-06-06 11:22:13 -0700 | [diff] [blame^] | 129 | setprop ${property}.size "" |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 130 | fi |
| 131 | if [ "true" = "${clear}" ]; then |
| 132 | # 20ms done, guarantees content stop before rm |
| 133 | sleep 1 |
| 134 | su logd,misc rm -rf "${data}" |
| 135 | fi |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 136 | ;; |
| 137 | *) |
Mark Salyzyn | 2e210e4 | 2016-04-12 14:35:27 -0700 | [diff] [blame] | 138 | echo "ERROR: Unexpected command ${0##*/} ${args}" >&2 |
Mark Salyzyn | 100658c | 2015-05-26 14:57:13 -0700 | [diff] [blame] | 139 | exit 1 |
| 140 | esac |