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 | |
Alex Vakulenko | 788d3b6 | 2014-12-11 09:48:46 -0800 | [diff] [blame] | 16 | #include "metrics/serialization/metric_sample.h" |
| 17 | #include "metrics/serialization/serialization_utils.h" |
Chris Masone | e10b548 | 2013-02-14 12:15:35 -0800 | [diff] [blame] | 18 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 19 | #include "policy/device_policy.h" |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 20 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 21 | static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; |
Luigi Semenzato | 9340842 | 2015-03-20 08:35:30 -0700 | [diff] [blame] | 22 | static const char kUMAEventsPath[] = "/var/lib/metrics/uma-events"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 23 | static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 24 | static const char kCrosEventHistogramName[] = "Platform.CrOSEvent"; |
| 25 | static const int kCrosEventHistogramMax = 100; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 26 | |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 27 | /* Add new cros events here. |
| 28 | * |
| 29 | * The index of the event is sent in the message, so please do not |
| 30 | * reorder the names. |
| 31 | */ |
| 32 | static const char *kCrosEventNames[] = { |
| 33 | "ModemManagerCommandSendFailure", // 0 |
| 34 | "HwWatchdogReboot", // 1 |
| 35 | "Cras.NoCodecsFoundAtBoot", // 2 |
Darren Krahn | 6e55c115 | 2013-07-19 14:09:50 -0700 | [diff] [blame] | 36 | "Chaps.DatabaseCorrupted", // 3 |
| 37 | "Chaps.DatabaseRepairFailure", // 4 |
| 38 | "Chaps.DatabaseCreateFailure", // 5 |
Darren Krahn | 86830ba | 2013-07-26 13:37:20 -0700 | [diff] [blame] | 39 | "Attestation.OriginSpecificExhausted", // 6 |
Luigi Semenzato | e57398a | 2013-11-11 14:24:44 -0800 | [diff] [blame] | 40 | "SpringPowerSupply.Original.High", // 7 |
| 41 | "SpringPowerSupply.Other.High", // 8 |
Luigi Semenzato | e8fd968 | 2013-11-13 16:28:43 -0800 | [diff] [blame] | 42 | "SpringPowerSupply.Original.Low", // 9 |
| 43 | "SpringPowerSupply.ChargerIdle", // 10 |
Darren Krahn | 09a15fa | 2014-02-07 16:51:15 -0800 | [diff] [blame] | 44 | "TPM.NonZeroDictionaryAttackCounter", // 11 |
Luigi Semenzato | 538e209 | 2015-03-19 17:18:24 -0700 | [diff] [blame^] | 45 | "TPM.EarlyResetDuringCommand", // 12 |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 46 | }; |
| 47 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 48 | time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 49 | bool MetricsLibrary::cached_enabled_ = false; |
| 50 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 51 | MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {} |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 52 | MetricsLibrary::~MetricsLibrary() {} |
| 53 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 54 | // We take buffer and buffer_size as parameters in order to simplify testing |
| 55 | // of various alignments of the |device_name| with |buffer_size|. |
| 56 | bool MetricsLibrary::IsDeviceMounted(const char* device_name, |
| 57 | const char* mounts_file, |
| 58 | char* buffer, |
| 59 | int buffer_size, |
| 60 | bool* result) { |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 61 | if (buffer == nullptr || buffer_size < 1) |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 62 | return false; |
| 63 | int mounts_fd = open(mounts_file, O_RDONLY); |
| 64 | if (mounts_fd < 0) |
| 65 | return false; |
| 66 | // match_offset describes: |
| 67 | // -1 -- not beginning of line |
| 68 | // 0..strlen(device_name)-1 -- this offset in device_name is next to match |
| 69 | // strlen(device_name) -- matched full name, just need a space. |
| 70 | int match_offset = 0; |
| 71 | bool match = false; |
| 72 | while (!match) { |
| 73 | int read_size = read(mounts_fd, buffer, buffer_size); |
| 74 | if (read_size <= 0) { |
| 75 | if (errno == -EINTR) |
| 76 | continue; |
| 77 | break; |
| 78 | } |
| 79 | for (int i = 0; i < read_size; ++i) { |
| 80 | if (buffer[i] == '\n') { |
| 81 | match_offset = 0; |
| 82 | continue; |
| 83 | } |
| 84 | if (match_offset < 0) { |
| 85 | continue; |
| 86 | } |
| 87 | if (device_name[match_offset] == '\0') { |
| 88 | if (buffer[i] == ' ') { |
| 89 | match = true; |
| 90 | break; |
| 91 | } |
| 92 | match_offset = -1; |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | if (buffer[i] == device_name[match_offset]) { |
| 97 | ++match_offset; |
| 98 | } else { |
| 99 | match_offset = -1; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | close(mounts_fd); |
| 104 | *result = match; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool MetricsLibrary::IsGuestMode() { |
| 109 | char buffer[256]; |
| 110 | bool result = false; |
| 111 | if (!IsDeviceMounted("guestfs", |
| 112 | "/proc/mounts", |
| 113 | buffer, |
| 114 | sizeof(buffer), |
| 115 | &result)) { |
| 116 | return false; |
| 117 | } |
Arkaitz Ruiz Alvarez | 9f1a774 | 2011-05-26 12:22:22 -0700 | [diff] [blame] | 118 | return result && (access("/var/run/state/logged-in", F_OK) == 0); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 121 | bool MetricsLibrary::AreMetricsEnabled() { |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 122 | static struct stat stat_buffer; |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 123 | time_t this_check_time = time(nullptr); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 124 | if (this_check_time != cached_enabled_time_) { |
| 125 | cached_enabled_time_ = this_check_time; |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 126 | |
| 127 | if (!policy_provider_.get()) |
| 128 | policy_provider_.reset(new policy::PolicyProvider()); |
Julian Pastarmov | d605a00 | 2013-02-04 17:58:14 +0100 | [diff] [blame] | 129 | policy_provider_->Reload(); |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 130 | // We initialize with the default value which is false and will be preserved |
| 131 | // if the policy is not set. |
| 132 | bool enabled = false; |
| 133 | bool has_policy = false; |
| 134 | if (policy_provider_->device_policy_is_loaded()) { |
| 135 | has_policy = |
| 136 | policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled); |
| 137 | } |
| 138 | // If policy couldn't be loaded or the metrics policy is not set we should |
| 139 | // still respect the consent file if it is present for migration purposes. |
| 140 | // TODO(pastarmovj) |
| 141 | if (!has_policy) { |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 142 | enabled = stat(consent_file_.c_str(), &stat_buffer) >= 0; |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 145 | if (enabled && !IsGuestMode()) |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 146 | cached_enabled_ = true; |
| 147 | else |
| 148 | cached_enabled_ = false; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 149 | } |
| 150 | return cached_enabled_; |
| 151 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 152 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 153 | void MetricsLibrary::Init() { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 154 | uma_events_file_ = kUMAEventsPath; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 157 | bool MetricsLibrary::SendToAutotest(const std::string& name, int value) { |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 158 | FILE* autotest_file = fopen(kAutotestPath, "a+"); |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 159 | if (autotest_file == nullptr) { |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 160 | PLOG(ERROR) << kAutotestPath << ": fopen"; |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | |
| 164 | fprintf(autotest_file, "%s=%d\n", name.c_str(), value); |
| 165 | fclose(autotest_file); |
| 166 | return true; |
| 167 | } |
| 168 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 169 | bool MetricsLibrary::SendToUMA(const std::string& name, |
| 170 | int sample, |
| 171 | int min, |
| 172 | int max, |
| 173 | int nbuckets) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 174 | return metrics::SerializationUtils::WriteMetricToFile( |
| 175 | *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets) |
| 176 | .get(), |
| 177 | kUMAEventsPath); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 178 | } |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 179 | |
Darin Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 180 | bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample, |
| 181 | int max) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 182 | return metrics::SerializationUtils::WriteMetricToFile( |
| 183 | *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(), |
| 184 | kUMAEventsPath); |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 187 | bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 188 | return metrics::SerializationUtils::WriteMetricToFile( |
| 189 | *metrics::MetricSample::SparseHistogramSample(name, sample).get(), |
| 190 | kUMAEventsPath); |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 193 | bool MetricsLibrary::SendUserActionToUMA(const std::string& action) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 194 | return metrics::SerializationUtils::WriteMetricToFile( |
| 195 | *metrics::MetricSample::UserActionSample(action).get(), kUMAEventsPath); |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 196 | } |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 197 | |
| 198 | bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) { |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 199 | return metrics::SerializationUtils::WriteMetricToFile( |
| 200 | *metrics::MetricSample::CrashSample(crash_kind).get(), kUMAEventsPath); |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 201 | } |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 202 | |
| 203 | void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) { |
| 204 | policy_provider_.reset(provider); |
| 205 | } |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 206 | |
| 207 | bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) { |
Alex Vakulenko | 788d3b6 | 2014-12-11 09:48:46 -0800 | [diff] [blame] | 208 | for (size_t i = 0; i < arraysize(kCrosEventNames); i++) { |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 209 | if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) { |
| 210 | return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax); |
| 211 | } |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 212 | } |
Chih-Chung Chang | 6844c06 | 2013-04-01 14:27:39 +0800 | [diff] [blame] | 213 | return false; |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 214 | } |