2010-04-14 22:32:20 +02:00
|
|
|
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "metrics_library.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/file.h>
|
2010-08-27 03:35:06 +02:00
|
|
|
#include <sys/stat.h>
|
2010-04-16 01:40:23 +02:00
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2011-02-25 23:19:30 +01:00
|
|
|
#include "base/eintr_wrapper.h" // HANDLE_EINTR macro, no libbase required.
|
2011-07-22 23:59:51 +02:00
|
|
|
#include "policy/device_policy.h"
|
2011-02-24 21:48:30 +01:00
|
|
|
|
2010-04-14 22:32:20 +02:00
|
|
|
#define READ_WRITE_ALL_FILE_FLAGS \
|
|
|
|
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
|
|
|
|
static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
|
|
|
|
static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
|
2010-04-16 01:40:23 +02:00
|
|
|
static const int32_t kBufferSize = 1024;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
time_t MetricsLibrary::cached_enabled_time_ = 0;
|
|
|
|
bool MetricsLibrary::cached_enabled_ = false;
|
|
|
|
|
2010-06-05 00:01:19 +02:00
|
|
|
using std::string;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
|
|
|
// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
|
2010-05-18 20:00:59 +02:00
|
|
|
static void PrintError(const char* message, const char* file,
|
2010-04-16 01:40:23 +02:00
|
|
|
int code) {
|
2010-06-05 00:01:19 +02:00
|
|
|
static const char kProgramName[] = "libmetrics";
|
2010-04-14 22:32:20 +02:00
|
|
|
if (code == 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", kProgramName, message);
|
|
|
|
} else if (file == NULL) {
|
|
|
|
fprintf(stderr, "%s: ", kProgramName);
|
|
|
|
perror(message);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %s: ", kProgramName, file);
|
|
|
|
perror(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-25 23:19:30 +01:00
|
|
|
// Copied from libbase to avoid pulling in all of libbase just for libmetrics.
|
|
|
|
static int WriteFileDescriptor(const int fd, const char* data, int size) {
|
|
|
|
// Allow for partial writes.
|
|
|
|
ssize_t bytes_written_total = 0;
|
|
|
|
for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
|
|
|
|
bytes_written_total += bytes_written_partial) {
|
|
|
|
bytes_written_partial =
|
|
|
|
HANDLE_EINTR(write(fd, data + bytes_written_total,
|
|
|
|
size - bytes_written_total));
|
|
|
|
if (bytes_written_partial < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_written_total;
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
MetricsLibrary::MetricsLibrary()
|
2010-08-27 03:35:06 +02:00
|
|
|
: uma_events_file_(NULL),
|
2011-07-22 23:59:51 +02:00
|
|
|
consent_file_(kConsentFile),
|
|
|
|
policy_provider_(NULL) {}
|
2010-08-27 03:35:06 +02:00
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
// We take buffer and buffer_size as parameters in order to simplify testing
|
|
|
|
// of various alignments of the |device_name| with |buffer_size|.
|
|
|
|
bool MetricsLibrary::IsDeviceMounted(const char* device_name,
|
|
|
|
const char* mounts_file,
|
|
|
|
char* buffer,
|
|
|
|
int buffer_size,
|
|
|
|
bool* result) {
|
|
|
|
if (buffer == NULL || buffer_size < 1)
|
|
|
|
return false;
|
|
|
|
int mounts_fd = open(mounts_file, O_RDONLY);
|
|
|
|
if (mounts_fd < 0)
|
|
|
|
return false;
|
|
|
|
// match_offset describes:
|
|
|
|
// -1 -- not beginning of line
|
|
|
|
// 0..strlen(device_name)-1 -- this offset in device_name is next to match
|
|
|
|
// strlen(device_name) -- matched full name, just need a space.
|
|
|
|
int match_offset = 0;
|
|
|
|
bool match = false;
|
|
|
|
while (!match) {
|
|
|
|
int read_size = read(mounts_fd, buffer, buffer_size);
|
|
|
|
if (read_size <= 0) {
|
|
|
|
if (errno == -EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < read_size; ++i) {
|
|
|
|
if (buffer[i] == '\n') {
|
|
|
|
match_offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (match_offset < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (device_name[match_offset] == '\0') {
|
|
|
|
if (buffer[i] == ' ') {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match_offset = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == device_name[match_offset]) {
|
|
|
|
++match_offset;
|
|
|
|
} else {
|
|
|
|
match_offset = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(mounts_fd);
|
|
|
|
*result = match;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetricsLibrary::IsGuestMode() {
|
|
|
|
char buffer[256];
|
|
|
|
bool result = false;
|
|
|
|
if (!IsDeviceMounted("guestfs",
|
|
|
|
"/proc/mounts",
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
&result)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-26 21:22:22 +02:00
|
|
|
return result && (access("/var/run/state/logged-in", F_OK) == 0);
|
2010-10-02 00:38:42 +02:00
|
|
|
}
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
bool MetricsLibrary::AreMetricsEnabled() {
|
2011-08-02 16:10:49 +02:00
|
|
|
static struct stat stat_buffer;
|
2010-08-27 03:35:06 +02:00
|
|
|
time_t this_check_time = time(NULL);
|
|
|
|
if (this_check_time != cached_enabled_time_) {
|
|
|
|
cached_enabled_time_ = this_check_time;
|
2011-08-02 16:10:49 +02:00
|
|
|
|
|
|
|
if (!policy_provider_.get())
|
|
|
|
policy_provider_.reset(new policy::PolicyProvider());
|
2013-02-04 17:58:14 +01:00
|
|
|
policy_provider_->Reload();
|
2011-08-02 16:10:49 +02:00
|
|
|
// We initialize with the default value which is false and will be preserved
|
|
|
|
// if the policy is not set.
|
|
|
|
bool enabled = false;
|
|
|
|
bool has_policy = false;
|
|
|
|
if (policy_provider_->device_policy_is_loaded()) {
|
|
|
|
has_policy =
|
|
|
|
policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
|
|
|
|
}
|
|
|
|
// If policy couldn't be loaded or the metrics policy is not set we should
|
|
|
|
// still respect the consent file if it is present for migration purposes.
|
|
|
|
// TODO(pastarmovj)
|
|
|
|
if (!has_policy) {
|
|
|
|
enabled = stat(consent_file_, &stat_buffer) >= 0;
|
|
|
|
}
|
|
|
|
|
2011-07-22 23:59:51 +02:00
|
|
|
if (enabled && !IsGuestMode())
|
2010-10-02 00:38:42 +02:00
|
|
|
cached_enabled_ = true;
|
|
|
|
else
|
|
|
|
cached_enabled_ = false;
|
2010-08-27 03:35:06 +02:00
|
|
|
}
|
|
|
|
return cached_enabled_;
|
|
|
|
}
|
2010-05-18 20:00:59 +02:00
|
|
|
|
|
|
|
bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
|
2010-08-27 03:35:06 +02:00
|
|
|
|
2011-02-24 21:48:30 +01:00
|
|
|
int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
|
|
|
|
O_WRONLY | O_APPEND | O_CREAT,
|
|
|
|
READ_WRITE_ALL_FILE_FLAGS));
|
2010-04-16 01:40:23 +02:00
|
|
|
// If we failed to open it, return.
|
2010-04-14 22:32:20 +02:00
|
|
|
if (chrome_fd < 0) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("open", uma_events_file_, errno);
|
2010-04-16 01:40:23 +02:00
|
|
|
return false;
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Need to chmod because open flags are anded with umask. Ignore the
|
|
|
|
// exit code -- a chronos process may fail chmoding because the file
|
|
|
|
// has been created by a root process but that should be OK.
|
|
|
|
fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Grab an exclusive lock to protect Chrome from truncating
|
|
|
|
// underneath us. Keep the file locked as briefly as possible.
|
2011-02-24 21:48:30 +01:00
|
|
|
if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("flock", uma_events_file_, errno);
|
2011-02-24 21:48:30 +01:00
|
|
|
HANDLE_EINTR(close(chrome_fd));
|
2010-04-16 01:40:23 +02:00
|
|
|
return false;
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
bool success = true;
|
2011-02-25 23:19:30 +01:00
|
|
|
if (WriteFileDescriptor(chrome_fd, message, length) != length) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("write", uma_events_file_, errno);
|
2010-04-16 01:40:23 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2011-02-24 21:48:30 +01:00
|
|
|
// Close the file and release the lock.
|
|
|
|
HANDLE_EINTR(close(chrome_fd));
|
2010-04-16 01:40:23 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
|
|
|
|
const char* format, ...) {
|
2010-04-16 01:40:23 +02:00
|
|
|
int32_t message_length;
|
|
|
|
size_t len_size = sizeof(message_length);
|
|
|
|
|
|
|
|
// Format the non-LENGTH contents in the buffer by leaving space for
|
|
|
|
// LENGTH at the start of the buffer.
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
|
|
|
|
format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (message_length < 0) {
|
|
|
|
PrintError("chrome message format error", NULL, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// +1 to account for the trailing \0.
|
|
|
|
message_length += len_size + 1;
|
|
|
|
if (message_length > buffer_size) {
|
|
|
|
PrintError("chrome message too long", NULL, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend LENGTH to the message.
|
|
|
|
memcpy(buffer, &message_length, len_size);
|
|
|
|
return message_length;
|
|
|
|
}
|
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
void MetricsLibrary::Init() {
|
2010-05-18 20:00:59 +02:00
|
|
|
uma_events_file_ = kUMAEventsPath;
|
2010-05-12 22:05:45 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 23:24:04 +02:00
|
|
|
bool MetricsLibrary::SendToAutotest(const string& name, int value) {
|
2010-06-05 00:01:19 +02:00
|
|
|
FILE* autotest_file = fopen(kAutotestPath, "a+");
|
2010-04-16 01:40:23 +02:00
|
|
|
if (autotest_file == NULL) {
|
|
|
|
PrintError("fopen", kAutotestPath, errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
|
|
|
|
fclose(autotest_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-13 00:26:16 +02:00
|
|
|
bool MetricsLibrary::SendToUMA(const string& name, int sample,
|
|
|
|
int min, int max, int nbuckets) {
|
2010-04-16 01:40:23 +02:00
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
2010-04-21 23:24:04 +02:00
|
|
|
"histogram%c%s %d %d %d %d", '\0',
|
|
|
|
name.c_str(), sample, min, max, nbuckets);
|
2010-04-16 01:40:23 +02:00
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
2010-04-22 00:45:10 +02:00
|
|
|
|
2010-05-13 00:26:16 +02:00
|
|
|
bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
|
|
|
|
int max) {
|
2010-04-22 00:45:10 +02:00
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
|
|
|
"linearhistogram%c%s %d %d", '\0',
|
|
|
|
name.c_str(), sample, max);
|
2011-01-06 19:51:47 +01:00
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
2010-04-22 00:45:10 +02:00
|
|
|
|
2011-01-06 19:51:47 +01:00
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
|
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
|
|
|
"useraction%c%s", '\0', action.c_str());
|
2010-04-22 00:45:10 +02:00
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
|
|
|
}
|
2011-01-22 15:15:56 +01:00
|
|
|
|
|
|
|
bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
|
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
|
|
|
"crash%c%s", '\0', crash_kind);
|
|
|
|
|
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
|
|
|
}
|
2011-07-22 23:59:51 +02:00
|
|
|
|
|
|
|
void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
|
|
|
|
policy_provider_.reset(provider);
|
|
|
|
}
|