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

Side by Side 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, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "metrics_library.h" 5 #include "metrics_library.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <sys/file.h> 8 #include <sys/file.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 10
(...skipping 26 matching lines...) Expand all
37 } else { 37 } else {
38 fprintf(stderr, "%s: %s: ", kProgramName, file); 38 fprintf(stderr, "%s: %s: ", kProgramName, file);
39 perror(message); 39 perror(message);
40 } 40 }
41 } 41 }
42 42
43 MetricsLibrary::MetricsLibrary() 43 MetricsLibrary::MetricsLibrary()
44 : uma_events_file_(NULL), 44 : uma_events_file_(NULL),
45 consent_file_(kConsentFile) {} 45 consent_file_(kConsentFile) {}
46 46
47 bool MetricsLibrary::IsDeviceMounted(const char* device_name,
48 const char* mounts_file,
49 char* buffer,
50 int buffer_size,
51 bool* result) {
52 if (buffer == NULL || buffer_size < 1)
53 return false;
petkov 2010/10/01 16:13:09 indent is off?
54 int mounts_fd = open(mounts_file, O_RDONLY);
55 if (mounts_fd < 0) {
56 return false;
57 }
58 // match_state describes:
59 // -1 -- not beginning of line
60 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
61 // strlen(device_name) -- matched full name, just need a space.
62 int match_state = 0;
petkov 2010/10/01 16:13:09 If you rename match_state to match_index or match_
63 bool match = false;
64 while (!match) {
65 int read_size = read(mounts_fd, buffer, buffer_size);
66 if (read_size <= 0) {
67 if (errno == -EINTR)
68 continue;
69 break;
70 }
71 for (int i = 0; i < read_size; ++i) {
72 if (buffer[i] == '\n') {
73 match_state = 0;
74 continue;
75 }
76 if (match_state < 0) {
77 continue;
78 }
79 if (device_name[match_state] == '\0') {
80 if (buffer[i] == ' ') {
81 match = true;
82 break;
83 }
84 match_state = -1;
85 continue;
86 }
87
88 if (buffer[i] == device_name[match_state]) {
89 ++match_state;
90 } else {
91 match_state = -1;
92 }
93 }
94 }
95 close(mounts_fd);
96 *result = match;
97 return true;
98 }
99
100 bool MetricsLibrary::IsGuestMode() {
101 char buffer[256];
102 bool result = false;
103 if (!IsDeviceMounted("guestfs",
104 "/proc/mounts",
105 buffer,
106 sizeof(buffer),
107 &result)) {
108 return false;
109 }
110 return result;
111 }
112
47 bool MetricsLibrary::AreMetricsEnabled() { 113 bool MetricsLibrary::AreMetricsEnabled() {
48 static struct stat stat_buffer; 114 static struct stat stat_buffer;
49 time_t this_check_time = time(NULL); 115 time_t this_check_time = time(NULL);
50 116
51 if (this_check_time != cached_enabled_time_) { 117 if (this_check_time != cached_enabled_time_) {
52 cached_enabled_time_ = this_check_time; 118 cached_enabled_time_ = this_check_time;
53 cached_enabled_ = (stat(consent_file_, &stat_buffer) >= 0); 119 if (stat(consent_file_, &stat_buffer) >= 0 &&
120 !IsGuestMode())
121 cached_enabled_ = true;
122 else
123 cached_enabled_ = false;
54 } 124 }
55 return cached_enabled_; 125 return cached_enabled_;
56 } 126 }
57 127
58 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { 128 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
59 if (!AreMetricsEnabled()) 129 if (!AreMetricsEnabled())
60 return true; 130 return true;
61 131
62 int chrome_fd = open(uma_events_file_, 132 int chrome_fd = open(uma_events_file_,
63 O_WRONLY | O_APPEND | O_CREAT, 133 O_WRONLY | O_APPEND | O_CREAT,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 FormatChromeMessage(kBufferSize, message, 236 FormatChromeMessage(kBufferSize, message,
167 "linearhistogram%c%s %d %d", '\0', 237 "linearhistogram%c%s %d %d", '\0',
168 name.c_str(), sample, max); 238 name.c_str(), sample, max);
169 239
170 if (message_length < 0) 240 if (message_length < 0)
171 return false; 241 return false;
172 242
173 // Send the message. 243 // Send the message.
174 return SendMessageToChrome(message_length, message); 244 return SendMessageToChrome(message_length, message);
175 } 245 }
OLDNEW
« 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