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

Side by Side Diff: metrics_library.cc

Issue 6576048: libmetrics -- release the shared file lock when closing, handle EINTR. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/metrics.git@master
Patch Set: Created 9 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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>
16
15 #define READ_WRITE_ALL_FILE_FLAGS \ 17 #define READ_WRITE_ALL_FILE_FLAGS \
16 (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)
17 19
18 static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; 20 static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
19 static const char kUMAEventsPath[] = "/var/log/metrics/uma-events"; 21 static const char kUMAEventsPath[] = "/var/log/metrics/uma-events";
20 static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; 22 static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
21 static const int32_t kBufferSize = 1024; 23 static const int32_t kBufferSize = 1024;
22 24
23 time_t MetricsLibrary::cached_enabled_time_ = 0; 25 time_t MetricsLibrary::cached_enabled_time_ = 0;
24 bool MetricsLibrary::cached_enabled_ = false; 26 bool MetricsLibrary::cached_enabled_ = false;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 else 125 else
124 cached_enabled_ = false; 126 cached_enabled_ = false;
125 } 127 }
126 return cached_enabled_; 128 return cached_enabled_;
127 } 129 }
128 130
129 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { 131 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
130 if (!AreMetricsEnabled()) 132 if (!AreMetricsEnabled())
131 return true; 133 return true;
132 134
133 int chrome_fd = open(uma_events_file_, 135 int chrome_fd = HANDLE_EINTR(open(uma_events_file_,
134 O_WRONLY | O_APPEND | O_CREAT, 136 O_WRONLY | O_APPEND | O_CREAT,
135 READ_WRITE_ALL_FILE_FLAGS); 137 READ_WRITE_ALL_FILE_FLAGS));
136 // If we failed to open it, return. 138 // If we failed to open it, return.
137 if (chrome_fd < 0) { 139 if (chrome_fd < 0) {
138 PrintError("open", uma_events_file_, errno); 140 PrintError("open", uma_events_file_, errno);
139 return false; 141 return false;
140 } 142 }
141 143
142 // Need to chmod because open flags are anded with umask. Ignore the 144 // Need to chmod because open flags are anded with umask. Ignore the
143 // exit code -- a chronos process may fail chmoding because the file 145 // exit code -- a chronos process may fail chmoding because the file
144 // has been created by a root process but that should be OK. 146 // has been created by a root process but that should be OK.
145 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS); 147 fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
146 148
147 // Grab an exclusive lock to protect Chrome from truncating 149 // Grab an exclusive lock to protect Chrome from truncating
148 // underneath us. Keep the file locked as briefly as possible. 150 // underneath us. Keep the file locked as briefly as possible.
149 if (flock(chrome_fd, LOCK_EX) < 0) { 151 if (HANDLE_EINTR(flock(chrome_fd, LOCK_EX)) < 0) {
150 PrintError("flock", uma_events_file_, errno); 152 PrintError("flock", uma_events_file_, errno);
151 close(chrome_fd); 153 HANDLE_EINTR(close(chrome_fd));
152 return false; 154 return false;
153 } 155 }
154 156
155 bool success = true; 157 bool success = true;
156 if (write(chrome_fd, message, length) != length) { 158 if (HANDLE_EINTR(write(chrome_fd, message, length)) != length) {
157 PrintError("write", uma_events_file_, errno); 159 PrintError("write", uma_events_file_, errno);
158 success = false; 160 success = false;
159 } 161 }
160 162
161 // Release the file lock and close file. 163 // Close the file and release the lock.
162 if (flock(chrome_fd, LOCK_UN) < 0) { 164 HANDLE_EINTR(close(chrome_fd));
163 PrintError("unlock", uma_events_file_, errno);
164 success = false;
165 }
166 close(chrome_fd);
167 return success; 165 return success;
168 } 166 }
169 167
170 int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer, 168 int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
171 const char* format, ...) { 169 const char* format, ...) {
172 int32_t message_length; 170 int32_t message_length;
173 size_t len_size = sizeof(message_length); 171 size_t len_size = sizeof(message_length);
174 172
175 // Format the non-LENGTH contents in the buffer by leaving space for 173 // Format the non-LENGTH contents in the buffer by leaving space for
176 // LENGTH at the start of the buffer. 174 // LENGTH at the start of the buffer.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 int32_t message_length = 260 int32_t message_length =
263 FormatChromeMessage(kBufferSize, message, 261 FormatChromeMessage(kBufferSize, message,
264 "crash%c%s", '\0', crash_kind); 262 "crash%c%s", '\0', crash_kind);
265 263
266 if (message_length < 0) 264 if (message_length < 0)
267 return false; 265 return false;
268 266
269 // Send the message. 267 // Send the message.
270 return SendMessageToChrome(message_length, message); 268 return SendMessageToChrome(message_length, message);
271 } 269 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698