Fix logpersist.stop race condition

The steps did by `logpersist.stop --clear` is
 1. setprop logd.logpersistd clear
 2. setprop logd.logpersistd.size ""
 3. wait logd.logpersistd changed to be empty by logcated.rc

The problem is, logpersist.stop may modify size earlier than located.rc invoking logcat.
See comment 5 in the bug for detailed reproduce steps.

Bug: b/275518567
Test: `logpersist.stop --clear` should delete all log files in /data/misc/logd
Change-Id: Ifacfacc6486c2b98a67fb7d9587e6f588b84f179
diff --git a/logcat/logpersist b/logcat/logpersist
index 7a41ab1..31b4c74 100755
--- a/logcat/logpersist
+++ b/logcat/logpersist
@@ -2,6 +2,16 @@
 # logpersist cat, start and stop handlers
 progname="${0##*/}"
 
+function setprop_and_wait_for_change() {
+  local name="$1"
+  local value="$2"
+
+  setprop "${name}" "${value}"
+  while [ "${value}" = "${getprop ${name}}" ]; do
+    sleep 0.1
+  done
+}
+
 property=persist.logd.logpersistd
 
 case `getprop ${property#persist.}.enable` in
@@ -81,7 +91,7 @@
   current_size="`getprop ${property#persist.}.size`"
   if [ "${service}" = "`getprop ${property#persist.}`" ]; then
     if [ "true" = "${clear}" ]; then
-      setprop ${property#persist.} "clear"
+      setprop_and_wait_for_change ${property#persist.} "clear"
     elif [ "${buffer}|${size}" != "${current_buffer}|${current_size}" ]; then
       echo   "ERROR: Changing existing collection parameters from" >&2
       if [ "${buffer}" != "${current_buffer}" ]; then
@@ -104,7 +114,7 @@
       exit 1
     fi
   elif [ "true" = "${clear}" ]; then
-    setprop ${property#persist.} "clear"
+    setprop_and_wait_for_change ${property#persist.} "clear"
   fi
   if [ -n "${buffer}${current_buffer}" ]; then
     setprop ${property}.buffer "${buffer}"
@@ -120,9 +130,6 @@
       setprop ${property#persist.}.size ""
     fi
   fi
-  while [ "clear" = "`getprop ${property#persist.}`" ]; do
-    continue
-  done
   # Tell Settings that we are back on again if we turned logging off
   tag="${log_tag#Settings}"
   if [ X"${log_tag}" != X"${tag}" ]; then
@@ -142,7 +149,7 @@
     echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2
   fi
   if [ "true" = "${clear}" ]; then
-    setprop ${property#persist.} "clear"
+    setprop_and_wait_for_change ${property#persist.} "clear"
   else
     setprop ${property#persist.} "stop"
   fi
@@ -156,9 +163,6 @@
     # deal with trampoline for empty properties
     setprop ${property#persist.}.size ""
   fi
-  while [ "clear" = "`getprop ${property#persist.}`" ]; do
-    continue
-  done
   ;;
 *)
   echo "ERROR: Unexpected command ${0##*/} ${args}" >&2