Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / components / metrics / chromeos / serialization_utils.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/chromeos/serialization_utils.h"
6
7 #include <sys/file.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/files/scoped_file.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "components/metrics/chromeos/metric_sample.h"
21
22 #define READ_WRITE_ALL_FILE_FLAGS \
23   (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
24
25 namespace metrics {
26 namespace {
27
28 // Reads the next message from |file_descriptor| into |message|.
29 //
30 // |message| will be set to the empty string if no message could be read (EOF)
31 // or the message was badly constructed.
32 //
33 // Returns false if no message can be read from this file anymore (EOF or
34 // unrecoverable error).
35 bool ReadMessage(int fd, std::string* message) {
36   CHECK(message);
37
38   int result;
39   int32 message_size;
40   // The file containing the metrics do not leave the device so the writer and
41   // the reader will always have the same endianness.
42   result = HANDLE_EINTR(read(fd, &message_size, sizeof(message_size)));
43   if (result < 0) {
44     DPLOG(ERROR) << "reading metrics message header";
45     return false;
46   }
47   if (result == 0) {
48     // This indicates a normal EOF.
49     return false;
50   }
51   if (result < static_cast<int>(sizeof(message_size))) {
52     DLOG(ERROR) << "bad read size " << result << ", expecting "
53                 << sizeof(message_size);
54     return false;
55   }
56
57   // kMessageMaxLength applies to the entire message: the 4-byte
58   // length field and the content.
59   if (message_size > metrics::SerializationUtils::kMessageMaxLength) {
60     DLOG(ERROR) << "message too long : " << message_size;
61     if (HANDLE_EINTR(lseek(fd, message_size - 4, SEEK_CUR)) == -1) {
62       DLOG(ERROR) << "error while skipping message. abort";
63       return false;
64     }
65     // Badly formatted message was skipped. Treat the badly formatted sample as
66     // an empty sample.
67     message->clear();
68     return true;
69   }
70
71   message_size -= sizeof(message_size);  // The message size includes itself.
72   char buffer[metrics::SerializationUtils::kMessageMaxLength];
73   if (!base::ReadFromFD(fd, buffer, message_size)) {
74     DPLOG(ERROR) << "reading metrics message body";
75     return false;
76   }
77   *message = std::string(buffer, message_size);
78   return true;
79 }
80
81 }  // namespace
82
83 scoped_ptr<MetricSample> SerializationUtils::ParseSample(
84     const std::string& sample) {
85   if (sample.empty())
86     return scoped_ptr<MetricSample>();
87
88   std::vector<std::string> parts;
89   base::SplitString(sample, '\0', &parts);
90   // We should have two null terminated strings so split should produce
91   // three chunks.
92   if (parts.size() != 3) {
93     DLOG(ERROR) << "splitting message on \\0 produced " << parts.size()
94                 << " parts (expected 3)";
95     return scoped_ptr<MetricSample>();
96   }
97   const std::string& name = parts[0];
98   const std::string& value = parts[1];
99
100   if (LowerCaseEqualsASCII(name, "crash")) {
101     return MetricSample::CrashSample(value);
102   } else if (LowerCaseEqualsASCII(name, "histogram")) {
103     return MetricSample::ParseHistogram(value);
104   } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
105     return MetricSample::ParseLinearHistogram(value);
106   } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
107     return MetricSample::ParseSparseHistogram(value);
108   } else if (LowerCaseEqualsASCII(name, "useraction")) {
109     return MetricSample::UserActionSample(value);
110   } else {
111     DLOG(ERROR) << "invalid event type: " << name << ", value: " << value;
112   }
113   return scoped_ptr<MetricSample>();
114 }
115
116 void SerializationUtils::ReadAndTruncateMetricsFromFile(
117     const std::string& filename,
118     ScopedVector<MetricSample>* metrics) {
119   struct stat stat_buf;
120   int result;
121
122   result = stat(filename.c_str(), &stat_buf);
123   if (result < 0) {
124     if (errno != ENOENT)
125       DPLOG(ERROR) << filename << ": bad metrics file stat";
126
127     // Nothing to collect---try later.
128     return;
129   }
130   if (stat_buf.st_size == 0) {
131     // Also nothing to collect.
132     return;
133   }
134   base::ScopedFD fd(open(filename.c_str(), O_RDWR));
135   if (fd.get() < 0) {
136     DPLOG(ERROR) << filename << ": cannot open";
137     return;
138   }
139   result = flock(fd.get(), LOCK_EX);
140   if (result < 0) {
141     DPLOG(ERROR) << filename << ": cannot lock";
142     return;
143   }
144
145   // This processes all messages in the log. When all messages are
146   // read and processed, or an error occurs, truncate the file to zero size.
147   for (;;) {
148     std::string message;
149
150     if (!ReadMessage(fd.get(), &message))
151       break;
152
153     scoped_ptr<MetricSample> sample = ParseSample(message);
154     if (sample)
155       metrics->push_back(sample.release());
156   }
157
158   result = ftruncate(fd.get(), 0);
159   if (result < 0)
160     DPLOG(ERROR) << "truncate metrics log";
161
162   result = flock(fd.get(), LOCK_UN);
163   if (result < 0)
164     DPLOG(ERROR) << "unlock metrics log";
165 }
166
167 bool SerializationUtils::WriteMetricToFile(const MetricSample& sample,
168                                            const std::string& filename) {
169   if (!sample.IsValid())
170     return false;
171
172   base::ScopedFD file_descriptor(open(filename.c_str(),
173                                       O_WRONLY | O_APPEND | O_CREAT,
174                                       READ_WRITE_ALL_FILE_FLAGS));
175
176   if (file_descriptor.get() < 0) {
177     DLOG(ERROR) << "error openning the file";
178     return false;
179   }
180
181   fchmod(file_descriptor.get(), READ_WRITE_ALL_FILE_FLAGS);
182   // Grab a lock to avoid chrome truncating the file
183   // underneath us. Keep the file locked as briefly as possible.
184   // Freeing file_descriptor will close the file and and remove the lock.
185   if (HANDLE_EINTR(flock(file_descriptor.get(), LOCK_EX)) < 0) {
186     DLOG(ERROR) << "error locking" << filename << " : " << errno;
187     return false;
188   }
189
190   std::string msg = sample.ToString();
191   int32 size = msg.length() + sizeof(int32);
192   if (size > kMessageMaxLength) {
193     DLOG(ERROR) << "cannot write message: too long";
194     return false;
195   }
196
197   // The file containing the metrics samples will only be read by programs on
198   // the same device so we do not check endianness.
199   if (base::WriteFileDescriptor(file_descriptor.get(),
200                                 reinterpret_cast<char*>(&size),
201                                 sizeof(size)) != sizeof(size)) {
202     DLOG(ERROR) << "error writing message length " << errno;
203     return false;
204   }
205
206   if (base::WriteFileDescriptor(
207           file_descriptor.get(), msg.c_str(), msg.length()) !=
208       static_cast<int>(msg.length())) {
209     DLOG(ERROR) << "error writing message" << errno;
210     return false;
211   }
212
213   return true;
214 }
215
216 }  // namespace metrics