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 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 18 | #include <base/files/file_path.h> |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 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 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 24 | using base::FilePath; |
| 25 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
| 28 | namespace { |
| 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. |
| 34 | base::LazyInstance<MemoryLog>::Leaky g_memory_log = LAZY_INSTANCE_INITIALIZER; |
| 35 | |
| 36 | // Can't get to these in base/logging.c. |
| 37 | const char *const kLogSeverityNames[logging::LOG_NUM_SEVERITIES] = { |
| 38 | "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" |
| 39 | }; |
| 40 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 41 | const char kLoggedInUserName[] = "chronos"; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 42 | } // namespace |
| 43 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 44 | const char MemoryLog::kDefaultLoggedInDumpPath[] = |
Christopher Wiley | 03f8e48 | 2013-04-12 14:59:15 -0700 | [diff] [blame] | 45 | "/var/run/shill/log/connectivity.log"; |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 46 | |
| 47 | const char MemoryLog::kDefaultLoggedOutDumpPath[] = |
| 48 | "/var/log/connectivity.log"; |
| 49 | |
| 50 | const char MemoryLog::kLoggedInTokenPath[] = |
| 51 | "/var/run/state/logged-in"; |
| 52 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 53 | // static |
| 54 | MemoryLog *MemoryLog::GetInstance() { |
| 55 | return g_memory_log.Pointer(); |
| 56 | } |
| 57 | |
| 58 | MemoryLog::MemoryLog() |
| 59 | : maximum_size_bytes_(kDefaultMaximumMemoryLogSizeInBytes), |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 60 | current_size_bytes_(0), |
| 61 | maximum_disk_log_size_bytes_(kDefaultMaxDiskLogSizeInBytes) { } |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 62 | |
| 63 | void MemoryLog::Append(const std::string &msg) { |
| 64 | current_size_bytes_ += msg.size(); |
| 65 | log_.push_back(msg); |
| 66 | ShrinkToTargetSize(maximum_size_bytes_); |
| 67 | } |
| 68 | |
| 69 | void MemoryLog::Clear() { |
| 70 | current_size_bytes_ = 0; |
| 71 | log_.clear(); |
| 72 | } |
| 73 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 74 | void MemoryLog::FlushToDisk() { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 75 | if (base::PathExists(FilePath(kLoggedInTokenPath))) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 76 | FlushToDiskImpl(FilePath(kDefaultLoggedInDumpPath)); |
| 77 | } else { |
| 78 | FlushToDiskImpl(FilePath(kDefaultLoggedOutDumpPath)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void MemoryLog::FlushToDiskImpl(const FilePath &file_path) { |
mukesh agrawal | 910d0d6 | 2012-12-06 14:35:40 -0800 | [diff] [blame] | 83 | LOG(INFO) << "Saving memory log to " << file_path.AsUTF8Unsafe(); |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 84 | 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 Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 87 | if (!base::PathExists(file_path)) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 88 | // No existing file means we can write without worry to a new file. |
| 89 | continue; |
| 90 | } |
| 91 | int64_t file_size = -1; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 92 | if (!base::GetFileSize(file_path, &file_size) || (file_size < 0)) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 93 | 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 Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 101 | if (!base::Move(file_path, backup_path)) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 102 | 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 | |
| 115 | ssize_t MemoryLog::FlushToFile(const FilePath &file_path) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 116 | FILE *f = base::OpenFile(file_path, "a"); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 117 | if (!f) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 118 | LOG(ERROR) << "Failed to open file for dumping memory log to disk."; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 119 | return -1; |
| 120 | } |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 121 | file_util::ScopedFILE file_closer(f); |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 122 | 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 Chan | a3cb063 | 2014-01-27 19:08:54 -0800 | [diff] [blame] | 129 | scoped_ptr<char[]> buf(new char[maximum_pw_string_size]); |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 130 | 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 146 | ssize_t bytes_written = 0; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 147 | for (const auto &entry : log_) { |
| 148 | bytes_written += fwrite(entry.c_str(), 1, entry.size(), f); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 149 | if (ferror(f)) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 150 | LOG(ERROR) << "Write to memory log dump file failed."; |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return bytes_written; |
| 156 | } |
| 157 | |
| 158 | void MemoryLog::SetMaximumSize(size_t size_in_bytes) { |
| 159 | ShrinkToTargetSize(size_in_bytes); |
| 160 | maximum_size_bytes_ = size_in_bytes; |
| 161 | } |
| 162 | |
| 163 | void 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 Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 171 | bool MemoryLog::TestContainsMessageWithText(const char *msg) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 172 | for (const auto &entry : log_) { |
| 173 | if (entry.find(msg) != std::string::npos) { |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 180 | MemoryLogMessage::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 | |
| 192 | MemoryLogMessage::~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. |
| 205 | void 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 Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 225 | if (severity_ >= 0) |
| 226 | stream_ << kLogSeverityNames[severity_]; |
| 227 | else |
| 228 | stream_ << "VERBOSE" << -severity_; |
| 229 | stream_ << ":" << filename << "(" << line_ << ") "; |
Christopher Wiley | f11cebb | 2012-08-08 12:22:20 -0700 | [diff] [blame] | 230 | |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 231 | message_start_ = stream_.tellp(); |
Christopher Wiley | 5781aa4 | 2012-07-30 14:42:23 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace shill |