metrics daemon: create /var/run/metrics/uma-events earlier

The uma-events file needs to be created earlier, for all users of the
metrics library.  This also avoids permission issues with writing
to /var/run/metrics.

BUG=chromium:376891
TEST=manual
CQ-DEPEND=CL:201416

Change-Id: I3a48a3779edb24f3bd08a91fbee4e8fe5a25ec49
Reviewed-on: https://chromium-review.googlesource.com/201347
Tested-by: Luigi Semenzato <semenzato@chromium.org>
Reviewed-by: Jorge Lucangeli Obes <jorgelo@chromium.org>
Commit-Queue: Luigi Semenzato <semenzato@chromium.org>
diff --git a/metrics/metrics_library.cc b/metrics/metrics_library.cc
index b15fd49..4fbc1d8 100644
--- a/metrics/metrics_library.cc
+++ b/metrics/metrics_library.cc
@@ -19,8 +19,6 @@
 
 #include "policy/device_policy.h"
 
-#define READ_WRITE_ALL_FILE_FLAGS \
-  (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
 
 static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
@@ -178,21 +176,15 @@
     return false;
   }
   // Use libc here instead of chromium base classes because we need a UNIX fd
-  // for flock.
+  // for flock. |uma_events_file_| must exist already.
   int chrome_fd = HANDLE_EINTR(open(uma_events_file_.c_str(),
-                                    O_WRONLY | O_APPEND | O_CREAT,
-                                    READ_WRITE_ALL_FILE_FLAGS));
+                                    O_WRONLY | O_APPEND));
   // If we failed to open it, return.
   if (chrome_fd < 0) {
     PLOG(ERROR) << uma_events_file_ << ": open";
     return false;
   }
 
-  // Need to chmod because open flags are anded with umask. Ignore the
-  // exit code -- a chronos process may fail chmoding because the file
-  // has been created by a root process but that should be OK.
-  fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
-
   // Grab an exclusive lock to protect Chrome from truncating
   // underneath us.
   if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {