blob: 745fd088311adaaceaed8df2da6ff482459ab1ce [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
Ben Chana0ddf462014-02-06 11:32:42 -080018#include <base/files/file_path.h>
Christopher Wiley5781aa42012-07-30 14:42:23 -070019#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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080024using base::FilePath;
25
Christopher Wiley5781aa42012-07-30 14:42:23 -070026namespace shill {
27
28namespace {
29
30// MemoryLog needs to be a 'leaky' singleton as it needs to survive to
31// handle logging till the very end of the shill process. Making MemoryLog
32// leaky is fine as it does not need to clean up or release any resource at
33// destruction.
34base::LazyInstance<MemoryLog>::Leaky g_memory_log = LAZY_INSTANCE_INITIALIZER;
35
36// Can't get to these in base/logging.c.
37const char *const kLogSeverityNames[logging::LOG_NUM_SEVERITIES] = {
38 "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL"
39};
40
Christopher Wiley3e7635e2012-08-15 09:46:17 -070041const char kLoggedInUserName[] = "chronos";
Christopher Wiley5781aa42012-07-30 14:42:23 -070042} // namespace
43
Christopher Wiley3e7635e2012-08-15 09:46:17 -070044const char MemoryLog::kDefaultLoggedInDumpPath[] =
Christopher Wiley03f8e482013-04-12 14:59:15 -070045 "/var/run/shill/log/connectivity.log";
Christopher Wiley3e7635e2012-08-15 09:46:17 -070046
47const char MemoryLog::kDefaultLoggedOutDumpPath[] =
48 "/var/log/connectivity.log";
49
50const char MemoryLog::kLoggedInTokenPath[] =
51 "/var/run/state/logged-in";
52
Christopher Wiley5781aa42012-07-30 14:42:23 -070053// static
54MemoryLog *MemoryLog::GetInstance() {
55 return g_memory_log.Pointer();
56}
57
58MemoryLog::MemoryLog()
59 : maximum_size_bytes_(kDefaultMaximumMemoryLogSizeInBytes),
Christopher Wiley3e7635e2012-08-15 09:46:17 -070060 current_size_bytes_(0),
61 maximum_disk_log_size_bytes_(kDefaultMaxDiskLogSizeInBytes) { }
Christopher Wiley5781aa42012-07-30 14:42:23 -070062
63void MemoryLog::Append(const std::string &msg) {
64 current_size_bytes_ += msg.size();
65 log_.push_back(msg);
66 ShrinkToTargetSize(maximum_size_bytes_);
67}
68
69void MemoryLog::Clear() {
70 current_size_bytes_ = 0;
71 log_.clear();
72}
73
Christopher Wiley3e7635e2012-08-15 09:46:17 -070074void MemoryLog::FlushToDisk() {
Ben Chana0ddf462014-02-06 11:32:42 -080075 if (base::PathExists(FilePath(kLoggedInTokenPath))) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -070076 FlushToDiskImpl(FilePath(kDefaultLoggedInDumpPath));
77 } else {
78 FlushToDiskImpl(FilePath(kDefaultLoggedOutDumpPath));
79 }
80}
81
82void MemoryLog::FlushToDiskImpl(const FilePath &file_path) {
mukesh agrawal910d0d62012-12-06 14:35:40 -080083 LOG(INFO) << "Saving memory log to " << file_path.AsUTF8Unsafe();
Christopher Wiley3e7635e2012-08-15 09:46:17 -070084 do {
85 // If the file exists, lets make sure it is of reasonable size before
86 // writing to it, and roll it over if it's too big.
Ben Chana0ddf462014-02-06 11:32:42 -080087 if (!base::PathExists(file_path)) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -070088 // No existing file means we can write without worry to a new file.
89 continue;
90 }
91 int64_t file_size = -1;
Ben Chana0ddf462014-02-06 11:32:42 -080092 if (!base::GetFileSize(file_path, &file_size) || (file_size < 0)) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -070093 LOG(ERROR) << "Failed to get size of existing memory log dump.";
94 return;
95 }
96 FilePath backup_path = file_path.ReplaceExtension(".bak");
97 if (static_cast<uint64_t>(file_size) < maximum_disk_log_size_bytes_) {
98 // File existed, but was below our threshold.
99 continue;
100 }
Ben Chana0ddf462014-02-06 11:32:42 -0800101 if (!base::Move(file_path, backup_path)) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700102 LOG(ERROR) << "Failed to move overly large memory log on disk from "
103 << file_path.value() << " to " << backup_path.value();
104 return;
105 }
106 } while (false);
107
108 if (FlushToFile(file_path) < 0) {
109 LOG(ERROR) << "Failed to flush memory log to disk";
110 }
111 // We don't want to see messages twice.
112 Clear();
113}
114
115ssize_t MemoryLog::FlushToFile(const FilePath &file_path) {
Ben Chana0ddf462014-02-06 11:32:42 -0800116 FILE *f = base::OpenFile(file_path, "a");
Christopher Wiley5781aa42012-07-30 14:42:23 -0700117 if (!f) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700118 LOG(ERROR) << "Failed to open file for dumping memory log to disk.";
Christopher Wiley5781aa42012-07-30 14:42:23 -0700119 return -1;
120 }
Christopher Wiley5781aa42012-07-30 14:42:23 -0700121 file_util::ScopedFILE file_closer(f);
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700122 long maximum_pw_string_size = sysconf(_SC_GETPW_R_SIZE_MAX);
123 if (maximum_pw_string_size < 0) {
124 LOG(ERROR) << "Setup for changing ownership of memory log file failed";
125 return -1;
126 }
127 struct passwd chronos;
128 struct passwd *pwresult = NULL;
Ben Chana3cb0632014-01-27 19:08:54 -0800129 scoped_ptr<char[]> buf(new char[maximum_pw_string_size]);
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700130 if (getpwnam_r(kLoggedInUserName,
131 &chronos,
132 buf.get(),
133 maximum_pw_string_size,
134 &pwresult) || pwresult == NULL) {
135 PLOG(ERROR) << "Failed to find user " << kLoggedInUserName
136 << " for memory log dump.";
137 return -1;
138 }
139 if (chown(file_path.value().c_str(), chronos.pw_uid, chronos.pw_gid)) {
140 PLOG(WARNING) << "Failed to change ownership of memory log file.";
141 }
142 if (chmod(file_path.value().c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
143 PLOG(WARNING) << "Failed to change permissions of memory log file. Error: "
144 << strerror(errno);
145 }
Christopher Wiley5781aa42012-07-30 14:42:23 -0700146 ssize_t bytes_written = 0;
Paul Stewart6db7b242014-05-02 15:34:21 -0700147 for (const auto &entry : log_) {
148 bytes_written += fwrite(entry.c_str(), 1, entry.size(), f);
Christopher Wiley5781aa42012-07-30 14:42:23 -0700149 if (ferror(f)) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700150 LOG(ERROR) << "Write to memory log dump file failed.";
Christopher Wiley5781aa42012-07-30 14:42:23 -0700151 return -1;
152 }
153 }
154
155 return bytes_written;
156}
157
158void MemoryLog::SetMaximumSize(size_t size_in_bytes) {
159 ShrinkToTargetSize(size_in_bytes);
160 maximum_size_bytes_ = size_in_bytes;
161}
162
163void MemoryLog::ShrinkToTargetSize(size_t number_bytes) {
164 while (current_size_bytes_ > number_bytes) {
165 const std::string &front = log_.front();
166 current_size_bytes_ -= front.size();
167 log_.pop_front();
168 }
169}
170
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700171bool MemoryLog::TestContainsMessageWithText(const char *msg) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700172 for (const auto &entry : log_) {
173 if (entry.find(msg) != std::string::npos) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700174 return true;
175 }
176 }
177 return false;
178}
179
Christopher Wiley5781aa42012-07-30 14:42:23 -0700180MemoryLogMessage::MemoryLogMessage(const char *file,
181 int line,
182 logging::LogSeverity severity,
183 bool propagate_down)
184 : file_(file),
185 line_(line),
186 severity_(severity),
187 propagate_down_(propagate_down),
188 message_start_(0) {
189 Init();
190}
191
192MemoryLogMessage::~MemoryLogMessage() {
193 if (propagate_down_) {
194 ::logging::LogMessage(file_, line_, severity_).stream()
195 << &(stream_.str()[message_start_]);
196 }
197 stream_ << std::endl;
198 MemoryLog::GetInstance()->Append(stream_.str());
199}
200
201// This owes heavily to the base/logging.cc:LogMessage implementation, but
202// without as much customization. Unfortunately, there isn't a good way to
203// get into that code without drastically changing how base/logging works. It
204// isn't exactly rocket science in any case.
205void MemoryLogMessage::Init() {
206 const char *filename = strrchr(file_, '/');
207 if (!filename) {
208 filename = file_;
209 }
210
211 // Just log a timestamp, number of ticks, severity, and a file name.
212 struct timeval tv = {0};
213 struct tm local_time = {0};
214 Time::GetInstance()->GetTimeOfDay(&tv, NULL);
215 localtime_r(&tv.tv_sec, &local_time);
216 stream_ << std::setfill('0')
217 << local_time.tm_year + 1900
218 << '-' << std::setw(2) << local_time.tm_mon + 1
219 << '-' << std::setw(2) << local_time.tm_mday
220 << 'T' << std::setw(2) << local_time.tm_hour
221 << ':' << std::setw(2) << local_time.tm_min
222 << ':' << std::setw(2) << local_time.tm_sec
223 << '.' << tv.tv_usec << ' ';
224
Christopher Wiley5781aa42012-07-30 14:42:23 -0700225 if (severity_ >= 0)
226 stream_ << kLogSeverityNames[severity_];
227 else
228 stream_ << "VERBOSE" << -severity_;
229 stream_ << ":" << filename << "(" << line_ << ") ";
Christopher Wileyf11cebb2012-08-08 12:22:20 -0700230
Christopher Wiley5781aa42012-07-30 14:42:23 -0700231 message_start_ = stream_.tellp();
Christopher Wiley5781aa42012-07-30 14:42:23 -0700232}
233
234} // namespace shill