logpersist 5.4 KB

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