| OLD | NEW |
| 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 |
| 11 #include <cstdarg> | 11 #include <cstdarg> |
| 12 #include <cstdio> | 12 #include <cstdio> |
| 13 #include <cstring> | 13 #include <cstring> |
| 14 | 14 |
| 15 #include <base/eintr_wrapper.h> | 15 #include "base/eintr_wrapper.h" // HANDLE_EINTR macro, no libbase required. |
| 16 | 16 |
| 17 #define READ_WRITE_ALL_FILE_FLAGS \ | 17 #define READ_WRITE_ALL_FILE_FLAGS \ |
| 18 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 18 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| 19 | 19 |
| 20 static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; | 20 static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; |
| 21 static const char kUMAEventsPath[] = "/var/log/metrics/uma-events"; | 21 static const char kUMAEventsPath[] = "/var/log/metrics/uma-events"; |
| 22 static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; | 22 static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; |
| 23 static const int32_t kBufferSize = 1024; | 23 static const int32_t kBufferSize = 1024; |
| 24 | 24 |
| 25 time_t MetricsLibrary::cached_enabled_time_ = 0; | 25 time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 26 bool MetricsLibrary::cached_enabled_ = false; | 26 bool MetricsLibrary::cached_enabled_ = false; |
| 27 | 27 |
| 28 using std::string; | 28 using std::string; |
| 29 | 29 |
| 30 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr | 30 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr |
| 31 static void PrintError(const char* message, const char* file, | 31 static void PrintError(const char* message, const char* file, |
| 32 int code) { | 32 int code) { |
| 33 static const char kProgramName[] = "libmetrics"; | 33 static const char kProgramName[] = "libmetrics"; |
| 34 if (code == 0) { | 34 if (code == 0) { |
| 35 fprintf(stderr, "%s: %s\n", kProgramName, message); | 35 fprintf(stderr, "%s: %s\n", kProgramName, message); |
| 36 } else if (file == NULL) { | 36 } else if (file == NULL) { |
| 37 fprintf(stderr, "%s: ", kProgramName); | 37 fprintf(stderr, "%s: ", kProgramName); |
| 38 perror(message); | 38 perror(message); |
| 39 } else { | 39 } else { |
| 40 fprintf(stderr, "%s: %s: ", kProgramName, file); | 40 fprintf(stderr, "%s: %s: ", kProgramName, file); |
| 41 perror(message); | 41 perror(message); |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Copied from libbase to avoid pulling in all of libbase just for libmetrics. |
| 46 static int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 47 // Allow for partial writes. |
| 48 ssize_t bytes_written_total = 0; |
| 49 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 50 bytes_written_total += bytes_written_partial) { |
| 51 bytes_written_partial = |
| 52 HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 53 size - bytes_written_total)); |
| 54 if (bytes_written_partial < 0) |
| 55 return -1; |
| 56 } |
| 57 |
| 58 return bytes_written_total; |
| 59 } |
| 60 |
| 45 MetricsLibrary::MetricsLibrary() | 61 MetricsLibrary::MetricsLibrary() |
| 46 : uma_events_file_(NULL), | 62 : uma_events_file_(NULL), |
| 47 consent_file_(kConsentFile) {} | 63 consent_file_(kConsentFile) {} |
| 48 | 64 |
| 49 // We take buffer and buffer_size as parameters in order to simplify testing | 65 // We take buffer and buffer_size as parameters in order to simplify testing |
| 50 // of various alignments of the |device_name| with |buffer_size|. | 66 // of various alignments of the |device_name| with |buffer_size|. |
| 51 bool MetricsLibrary::IsDeviceMounted(const char* device_name, | 67 bool MetricsLibrary::IsDeviceMounted(const char* device_name, |
| 52 const char* mounts_file, | 68 const char* mounts_file, |
| 53 char* buffer, | 69 char* buffer, |
| 54 int buffer_size, | 70 int buffer_size, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 164 |
| 149 // Grab an exclusive lock to protect Chrome from truncating | 165 // Grab an exclusive lock to protect Chrome from truncating |
| 150 // underneath us. Keep the file locked as briefly as possible. | 166 // underneath us. Keep the file locked as briefly as possible. |
| 151 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) { | 167 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) { |
| 152 PrintError("flock", uma_events_file_, errno); | 168 PrintError("flock", uma_events_file_, errno); |
| 153 HANDLE_EINTR(close(chrome_fd)); | 169 HANDLE_EINTR(close(chrome_fd)); |
| 154 return false; | 170 return false; |
| 155 } | 171 } |
| 156 | 172 |
| 157 bool success = true; | 173 bool success = true; |
| 158 if (HANDLE_EINTR(write(chrome_fd, message, length)) != length) { | 174 if (WriteFileDescriptor(chrome_fd, message, length) != length) { |
| 159 PrintError("write", uma_events_file_, errno); | 175 PrintError("write", uma_events_file_, errno); |
| 160 success = false; | 176 success = false; |
| 161 } | 177 } |
| 162 | 178 |
| 163 // Close the file and release the lock. | 179 // Close the file and release the lock. |
| 164 HANDLE_EINTR(close(chrome_fd)); | 180 HANDLE_EINTR(close(chrome_fd)); |
| 165 return success; | 181 return success; |
| 166 } | 182 } |
| 167 | 183 |
| 168 int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer, | 184 int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 int32_t message_length = | 276 int32_t message_length = |
| 261 FormatChromeMessage(kBufferSize, message, | 277 FormatChromeMessage(kBufferSize, message, |
| 262 "crash%c%s", '\0', crash_kind); | 278 "crash%c%s", '\0', crash_kind); |
| 263 | 279 |
| 264 if (message_length < 0) | 280 if (message_length < 0) |
| 265 return false; | 281 return false; |
| 266 | 282 |
| 267 // Send the message. | 283 // Send the message. |
| 268 return SendMessageToChrome(message_length, message); | 284 return SendMessageToChrome(message_length, message); |
| 269 } | 285 } |
| OLD | NEW |