Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1052)

Unified Diff: metrics_library.cc

Issue 3571009: metrics: Add guest mode detection to metrics library and client (Closed) Base URL: http://git.chromium.org/git/metrics.git
Patch Set: Fix metrics_client semantics bug Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: metrics_library.cc
diff --git a/metrics_library.cc b/metrics_library.cc
index 3e3987d75e144598e4bf3d8bb83b8db7c92a99fa..7adbe1b618c0e5440ccdeb0a165bdaabc28f11ab 100644
--- a/metrics_library.cc
+++ b/metrics_library.cc
@@ -44,13 +44,83 @@ MetricsLibrary::MetricsLibrary()
: uma_events_file_(NULL),
consent_file_(kConsentFile) {}
+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;
petkov 2010/10/01 16:13:09 indent is off?
+ int mounts_fd = open(mounts_file, O_RDONLY);
+ if (mounts_fd < 0) {
+ return false;
+ }
+ // match_state 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_state = 0;
petkov 2010/10/01 16:13:09 If you rename match_state to match_index or match_
+ 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_state = 0;
+ continue;
+ }
+ if (match_state < 0) {
+ continue;
+ }
+ if (device_name[match_state] == '\0') {
+ if (buffer[i] == ' ') {
+ match = true;
+ break;
+ }
+ match_state = -1;
+ continue;
+ }
+
+ if (buffer[i] == device_name[match_state]) {
+ ++match_state;
+ } else {
+ match_state = -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;
+ }
+ return result;
+}
+
bool MetricsLibrary::AreMetricsEnabled() {
static struct stat stat_buffer;
time_t this_check_time = time(NULL);
if (this_check_time != cached_enabled_time_) {
cached_enabled_time_ = this_check_time;
- cached_enabled_ = (stat(consent_file_, &stat_buffer) >= 0);
+ if (stat(consent_file_, &stat_buffer) >= 0 &&
+ !IsGuestMode())
+ cached_enabled_ = true;
+ else
+ cached_enabled_ = false;
}
return cached_enabled_;
}
« metrics_library.h ('K') | « metrics_library.h ('k') | metrics_library_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698