blob: 6449a2444fdd9309bf435b9232adaeb3368208eb [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Darin Petkov65b01462010-04-14 13:32:20 -070016
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -070017#include "metrics/metrics_library.h"
Darin Petkov65b01462010-04-14 13:32:20 -070018
Luigi Semenzato41c54502014-05-13 15:16:24 -070019#include <base/logging.h>
20#include <base/strings/stringprintf.h>
Darin Petkov65b01462010-04-14 13:32:20 -070021#include <errno.h>
22#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070023#include <sys/stat.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070024
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070025#include <cstdio>
26#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070027
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070028#include "constants.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070029#include "serialization/metric_sample.h"
30#include "serialization/serialization_utils.h"
Chris Masonee10b5482013-02-14 12:15:35 -080031
Luigi Semenzato32684222013-03-13 10:53:55 -070032static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
33static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070034
Chih-Chung Chang6844c062013-04-01 14:27:39 +080035/* Add new cros events here.
36 *
37 * The index of the event is sent in the message, so please do not
38 * reorder the names.
39 */
40static const char *kCrosEventNames[] = {
41 "ModemManagerCommandSendFailure", // 0
42 "HwWatchdogReboot", // 1
43 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070044 "Chaps.DatabaseCorrupted", // 3
45 "Chaps.DatabaseRepairFailure", // 4
46 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070047 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080048 "SpringPowerSupply.Original.High", // 7
49 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080050 "SpringPowerSupply.Original.Low", // 9
51 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080052 "TPM.NonZeroDictionaryAttackCounter", // 11
Luigi Semenzato538e2092015-03-19 17:18:24 -070053 "TPM.EarlyResetDuringCommand", // 12
Chih-Chung Chang6844c062013-04-01 14:27:39 +080054};
55
Ken Mixter4c5daa42010-08-26 18:35:06 -070056time_t MetricsLibrary::cached_enabled_time_ = 0;
57bool MetricsLibrary::cached_enabled_ = false;
58
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070059MetricsLibrary::MetricsLibrary() {}
Daniel Eratfd158292014-03-09 21:39:08 -070060MetricsLibrary::~MetricsLibrary() {}
61
Ken Mixtereafbbdf2010-10-01 15:38:42 -070062// We take buffer and buffer_size as parameters in order to simplify testing
63// of various alignments of the |device_name| with |buffer_size|.
64bool MetricsLibrary::IsDeviceMounted(const char* device_name,
65 const char* mounts_file,
66 char* buffer,
67 int buffer_size,
68 bool* result) {
Alex Vakulenko14595032014-08-28 14:59:56 -070069 if (buffer == nullptr || buffer_size < 1)
Ken Mixtereafbbdf2010-10-01 15:38:42 -070070 return false;
71 int mounts_fd = open(mounts_file, O_RDONLY);
72 if (mounts_fd < 0)
73 return false;
74 // match_offset describes:
75 // -1 -- not beginning of line
76 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
77 // strlen(device_name) -- matched full name, just need a space.
78 int match_offset = 0;
79 bool match = false;
80 while (!match) {
81 int read_size = read(mounts_fd, buffer, buffer_size);
82 if (read_size <= 0) {
83 if (errno == -EINTR)
84 continue;
85 break;
86 }
87 for (int i = 0; i < read_size; ++i) {
88 if (buffer[i] == '\n') {
89 match_offset = 0;
90 continue;
91 }
92 if (match_offset < 0) {
93 continue;
94 }
95 if (device_name[match_offset] == '\0') {
96 if (buffer[i] == ' ') {
97 match = true;
98 break;
99 }
100 match_offset = -1;
101 continue;
102 }
103
104 if (buffer[i] == device_name[match_offset]) {
105 ++match_offset;
106 } else {
107 match_offset = -1;
108 }
109 }
110 }
111 close(mounts_fd);
112 *result = match;
113 return true;
114}
115
116bool MetricsLibrary::IsGuestMode() {
117 char buffer[256];
118 bool result = false;
119 if (!IsDeviceMounted("guestfs",
120 "/proc/mounts",
121 buffer,
122 sizeof(buffer),
123 &result)) {
124 return false;
125 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700126 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700127}
128
Ken Mixter4c5daa42010-08-26 18:35:06 -0700129bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200130 static struct stat stat_buffer;
Alex Vakulenko14595032014-08-28 14:59:56 -0700131 time_t this_check_time = time(nullptr);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700132 if (this_check_time != cached_enabled_time_) {
133 cached_enabled_time_ = this_check_time;
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700134 cached_enabled_ = stat(consent_file_.value().data(), &stat_buffer) >= 0;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700135 }
136 return cached_enabled_;
137}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700138
Darin Petkovfc91b422010-05-12 13:05:45 -0700139void MetricsLibrary::Init() {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700140 base::FilePath dir = base::FilePath(metrics::kMetricsDirectory);
141 uma_events_file_ = dir.Append(metrics::kMetricsEventsFileName);
142 consent_file_ = dir.Append(metrics::kConsentFileName);
Darin Petkovfc91b422010-05-12 13:05:45 -0700143}
144
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700145void MetricsLibrary::InitForTest(const base::FilePath& metrics_directory) {
146 uma_events_file_ = metrics_directory.Append(metrics::kMetricsEventsFileName);
147 consent_file_ = metrics_directory.Append(metrics::kConsentFileName);
Bertrand SIMONNET12531862015-08-31 11:11:57 -0700148}
149
Luigi Semenzato41c54502014-05-13 15:16:24 -0700150bool MetricsLibrary::SendToUMA(const std::string& name,
151 int sample,
152 int min,
153 int max,
154 int nbuckets) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700155 return metrics::SerializationUtils::WriteMetricToFile(
156 *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
157 .get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700158 uma_events_file_.value());
Darin Petkov65b01462010-04-14 13:32:20 -0700159}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700160
Darin Petkov21cd2c52010-05-12 15:26:16 -0700161bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
162 int max) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700163 return metrics::SerializationUtils::WriteMetricToFile(
164 *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700165 uma_events_file_.value());
Darin Petkoved824852011-01-06 10:51:47 -0800166}
167
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700168bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700169 return metrics::SerializationUtils::WriteMetricToFile(
170 *metrics::MetricSample::SparseHistogramSample(name, sample).get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700171 uma_events_file_.value());
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700172}
173
Darin Petkoved824852011-01-06 10:51:47 -0800174bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700175 return metrics::SerializationUtils::WriteMetricToFile(
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700176 *metrics::MetricSample::UserActionSample(action).get(),
177 uma_events_file_.value());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700178}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800179
180bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700181 return metrics::SerializationUtils::WriteMetricToFile(
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700182 *metrics::MetricSample::CrashSample(crash_kind).get(),
183 uma_events_file_.value());
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800184}
Ken Mixterb2f17092011-07-22 14:59:51 -0700185
Luigi Semenzato32684222013-03-13 10:53:55 -0700186bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Alex Vakulenko788d3b62014-12-11 09:48:46 -0800187 for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800188 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
189 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
190 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700191 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800192 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700193}