blob: ff040e4fcad61624be89b69b28207bb645e91722 [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
Darin Petkov8d3305e2011-02-25 14:19:30 -080015#include "base/eintr_wrapper.h" // HANDLE_EINTR macro, no libbase required.
Ken Mixterb2f17092011-07-22 14:59:51 -070016#include "policy/device_policy.h"
Darin Petkov8842c8c2011-02-24 12:48:30 -080017
Darin Petkov65b01462010-04-14 13:32:20 -070018#define READ_WRITE_ALL_FILE_FLAGS \
19 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
20
Ken Mixter4c5daa42010-08-26 18:35:06 -070021static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
22static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
23static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070024static const int32_t kBufferSize = 1024;
Darin Petkov65b01462010-04-14 13:32:20 -070025
Ken Mixter4c5daa42010-08-26 18:35:06 -070026time_t MetricsLibrary::cached_enabled_time_ = 0;
27bool MetricsLibrary::cached_enabled_ = false;
28
David James3b3add52010-06-04 15:01:19 -070029using std::string;
Darin Petkov65b01462010-04-14 13:32:20 -070030
31// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
Darin Petkov11b8eb32010-05-18 11:00:59 -070032static void PrintError(const char* message, const char* file,
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070033 int code) {
David James3b3add52010-06-04 15:01:19 -070034 static const char kProgramName[] = "libmetrics";
Darin Petkov65b01462010-04-14 13:32:20 -070035 if (code == 0) {
36 fprintf(stderr, "%s: %s\n", kProgramName, message);
37 } else if (file == NULL) {
38 fprintf(stderr, "%s: ", kProgramName);
39 perror(message);
40 } else {
41 fprintf(stderr, "%s: %s: ", kProgramName, file);
42 perror(message);
43 }
44}
45
Darin Petkov8d3305e2011-02-25 14:19:30 -080046// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
47static int WriteFileDescriptor(const int fd, const char* data, int size) {
48 // Allow for partial writes.
49 ssize_t bytes_written_total = 0;
50 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
51 bytes_written_total += bytes_written_partial) {
52 bytes_written_partial =
53 HANDLE_EINTR(write(fd, data + bytes_written_total,
54 size - bytes_written_total));
55 if (bytes_written_partial < 0)
56 return -1;
57 }
58
59 return bytes_written_total;
60}
61
Darin Petkov11b8eb32010-05-18 11:00:59 -070062MetricsLibrary::MetricsLibrary()
Ken Mixter4c5daa42010-08-26 18:35:06 -070063 : uma_events_file_(NULL),
Ken Mixterb2f17092011-07-22 14:59:51 -070064 consent_file_(kConsentFile),
65 policy_provider_(NULL) {}
Ken Mixter4c5daa42010-08-26 18:35:06 -070066
Ken Mixtereafbbdf2010-10-01 15:38:42 -070067// We take buffer and buffer_size as parameters in order to simplify testing
68// of various alignments of the |device_name| with |buffer_size|.
69bool MetricsLibrary::IsDeviceMounted(const char* device_name,
70 const char* mounts_file,
71 char* buffer,
72 int buffer_size,
73 bool* result) {
74 if (buffer == NULL || buffer_size < 1)
75 return false;
76 int mounts_fd = open(mounts_file, O_RDONLY);
77 if (mounts_fd < 0)
78 return false;
79 // match_offset describes:
80 // -1 -- not beginning of line
81 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
82 // strlen(device_name) -- matched full name, just need a space.
83 int match_offset = 0;
84 bool match = false;
85 while (!match) {
86 int read_size = read(mounts_fd, buffer, buffer_size);
87 if (read_size <= 0) {
88 if (errno == -EINTR)
89 continue;
90 break;
91 }
92 for (int i = 0; i < read_size; ++i) {
93 if (buffer[i] == '\n') {
94 match_offset = 0;
95 continue;
96 }
97 if (match_offset < 0) {
98 continue;
99 }
100 if (device_name[match_offset] == '\0') {
101 if (buffer[i] == ' ') {
102 match = true;
103 break;
104 }
105 match_offset = -1;
106 continue;
107 }
108
109 if (buffer[i] == device_name[match_offset]) {
110 ++match_offset;
111 } else {
112 match_offset = -1;
113 }
114 }
115 }
116 close(mounts_fd);
117 *result = match;
118 return true;
119}
120
121bool MetricsLibrary::IsGuestMode() {
122 char buffer[256];
123 bool result = false;
124 if (!IsDeviceMounted("guestfs",
125 "/proc/mounts",
126 buffer,
127 sizeof(buffer),
128 &result)) {
129 return false;
130 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700131 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700132}
133
Ken Mixter4c5daa42010-08-26 18:35:06 -0700134bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200135 static struct stat stat_buffer;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700136 time_t this_check_time = time(NULL);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700137 if (this_check_time != cached_enabled_time_) {
138 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200139
140 if (!policy_provider_.get())
141 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100142 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200143 // We initialize with the default value which is false and will be preserved
144 // if the policy is not set.
145 bool enabled = false;
146 bool has_policy = false;
147 if (policy_provider_->device_policy_is_loaded()) {
148 has_policy =
149 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
150 }
151 // If policy couldn't be loaded or the metrics policy is not set we should
152 // still respect the consent file if it is present for migration purposes.
153 // TODO(pastarmovj)
154 if (!has_policy) {
155 enabled = stat(consent_file_, &stat_buffer) >= 0;
156 }
157
Ken Mixterb2f17092011-07-22 14:59:51 -0700158 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700159 cached_enabled_ = true;
160 else
161 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700162 }
163 return cached_enabled_;
164}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700165
166bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700167
Darin Petkov8842c8c2011-02-24 12:48:30 -0800168 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
169 O_WRONLY | O_APPEND | O_CREAT,
170 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700171 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700172 if (chrome_fd < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700173 PrintError("open", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700174 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700175 }
176
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700177 // Need to chmod because open flags are anded with umask. Ignore the
178 // exit code -- a chronos process may fail chmoding because the file
179 // has been created by a root process but that should be OK.
180 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700181
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700182 // Grab an exclusive lock to protect Chrome from truncating
183 // underneath us. Keep the file locked as briefly as possible.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800184 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700185 PrintError("flock", uma_events_file_, errno);
Darin Petkov8842c8c2011-02-24 12:48:30 -0800186 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700187 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700188 }
189
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700190 bool success = true;
Darin Petkov8d3305e2011-02-25 14:19:30 -0800191 if (WriteFileDescriptor(chrome_fd, message, length) != length) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700192 PrintError("write", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700193 success = false;
194 }
Darin Petkov65b01462010-04-14 13:32:20 -0700195
Darin Petkov8842c8c2011-02-24 12:48:30 -0800196 // Close the file and release the lock.
197 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700198 return success;
199}
200
Darin Petkov11b8eb32010-05-18 11:00:59 -0700201int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
202 const char* format, ...) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700203 int32_t message_length;
204 size_t len_size = sizeof(message_length);
205
206 // Format the non-LENGTH contents in the buffer by leaving space for
207 // LENGTH at the start of the buffer.
208 va_list args;
209 va_start(args, format);
210 message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
211 format, args);
212 va_end(args);
213
214 if (message_length < 0) {
215 PrintError("chrome message format error", NULL, 0);
216 return -1;
217 }
218
219 // +1 to account for the trailing \0.
220 message_length += len_size + 1;
221 if (message_length > buffer_size) {
222 PrintError("chrome message too long", NULL, 0);
223 return -1;
224 }
225
226 // Prepend LENGTH to the message.
227 memcpy(buffer, &message_length, len_size);
228 return message_length;
229}
230
Darin Petkovfc91b422010-05-12 13:05:45 -0700231void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700232 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700233}
234
Darin Petkovc2526a12010-04-21 14:24:04 -0700235bool MetricsLibrary::SendToAutotest(const string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700236 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700237 if (autotest_file == NULL) {
238 PrintError("fopen", kAutotestPath, errno);
239 return false;
240 }
241
242 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
243 fclose(autotest_file);
244 return true;
245}
246
Darin Petkov21cd2c52010-05-12 15:26:16 -0700247bool MetricsLibrary::SendToUMA(const string& name, int sample,
248 int min, int max, int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700249 // Format the message.
250 char message[kBufferSize];
251 int32_t message_length =
252 FormatChromeMessage(kBufferSize, message,
Darin Petkovc2526a12010-04-21 14:24:04 -0700253 "histogram%c%s %d %d %d %d", '\0',
254 name.c_str(), sample, min, max, nbuckets);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700255 if (message_length < 0)
256 return false;
257
258 // Send the message.
259 return SendMessageToChrome(message_length, message);
Darin Petkov65b01462010-04-14 13:32:20 -0700260}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700261
Darin Petkov21cd2c52010-05-12 15:26:16 -0700262bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
263 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700264 // Format the message.
265 char message[kBufferSize];
266 int32_t message_length =
267 FormatChromeMessage(kBufferSize, message,
268 "linearhistogram%c%s %d %d", '\0',
269 name.c_str(), sample, max);
Darin Petkoved824852011-01-06 10:51:47 -0800270 if (message_length < 0)
271 return false;
Darin Petkov5b7dce12010-04-21 15:45:10 -0700272
Darin Petkoved824852011-01-06 10:51:47 -0800273 // Send the message.
274 return SendMessageToChrome(message_length, message);
275}
276
277bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
278 // Format the message.
279 char message[kBufferSize];
280 int32_t message_length =
281 FormatChromeMessage(kBufferSize, message,
282 "useraction%c%s", '\0', action.c_str());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700283 if (message_length < 0)
284 return false;
285
286 // Send the message.
287 return SendMessageToChrome(message_length, message);
288}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800289
290bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
291 // Format the message.
292 char message[kBufferSize];
293 int32_t message_length =
294 FormatChromeMessage(kBufferSize, message,
295 "crash%c%s", '\0', crash_kind);
296
297 if (message_length < 0)
298 return false;
299
300 // Send the message.
301 return SendMessageToChrome(message_length, message);
302}
Ken Mixterb2f17092011-07-22 14:59:51 -0700303
304void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
305 policy_provider_.reset(provider);
306}