blob: 1214dd060584a7435863519d175dbc866985d1a1 [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
7#include <errno.h>
8#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -07009#include <sys/stat.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070010
11#include <cstdarg>
12#include <cstdio>
13#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070014
Chris Masonee10b5482013-02-14 12:15:35 -080015// HANDLE_EINTR macro, no libbase required.
16#include <base/posix/eintr_wrapper.h>
17
Ken Mixterb2f17092011-07-22 14:59:51 -070018#include "policy/device_policy.h"
Darin Petkov8842c8c2011-02-24 12:48:30 -080019
Darin Petkov65b01462010-04-14 13:32:20 -070020#define READ_WRITE_ALL_FILE_FLAGS \
21 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
Chih-Chung Chang6844c062013-04-01 14:27:39 +080022#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Darin Petkov65b01462010-04-14 13:32:20 -070023
Ken Mixter4c5daa42010-08-26 18:35:06 -070024static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
25static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
26static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070027static const int32_t kBufferSize = 1024;
Luigi Semenzato32684222013-03-13 10:53:55 -070028static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
29static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070030
Chih-Chung Chang6844c062013-04-01 14:27:39 +080031/* Add new cros events here.
32 *
33 * The index of the event is sent in the message, so please do not
34 * reorder the names.
35 */
36static const char *kCrosEventNames[] = {
37 "ModemManagerCommandSendFailure", // 0
38 "HwWatchdogReboot", // 1
39 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070040 "Chaps.DatabaseCorrupted", // 3
41 "Chaps.DatabaseRepairFailure", // 4
42 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070043 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080044 "SpringPowerSupply.Original.High", // 7
45 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080046 "SpringPowerSupply.Original.Low", // 9
47 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080048 "TPM.NonZeroDictionaryAttackCounter", // 11
Chih-Chung Chang6844c062013-04-01 14:27:39 +080049};
50
Ken Mixter4c5daa42010-08-26 18:35:06 -070051time_t MetricsLibrary::cached_enabled_time_ = 0;
52bool MetricsLibrary::cached_enabled_ = false;
53
David James3b3add52010-06-04 15:01:19 -070054using std::string;
Darin Petkov65b01462010-04-14 13:32:20 -070055
56// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
Darin Petkov11b8eb32010-05-18 11:00:59 -070057static void PrintError(const char* message, const char* file,
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070058 int code) {
David James3b3add52010-06-04 15:01:19 -070059 static const char kProgramName[] = "libmetrics";
Darin Petkov65b01462010-04-14 13:32:20 -070060 if (code == 0) {
61 fprintf(stderr, "%s: %s\n", kProgramName, message);
62 } else if (file == NULL) {
63 fprintf(stderr, "%s: ", kProgramName);
64 perror(message);
65 } else {
66 fprintf(stderr, "%s: %s: ", kProgramName, file);
67 perror(message);
68 }
69}
70
Darin Petkov8d3305e2011-02-25 14:19:30 -080071// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
72static int WriteFileDescriptor(const int fd, const char* data, int size) {
73 // Allow for partial writes.
74 ssize_t bytes_written_total = 0;
75 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
76 bytes_written_total += bytes_written_partial) {
77 bytes_written_partial =
78 HANDLE_EINTR(write(fd, data + bytes_written_total,
79 size - bytes_written_total));
80 if (bytes_written_partial < 0)
81 return -1;
82 }
83
84 return bytes_written_total;
85}
86
Darin Petkov11b8eb32010-05-18 11:00:59 -070087MetricsLibrary::MetricsLibrary()
Ken Mixter4c5daa42010-08-26 18:35:06 -070088 : uma_events_file_(NULL),
Ben Chan2e6543d2014-02-05 23:26:25 -080089 consent_file_(kConsentFile) {}
Ken Mixter4c5daa42010-08-26 18:35:06 -070090
Daniel Eratfd158292014-03-09 21:39:08 -070091MetricsLibrary::~MetricsLibrary() {}
92
Ken Mixtereafbbdf2010-10-01 15:38:42 -070093// We take buffer and buffer_size as parameters in order to simplify testing
94// of various alignments of the |device_name| with |buffer_size|.
95bool MetricsLibrary::IsDeviceMounted(const char* device_name,
96 const char* mounts_file,
97 char* buffer,
98 int buffer_size,
99 bool* result) {
100 if (buffer == NULL || buffer_size < 1)
101 return false;
102 int mounts_fd = open(mounts_file, O_RDONLY);
103 if (mounts_fd < 0)
104 return false;
105 // match_offset describes:
106 // -1 -- not beginning of line
107 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
108 // strlen(device_name) -- matched full name, just need a space.
109 int match_offset = 0;
110 bool match = false;
111 while (!match) {
112 int read_size = read(mounts_fd, buffer, buffer_size);
113 if (read_size <= 0) {
114 if (errno == -EINTR)
115 continue;
116 break;
117 }
118 for (int i = 0; i < read_size; ++i) {
119 if (buffer[i] == '\n') {
120 match_offset = 0;
121 continue;
122 }
123 if (match_offset < 0) {
124 continue;
125 }
126 if (device_name[match_offset] == '\0') {
127 if (buffer[i] == ' ') {
128 match = true;
129 break;
130 }
131 match_offset = -1;
132 continue;
133 }
134
135 if (buffer[i] == device_name[match_offset]) {
136 ++match_offset;
137 } else {
138 match_offset = -1;
139 }
140 }
141 }
142 close(mounts_fd);
143 *result = match;
144 return true;
145}
146
147bool MetricsLibrary::IsGuestMode() {
148 char buffer[256];
149 bool result = false;
150 if (!IsDeviceMounted("guestfs",
151 "/proc/mounts",
152 buffer,
153 sizeof(buffer),
154 &result)) {
155 return false;
156 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700157 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700158}
159
Ken Mixter4c5daa42010-08-26 18:35:06 -0700160bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200161 static struct stat stat_buffer;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700162 time_t this_check_time = time(NULL);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700163 if (this_check_time != cached_enabled_time_) {
164 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200165
166 if (!policy_provider_.get())
167 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100168 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200169 // We initialize with the default value which is false and will be preserved
170 // if the policy is not set.
171 bool enabled = false;
172 bool has_policy = false;
173 if (policy_provider_->device_policy_is_loaded()) {
174 has_policy =
175 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
176 }
177 // If policy couldn't be loaded or the metrics policy is not set we should
178 // still respect the consent file if it is present for migration purposes.
179 // TODO(pastarmovj)
180 if (!has_policy) {
181 enabled = stat(consent_file_, &stat_buffer) >= 0;
182 }
183
Ken Mixterb2f17092011-07-22 14:59:51 -0700184 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700185 cached_enabled_ = true;
186 else
187 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700188 }
189 return cached_enabled_;
190}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700191
192bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700193
Darin Petkov8842c8c2011-02-24 12:48:30 -0800194 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
195 O_WRONLY | O_APPEND | O_CREAT,
196 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700197 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700198 if (chrome_fd < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700199 PrintError("open", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700200 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700201 }
202
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700203 // Need to chmod because open flags are anded with umask. Ignore the
204 // exit code -- a chronos process may fail chmoding because the file
205 // has been created by a root process but that should be OK.
206 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700207
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700208 // Grab an exclusive lock to protect Chrome from truncating
209 // underneath us. Keep the file locked as briefly as possible.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800210 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700211 PrintError("flock", uma_events_file_, errno);
Darin Petkov8842c8c2011-02-24 12:48:30 -0800212 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700213 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700214 }
215
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700216 bool success = true;
Darin Petkov8d3305e2011-02-25 14:19:30 -0800217 if (WriteFileDescriptor(chrome_fd, message, length) != length) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700218 PrintError("write", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700219 success = false;
220 }
Darin Petkov65b01462010-04-14 13:32:20 -0700221
Darin Petkov8842c8c2011-02-24 12:48:30 -0800222 // Close the file and release the lock.
223 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700224 return success;
225}
226
Darin Petkov11b8eb32010-05-18 11:00:59 -0700227int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
228 const char* format, ...) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700229 int32_t message_length;
230 size_t len_size = sizeof(message_length);
231
232 // Format the non-LENGTH contents in the buffer by leaving space for
233 // LENGTH at the start of the buffer.
234 va_list args;
235 va_start(args, format);
236 message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
237 format, args);
238 va_end(args);
239
240 if (message_length < 0) {
241 PrintError("chrome message format error", NULL, 0);
242 return -1;
243 }
244
245 // +1 to account for the trailing \0.
246 message_length += len_size + 1;
247 if (message_length > buffer_size) {
248 PrintError("chrome message too long", NULL, 0);
249 return -1;
250 }
251
252 // Prepend LENGTH to the message.
253 memcpy(buffer, &message_length, len_size);
254 return message_length;
255}
256
Darin Petkovfc91b422010-05-12 13:05:45 -0700257void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700258 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700259}
260
Darin Petkovc2526a12010-04-21 14:24:04 -0700261bool MetricsLibrary::SendToAutotest(const string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700262 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700263 if (autotest_file == NULL) {
264 PrintError("fopen", kAutotestPath, errno);
265 return false;
266 }
267
268 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
269 fclose(autotest_file);
270 return true;
271}
272
Darin Petkov21cd2c52010-05-12 15:26:16 -0700273bool MetricsLibrary::SendToUMA(const string& name, int sample,
274 int min, int max, int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700275 // Format the message.
276 char message[kBufferSize];
277 int32_t message_length =
278 FormatChromeMessage(kBufferSize, message,
Darin Petkovc2526a12010-04-21 14:24:04 -0700279 "histogram%c%s %d %d %d %d", '\0',
280 name.c_str(), sample, min, max, nbuckets);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700281 if (message_length < 0)
282 return false;
283
284 // Send the message.
285 return SendMessageToChrome(message_length, message);
Darin Petkov65b01462010-04-14 13:32:20 -0700286}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700287
Darin Petkov21cd2c52010-05-12 15:26:16 -0700288bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
289 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700290 // Format the message.
291 char message[kBufferSize];
292 int32_t message_length =
293 FormatChromeMessage(kBufferSize, message,
294 "linearhistogram%c%s %d %d", '\0',
295 name.c_str(), sample, max);
Darin Petkoved824852011-01-06 10:51:47 -0800296 if (message_length < 0)
297 return false;
Darin Petkov5b7dce12010-04-21 15:45:10 -0700298
Darin Petkoved824852011-01-06 10:51:47 -0800299 // Send the message.
300 return SendMessageToChrome(message_length, message);
301}
302
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700303bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
304 // Format the message.
305 char message[kBufferSize];
306 int32_t message_length =
307 FormatChromeMessage(kBufferSize, message, "sparsehistogram%c%s %d",
308 '\0', name.c_str(), sample);
309 if (message_length < 0)
310 return false;
311
312 // Send the message.
313 return SendMessageToChrome(message_length, message);
314}
315
Darin Petkoved824852011-01-06 10:51:47 -0800316bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
317 // Format the message.
318 char message[kBufferSize];
319 int32_t message_length =
320 FormatChromeMessage(kBufferSize, message,
321 "useraction%c%s", '\0', action.c_str());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700322 if (message_length < 0)
323 return false;
324
325 // Send the message.
326 return SendMessageToChrome(message_length, message);
327}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800328
329bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
330 // Format the message.
331 char message[kBufferSize];
332 int32_t message_length =
333 FormatChromeMessage(kBufferSize, message,
334 "crash%c%s", '\0', crash_kind);
335
336 if (message_length < 0)
337 return false;
338
339 // Send the message.
340 return SendMessageToChrome(message_length, message);
341}
Ken Mixterb2f17092011-07-22 14:59:51 -0700342
343void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
344 policy_provider_.reset(provider);
345}
Luigi Semenzato32684222013-03-13 10:53:55 -0700346
347bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800348 for (size_t i = 0; i < ARRAY_SIZE(kCrosEventNames); i++) {
349 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
350 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
351 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700352 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800353 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700354}