blob: 3bc800716df9903034363cf678861de5d3bac8dd [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 Petkov8842c8c2011-02-24 12:48:30 -080015#include <base/eintr_wrapper.h>
16
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 Petkov11b8eb32010-05-18 11:00:59 -070045MetricsLibrary::MetricsLibrary()
Ken Mixter4c5daa42010-08-26 18:35:06 -070046 : uma_events_file_(NULL),
47 consent_file_(kConsentFile) {}
48
Ken Mixtereafbbdf2010-10-01 15:38:42 -070049// We take buffer and buffer_size as parameters in order to simplify testing
50// of various alignments of the |device_name| with |buffer_size|.
51bool MetricsLibrary::IsDeviceMounted(const char* device_name,
52 const char* mounts_file,
53 char* buffer,
54 int buffer_size,
55 bool* result) {
56 if (buffer == NULL || buffer_size < 1)
57 return false;
58 int mounts_fd = open(mounts_file, O_RDONLY);
59 if (mounts_fd < 0)
60 return false;
61 // match_offset describes:
62 // -1 -- not beginning of line
63 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
64 // strlen(device_name) -- matched full name, just need a space.
65 int match_offset = 0;
66 bool match = false;
67 while (!match) {
68 int read_size = read(mounts_fd, buffer, buffer_size);
69 if (read_size <= 0) {
70 if (errno == -EINTR)
71 continue;
72 break;
73 }
74 for (int i = 0; i < read_size; ++i) {
75 if (buffer[i] == '\n') {
76 match_offset = 0;
77 continue;
78 }
79 if (match_offset < 0) {
80 continue;
81 }
82 if (device_name[match_offset] == '\0') {
83 if (buffer[i] == ' ') {
84 match = true;
85 break;
86 }
87 match_offset = -1;
88 continue;
89 }
90
91 if (buffer[i] == device_name[match_offset]) {
92 ++match_offset;
93 } else {
94 match_offset = -1;
95 }
96 }
97 }
98 close(mounts_fd);
99 *result = match;
100 return true;
101}
102
103bool MetricsLibrary::IsGuestMode() {
104 char buffer[256];
105 bool result = false;
106 if (!IsDeviceMounted("guestfs",
107 "/proc/mounts",
108 buffer,
109 sizeof(buffer),
110 &result)) {
111 return false;
112 }
113 return result;
114}
115
Ken Mixter4c5daa42010-08-26 18:35:06 -0700116bool MetricsLibrary::AreMetricsEnabled() {
117 static struct stat stat_buffer;
118 time_t this_check_time = time(NULL);
119
120 if (this_check_time != cached_enabled_time_) {
121 cached_enabled_time_ = this_check_time;
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700122 if (stat(consent_file_, &stat_buffer) >= 0 &&
123 !IsGuestMode())
124 cached_enabled_ = true;
125 else
126 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700127 }
128 return cached_enabled_;
129}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700130
131bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700132 if (!AreMetricsEnabled())
133 return true;
134
Darin Petkov8842c8c2011-02-24 12:48:30 -0800135 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
136 O_WRONLY | O_APPEND | O_CREAT,
137 READ_WRITE_ALL_FILE_FLAGS));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700138 // If we failed to open it, return.
Darin Petkov65b01462010-04-14 13:32:20 -0700139 if (chrome_fd < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700140 PrintError("open", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700141 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700142 }
143
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700144 // Need to chmod because open flags are anded with umask. Ignore the
145 // exit code -- a chronos process may fail chmoding because the file
146 // has been created by a root process but that should be OK.
147 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
Darin Petkov65b01462010-04-14 13:32:20 -0700148
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700149 // Grab an exclusive lock to protect Chrome from truncating
150 // underneath us. Keep the file locked as briefly as possible.
Darin Petkov8842c8c2011-02-24 12:48:30 -0800151 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700152 PrintError("flock", uma_events_file_, errno);
Darin Petkov8842c8c2011-02-24 12:48:30 -0800153 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700154 return false;
Darin Petkov65b01462010-04-14 13:32:20 -0700155 }
156
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700157 bool success = true;
Darin Petkov8842c8c2011-02-24 12:48:30 -0800158 if (HANDLE_EINTR(write(chrome_fd, message, length)) != length) {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700159 PrintError("write", uma_events_file_, errno);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700160 success = false;
161 }
Darin Petkov65b01462010-04-14 13:32:20 -0700162
Darin Petkov8842c8c2011-02-24 12:48:30 -0800163 // Close the file and release the lock.
164 HANDLE_EINTR(close(chrome_fd));
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700165 return success;
166}
167
Darin Petkov11b8eb32010-05-18 11:00:59 -0700168int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
169 const char* format, ...) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700170 int32_t message_length;
171 size_t len_size = sizeof(message_length);
172
173 // Format the non-LENGTH contents in the buffer by leaving space for
174 // LENGTH at the start of the buffer.
175 va_list args;
176 va_start(args, format);
177 message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
178 format, args);
179 va_end(args);
180
181 if (message_length < 0) {
182 PrintError("chrome message format error", NULL, 0);
183 return -1;
184 }
185
186 // +1 to account for the trailing \0.
187 message_length += len_size + 1;
188 if (message_length > buffer_size) {
189 PrintError("chrome message too long", NULL, 0);
190 return -1;
191 }
192
193 // Prepend LENGTH to the message.
194 memcpy(buffer, &message_length, len_size);
195 return message_length;
196}
197
Darin Petkovfc91b422010-05-12 13:05:45 -0700198void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700199 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700200}
201
Darin Petkovc2526a12010-04-21 14:24:04 -0700202bool MetricsLibrary::SendToAutotest(const string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700203 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700204 if (autotest_file == NULL) {
205 PrintError("fopen", kAutotestPath, errno);
206 return false;
207 }
208
209 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
210 fclose(autotest_file);
211 return true;
212}
213
Darin Petkov21cd2c52010-05-12 15:26:16 -0700214bool MetricsLibrary::SendToUMA(const string& name, int sample,
215 int min, int max, int nbuckets) {
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700216 // Format the message.
217 char message[kBufferSize];
218 int32_t message_length =
219 FormatChromeMessage(kBufferSize, message,
Darin Petkovc2526a12010-04-21 14:24:04 -0700220 "histogram%c%s %d %d %d %d", '\0',
221 name.c_str(), sample, min, max, nbuckets);
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700222 if (message_length < 0)
223 return false;
224
225 // Send the message.
226 return SendMessageToChrome(message_length, message);
Darin Petkov65b01462010-04-14 13:32:20 -0700227}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700228
Darin Petkov21cd2c52010-05-12 15:26:16 -0700229bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
230 int max) {
Darin Petkov5b7dce12010-04-21 15:45:10 -0700231 // Format the message.
232 char message[kBufferSize];
233 int32_t message_length =
234 FormatChromeMessage(kBufferSize, message,
235 "linearhistogram%c%s %d %d", '\0',
236 name.c_str(), sample, max);
Darin Petkoved824852011-01-06 10:51:47 -0800237 if (message_length < 0)
238 return false;
Darin Petkov5b7dce12010-04-21 15:45:10 -0700239
Darin Petkoved824852011-01-06 10:51:47 -0800240 // Send the message.
241 return SendMessageToChrome(message_length, message);
242}
243
244bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
245 // Format the message.
246 char message[kBufferSize];
247 int32_t message_length =
248 FormatChromeMessage(kBufferSize, message,
249 "useraction%c%s", '\0', action.c_str());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700250 if (message_length < 0)
251 return false;
252
253 // Send the message.
254 return SendMessageToChrome(message_length, message);
255}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800256
257bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
258 // Format the message.
259 char message[kBufferSize];
260 int32_t message_length =
261 FormatChromeMessage(kBufferSize, message,
262 "crash%c%s", '\0', crash_kind);
263
264 if (message_length < 0)
265 return false;
266
267 // Send the message.
268 return SendMessageToChrome(message_length, message);
269}