metrics: move sample file from /var/run to /var/lib

Some metrics are generated near shutdown and we expect to collect
them at reboot, but /var/run is a tmpfs and /var/run/uma-events
is lost.

This changes the uma events file location from the perspective
of the metrics daemon.  I am making the same change to Chrome.
I am adding a symlink to allow an older Chrome to read the
samples from the old location.  A newer Chrome will be screwed
but I hope that's OK (i.e. this will work for official builds
and for Chrome OS developers, but may cause malfunction for
Chrome developers)

BUG=chromium:447256
TEST=verified that /var/lib/metrics/uma-events grows and is truncated
CQ-DEPEND=CL:258971

Change-Id: I677cda16486de239dd205247083d4ad7240d992b
Reviewed-on: https://chromium-review.googlesource.com/257084
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
Tested-by: Luigi Semenzato <semenzato@chromium.org>
Commit-Queue: Luigi Semenzato <semenzato@chromium.org>
diff --git a/metrics/init/metrics_daemon.conf b/metrics/init/metrics_daemon.conf
index 3fc2dd7..e6932cf 100644
--- a/metrics/init/metrics_daemon.conf
+++ b/metrics/init/metrics_daemon.conf
@@ -11,11 +11,6 @@
 stop on stopping system-services
 respawn
 
-pre-start script
-  # Make directory, but don't die on error.  Let someone else complain.
-  mkdir -p /var/lib/metrics
-end script
-
 # metrics will update the next line to add -uploader for embedded builds.
 env DAEMON_FLAGS=""
 
diff --git a/metrics/init/metrics_library.conf b/metrics/init/metrics_library.conf
index 6aa49ed..8202cc1 100644
--- a/metrics/init/metrics_library.conf
+++ b/metrics/init/metrics_library.conf
@@ -11,9 +11,15 @@
 
 pre-start script
   # Create the file used as communication endpoint for metrics.
-  RUNDIR=/var/run/metrics
-  EVENTS_FILE=${RUNDIR}/uma-events
-  mkdir -p ${RUNDIR}
+  METRICS_DIR=/var/lib/metrics
+  EVENTS_FILE=${METRICS_DIR}/uma-events
+  mkdir -p ${METRICS_DIR}
   touch ${EVENTS_FILE}
+  chown chronos.chronos ${EVENT_FILE}
   chmod 666 ${EVENTS_FILE}
+  # TRANSITION ONLY.
+  # TODO(semenzato) Remove after Chrome change, see issue 447256.
+  # Let Chrome read the metrics file from the old location.
+  mkdir -p /var/run/metrics
+  ln -s ${EVENTS_FILE} /var/run/metrics
 end script
diff --git a/metrics/metrics_daemon_main.cc b/metrics/metrics_daemon_main.cc
index cc0812d..a6d040c 100644
--- a/metrics/metrics_daemon_main.cc
+++ b/metrics/metrics_daemon_main.cc
@@ -63,7 +63,7 @@
                 "https://clients4.google.com/uma/v2",
                 "Server to upload the metrics to. (needs -uploader)");
   DEFINE_string(metrics_file,
-                "/var/run/metrics/uma-events",
+                "/var/lib/metrics/uma-events",
                 "File to use as a proxy for uploading the metrics");
   DEFINE_string(config_root,
                 "/", "Root of the configuration files (testing only)");