diff options
author | cpu <cpu@chromium.org> | 2015-02-03 13:55:04 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-03 21:55:50 +0000 |
commit | 51747bc6af43f662054ebee3a73976edad07f4a5 (patch) | |
tree | fddc787f716c9297cf46d2fcc991cbf77a78dc1b /chrome/tools | |
parent | 8d81593b1a531d950261cbb0d48cdf8464aae442 (diff) | |
download | chromium_src-51747bc6af43f662054ebee3a73976edad07f4a5.zip chromium_src-51747bc6af43f662054ebee3a73976edad07f4a5.tar.gz chromium_src-51747bc6af43f662054ebee3a73976edad07f4a5.tar.bz2 |
Add basic support for CAPS exe
1- A singleton
2- A logger
Now CAPS depends on base and on chrome/common_version. About 190KB
on plain release build.
BUG=447073
Review URL: https://codereview.chromium.org/890213002
Cr-Commit-Position: refs/heads/master@{#314411}
Diffstat (limited to 'chrome/tools')
-rw-r--r-- | chrome/tools/crash_service/caps/caps.gyp | 9 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/exit_codes.h | 21 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/logger_win.cc | 89 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/logger_win.h | 32 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/main_win.cc | 29 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/process_singleton_win.cc | 29 | ||||
-rw-r--r-- | chrome/tools/crash_service/caps/process_singleton_win.h | 30 |
7 files changed, 237 insertions, 2 deletions
diff --git a/chrome/tools/crash_service/caps/caps.gyp b/chrome/tools/crash_service/caps/caps.gyp index f18f84d..bcbe414 100644 --- a/chrome/tools/crash_service/caps/caps.gyp +++ b/chrome/tools/crash_service/caps/caps.gyp @@ -39,14 +39,21 @@ 'target_name': 'caps', 'type': 'executable', 'include_dirs': [ - '../..', + '..', ], 'sources': [ + 'exit_codes.h', + 'logger_win.cc', + 'logger_win.h', 'main_win.cc', + 'process_singleton_win.cc', + 'process_singleton_win.h', '<(SHARED_INTERMEDIATE_DIR)/caps/caps_version.rc', ], 'dependencies': [ 'caps_resources', + '../../../../base/base.gyp:base', + '../../../../chrome/chrome.gyp:common_version', ], 'msvs_settings': { 'VCLinkerTool': { diff --git a/chrome/tools/crash_service/caps/exit_codes.h b/chrome/tools/crash_service/caps/exit_codes.h new file mode 100644 index 0000000..583e0a5 --- /dev/null +++ b/chrome/tools/crash_service/caps/exit_codes.h @@ -0,0 +1,21 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_TOOLS_CRASH_SERVICE_CAPS_EXIT_CODES_H_ +#define CHROME_TOOLS_CRASH_SERVICE_CAPS_EXIT_CODES_H_ + +namespace caps { + +// Exit codes for CAPS. Don't reorder these values. +enum ExitCodes { + EC_NORMAL_EXIT = 0, + EC_EXISTING_INSTANCE, + EC_INIT_ERROR, + EC_LAST_CODE +}; + +} // namespace caps + +#endif // CHROME_TOOLS_CRASH_SERVICE_CAPS_EXIT_CODES_H_ + diff --git a/chrome/tools/crash_service/caps/logger_win.cc b/chrome/tools/crash_service/caps/logger_win.cc new file mode 100644 index 0000000..2e4a1cf --- /dev/null +++ b/chrome/tools/crash_service/caps/logger_win.cc @@ -0,0 +1,89 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <windows.h> +#include <time.h> + +#include "base/files/file_path.h" +#include "base/strings/stringprintf.h" +#include "chrome/common/chrome_version_info_values.h" +#include "chrome/tools/crash_service/caps/logger_win.h" + +namespace { +// Every message has this structure: +// <index>~ <tick_count> <message> ~\n" +const char kMessageHeader[] = "%03d~ %08x %s ~\n"; + +// Do not re-order these messages. Only add at the end. +const char* kMessages[] { + "start pid(%lu) version(%s) time(%s)", // 0 + "exit pid(%lu) time(%s)", // 1 + "instance found", // 2 +}; + +bool WriteLogLine(HANDLE file, const std::string& txt) { + if (txt.empty()) + return true; + DWORD written; + auto rc = ::WriteFile(file, txt.c_str(), + static_cast<DWORD>(txt.size()), + &written, + nullptr); + return (rc == TRUE); +} + +// Uses |kMessages| at |index| above to write to disk a formatted log line. +bool WriteFormattedLogLine(HANDLE file, size_t index, ...) { + va_list ap; + va_start(ap, index); + auto fmt = base::StringPrintf( + kMessageHeader, index, ::GetTickCount(), kMessages[index]); + auto msg = base::StringPrintV(fmt.c_str(), ap); + auto rc = WriteLogLine(file, msg.c_str()); + va_end(ap); + return rc; +} + +// Returns the current time and date formatted in standard C style. +std::string DateTime() { + time_t current_time = 0; + time(¤t_time); + struct tm local_time = {0}; + char time_buf[26] = {0}; + localtime_s(&local_time, ¤t_time); + asctime_s(time_buf, &local_time); + time_buf[24] = '\0'; + return time_buf; +} + +} // namespace + +namespace caps { + +Logger::Logger(const base::FilePath& path) : file_(INVALID_HANDLE_VALUE) { + auto logfile = path.Append(L"caps_log.txt"); + // Opening a file like so allows atomic appends, but can't be used + // for anything else. + DWORD kShareAll = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; + file_ = ::CreateFile(logfile.value().c_str(), + FILE_APPEND_DATA, kShareAll, + nullptr, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, + nullptr); + if (file_ == INVALID_HANDLE_VALUE) + return; + WriteFormattedLogLine( + file_, 0, ::GetCurrentProcessId(), PRODUCT_VERSION, DateTime().c_str()); +} + +Logger::~Logger() { + if (file_ != INVALID_HANDLE_VALUE) { + WriteFormattedLogLine( + file_, 1, ::GetCurrentProcessId(), DateTime().c_str()); + ::CloseHandle(file_); + } +} + +} // namespace caps + diff --git a/chrome/tools/crash_service/caps/logger_win.h b/chrome/tools/crash_service/caps/logger_win.h new file mode 100644 index 0000000..efa0b94 --- /dev/null +++ b/chrome/tools/crash_service/caps/logger_win.h @@ -0,0 +1,32 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_TOOLS_CRASH_SERVICE_CAPS_LOGGER_WIN_H_ +#define CHROME_TOOLS_CRASH_SERVICE_CAPS_LOGGER_WIN_H_ + +#include <windows.h> + +#include "base/macros.h" + +namespace base { +class FilePath; +} + +namespace caps { +// Creates a human-readable activity log file. +class Logger { +public: + explicit Logger(const base::FilePath& path); + ~Logger(); + +private: + HANDLE file_; + + DISALLOW_COPY_AND_ASSIGN(Logger); +}; + +} // namespace caps + +#endif // CHROME_TOOLS_CRASH_SERVICE_CAPS_LOGGER_WIN_H_ + diff --git a/chrome/tools/crash_service/caps/main_win.cc b/chrome/tools/crash_service/caps/main_win.cc index b2c0174..6146165 100644 --- a/chrome/tools/crash_service/caps/main_win.cc +++ b/chrome/tools/crash_service/caps/main_win.cc @@ -4,6 +4,33 @@ #include <windows.h> +#include "base/at_exit.h" +#include "base/files/file_path.h" +#include "base/path_service.h" +#include "base/version.h" +#include "chrome/tools/crash_service/caps/exit_codes.h" +#include "chrome/tools/crash_service/caps/logger_win.h" +#include "chrome/tools/crash_service/caps/process_singleton_win.h" + int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) { - return 0; + base::AtExitManager at_exit_manager; + base::FilePath dir_exe; + if (!PathService::Get(base::DIR_EXE, &dir_exe)) + return caps::EC_INIT_ERROR; + + // What directory we write depends if we are being run from the actual + // location (a versioned directory) or from a build output directory. + base::Version version(dir_exe.BaseName().MaybeAsASCII()); + auto data_path = version.IsValid() ? dir_exe.DirName() : dir_exe; + + // Start logging. + caps::Logger logger(data_path); + + // Check for an existing running instance. + caps::ProcessSingleton process_singleton; + if (process_singleton.other_instance()) + return caps::EC_EXISTING_INSTANCE; + + return caps::EC_NORMAL_EXIT; } + diff --git a/chrome/tools/crash_service/caps/process_singleton_win.cc b/chrome/tools/crash_service/caps/process_singleton_win.cc new file mode 100644 index 0000000..f51accc --- /dev/null +++ b/chrome/tools/crash_service/caps/process_singleton_win.cc @@ -0,0 +1,29 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <windows.h> + +#include "chrome/tools/crash_service/caps/process_singleton_win.h" + +namespace caps { + +ProcessSingleton::ProcessSingleton() : mutex_(nullptr) { + auto mutex = ::CreateMutex(nullptr, TRUE, L"CHROME.CAPS.V1"); + if (!mutex) + return; + if (::GetLastError() == ERROR_ALREADY_EXISTS) { + ::CloseHandle(mutex); + return; + } + // We are now the single instance. + mutex_ = mutex; +} + +ProcessSingleton::~ProcessSingleton() { + if (mutex_) + ::CloseHandle(mutex_); +} + +} // namespace caps + diff --git a/chrome/tools/crash_service/caps/process_singleton_win.h b/chrome/tools/crash_service/caps/process_singleton_win.h new file mode 100644 index 0000000..5440d9c --- /dev/null +++ b/chrome/tools/crash_service/caps/process_singleton_win.h @@ -0,0 +1,30 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_TOOLS_CRASH_SERVICE_CAPS_PROCESS_SINGLETON_WIN_H_ +#define CHROME_TOOLS_CRASH_SERVICE_CAPS_PROCESS_SINGLETON_WIN_H_ + +#include <windows.h> + +#include "base/macros.h" + +namespace caps { +// Uses a named mutex to make sure that only one process of +// with this particular version is launched. +class ProcessSingleton { +public: + ProcessSingleton(); + ~ProcessSingleton(); + bool other_instance() const { return mutex_ == NULL; } + +private: + HANDLE mutex_; + + DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); +}; + +} // namespace caps + +#endif // CHROME_TOOLS_CRASH_SERVICE_CAPS_PROCESS_SINGLETON_WIN_H_ + |