shill: Save memory log to user home directory

Save memory logs to the home directory of the first logged in user.

TEST=Connect/disconnect from WiFi, observe that logs appear in
/var/run/shill/log/ both when signed in to normal and guest mode.
Observe that this directory is a symlink to an appropriate home
directory in both cases.  network_ShillInitScripts passes.
BUG=chromium:223982

Change-Id: Ie73ea2c1234191963794dc03cf85cc5cc061f9bc
Reviewed-on: https://gerrit.chromium.org/gerrit/48052
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/bin/shill_login_user b/bin/shill_login_user
index ddd696b..ab4b62d 100755
--- a/bin/shill_login_user
+++ b/bin/shill_login_user
@@ -30,8 +30,10 @@
 
 profile_user=chronos
 profile_name="~${profile_user}/shill"
-profile_link_root=/var/run/shill/user_profiles
+shill_state_root=/var/run/shill
+profile_link_root="${shill_state_root}/user_profiles"
 profile_link_dir="$profile_link_root/$profile_user"
+log_link_dir="${shill_state_root}/log"
 
 # For multiprofile: Choose a different link name if "chronos" exists.
 profile_user_suffix=0
@@ -62,7 +64,7 @@
 else
   # If no user is given, create a temporary profile directory which will
   # be erased on logout.
-  profile_base="/var/run/shill/guest_user_profile"
+  profile_base="${shill_state_root}/guest_user_profile"
   rm -rf "$profile_base"
   flimflam_profile=""
   old_profile=""
@@ -70,6 +72,7 @@
 
 profile_dir="$profile_base/shill"
 profile="$profile_dir/shill.profile"
+log_dir="$profile_base/shill_logs"
 
 if [ ! -d "$profile_dir" ]; then
   if ! mkdir -p --mode=700 $profile_dir ; then
@@ -88,6 +91,17 @@
   logger -t "$script_name" \
     "Failed to create shill user cryptohome link $profile_link_dir"
 
+if ! mkdir -p --mode 0700 "$log_dir"; then
+  logger -t "$script_name" \
+    "Unable to create shill log directory"
+fi
+
+if [ ! -h "$log_link_dir" ]; then
+  ln -s "$log_dir" "$log_link_dir" ||
+    logger -t "$script_name" \
+      "Failed to create shill logs link $log_link_dir"
+fi
+
 if [ ! -f "$profile" ]; then
   if [ -f "$flimflam_profile" ]; then
     if [ -f "$old_profile" ]; then
diff --git a/bin/shill_logout_user b/bin/shill_logout_user
index 394ca82..faca275 100755
--- a/bin/shill_logout_user
+++ b/bin/shill_logout_user
@@ -10,4 +10,4 @@
   org.chromium.flimflam.Manager.PopAllUserProfiles ||
     logger -t "$script_name"  "Failed to pop shill user profiles"
 rm -rf /var/run/shill/user_profiles /var/run/shill/guest_user_profile
-
+rm -f /var/run/shill/log
diff --git a/memory_log.cc b/memory_log.cc
index efe62da..fbf0b45 100644
--- a/memory_log.cc
+++ b/memory_log.cc
@@ -42,7 +42,7 @@
 }  // namespace
 
 const char MemoryLog::kDefaultLoggedInDumpPath[] =
-    "/home/chronos/user/log/connectivity.log";
+    "/var/run/shill/log/connectivity.log";
 
 const char MemoryLog::kDefaultLoggedOutDumpPath[] =
     "/var/log/connectivity.log";