blob: c71234efdd8383a39b08f748814e2ab91b3cf32a [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
Darin Petkov65b01462010-04-14 13:32:20 -07005#include "metrics_library.h"
6
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
13#include <cstdarg>
14#include <cstdio>
15#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070016
Chris Masonee10b5482013-02-14 12:15:35 -080017// HANDLE_EINTR macro, no libbase required.
18#include <base/posix/eintr_wrapper.h>
19
Ken Mixterb2f17092011-07-22 14:59:51 -070020#include "policy/device_policy.h"
Darin Petkov8842c8c2011-02-24 12:48:30 -080021
Darin Petkov65b01462010-04-14 13:32:20 -070022#define READ_WRITE_ALL_FILE_FLAGS \
23 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
Chih-Chung Chang6844c062013-04-01 14:27:39 +080024#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Darin Petkov65b01462010-04-14 13:32:20 -070025
Ken Mixter4c5daa42010-08-26 18:35:06 -070026static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
27static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
Luigi Semenzato960e0e12014-05-15 10:48:19 -070028static const char kNewUMAEventsPath[] = "/var/run/metrics/uma-events";
Ken Mixter4c5daa42010-08-26 18:35:06 -070029static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070030static const int32_t kBufferSize = 1024;
Luigi Semenzato32684222013-03-13 10:53:55 -070031static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
32static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070033
Chih-Chung Chang6844c062013-04-01 14:27:39 +080034/* Add new cros events here.
35 *
36 * The index of the event is sent in the message, so please do not
37 * reorder the names.
38 */
39static const char *kCrosEventNames[] = {
40 "ModemManagerCommandSendFailure", // 0
41 "HwWatchdogReboot", // 1
42 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070043 "Chaps.DatabaseCorrupted", // 3
44 "Chaps.DatabaseRepairFailure", // 4
45 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070046 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080047 "SpringPowerSupply.Original.High", // 7
48 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080049 "SpringPowerSupply.Original.Low", // 9
50 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080051 "TPM.NonZeroDictionaryAttackCounter", // 11
Chih-Chung Chang6844c062013-04-01 14:27:39 +080052};
53
Ken Mixter4c5daa42010-08-26 18:35:06 -070054time_t MetricsLibrary::cached_enabled_time_ = 0;
55bool MetricsLibrary::cached_enabled_ = false;
56
Darin Petkov8d3305e2011-02-25 14:19:30 -080057// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
58static int WriteFileDescriptor(const int fd, const char* data, int size) {
59 // Allow for partial writes.
60 ssize_t bytes_written_total = 0;
61 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
62 bytes_written_total += bytes_written_partial) {
63 bytes_written_partial =
64 HANDLE_EINTR(write(fd, data + bytes_written_total,
65 size - bytes_written_total));
66 if (bytes_written_partial < 0)
67 return -1;
68 }
69
70 return bytes_written_total;
71}
72
Luigi Semenzato41c54502014-05-13 15:16:24 -070073MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {}
Daniel Eratfd158292014-03-09 21:39:08 -070074MetricsLibrary::~MetricsLibrary() {}
75
Ken Mixtereafbbdf2010-10-01 15:38:42 -070076// We take buffer and buffer_size as parameters in order to simplify testing
77// of various alignments of the |device_name| with |buffer_size|.
78bool MetricsLibrary::IsDeviceMounted(const char* device_name,
79 const char* mounts_file,
80 char* buffer,
81 int buffer_size,
82 bool* result) {
83 if (buffer == NULL || buffer_size < 1)
84 return false;
85 int mounts_fd = open(mounts_file, O_RDONLY);
86 if (mounts_fd < 0)
87 return false;
88 // match_offset describes:
89 // -1 -- not beginning of line
90 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
91 // strlen(device_name) -- matched full name, just need a space.
92 int match_offset = 0;
93 bool match = false;
94 while (!match) {
95 int read_size = read(mounts_fd, buffer, buffer_size);
96 if (read_size <= 0) {
97 if (errno == -EINTR)
98 continue;
99 break;
100 }
101 for (int i = 0; i < read_size; ++i) {
102 if (buffer[i] == '\n') {
103 match_offset = 0;
104 continue;
105 }
106 if (match_offset < 0) {
107 continue;
108 }
109 if (device_name[match_offset] == '\0') {
110 if (buffer[i] == ' ') {
111 match = true;
112 break;
113 }
114 match_offset = -1;
115 continue;
116 }
117
118 if (buffer[i] == device_name[match_offset]) {
119 ++match_offset;
120 } else {
121 match_offset = -1;
122 }
123 }
124 }
125 close(mounts_fd);
126 *result = match;
127 return true;
128}
129
130bool MetricsLibrary::IsGuestMode() {
131 char buffer[256];
132 bool result = false;
133 if (!IsDeviceMounted("guestfs",
134 "/proc/mounts",
135 buffer,
136 sizeof(buffer),
137 &result)) {
138 return false;
139 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700140 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700141}
142
Ken Mixter4c5daa42010-08-26 18:35:06 -0700143bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200144 static struct stat stat_buffer;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700145 time_t this_check_time = time(NULL);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700146 if (this_check_time != cached_enabled_time_) {
147 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200148
149 if (!policy_provider_.get())
150 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100151 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200152 // We initialize with the default value which is false and will be preserved
153 // if the policy is not set.
154 bool enabled = false;
155 bool has_policy = false;
156 if (policy_provider_->device_policy_is_loaded()) {
157 has_policy =
158 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
159 }
160 // If policy couldn't be loaded or the metrics policy is not set we should
161 // still respect the consent file if it is present for migration purposes.
162 // TODO(pastarmovj)
163 if (!has_policy) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700164 enabled = stat(consent_file_.c_str(), &stat_buffer) >= 0;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200165 }
166
Ken Mixterb2f17092011-07-22 14:59:51 -0700167 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700168 cached_enabled_ = true;
169 else
170 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700171 }
172 return cached_enabled_;
173}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700174
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700175bool MetricsLibrary::StoreMessageInFile(const std::string& message,
176 const std::string& events_file) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700177 int size = static_cast<int>(message.size());
178 if (size > kBufferSize) {
179 LOG(ERROR) << "chrome message too big (" << size << " bytes)";
180 return false;
181 }
182 // Use libc here instead of chromium base classes because we need a UNIX fd
183 // for flock.
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700184 int chrome_fd = HANDLE_EINTR(open(events_file.c_str(),
Darin Petkov8842c8c2011-02-24 12:48:30 -0800185 O_WRONLY | O_APPEND | O_CREAT,
186 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700187 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700188 if (chrome_fd < 0) {
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700189 PLOG(ERROR) << events_file << ": open";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700190 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700191 }
192
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700193 // Need to chmod because open flags are anded with umask. Ignore the
194 // exit code -- a chronos process may fail chmoding because the file
195 // has been created by a root process but that should be OK.
196 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700197
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700198 // Grab an exclusive lock to protect Chrome from truncating
Luigi Semenzato41c54502014-05-13 15:16:24 -0700199 // underneath us.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800200 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700201 PLOG(ERROR) << events_file << ": flock";
Mike Frysinger3e8a8512014-05-14 16:14:37 -0400202 IGNORE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700203 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700204 }
205
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700206 bool success = true;
Luigi Semenzato41c54502014-05-13 15:16:24 -0700207 if (WriteFileDescriptor(chrome_fd, message.c_str(), size) != size) {
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700208 PLOG(ERROR) << events_file << ": write";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700209 success = false;
210 }
Darin Petkov65b01462010-04-14 13:32:20 -0700211
Darin Petkov8842c8c2011-02-24 12:48:30 -0800212 // Close the file and release the lock.
Mike Frysinger3e8a8512014-05-14 16:14:37 -0400213 IGNORE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700214 return success;
215}
216
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700217bool MetricsLibrary::SendMessageToChrome(const std::string& message) {
218 // TEMPORARY: store to both new and old file, to facilitate change if the
219 // Chrome side is out of sync with this. See crbug.com/373833.
220
221 // If one store fails, we'll be cool... hey man it's OK, you know, whatever.
222 // If both stores fail, then definitely something is wrong.
223 bool success = StoreMessageInFile(message, uma_events_file_);
224 success |= StoreMessageInFile(message, new_uma_events_file_);
225 return success;
226}
227
Luigi Semenzato41c54502014-05-13 15:16:24 -0700228const std::string MetricsLibrary::FormatChromeMessage(
229 const std::string& name,
230 const std::string& value) {
231 uint32 message_length =
232 sizeof(message_length) + name.size() + 1 + value.size() + 1;
233 std::string result;
234 result.reserve(message_length);
235 // Marshal the total message length in the native byte order.
236 result.assign(reinterpret_cast<char*>(&message_length),
237 sizeof(message_length));
238 result += name + '\0' + value + '\0';
239 return result;
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700240}
241
Darin Petkovfc91b422010-05-12 13:05:45 -0700242void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700243 uma_events_file_ = kUMAEventsPath;
Luigi Semenzato960e0e12014-05-15 10:48:19 -0700244 new_uma_events_file_ = kNewUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700245}
246
Luigi Semenzato41c54502014-05-13 15:16:24 -0700247bool MetricsLibrary::SendToAutotest(const std::string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700248 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700249 if (autotest_file == NULL) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700250 PLOG(ERROR) << kAutotestPath << ": fopen";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700251 return false;
252 }
253
254 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
255 fclose(autotest_file);
256 return true;
257}
258
Luigi Semenzato41c54502014-05-13 15:16:24 -0700259bool MetricsLibrary::SendToUMA(const std::string& name,
260 int sample,
261 int min,
262 int max,
263 int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700264 // Format the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700265 std::string value = base::StringPrintf("%s %d %d %d %d",
266 name.c_str(), sample, min, max, nbuckets);
267 std::string message = FormatChromeMessage("histogram", value);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700268 // Send the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700269 return SendMessageToChrome(message);
Darin Petkov65b01462010-04-14 13:32:20 -0700270}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700271
Darin Petkov21cd2c52010-05-12 15:26:16 -0700272bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
273 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700274 // Format the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700275 std::string value = base::StringPrintf("%s %d %d", name.c_str(), sample, max);
276 std::string message = FormatChromeMessage("linearhistogram", value);
Darin Petkoved824852011-01-06 10:51:47 -0800277 // Send the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700278 return SendMessageToChrome(message);
Darin Petkoved824852011-01-06 10:51:47 -0800279}
280
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700281bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
282 // Format the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700283 std::string value = base::StringPrintf("%s %d", name.c_str(), sample);
284 std::string message = FormatChromeMessage("sparsehistogram", value);
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700285 // Send the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700286 return SendMessageToChrome(message);
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700287}
288
Darin Petkoved824852011-01-06 10:51:47 -0800289bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
290 // Format the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700291 std::string message = FormatChromeMessage("useraction", action);
Darin Petkov5b7dce12010-04-21 15:45:10 -0700292 // Send the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700293 return SendMessageToChrome(message);
Darin Petkov5b7dce12010-04-21 15:45:10 -0700294}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800295
296bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
297 // Format the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700298 std::string message = FormatChromeMessage("crash", crash_kind);
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800299 // Send the message.
Luigi Semenzato41c54502014-05-13 15:16:24 -0700300 return SendMessageToChrome(message);
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800301}
Ken Mixterb2f17092011-07-22 14:59:51 -0700302
303void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
304 policy_provider_.reset(provider);
305}
Luigi Semenzato32684222013-03-13 10:53:55 -0700306
307bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800308 for (size_t i = 0; i < ARRAY_SIZE(kCrosEventNames); i++) {
309 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
310 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
311 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700312 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800313 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700314}