Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS 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 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 5 | #include "metrics/metrics_library.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 6 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | #include <base/strings/stringprintf.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <sys/file.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 11 | #include <sys/stat.h> |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 12 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 13 | #include <cstdio> |
| 14 | #include <cstring> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 15 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 16 | #include "constants.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 17 | #include "serialization/metric_sample.h" |
| 18 | #include "serialization/serialization_utils.h" |
Chris Masone | e10b548 | 2013-02-14 12:15:35 -0800 | [diff] [blame] | 19 | |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 20 | static const char kCrosEventHistogramName[] = "Platform.CrOSEvent"; |
| 21 | static const int kCrosEventHistogramMax = 100; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 22 | |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 23 | /* Add new cros events here. |
| 24 | * |
| 25 | * The index of the event is sent in the message, so please do not |
| 26 | * reorder the names. |
| 27 | */ |
| 28 | static const char *kCrosEventNames[] = { |
| 29 | "ModemManagerCommandSendFailure", // 0 |
| 30 | "HwWatchdogReboot", // 1 |
| 31 | "Cras.NoCodecsFoundAtBoot", // 2 |
Darren Krahn | 6e55c115 | 2013-07-19 14:09:50 -0700 | [diff] [blame] | 32 | "Chaps.DatabaseCorrupted", // 3 |
| 33 | "Chaps.DatabaseRepairFailure", // 4 |
| 34 | "Chaps.DatabaseCreateFailure", // 5 |
Darren Krahn | 86830ba | 2013-07-26 13:37:20 -0700 | [diff] [blame] | 35 | "Attestation.OriginSpecificExhausted", // 6 |
Luigi Semenzato | e57398a | 2013-11-11 14:24:44 -0800 | [diff] [blame] | 36 | "SpringPowerSupply.Original.High", // 7 |
| 37 | "SpringPowerSupply.Other.High", // 8 |
Luigi Semenzato | e8fd968 | 2013-11-13 16:28:43 -0800 | [diff] [blame] | 38 | "SpringPowerSupply.Original.Low", // 9 |
| 39 | "SpringPowerSupply.ChargerIdle", // 10 |
Darren Krahn | 09a15fa | 2014-02-07 16:51:15 -0800 | [diff] [blame] | 40 | "TPM.NonZeroDictionaryAttackCounter", // 11 |
Luigi Semenzato | 538e209 | 2015-03-19 17:18:24 -0700 | [diff] [blame] | 41 | "TPM.EarlyResetDuringCommand", // 12 |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 42 | }; |
| 43 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 44 | time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 45 | bool MetricsLibrary::cached_enabled_ = false; |
| 46 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 47 | MetricsLibrary::MetricsLibrary() : consent_file_(metrics::kConsentFilePath) {} |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 48 | MetricsLibrary::~MetricsLibrary() {} |
| 49 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 50 | // We take buffer and buffer_size as parameters in order to simplify testing |
| 51 | // of various alignments of the |device_name| with |buffer_size|. |
| 52 | bool MetricsLibrary::IsDeviceMounted(const char* device_name, |
| 53 | const char* mounts_file, |
| 54 | char* buffer, |
| 55 | int buffer_size, |
| 56 | bool* result) { |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 57 | if (buffer == nullptr || buffer_size < 1) |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 58 | return false; |
| 59 | int mounts_fd = open(mounts_file, O_RDONLY); |
| 60 | if (mounts_fd < 0) |
| 61 | return false; |
| 62 | // match_offset describes: |
| 63 | // -1 -- not beginning of line |
| 64 | // 0..strlen(device_name)-1 -- this offset in device_name is next to match |
| 65 | // strlen(device_name) -- matched full name, just need a space. |
| 66 | int match_offset = 0; |
| 67 | bool match = false; |
| 68 | while (!match) { |
| 69 | int read_size = read(mounts_fd, buffer, buffer_size); |
| 70 | if (read_size <= 0) { |
| 71 | if (errno == -EINTR) |
| 72 | continue; |
| 73 | break; |
| 74 | } |
| 75 | for (int i = 0; i < read_size; ++i) { |
| 76 | if (buffer[i] == '\n') { |
| 77 | match_offset = 0; |
| 78 | continue; |
| 79 | } |
| 80 | if (match_offset < 0) { |
| 81 | continue; |
| 82 | } |
| 83 | if (device_name[match_offset] == '\0') { |
| 84 | if (buffer[i] == ' ') { |
| 85 | match = true; |
| 86 | break; |
| 87 | } |
| 88 | match_offset = -1; |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (buffer[i] == device_name[match_offset]) { |
| 93 | ++match_offset; |
| 94 | } else { |
| 95 | match_offset = -1; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | close(mounts_fd); |
| 100 | *result = match; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool MetricsLibrary::IsGuestMode() { |
| 105 | char buffer[256]; |
| 106 | bool result = false; |
| 107 | if (!IsDeviceMounted("guestfs", |
| 108 | "/proc/mounts", |
| 109 | buffer, |
| 110 | sizeof(buffer), |
| 111 | &result)) { |
| 112 | return false; |
| 113 | } |
Arkaitz Ruiz Alvarez | 9f1a774 | 2011-05-26 12:22:22 -0700 | [diff] [blame] | 114 | return result && (access("/var/run/state/logged-in", F_OK) == 0); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 117 | bool MetricsLibrary::AreMetricsEnabled() { |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 118 | static struct stat stat_buffer; |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 119 | time_t this_check_time = time(nullptr); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 120 | if (this_check_time != cached_enabled_time_) { |
| 121 | cached_enabled_time_ = this_check_time; |
Bertrand SIMONNET | 5db66c3 | 2015-07-28 15:42:42 -0700 | [diff] [blame] | 122 | cached_enabled_ = stat(consent_file_.c_str(), &stat_buffer) >= 0; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 123 | } |
| 124 | return cached_enabled_; |
| 125 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 126 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 127 | void MetricsLibrary::Init() { |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 128 | uma_events_file_ = metrics::kMetricsEventsFilePath; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 131 | bool MetricsLibrary::SendToUMA(const std::string& name, |
| 132 | int sample, |
| 133 | int min, |
| 134 | int max, |
| 135 | int nbuckets) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 136 | return metrics::SerializationUtils::WriteMetricToFile( |
| 137 | *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets) |
| 138 | .get(), |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 139 | metrics::kMetricsEventsFilePath); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 140 | } |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 141 | |
Darin Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 142 | bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample, |
| 143 | int max) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 144 | return metrics::SerializationUtils::WriteMetricToFile( |
| 145 | *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(), |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 146 | metrics::kMetricsEventsFilePath); |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 149 | bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 150 | return metrics::SerializationUtils::WriteMetricToFile( |
| 151 | *metrics::MetricSample::SparseHistogramSample(name, sample).get(), |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 152 | metrics::kMetricsEventsFilePath); |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 155 | bool MetricsLibrary::SendUserActionToUMA(const std::string& action) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 156 | return metrics::SerializationUtils::WriteMetricToFile( |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 157 | *metrics::MetricSample::UserActionSample(action).get(), metrics::kMetricsEventsFilePath); |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 158 | } |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 159 | |
| 160 | bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 161 | return metrics::SerializationUtils::WriteMetricToFile( |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 162 | *metrics::MetricSample::CrashSample(crash_kind).get(), metrics::kMetricsEventsFilePath); |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 163 | } |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 164 | |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 165 | bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) { |
Alex Vakulenko | 788d3b6 | 2014-12-11 09:48:46 -0800 | [diff] [blame] | 166 | for (size_t i = 0; i < arraysize(kCrosEventNames); i++) { |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 167 | if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) { |
| 168 | return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax); |
| 169 | } |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 170 | } |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 171 | return false; |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 172 | } |