blob: b6415284461a0ca1a91e4cac40cbba88dd30bd25 [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// 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 SIMONNETd83ca802014-07-09 16:34:29 -07005#include "metrics/metrics_library.h"
Darin Petkov65b01462010-04-14 13:32:20 -07006
Luigi Semenzato41c54502014-05-13 15:16:24 -07007#include <base/logging.h>
8#include <base/strings/stringprintf.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009#include <errno.h>
10#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070011#include <sys/stat.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070012
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070013#include <cstdio>
14#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070015
Alex Vakulenko788d3b62014-12-11 09:48:46 -080016#include "metrics/serialization/metric_sample.h"
17#include "metrics/serialization/serialization_utils.h"
Chris Masonee10b5482013-02-14 12:15:35 -080018
Ken Mixterb2f17092011-07-22 14:59:51 -070019#include "policy/device_policy.h"
Darin Petkov8842c8c2011-02-24 12:48:30 -080020
Ken Mixter4c5daa42010-08-26 18:35:06 -070021static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
Luigi Semenzato93408422015-03-20 08:35:30 -070022static const char kUMAEventsPath[] = "/var/lib/metrics/uma-events";
Ken Mixter4c5daa42010-08-26 18:35:06 -070023static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Luigi Semenzato32684222013-03-13 10:53:55 -070024static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
25static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070026
Chih-Chung Chang6844c062013-04-01 14:27:39 +080027/* 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 */
32static const char *kCrosEventNames[] = {
33 "ModemManagerCommandSendFailure", // 0
34 "HwWatchdogReboot", // 1
35 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070036 "Chaps.DatabaseCorrupted", // 3
37 "Chaps.DatabaseRepairFailure", // 4
38 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070039 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080040 "SpringPowerSupply.Original.High", // 7
41 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080042 "SpringPowerSupply.Original.Low", // 9
43 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080044 "TPM.NonZeroDictionaryAttackCounter", // 11
Chih-Chung Chang6844c062013-04-01 14:27:39 +080045};
46
Ken Mixter4c5daa42010-08-26 18:35:06 -070047time_t MetricsLibrary::cached_enabled_time_ = 0;
48bool MetricsLibrary::cached_enabled_ = false;
49
Luigi Semenzato41c54502014-05-13 15:16:24 -070050MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {}
Daniel Eratfd158292014-03-09 21:39:08 -070051MetricsLibrary::~MetricsLibrary() {}
52
Ken Mixtereafbbdf2010-10-01 15:38:42 -070053// We take buffer and buffer_size as parameters in order to simplify testing
54// of various alignments of the |device_name| with |buffer_size|.
55bool MetricsLibrary::IsDeviceMounted(const char* device_name,
56 const char* mounts_file,
57 char* buffer,
58 int buffer_size,
59 bool* result) {
Alex Vakulenko14595032014-08-28 14:59:56 -070060 if (buffer == nullptr || buffer_size < 1)
Ken Mixtereafbbdf2010-10-01 15:38:42 -070061 return false;
62 int mounts_fd = open(mounts_file, O_RDONLY);
63 if (mounts_fd < 0)
64 return false;
65 // match_offset describes:
66 // -1 -- not beginning of line
67 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
68 // strlen(device_name) -- matched full name, just need a space.
69 int match_offset = 0;
70 bool match = false;
71 while (!match) {
72 int read_size = read(mounts_fd, buffer, buffer_size);
73 if (read_size <= 0) {
74 if (errno == -EINTR)
75 continue;
76 break;
77 }
78 for (int i = 0; i < read_size; ++i) {
79 if (buffer[i] == '\n') {
80 match_offset = 0;
81 continue;
82 }
83 if (match_offset < 0) {
84 continue;
85 }
86 if (device_name[match_offset] == '\0') {
87 if (buffer[i] == ' ') {
88 match = true;
89 break;
90 }
91 match_offset = -1;
92 continue;
93 }
94
95 if (buffer[i] == device_name[match_offset]) {
96 ++match_offset;
97 } else {
98 match_offset = -1;
99 }
100 }
101 }
102 close(mounts_fd);
103 *result = match;
104 return true;
105}
106
107bool MetricsLibrary::IsGuestMode() {
108 char buffer[256];
109 bool result = false;
110 if (!IsDeviceMounted("guestfs",
111 "/proc/mounts",
112 buffer,
113 sizeof(buffer),
114 &result)) {
115 return false;
116 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700117 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700118}
119
Ken Mixter4c5daa42010-08-26 18:35:06 -0700120bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200121 static struct stat stat_buffer;
Alex Vakulenko14595032014-08-28 14:59:56 -0700122 time_t this_check_time = time(nullptr);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700123 if (this_check_time != cached_enabled_time_) {
124 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200125
126 if (!policy_provider_.get())
127 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100128 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200129 // We initialize with the default value which is false and will be preserved
130 // if the policy is not set.
131 bool enabled = false;
132 bool has_policy = false;
133 if (policy_provider_->device_policy_is_loaded()) {
134 has_policy =
135 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
136 }
137 // If policy couldn't be loaded or the metrics policy is not set we should
138 // still respect the consent file if it is present for migration purposes.
139 // TODO(pastarmovj)
140 if (!has_policy) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700141 enabled = stat(consent_file_.c_str(), &stat_buffer) >= 0;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200142 }
143
Ken Mixterb2f17092011-07-22 14:59:51 -0700144 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700145 cached_enabled_ = true;
146 else
147 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700148 }
149 return cached_enabled_;
150}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700151
Darin Petkovfc91b422010-05-12 13:05:45 -0700152void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700153 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700154}
155
Luigi Semenzato41c54502014-05-13 15:16:24 -0700156bool MetricsLibrary::SendToAutotest(const std::string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700157 FILE* autotest_file = fopen(kAutotestPath, "a+");
Alex Vakulenko14595032014-08-28 14:59:56 -0700158 if (autotest_file == nullptr) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700159 PLOG(ERROR) << kAutotestPath << ": fopen";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700160 return false;
161 }
162
163 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
164 fclose(autotest_file);
165 return true;
166}
167
Luigi Semenzato41c54502014-05-13 15:16:24 -0700168bool MetricsLibrary::SendToUMA(const std::string& name,
169 int sample,
170 int min,
171 int max,
172 int nbuckets) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700173 return metrics::SerializationUtils::WriteMetricToFile(
174 *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
175 .get(),
176 kUMAEventsPath);
Darin Petkov65b01462010-04-14 13:32:20 -0700177}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700178
Darin Petkov21cd2c52010-05-12 15:26:16 -0700179bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
180 int max) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700181 return metrics::SerializationUtils::WriteMetricToFile(
182 *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
183 kUMAEventsPath);
Darin Petkoved824852011-01-06 10:51:47 -0800184}
185
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700186bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700187 return metrics::SerializationUtils::WriteMetricToFile(
188 *metrics::MetricSample::SparseHistogramSample(name, sample).get(),
189 kUMAEventsPath);
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700190}
191
Darin Petkoved824852011-01-06 10:51:47 -0800192bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700193 return metrics::SerializationUtils::WriteMetricToFile(
194 *metrics::MetricSample::UserActionSample(action).get(), kUMAEventsPath);
Darin Petkov5b7dce12010-04-21 15:45:10 -0700195}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800196
197bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700198 return metrics::SerializationUtils::WriteMetricToFile(
199 *metrics::MetricSample::CrashSample(crash_kind).get(), kUMAEventsPath);
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800200}
Ken Mixterb2f17092011-07-22 14:59:51 -0700201
202void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
203 policy_provider_.reset(provider);
204}
Luigi Semenzato32684222013-03-13 10:53:55 -0700205
206bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Alex Vakulenko788d3b62014-12-11 09:48:46 -0800207 for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800208 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
209 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
210 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700211 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800212 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700213}