| Index: chromecast/crash/linux/minidump_manager.cc
|
| diff --git a/chromecast/crash/linux/minidump_manager.cc b/chromecast/crash/linux/minidump_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9d48aa1a3d9fd3772fcbfdde9a9ef9a12adc12d4
|
| --- /dev/null
|
| +++ b/chromecast/crash/linux/minidump_manager.cc
|
| @@ -0,0 +1,125 @@
|
| +// 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 "chromecast/crash/linux/minidump_manager.h"
|
| +
|
| +#include <dirent.h>
|
| +#include <errno.h>
|
| +#include <fcntl.h>
|
| +#include <grp.h>
|
| +#include <string.h>
|
| +#include <sys/file.h>
|
| +#include <sys/stat.h>
|
| +#include <sys/types.h>
|
| +#include <unistd.h>
|
| +
|
| +#include "base/logging.h"
|
| +#include "chromecast/base/path_utils.h"
|
| +
|
| +namespace chromecast {
|
| +
|
| +namespace {
|
| +
|
| +const mode_t kDirMode = 0770;
|
| +const mode_t kFileMode = 0660;
|
| +
|
| +} // namespace
|
| +
|
| +MinidumpManager::MinidumpManager() : nonblocking_(false), lockfile_fd_(-1) {
|
| + dump_path_ = GetHomePathASCII("minidumps").value();
|
| + lockfile_path_ = dump_path_ + "/" + "lockfile";
|
| +}
|
| +
|
| +MinidumpManager::~MinidumpManager() {
|
| + // Release the lock if held.
|
| + ReleaseLockFile();
|
| +}
|
| +
|
| +int MinidumpManager::GetNumDumps(bool delete_all_dumps) {
|
| + DIR* dirp;
|
| + struct dirent* dptr;
|
| + int num_dumps = 0;
|
| +
|
| + // folder does not exist
|
| + dirp = opendir(dump_path_.c_str());
|
| + if (dirp == NULL) {
|
| + LOG(ERROR) << "Unable to open directory " << dump_path_;
|
| + return 0;
|
| + }
|
| +
|
| + while ((dptr = readdir(dirp)) != NULL) {
|
| + struct stat buf;
|
| + const std::string file_fullname = dump_path_ + "/" + dptr->d_name;
|
| + if (lstat(file_fullname.c_str(), &buf) == -1 || !S_ISREG(buf.st_mode)) {
|
| + // if we cannot lstat this file, it is probably bad, so skip
|
| + // if the file is not regular, skip
|
| + continue;
|
| + }
|
| + // 'lockfile' is not counted
|
| + if (lockfile_path_ != file_fullname) {
|
| + ++num_dumps;
|
| + if (delete_all_dumps) {
|
| + LOG(INFO) << "Removing " << dptr->d_name
|
| + << "which was not in the lockfile";
|
| + if (remove(file_fullname.c_str()) < 0) {
|
| + LOG(INFO) << "remove failed. error " << strerror(errno);
|
| + }
|
| + }
|
| + }
|
| + }
|
| +
|
| + closedir(dirp);
|
| + return num_dumps;
|
| +}
|
| +
|
| +int MinidumpManager::DoWorkLocked() {
|
| + int success = -1;
|
| + if (AcquireLockFile() >= 0) {
|
| + success = DoWork();
|
| + ReleaseLockFile();
|
| + }
|
| + return success;
|
| +}
|
| +
|
| +int MinidumpManager::AcquireLockFile() {
|
| + // make the directory for the minidumps if it does not exist
|
| + if (mkdir(dump_path_.c_str(), kDirMode) < 0 && errno != EEXIST) {
|
| + LOG(ERROR) << "mkdir for " << dump_path_.c_str()
|
| + << " failed. error = " << strerror(errno);
|
| + return -1;
|
| + }
|
| +
|
| + lockfile_fd_ = open(lockfile_path_.c_str(), O_RDWR | O_CREAT, kFileMode);
|
| +
|
| + // if opening or creating the lockfile failed, we don't want to proceed
|
| + // with dump writing for fear of exhausting up system resources.
|
| + if (lockfile_fd_ < 0) {
|
| + LOG(ERROR) << "open lockfile failed " << lockfile_path_;
|
| + return -1;
|
| + }
|
| +
|
| + // acquire the lock on the file. Whether or not we are in non-blocking mode,
|
| + // flock failure means that we did not acquire it and this method should fail.
|
| + int operation_mode = nonblocking_ ? (LOCK_EX | LOCK_NB) : LOCK_EX;
|
| + if (flock(lockfile_fd_, operation_mode) < 0) {
|
| + ReleaseLockFile();
|
| + LOG(INFO) << "flock lockfile failed, error = " << strerror(errno);
|
| + return -1;
|
| + }
|
| +
|
| + return 0;
|
| +}
|
| +
|
| +void MinidumpManager::ReleaseLockFile() {
|
| + // flock is associated with the fd entry in the open fd table, so closing
|
| + // all fd's will release the lock. To be safe, we explicitly unlock.
|
| + if (lockfile_fd_ >= 0) {
|
| + flock(lockfile_fd_, LOCK_UN);
|
| + close(lockfile_fd_);
|
| + // We may use this object again, so we should reset this.
|
| + lockfile_fd_ = -1;
|
| + }
|
| +}
|
| +
|
| +} // namespace chromecast
|
|
|