libmetrics -- release the shared file lock when closing, handle EINTR.

BUG=chromium-os:11125
TEST=unit tests, tested on device

Change-Id: I126af62f77e4fd0f098d441038f8dc94c0020ac2

Review URL: http://codereview.chromium.org/6576048
This commit is contained in:
Darin Petkov 2011-02-24 12:48:30 -08:00
parent c88e42dea9
commit 8842c8c42d
1 changed files with 10 additions and 12 deletions

View File

@ -12,6 +12,8 @@
#include <cstdio>
#include <cstring>
#include <base/eintr_wrapper.h>
#define READ_WRITE_ALL_FILE_FLAGS \
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
@ -130,9 +132,9 @@ bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
if (!AreMetricsEnabled())
return true;
int chrome_fd = open(uma_events_file_,
O_WRONLY | O_APPEND | O_CREAT,
READ_WRITE_ALL_FILE_FLAGS);
int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
O_WRONLY | O_APPEND | O_CREAT,
READ_WRITE_ALL_FILE_FLAGS));
// If we failed to open it, return.
if (chrome_fd < 0) {
PrintError("open", uma_events_file_, errno);
@ -146,24 +148,20 @@ bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
// Grab an exclusive lock to protect Chrome from truncating
// underneath us. Keep the file locked as briefly as possible.
if (flock(chrome_fd, LOCK_EX) < 0) {
if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
PrintError("flock", uma_events_file_, errno);
close(chrome_fd);
HANDLE_EINTR(close(chrome_fd));
return false;
}
bool success = true;
if (write(chrome_fd, message, length) != length) {
if (HANDLE_EINTR(write(chrome_fd, message, length)) != length) {
PrintError("write", uma_events_file_, errno);
success = false;
}
// Release the file lock and close file.
if (flock(chrome_fd, LOCK_UN) < 0) {
PrintError("unlock", uma_events_file_, errno);
success = false;
}
close(chrome_fd);
// Close the file and release the lock.
HANDLE_EINTR(close(chrome_fd));
return success;
}