blob: 8c98696c13240ce082d2c8b8af48c0a1b0e1859c [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.
Darin Petkov8842c8c2011-02-24 12:48:30 -080016
Darin Petkov65b01462010-04-14 13:32:20 -070017#define READ_WRITE_ALL_FILE_FLAGS \
18 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
19
Ken Mixter4c5daa42010-08-26 18:35:06 -070020static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
21static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
22static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070023static const int32_t kBufferSize = 1024;
Darin Petkov65b01462010-04-14 13:32:20 -070024
Ken Mixter4c5daa42010-08-26 18:35:06 -070025time_t MetricsLibrary::cached_enabled_time_ = 0;
26bool MetricsLibrary::cached_enabled_ = false;
27
David James3b3add52010-06-04 15:01:19 -070028using std::string;
Darin Petkov65b01462010-04-14 13:32:20 -070029
30// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
Darin Petkov11b8eb32010-05-18 11:00:59 -070031static void PrintError(const char* message, const char* file,
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070032 int code) {
David James3b3add52010-06-04 15:01:19 -070033 static const char kProgramName[] = "libmetrics";
Darin Petkov65b01462010-04-14 13:32:20 -070034 if (code == 0) {
35 fprintf(stderr, "%s: %s\n", kProgramName, message);
36 } else if (file == NULL) {
37 fprintf(stderr, "%s: ", kProgramName);
38 perror(message);
39 } else {
40 fprintf(stderr, "%s: %s: ", kProgramName, file);
41 perror(message);
42 }
43}
44
Darin Petkov8d3305e2011-02-25 14:19:30 -080045// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
46static int WriteFileDescriptor(const int fd, const char* data, int size) {
47 // Allow for partial writes.
48 ssize_t bytes_written_total = 0;
49 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
50 bytes_written_total += bytes_written_partial) {
51 bytes_written_partial =
52 HANDLE_EINTR(write(fd, data + bytes_written_total,
53 size - bytes_written_total));
54 if (bytes_written_partial < 0)
55 return -1;
56 }
57
58 return bytes_written_total;
59}
60
Darin Petkov11b8eb32010-05-18 11:00:59 -070061MetricsLibrary::MetricsLibrary()
Ken Mixter4c5daa42010-08-26 18:35:06 -070062 : uma_events_file_(NULL),
63 consent_file_(kConsentFile) {}
64
Ken Mixtereafbbdf2010-10-01 15:38:42 -070065// We take buffer and buffer_size as parameters in order to simplify testing
66// of various alignments of the |device_name| with |buffer_size|.
67bool MetricsLibrary::IsDeviceMounted(const char* device_name,
68 const char* mounts_file,
69 char* buffer,
70 int buffer_size,
71 bool* result) {
72 if (buffer == NULL || buffer_size < 1)
73 return false;
74 int mounts_fd = open(mounts_file, O_RDONLY);
75 if (mounts_fd < 0)
76 return false;
77 // match_offset describes:
78 // -1 -- not beginning of line
79 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
80 // strlen(device_name) -- matched full name, just need a space.
81 int match_offset = 0;
82 bool match = false;
83 while (!match) {
84 int read_size = read(mounts_fd, buffer, buffer_size);
85 if (read_size <= 0) {
86 if (errno == -EINTR)
87 continue;
88 break;
89 }
90 for (int i = 0; i < read_size; ++i) {
91 if (buffer[i] == '\n') {
92 match_offset = 0;
93 continue;
94 }
95 if (match_offset < 0) {
96 continue;
97 }
98 if (device_name[match_offset] == '\0') {
99 if (buffer[i] == ' ') {
100 match = true;
101 break;
102 }
103 match_offset = -1;
104 continue;
105 }
106
107 if (buffer[i] == device_name[match_offset]) {
108 ++match_offset;
109 } else {
110 match_offset = -1;
111 }
112 }
113 }
114 close(mounts_fd);
115 *result = match;
116 return true;
117}
118
119bool MetricsLibrary::IsGuestMode() {
120 char buffer[256];
121 bool result = false;
122 if (!IsDeviceMounted("guestfs",
123 "/proc/mounts",
124 buffer,
125 sizeof(buffer),
126 &result)) {
127 return false;
128 }
129 return result;
130}
131
Ken Mixter4c5daa42010-08-26 18:35:06 -0700132bool MetricsLibrary::AreMetricsEnabled() {
133 static struct stat stat_buffer;
134 time_t this_check_time = time(NULL);
135
136 if (this_check_time != cached_enabled_time_) {
137 cached_enabled_time_ = this_check_time;
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700138 if (stat(consent_file_, &stat_buffer) >= 0 &&
139 !IsGuestMode())
140 cached_enabled_ = true;
141 else
142 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700143 }
144 return cached_enabled_;
145}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700146
147bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700148 if (!AreMetricsEnabled())
149 return true;
150
Darin Petkov8842c8c2011-02-24 12:48:30 -0800151 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
152 O_WRONLY | O_APPEND | O_CREAT,
153 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700154 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700155 if (chrome_fd < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700156 PrintError("open", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700157 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700158 }
159
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700160 // Need to chmod because open flags are anded with umask. Ignore the
161 // exit code -- a chronos process may fail chmoding because the file
162 // has been created by a root process but that should be OK.
163 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700164
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700165 // Grab an exclusive lock to protect Chrome from truncating
166 // underneath us. Keep the file locked as briefly as possible.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800167 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700168 PrintError("flock", uma_events_file_, errno);
Darin Petkov8842c8c2011-02-24 12:48:30 -0800169 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700170 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700171 }
172
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700173 bool success = true;
Darin Petkov8d3305e2011-02-25 14:19:30 -0800174 if (WriteFileDescriptor(chrome_fd, message, length) != length) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700175 PrintError("write", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700176 success = false;
177 }
Darin Petkov65b01462010-04-14 13:32:20 -0700178
Darin Petkov8842c8c2011-02-24 12:48:30 -0800179 // Close the file and release the lock.
180 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700181 return success;
182}
183
Darin Petkov11b8eb32010-05-18 11:00:59 -0700184int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
185 const char* format, ...) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700186 int32_t message_length;
187 size_t len_size = sizeof(message_length);
188
189 // Format the non-LENGTH contents in the buffer by leaving space for
190 // LENGTH at the start of the buffer.
191 va_list args;
192 va_start(args, format);
193 message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
194 format, args);
195 va_end(args);
196
197 if (message_length < 0) {
198 PrintError("chrome message format error", NULL, 0);
199 return -1;
200 }
201
202 // +1 to account for the trailing \0.
203 message_length += len_size + 1;
204 if (message_length > buffer_size) {
205 PrintError("chrome message too long", NULL, 0);
206 return -1;
207 }
208
209 // Prepend LENGTH to the message.
210 memcpy(buffer, &message_length, len_size);
211 return message_length;
212}
213
Darin Petkovfc91b422010-05-12 13:05:45 -0700214void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700215 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700216}
217
Darin Petkovc2526a12010-04-21 14:24:04 -0700218bool MetricsLibrary::SendToAutotest(const string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700219 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700220 if (autotest_file == NULL) {
221 PrintError("fopen", kAutotestPath, errno);
222 return false;
223 }
224
225 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
226 fclose(autotest_file);
227 return true;
228}
229
Darin Petkov21cd2c52010-05-12 15:26:16 -0700230bool MetricsLibrary::SendToUMA(const string& name, int sample,
231 int min, int max, int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700232 // Format the message.
233 char message[kBufferSize];
234 int32_t message_length =
235 FormatChromeMessage(kBufferSize, message,
Darin Petkovc2526a12010-04-21 14:24:04 -0700236 "histogram%c%s %d %d %d %d", '\0',
237 name.c_str(), sample, min, max, nbuckets);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700238 if (message_length < 0)
239 return false;
240
241 // Send the message.
242 return SendMessageToChrome(message_length, message);
Darin Petkov65b01462010-04-14 13:32:20 -0700243}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700244
Darin Petkov21cd2c52010-05-12 15:26:16 -0700245bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
246 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700247 // Format the message.
248 char message[kBufferSize];
249 int32_t message_length =
250 FormatChromeMessage(kBufferSize, message,
251 "linearhistogram%c%s %d %d", '\0',
252 name.c_str(), sample, max);
Darin Petkoved824852011-01-06 10:51:47 -0800253 if (message_length < 0)
254 return false;
Darin Petkov5b7dce12010-04-21 15:45:10 -0700255
Darin Petkoved824852011-01-06 10:51:47 -0800256 // Send the message.
257 return SendMessageToChrome(message_length, message);
258}
259
260bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
261 // Format the message.
262 char message[kBufferSize];
263 int32_t message_length =
264 FormatChromeMessage(kBufferSize, message,
265 "useraction%c%s", '\0', action.c_str());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700266 if (message_length < 0)
267 return false;
268
269 // Send the message.
270 return SendMessageToChrome(message_length, message);
271}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800272
273bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
274 // Format the message.
275 char message[kBufferSize];
276 int32_t message_length =
277 FormatChromeMessage(kBufferSize, message,
278 "crash%c%s", '\0', crash_kind);
279
280 if (message_length < 0)
281 return false;
282
283 // Send the message.
284 return SendMessageToChrome(message_length, message);
285}