Chromium Code Reviews| 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_; |
| } |