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; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 27 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 28 | time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 29 | bool MetricsLibrary::cached_enabled_ = false; |
| 30 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 31 | using std::string; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 32 | |
| 33 | // TODO(sosa@chromium.org) - use Chromium logger instead of stderr |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 34 | static void PrintError(const char* message, const char* file, |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 35 | int code) { |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 36 | static const char kProgramName[] = "libmetrics"; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 37 | if (code == 0) { |
| 38 | fprintf(stderr, "%s: %s\n", kProgramName, message); |
| 39 | } else if (file == NULL) { |
| 40 | fprintf(stderr, "%s: ", kProgramName); |
| 41 | perror(message); |
| 42 | } else { |
| 43 | fprintf(stderr, "%s: %s: ", kProgramName, file); |
| 44 | perror(message); |
| 45 | } |
| 46 | } |
| 47 | |
Darin Petkov | 8d3305e | 2011-02-25 14:19:30 -0800 | [diff] [blame] | 48 | // Copied from libbase to avoid pulling in all of libbase just for libmetrics. |
| 49 | static int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 50 | // Allow for partial writes. |
| 51 | ssize_t bytes_written_total = 0; |
| 52 | for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 53 | bytes_written_total += bytes_written_partial) { |
| 54 | bytes_written_partial = |
| 55 | HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 56 | size - bytes_written_total)); |
| 57 | if (bytes_written_partial < 0) |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | return bytes_written_total; |
| 62 | } |
| 63 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 64 | MetricsLibrary::MetricsLibrary() |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 65 | : uma_events_file_(NULL), |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 66 | consent_file_(kConsentFile), |
| 67 | policy_provider_(NULL) {} |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 68 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 69 | // We take buffer and buffer_size as parameters in order to simplify testing |
| 70 | // of various alignments of the |device_name| with |buffer_size|. |
| 71 | bool MetricsLibrary::IsDeviceMounted(const char* device_name, |
| 72 | const char* mounts_file, |
| 73 | char* buffer, |
| 74 | int buffer_size, |
| 75 | bool* result) { |
| 76 | if (buffer == NULL || buffer_size < 1) |
| 77 | return false; |
| 78 | int mounts_fd = open(mounts_file, O_RDONLY); |
| 79 | if (mounts_fd < 0) |
| 80 | return false; |
| 81 | // match_offset describes: |
| 82 | // -1 -- not beginning of line |
| 83 | // 0..strlen(device_name)-1 -- this offset in device_name is next to match |
| 84 | // strlen(device_name) -- matched full name, just need a space. |
| 85 | int match_offset = 0; |
| 86 | bool match = false; |
| 87 | while (!match) { |
| 88 | int read_size = read(mounts_fd, buffer, buffer_size); |
| 89 | if (read_size <= 0) { |
| 90 | if (errno == -EINTR) |
| 91 | continue; |
| 92 | break; |
| 93 | } |
| 94 | for (int i = 0; i < read_size; ++i) { |
| 95 | if (buffer[i] == '\n') { |
| 96 | match_offset = 0; |
| 97 | continue; |
| 98 | } |
| 99 | if (match_offset < 0) { |
| 100 | continue; |
| 101 | } |
| 102 | if (device_name[match_offset] == '\0') { |
| 103 | if (buffer[i] == ' ') { |
| 104 | match = true; |
| 105 | break; |
| 106 | } |
| 107 | match_offset = -1; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | if (buffer[i] == device_name[match_offset]) { |
| 112 | ++match_offset; |
| 113 | } else { |
| 114 | match_offset = -1; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | close(mounts_fd); |
| 119 | *result = match; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | bool MetricsLibrary::IsGuestMode() { |
| 124 | char buffer[256]; |
| 125 | bool result = false; |
| 126 | if (!IsDeviceMounted("guestfs", |
| 127 | "/proc/mounts", |
| 128 | buffer, |
| 129 | sizeof(buffer), |
| 130 | &result)) { |
| 131 | return false; |
| 132 | } |
Arkaitz Ruiz Alvarez | 9f1a774 | 2011-05-26 12:22:22 -0700 | [diff] [blame] | 133 | return result && (access("/var/run/state/logged-in", F_OK) == 0); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 136 | bool MetricsLibrary::AreMetricsEnabled() { |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 137 | static struct stat stat_buffer; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 138 | time_t this_check_time = time(NULL); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 139 | if (this_check_time != cached_enabled_time_) { |
| 140 | cached_enabled_time_ = this_check_time; |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 141 | |
| 142 | if (!policy_provider_.get()) |
| 143 | policy_provider_.reset(new policy::PolicyProvider()); |
Julian Pastarmov | d605a00 | 2013-02-04 17:58:14 +0100 | [diff] [blame] | 144 | policy_provider_->Reload(); |
Julian Pastarmov | 70b7abd | 2011-08-02 16:10:49 +0200 | [diff] [blame] | 145 | // We initialize with the default value which is false and will be preserved |
| 146 | // if the policy is not set. |
| 147 | bool enabled = false; |
| 148 | bool has_policy = false; |
| 149 | if (policy_provider_->device_policy_is_loaded()) { |
| 150 | has_policy = |
| 151 | policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled); |
| 152 | } |
| 153 | // If policy couldn't be loaded or the metrics policy is not set we should |
| 154 | // still respect the consent file if it is present for migration purposes. |
| 155 | // TODO(pastarmovj) |
| 156 | if (!has_policy) { |
| 157 | enabled = stat(consent_file_, &stat_buffer) >= 0; |
| 158 | } |
| 159 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 160 | if (enabled && !IsGuestMode()) |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 161 | cached_enabled_ = true; |
| 162 | else |
| 163 | cached_enabled_ = false; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 164 | } |
| 165 | return cached_enabled_; |
| 166 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 167 | |
| 168 | bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 169 | |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 170 | int chrome_fd = HANDLE_EINTR(open(uma_events_file_, |
| 171 | O_WRONLY | O_APPEND | O_CREAT, |
| 172 | READ_WRITE_ALL_FILE_FLAGS)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 173 | // If we failed to open it, return. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 174 | if (chrome_fd < 0) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 175 | PrintError("open", uma_events_file_, errno); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 176 | return false; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 179 | // Need to chmod because open flags are anded with umask. Ignore the |
| 180 | // exit code -- a chronos process may fail chmoding because the file |
| 181 | // has been created by a root process but that should be OK. |
| 182 | fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 183 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 184 | // Grab an exclusive lock to protect Chrome from truncating |
| 185 | // underneath us. Keep the file locked as briefly as possible. |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 186 | if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 187 | PrintError("flock", uma_events_file_, errno); |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 188 | HANDLE_EINTR(close(chrome_fd)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 189 | return false; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 192 | bool success = true; |
Darin Petkov | 8d3305e | 2011-02-25 14:19:30 -0800 | [diff] [blame] | 193 | if (WriteFileDescriptor(chrome_fd, message, length) != length) { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 194 | PrintError("write", uma_events_file_, errno); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 195 | success = false; |
| 196 | } |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 197 | |
Darin Petkov | 8842c8c | 2011-02-24 12:48:30 -0800 | [diff] [blame] | 198 | // Close the file and release the lock. |
| 199 | HANDLE_EINTR(close(chrome_fd)); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 200 | return success; |
| 201 | } |
| 202 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 203 | int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer, |
| 204 | const char* format, ...) { |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 205 | int32_t message_length; |
| 206 | size_t len_size = sizeof(message_length); |
| 207 | |
| 208 | // Format the non-LENGTH contents in the buffer by leaving space for |
| 209 | // LENGTH at the start of the buffer. |
| 210 | va_list args; |
| 211 | va_start(args, format); |
| 212 | message_length = vsnprintf(&buffer[len_size], buffer_size - len_size, |
| 213 | format, args); |
| 214 | va_end(args); |
| 215 | |
| 216 | if (message_length < 0) { |
| 217 | PrintError("chrome message format error", NULL, 0); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | // +1 to account for the trailing \0. |
| 222 | message_length += len_size + 1; |
| 223 | if (message_length > buffer_size) { |
| 224 | PrintError("chrome message too long", NULL, 0); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | // Prepend LENGTH to the message. |
| 229 | memcpy(buffer, &message_length, len_size); |
| 230 | return message_length; |
| 231 | } |
| 232 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 233 | void MetricsLibrary::Init() { |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 234 | uma_events_file_ = kUMAEventsPath; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 237 | bool MetricsLibrary::SendToAutotest(const string& name, int value) { |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 238 | FILE* autotest_file = fopen(kAutotestPath, "a+"); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 239 | if (autotest_file == NULL) { |
| 240 | PrintError("fopen", kAutotestPath, errno); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | fprintf(autotest_file, "%s=%d\n", name.c_str(), value); |
| 245 | fclose(autotest_file); |
| 246 | return true; |
| 247 | } |
| 248 | |
Darin Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 249 | bool MetricsLibrary::SendToUMA(const string& name, int sample, |
| 250 | int min, int max, int nbuckets) { |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 251 | // Format the message. |
| 252 | char message[kBufferSize]; |
| 253 | int32_t message_length = |
| 254 | FormatChromeMessage(kBufferSize, message, |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 255 | "histogram%c%s %d %d %d %d", '\0', |
| 256 | name.c_str(), sample, min, max, nbuckets); |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 257 | if (message_length < 0) |
| 258 | return false; |
| 259 | |
| 260 | // Send the message. |
| 261 | return SendMessageToChrome(message_length, message); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 262 | } |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 263 | |
Darin Petkov | 21cd2c5 | 2010-05-12 15:26:16 -0700 | [diff] [blame] | 264 | bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample, |
| 265 | int max) { |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 266 | // Format the message. |
| 267 | char message[kBufferSize]; |
| 268 | int32_t message_length = |
| 269 | FormatChromeMessage(kBufferSize, message, |
| 270 | "linearhistogram%c%s %d %d", '\0', |
| 271 | name.c_str(), sample, max); |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 272 | if (message_length < 0) |
| 273 | return false; |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 274 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 275 | // Send the message. |
| 276 | return SendMessageToChrome(message_length, message); |
| 277 | } |
| 278 | |
| 279 | bool MetricsLibrary::SendUserActionToUMA(const std::string& action) { |
| 280 | // Format the message. |
| 281 | char message[kBufferSize]; |
| 282 | int32_t message_length = |
| 283 | FormatChromeMessage(kBufferSize, message, |
| 284 | "useraction%c%s", '\0', action.c_str()); |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 285 | if (message_length < 0) |
| 286 | return false; |
| 287 | |
| 288 | // Send the message. |
| 289 | return SendMessageToChrome(message_length, message); |
| 290 | } |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 291 | |
| 292 | bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) { |
| 293 | // Format the message. |
| 294 | char message[kBufferSize]; |
| 295 | int32_t message_length = |
| 296 | FormatChromeMessage(kBufferSize, message, |
| 297 | "crash%c%s", '\0', crash_kind); |
| 298 | |
| 299 | if (message_length < 0) |
| 300 | return false; |
| 301 | |
| 302 | // Send the message. |
| 303 | return SendMessageToChrome(message_length, message); |
| 304 | } |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 305 | |
| 306 | void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) { |
| 307 | policy_provider_.reset(provider); |
| 308 | } |