blob: a2227ac1bba0cab7f4b63205d161f3debfe34905 [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)
22
Ken Mixter4c5daa42010-08-26 18:35:06 -070023static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
24static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
25static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070026static const int32_t kBufferSize = 1024;
Luigi Semenzato32684222013-03-13 10:53:55 -070027static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
28static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070029
Ken Mixter4c5daa42010-08-26 18:35:06 -070030time_t MetricsLibrary::cached_enabled_time_ = 0;
31bool MetricsLibrary::cached_enabled_ = false;
32
David James3b3add52010-06-04 15:01:19 -070033using std::string;
Darin Petkov65b01462010-04-14 13:32:20 -070034
35// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
Darin Petkov11b8eb32010-05-18 11:00:59 -070036static void PrintError(const char* message, const char* file,
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070037 int code) {
David James3b3add52010-06-04 15:01:19 -070038 static const char kProgramName[] = "libmetrics";
Darin Petkov65b01462010-04-14 13:32:20 -070039 if (code == 0) {
40 fprintf(stderr, "%s: %s\n", kProgramName, message);
41 } else if (file == NULL) {
42 fprintf(stderr, "%s: ", kProgramName);
43 perror(message);
44 } else {
45 fprintf(stderr, "%s: %s: ", kProgramName, file);
46 perror(message);
47 }
48}
49
Darin Petkov8d3305e2011-02-25 14:19:30 -080050// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
51static int WriteFileDescriptor(const int fd, const char* data, int size) {
52 // Allow for partial writes.
53 ssize_t bytes_written_total = 0;
54 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
55 bytes_written_total += bytes_written_partial) {
56 bytes_written_partial =
57 HANDLE_EINTR(write(fd, data + bytes_written_total,
58 size - bytes_written_total));
59 if (bytes_written_partial < 0)
60 return -1;
61 }
62
63 return bytes_written_total;
64}
65
Darin Petkov11b8eb32010-05-18 11:00:59 -070066MetricsLibrary::MetricsLibrary()
Ken Mixter4c5daa42010-08-26 18:35:06 -070067 : uma_events_file_(NULL),
Ken Mixterb2f17092011-07-22 14:59:51 -070068 consent_file_(kConsentFile),
69 policy_provider_(NULL) {}
Ken Mixter4c5daa42010-08-26 18:35:06 -070070
Ken Mixtereafbbdf2010-10-01 15:38:42 -070071// We take buffer and buffer_size as parameters in order to simplify testing
72// of various alignments of the |device_name| with |buffer_size|.
73bool MetricsLibrary::IsDeviceMounted(const char* device_name,
74 const char* mounts_file,
75 char* buffer,
76 int buffer_size,
77 bool* result) {
78 if (buffer == NULL || buffer_size < 1)
79 return false;
80 int mounts_fd = open(mounts_file, O_RDONLY);
81 if (mounts_fd < 0)
82 return false;
83 // match_offset describes:
84 // -1 -- not beginning of line
85 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
86 // strlen(device_name) -- matched full name, just need a space.
87 int match_offset = 0;
88 bool match = false;
89 while (!match) {
90 int read_size = read(mounts_fd, buffer, buffer_size);
91 if (read_size <= 0) {
92 if (errno == -EINTR)
93 continue;
94 break;
95 }
96 for (int i = 0; i < read_size; ++i) {
97 if (buffer[i] == '\n') {
98 match_offset = 0;
99 continue;
100 }
101 if (match_offset < 0) {
102 continue;
103 }
104 if (device_name[match_offset] == '\0') {
105 if (buffer[i] == ' ') {
106 match = true;
107 break;
108 }
109 match_offset = -1;
110 continue;
111 }
112
113 if (buffer[i] == device_name[match_offset]) {
114 ++match_offset;
115 } else {
116 match_offset = -1;
117 }
118 }
119 }
120 close(mounts_fd);
121 *result = match;
122 return true;
123}
124
125bool MetricsLibrary::IsGuestMode() {
126 char buffer[256];
127 bool result = false;
128 if (!IsDeviceMounted("guestfs",
129 "/proc/mounts",
130 buffer,
131 sizeof(buffer),
132 &result)) {
133 return false;
134 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700135 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700136}
137
Ken Mixter4c5daa42010-08-26 18:35:06 -0700138bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200139 static struct stat stat_buffer;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700140 time_t this_check_time = time(NULL);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700141 if (this_check_time != cached_enabled_time_) {
142 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200143
144 if (!policy_provider_.get())
145 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100146 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200147 // We initialize with the default value which is false and will be preserved
148 // if the policy is not set.
149 bool enabled = false;
150 bool has_policy = false;
151 if (policy_provider_->device_policy_is_loaded()) {
152 has_policy =
153 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
154 }
155 // If policy couldn't be loaded or the metrics policy is not set we should
156 // still respect the consent file if it is present for migration purposes.
157 // TODO(pastarmovj)
158 if (!has_policy) {
159 enabled = stat(consent_file_, &stat_buffer) >= 0;
160 }
161
Ken Mixterb2f17092011-07-22 14:59:51 -0700162 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700163 cached_enabled_ = true;
164 else
165 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700166 }
167 return cached_enabled_;
168}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700169
170bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700171
Darin Petkov8842c8c2011-02-24 12:48:30 -0800172 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
173 O_WRONLY | O_APPEND | O_CREAT,
174 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700175 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700176 if (chrome_fd < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700177 PrintError("open", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700178 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700179 }
180
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700181 // Need to chmod because open flags are anded with umask. Ignore the
182 // exit code -- a chronos process may fail chmoding because the file
183 // has been created by a root process but that should be OK.
184 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700185
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700186 // Grab an exclusive lock to protect Chrome from truncating
187 // underneath us. Keep the file locked as briefly as possible.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800188 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700189 PrintError("flock", uma_events_file_, errno);
Darin Petkov8842c8c2011-02-24 12:48:30 -0800190 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700191 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700192 }
193
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700194 bool success = true;
Darin Petkov8d3305e2011-02-25 14:19:30 -0800195 if (WriteFileDescriptor(chrome_fd, message, length) != length) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700196 PrintError("write", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700197 success = false;
198 }
Darin Petkov65b01462010-04-14 13:32:20 -0700199
Darin Petkov8842c8c2011-02-24 12:48:30 -0800200 // Close the file and release the lock.
201 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700202 return success;
203}
204
Darin Petkov11b8eb32010-05-18 11:00:59 -0700205int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
206 const char* format, ...) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700207 int32_t message_length;
208 size_t len_size = sizeof(message_length);
209
210 // Format the non-LENGTH contents in the buffer by leaving space for
211 // LENGTH at the start of the buffer.
212 va_list args;
213 va_start(args, format);
214 message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
215 format, args);
216 va_end(args);
217
218 if (message_length < 0) {
219 PrintError("chrome message format error", NULL, 0);
220 return -1;
221 }
222
223 // +1 to account for the trailing \0.
224 message_length += len_size + 1;
225 if (message_length > buffer_size) {
226 PrintError("chrome message too long", NULL, 0);
227 return -1;
228 }
229
230 // Prepend LENGTH to the message.
231 memcpy(buffer, &message_length, len_size);
232 return message_length;
233}
234
Darin Petkovfc91b422010-05-12 13:05:45 -0700235void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700236 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700237}
238
Darin Petkovc2526a12010-04-21 14:24:04 -0700239bool MetricsLibrary::SendToAutotest(const string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700240 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700241 if (autotest_file == NULL) {
242 PrintError("fopen", kAutotestPath, errno);
243 return false;
244 }
245
246 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
247 fclose(autotest_file);
248 return true;
249}
250
Darin Petkov21cd2c52010-05-12 15:26:16 -0700251bool MetricsLibrary::SendToUMA(const string& name, int sample,
252 int min, int max, int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700253 // Format the message.
254 char message[kBufferSize];
255 int32_t message_length =
256 FormatChromeMessage(kBufferSize, message,
Darin Petkovc2526a12010-04-21 14:24:04 -0700257 "histogram%c%s %d %d %d %d", '\0',
258 name.c_str(), sample, min, max, nbuckets);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700259 if (message_length < 0)
260 return false;
261
262 // Send the message.
263 return SendMessageToChrome(message_length, message);
Darin Petkov65b01462010-04-14 13:32:20 -0700264}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700265
Darin Petkov21cd2c52010-05-12 15:26:16 -0700266bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
267 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700268 // Format the message.
269 char message[kBufferSize];
270 int32_t message_length =
271 FormatChromeMessage(kBufferSize, message,
272 "linearhistogram%c%s %d %d", '\0',
273 name.c_str(), sample, max);
Darin Petkoved824852011-01-06 10:51:47 -0800274 if (message_length < 0)
275 return false;
Darin Petkov5b7dce12010-04-21 15:45:10 -0700276
Darin Petkoved824852011-01-06 10:51:47 -0800277 // Send the message.
278 return SendMessageToChrome(message_length, message);
279}
280
281bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
282 // Format the message.
283 char message[kBufferSize];
284 int32_t message_length =
285 FormatChromeMessage(kBufferSize, message,
286 "useraction%c%s", '\0', action.c_str());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700287 if (message_length < 0)
288 return false;
289
290 // Send the message.
291 return SendMessageToChrome(message_length, message);
292}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800293
294bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
295 // Format the message.
296 char message[kBufferSize];
297 int32_t message_length =
298 FormatChromeMessage(kBufferSize, message,
299 "crash%c%s", '\0', crash_kind);
300
301 if (message_length < 0)
302 return false;
303
304 // Send the message.
305 return SendMessageToChrome(message_length, message);
306}
Ken Mixterb2f17092011-07-22 14:59:51 -0700307
308void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
309 policy_provider_.reset(provider);
310}
Luigi Semenzato32684222013-03-13 10:53:55 -0700311
312bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
313 int n;
314 /* Add new events here.
315 *
316 * Whoever adds the second event (if anybody) please be so kind to change
317 * this to a map lookup. (Or at least change "second" above to "third",
318 * etc.)
319 */
320 if (event.compare("ModemManagerCommandSendFailure") == 0) {
321 n = 0;
322 } else {
323 return false;
324 }
325 return SendEnumToUMA(kCrosEventHistogramName, n, kCrosEventHistogramMax);
326}