blob: ce751d00394670727f68502f5384fde891f25692 [file] [log] [blame]
Christopher Wiley5781aa42012-07-30 14:42:23 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Christopher Wileyb691efd2012-08-09 13:51:51 -07005#include "shill/memory_log.h"
6
Christopher Wiley3e7635e2012-08-15 09:46:17 -07007#include <errno.h>
8#include <pwd.h>
Christopher Wiley5781aa42012-07-30 14:42:23 -07009#include <stdio.h>
Christopher Wiley3e7635e2012-08-15 09:46:17 -070010#include <string.h>
11#include <sys/stat.h>
12#include <unistd.h>
Christopher Wiley5781aa42012-07-30 14:42:23 -070013
14#include <ctime>
15#include <iomanip>
Christopher Wileyf11cebb2012-08-08 12:22:20 -070016#include <string>
Christopher Wiley5781aa42012-07-30 14:42:23 -070017
18#include <base/file_path.h>
19#include <base/file_util.h>
20
Christopher Wileyb691efd2012-08-09 13:51:51 -070021#include "shill/logging.h"
Christopher Wiley5781aa42012-07-30 14:42:23 -070022#include "shill/shill_time.h"
23
24namespace shill {
25
26namespace {
27
28// MemoryLog needs to be a 'leaky' singleton as it needs to survive to
29// handle logging till the very end of the shill process. Making MemoryLog
30// leaky is fine as it does not need to clean up or release any resource at
31// destruction.
32base::LazyInstance<MemoryLog>::Leaky g_memory_log = LAZY_INSTANCE_INITIALIZER;
33
34// Can't get to these in base/logging.c.
35const char *const kLogSeverityNames[logging::LOG_NUM_SEVERITIES] = {
36 "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL"
37};
38
Christopher Wiley3e7635e2012-08-15 09:46:17 -070039const char kLoggedInUserName[] = "chronos";
Christopher Wiley5781aa42012-07-30 14:42:23 -070040} // namespace
41
Christopher Wiley3e7635e2012-08-15 09:46:17 -070042const char MemoryLog::kDefaultLoggedInDumpPath[] =
43 "/home/chronos/user/log/connectivity.log";
44
45const char MemoryLog::kDefaultLoggedOutDumpPath[] =
46 "/var/log/connectivity.log";
47
48const char MemoryLog::kLoggedInTokenPath[] =
49 "/var/run/state/logged-in";
50
Christopher Wiley5781aa42012-07-30 14:42:23 -070051// static
52MemoryLog *MemoryLog::GetInstance() {
53 return g_memory_log.Pointer();
54}
55
56MemoryLog::MemoryLog()
57 : maximum_size_bytes_(kDefaultMaximumMemoryLogSizeInBytes),
Christopher Wiley3e7635e2012-08-15 09:46:17 -070058 current_size_bytes_(0),
59 maximum_disk_log_size_bytes_(kDefaultMaxDiskLogSizeInBytes) { }
Christopher Wiley5781aa42012-07-30 14:42:23 -070060
61void MemoryLog::Append(const std::string &msg) {
62 current_size_bytes_ += msg.size();
63 log_.push_back(msg);
64 ShrinkToTargetSize(maximum_size_bytes_);
65}
66
67void MemoryLog::Clear() {
68 current_size_bytes_ = 0;
69 log_.clear();
70}
71
Christopher Wiley3e7635e2012-08-15 09:46:17 -070072void MemoryLog::FlushToDisk() {
73 if (file_util::PathExists(FilePath(kLoggedInTokenPath))) {
74 FlushToDiskImpl(FilePath(kDefaultLoggedInDumpPath));
75 } else {
76 FlushToDiskImpl(FilePath(kDefaultLoggedOutDumpPath));
77 }
78}
79
80void MemoryLog::FlushToDiskImpl(const FilePath &file_path) {
81 do {
82 // If the file exists, lets make sure it is of reasonable size before
83 // writing to it, and roll it over if it's too big.
84 if (!file_util::PathExists(file_path)) {
85 // No existing file means we can write without worry to a new file.
86 continue;
87 }
88 int64_t file_size = -1;
89 if (!file_util::GetFileSize(file_path, &file_size) || (file_size < 0)) {
90 LOG(ERROR) << "Failed to get size of existing memory log dump.";
91 return;
92 }
93 FilePath backup_path = file_path.ReplaceExtension(".bak");
94 if (static_cast<uint64_t>(file_size) < maximum_disk_log_size_bytes_) {
95 // File existed, but was below our threshold.
96 continue;
97 }
98 if (!file_util::Move(file_path, backup_path)) {
99 LOG(ERROR) << "Failed to move overly large memory log on disk from "
100 << file_path.value() << " to " << backup_path.value();
101 return;
102 }
103 } while (false);
104
105 if (FlushToFile(file_path) < 0) {
106 LOG(ERROR) << "Failed to flush memory log to disk";
107 }
108 // We don't want to see messages twice.
109 Clear();
110}
111
112ssize_t MemoryLog::FlushToFile(const FilePath &file_path) {
113 FILE *f = file_util::OpenFile(file_path, "a");
Christopher Wiley5781aa42012-07-30 14:42:23 -0700114 if (!f) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700115 LOG(ERROR) << "Failed to open file for dumping memory log to disk.";
Christopher Wiley5781aa42012-07-30 14:42:23 -0700116 return -1;
117 }
Christopher Wiley5781aa42012-07-30 14:42:23 -0700118 file_util::ScopedFILE file_closer(f);
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700119 long maximum_pw_string_size = sysconf(_SC_GETPW_R_SIZE_MAX);
120 if (maximum_pw_string_size < 0) {
121 LOG(ERROR) << "Setup for changing ownership of memory log file failed";
122 return -1;
123 }
124 struct passwd chronos;
125 struct passwd *pwresult = NULL;
126 scoped_array<char> buf(new char[maximum_pw_string_size]);
127 if (getpwnam_r(kLoggedInUserName,
128 &chronos,
129 buf.get(),
130 maximum_pw_string_size,
131 &pwresult) || pwresult == NULL) {
132 PLOG(ERROR) << "Failed to find user " << kLoggedInUserName
133 << " for memory log dump.";
134 return -1;
135 }
136 if (chown(file_path.value().c_str(), chronos.pw_uid, chronos.pw_gid)) {
137 PLOG(WARNING) << "Failed to change ownership of memory log file.";
138 }
139 if (chmod(file_path.value().c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
140 PLOG(WARNING) << "Failed to change permissions of memory log file. Error: "
141 << strerror(errno);
142 }
Christopher Wiley5781aa42012-07-30 14:42:23 -0700143 ssize_t bytes_written = 0;
144 std::deque<std::string>::iterator it;
145 for (it = log_.begin(); it != log_.end(); it++) {
146 bytes_written += fwrite(it->c_str(), 1, it->size(), f);
147 if (ferror(f)) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700148 LOG(ERROR) << "Write to memory log dump file failed.";
Christopher Wiley5781aa42012-07-30 14:42:23 -0700149 return -1;
150 }
151 }
152
153 return bytes_written;
154}
155
156void MemoryLog::SetMaximumSize(size_t size_in_bytes) {
157 ShrinkToTargetSize(size_in_bytes);
158 maximum_size_bytes_ = size_in_bytes;
159}
160
161void MemoryLog::ShrinkToTargetSize(size_t number_bytes) {
162 while (current_size_bytes_ > number_bytes) {
163 const std::string &front = log_.front();
164 current_size_bytes_ -= front.size();
165 log_.pop_front();
166 }
167}
168
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700169bool MemoryLog::TestContainsMessageWithText(const char *msg) {
170 std::deque<std::string>::const_iterator it;
171 for (it = log_.begin(); it != log_.end(); ++it) {
172 if (it->find(msg) != std::string::npos) {
173 return true;
174 }
175 }
176 return false;
177}
178
Christopher Wiley5781aa42012-07-30 14:42:23 -0700179MemoryLogMessage::MemoryLogMessage(const char *file,
180 int line,
181 logging::LogSeverity severity,
182 bool propagate_down)
183 : file_(file),
184 line_(line),
185 severity_(severity),
186 propagate_down_(propagate_down),
187 message_start_(0) {
188 Init();
189}
190
191MemoryLogMessage::~MemoryLogMessage() {
192 if (propagate_down_) {
193 ::logging::LogMessage(file_, line_, severity_).stream()
194 << &(stream_.str()[message_start_]);
195 }
196 stream_ << std::endl;
197 MemoryLog::GetInstance()->Append(stream_.str());
198}
199
200// This owes heavily to the base/logging.cc:LogMessage implementation, but
201// without as much customization. Unfortunately, there isn't a good way to
202// get into that code without drastically changing how base/logging works. It
203// isn't exactly rocket science in any case.
204void MemoryLogMessage::Init() {
205 const char *filename = strrchr(file_, '/');
206 if (!filename) {
207 filename = file_;
208 }
209
210 // Just log a timestamp, number of ticks, severity, and a file name.
211 struct timeval tv = {0};
212 struct tm local_time = {0};
213 Time::GetInstance()->GetTimeOfDay(&tv, NULL);
214 localtime_r(&tv.tv_sec, &local_time);
215 stream_ << std::setfill('0')
216 << local_time.tm_year + 1900
217 << '-' << std::setw(2) << local_time.tm_mon + 1
218 << '-' << std::setw(2) << local_time.tm_mday
219 << 'T' << std::setw(2) << local_time.tm_hour
220 << ':' << std::setw(2) << local_time.tm_min
221 << ':' << std::setw(2) << local_time.tm_sec
222 << '.' << tv.tv_usec << ' ';
223
Christopher Wiley5781aa42012-07-30 14:42:23 -0700224 if (severity_ >= 0)
225 stream_ << kLogSeverityNames[severity_];
226 else
227 stream_ << "VERBOSE" << -severity_;
228 stream_ << ":" << filename << "(" << line_ << ") ";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700229
Christopher Wiley5781aa42012-07-30 14:42:23 -0700230 message_start_ = stream_.tellp();
Christopher Wiley5781aa42012-07-30 14:42:23 -0700231}
232
233} // namespace shill