Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 1 | // 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 Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 5 | #include "shill/memory_log.h" |
| 6 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <pwd.h> |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 10 | #include <string.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <unistd.h> |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 13 | |
| 14 | #include <ctime> |
| 15 | #include <iomanip> |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 16 | #include <string> |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 17 | |
| 18 | #include <base/file_path.h> |
| 19 | #include <base/file_util.h> |
| 20 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 21 | #include "shill/logging.h" |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 22 | #include "shill/shill_time.h" |
| 23 | |
| 24 | namespace shill { |
| 25 | |
| 26 | namespace { |
| 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. |
| 32 | base::LazyInstance<MemoryLog>::Leaky g_memory_log = LAZY_INSTANCE_INITIALIZER; |
| 33 | |
| 34 | // Can't get to these in base/logging.c. |
| 35 | const char *const kLogSeverityNames[logging::LOG_NUM_SEVERITIES] = { |
| 36 | "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" |
| 37 | }; |
| 38 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 39 | const char kLoggedInUserName[] = "chronos"; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 40 | } // namespace |
| 41 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 42 | const char MemoryLog::kDefaultLoggedInDumpPath[] = |
| 43 | "/home/chronos/user/log/connectivity.log"; |
| 44 | |
| 45 | const char MemoryLog::kDefaultLoggedOutDumpPath[] = |
| 46 | "/var/log/connectivity.log"; |
| 47 | |
| 48 | const char MemoryLog::kLoggedInTokenPath[] = |
| 49 | "/var/run/state/logged-in"; |
| 50 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 51 | // static |
| 52 | MemoryLog *MemoryLog::GetInstance() { |
| 53 | return g_memory_log.Pointer(); |
| 54 | } |
| 55 | |
| 56 | MemoryLog::MemoryLog() |
| 57 | : maximum_size_bytes_(kDefaultMaximumMemoryLogSizeInBytes), |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 58 | current_size_bytes_(0), |
| 59 | maximum_disk_log_size_bytes_(kDefaultMaxDiskLogSizeInBytes) { } |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 60 | |
| 61 | void MemoryLog::Append(const std::string &msg) { |
| 62 | current_size_bytes_ += msg.size(); |
| 63 | log_.push_back(msg); |
| 64 | ShrinkToTargetSize(maximum_size_bytes_); |
| 65 | } |
| 66 | |
| 67 | void MemoryLog::Clear() { |
| 68 | current_size_bytes_ = 0; |
| 69 | log_.clear(); |
| 70 | } |
| 71 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 72 | void MemoryLog::FlushToDisk() { |
| 73 | if (file_util::PathExists(FilePath(kLoggedInTokenPath))) { |
| 74 | FlushToDiskImpl(FilePath(kDefaultLoggedInDumpPath)); |
| 75 | } else { |
| 76 | FlushToDiskImpl(FilePath(kDefaultLoggedOutDumpPath)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void 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 | |
| 112 | ssize_t MemoryLog::FlushToFile(const FilePath &file_path) { |
| 113 | FILE *f = file_util::OpenFile(file_path, "a"); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 114 | if (!f) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 115 | LOG(ERROR) << "Failed to open file for dumping memory log to disk."; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 116 | return -1; |
| 117 | } |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 118 | file_util::ScopedFILE file_closer(f); |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 119 | 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 143 | 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 Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 148 | LOG(ERROR) << "Write to memory log dump file failed."; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return bytes_written; |
| 154 | } |
| 155 | |
| 156 | void MemoryLog::SetMaximumSize(size_t size_in_bytes) { |
| 157 | ShrinkToTargetSize(size_in_bytes); |
| 158 | maximum_size_bytes_ = size_in_bytes; |
| 159 | } |
| 160 | |
| 161 | void 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 Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 169 | bool 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 179 | MemoryLogMessage::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 | |
| 191 | MemoryLogMessage::~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. |
| 204 | void 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 224 | if (severity_ >= 0) |
| 225 | stream_ << kLogSeverityNames[severity_]; |
| 226 | else |
| 227 | stream_ << "VERBOSE" << -severity_; |
| 228 | stream_ << ":" << filename << "(" << line_ << ") "; |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 229 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 230 | message_start_ = stream_.tellp(); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | } // namespace shill |