serialization_utils.cc 7.5 KB

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