Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 5 | #include "metrics_library.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <sys/file.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 9 | #include <sys/stat.h> |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 10 | |
| 11 | #include <cstdarg> |
| 12 | #include <cstdio> |
| 13 | #include <cstring> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 14 | |
Chris Masone | e10b548 | 2013-02-14 12:15:35 -0800 | [diff] [blame] | 15 | // HANDLE_EINTR macro, no libbase required. |
| 16 | #include <base/posix/eintr_wrapper.h> |
| 17 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 18 | #include "policy/device_policy.h" |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 19 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 20 | #define READ_WRITE_ALL_FILE_FLAGS \ |
| 21 | (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| 22 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 23 | static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; |
| 24 | static const char kUMAEventsPath[] = "/var/log/metrics/uma-events"; |
| 25 | static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 26 | static const int32_t kBufferSize = 1024; |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 27 | static const char kCrosEventHistogramName[] = "Platform.CrOSEvent"; |
| 28 | static const int kCrosEventHistogramMax = 100; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 29 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 30 | time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 31 | bool MetricsLibrary::cached_enabled_ = false; |
| 32 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 33 | using std::string; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 34 | |
| 35 | // TODO(sosa@chromium.org) - use Chromium logger instead of stderr |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 36 | static void PrintError(const char* message, const char* file, |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 37 | int code) { |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 38 | static const char kProgramName[] = "libmetrics"; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 39 | 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 Petkov | 8d3305e | 2011-02-25 14:19:30 -0800 | [diff] [blame] | 50 | // Copied from libbase to avoid pulling in all of libbase just for libmetrics. |
| 51 | static 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 Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 66 | MetricsLibrary::MetricsLibrary() |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 67 | : uma_events_file_(NULL), |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 68 | consent_file_(kConsentFile), |
| 69 | policy_provider_(NULL) {} |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 70 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 71 | // We take buffer and buffer_size as parameters in order to simplify testing |
| 72 | // of various alignments of the |device_name| with |buffer_size|. |
| 73 | bool 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 | |
| 125 | bool 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 Alvarez | 9f1a774 | 2011-05-26 12:22:22 -0700 | [diff] [blame] | 135 | return result && (access("/var/run/state/logged-in", F_OK) == 0); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 138 | bool MetricsLibrary::AreMetricsEnabled() { |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 139 | static struct stat stat_buffer; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 140 | time_t this_check_time = time(NULL); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 141 | if (this_check_time != cached_enabled_time_) { |
| 142 | cached_enabled_time_ = this_check_time; |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 143 | |
| 144 | if (!policy_provider_.get()) |
| 145 | policy_provider_.reset(new policy::PolicyProvider()); |
Julian Pastarmov | d605a00 | 2013-02-04 17:58:14 +0100 | [diff] [blame] | 146 | policy_provider_->Reload(); |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 147 | // 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 Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 162 | if (enabled && !IsGuestMode()) |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 163 | cached_enabled_ = true; |
| 164 | else |
| 165 | cached_enabled_ = false; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 166 | } |
| 167 | return cached_enabled_; |
| 168 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 169 | |
| 170 | bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 171 | |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 172 | int chrome_fd = HANDLE_EINTR(open(uma_events_file_, |
| 173 | O_WRONLY | O_APPEND | O_CREAT, |
| 174 | READ_WRITE_ALL_FILE_FLAGS)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 175 | // If we failed to open it, return. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 176 | if (chrome_fd < 0) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 177 | PrintError("open", uma_events_file_, errno); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 178 | return false; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 181 | // 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 185 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 186 | // Grab an exclusive lock to protect Chrome from truncating |
| 187 | // underneath us. Keep the file locked as briefly as possible. |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 188 | if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 189 | PrintError("flock", uma_events_file_, errno); |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 190 | HANDLE_EINTR(close(chrome_fd)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 191 | return false; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 194 | bool success = true; |
Darin Petkov | 8d3305e | 2011-02-25 14:19:30 -0800 | [diff] [blame] | 195 | if (WriteFileDescriptor(chrome_fd, message, length) != length) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 196 | PrintError("write", uma_events_file_, errno); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 197 | success = false; |
| 198 | } |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 199 | |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 200 | // Close the file and release the lock. |
| 201 | HANDLE_EINTR(close(chrome_fd)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 202 | return success; |
| 203 | } |
| 204 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 205 | int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer, |
| 206 | const char* format, ...) { |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 207 | 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 235 | void MetricsLibrary::Init() { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 236 | uma_events_file_ = kUMAEventsPath; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 239 | bool MetricsLibrary::SendToAutotest(const string& name, int value) { |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 240 | FILE* autotest_file = fopen(kAutotestPath, "a+"); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 241 | 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 Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 251 | bool MetricsLibrary::SendToUMA(const string& name, int sample, |
| 252 | int min, int max, int nbuckets) { |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 253 | // Format the message. |
| 254 | char message[kBufferSize]; |
| 255 | int32_t message_length = |
| 256 | FormatChromeMessage(kBufferSize, message, |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 257 | "histogram%c%s %d %d %d %d", '\0', |
| 258 | name.c_str(), sample, min, max, nbuckets); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 259 | if (message_length < 0) |
| 260 | return false; |
| 261 | |
| 262 | // Send the message. |
| 263 | return SendMessageToChrome(message_length, message); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 264 | } |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 265 | |
Darin Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 266 | bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample, |
| 267 | int max) { |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 268 | // 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 Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 274 | if (message_length < 0) |
| 275 | return false; |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 276 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 277 | // Send the message. |
| 278 | return SendMessageToChrome(message_length, message); |
| 279 | } |
| 280 | |
| 281 | bool 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 Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 287 | if (message_length < 0) |
| 288 | return false; |
| 289 | |
| 290 | // Send the message. |
| 291 | return SendMessageToChrome(message_length, message); |
| 292 | } |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 293 | |
| 294 | bool 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 Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 307 | |
| 308 | void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) { |
| 309 | policy_provider_.reset(provider); |
| 310 | } |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 311 | |
| 312 | bool 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; |
Grant Grundler | 4b5164b | 2013-03-26 17:13:49 -0700 | [diff] [blame^] | 322 | } else if (event.compare("HwWatchdogReboot") == 0) { |
| 323 | n = 1; |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 324 | } else { |
| 325 | return false; |
| 326 | } |
| 327 | return SendEnumToUMA(kCrosEventHistogramName, n, kCrosEventHistogramMax); |
| 328 | } |