blob: 8a092ecd118d98a9142485bbd189bc6d6b01d85e [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ken Mixter03403162010-08-18 15:23:16 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "unclean_shutdown_collector.h"
Ken Mixter03403162010-08-18 15:23:16 -070018
Ben Chanab6cc902014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070020#include <base/logging.h>
Ken Mixter03403162010-08-18 15:23:16 -070021
22static const char kUncleanShutdownFile[] =
23 "/var/lib/crash_reporter/pending_clean_shutdown";
24
Simon Que3f7ed5d2010-11-30 17:10:54 -080025// Files created by power manager used for crash reporting.
26static const char kPowerdTracePath[] = "/var/lib/power_manager";
27// Presence of this file indicates that the system was suspended
28static const char kPowerdSuspended[] = "powerd_suspended";
Simon Que3f7ed5d2010-11-30 17:10:54 -080029
Simon Que9f90aca2013-02-19 17:19:52 -080030using base::FilePath;
31
Ken Mixter03403162010-08-18 15:23:16 -070032UncleanShutdownCollector::UncleanShutdownCollector()
Simon Que3f7ed5d2010-11-30 17:10:54 -080033 : unclean_shutdown_file_(kUncleanShutdownFile),
34 powerd_trace_path_(kPowerdTracePath),
Daniel Erat9c9e1c42013-07-18 15:57:11 -070035 powerd_suspended_file_(powerd_trace_path_.Append(kPowerdSuspended)) {
Ken Mixter03403162010-08-18 15:23:16 -070036}
37
38UncleanShutdownCollector::~UncleanShutdownCollector() {
39}
40
41bool UncleanShutdownCollector::Enable() {
42 FilePath file_path(unclean_shutdown_file_);
Mike Frysingera557c112014-02-05 22:55:39 -050043 base::CreateDirectory(file_path.DirName());
Ben Chanf30c6412014-05-22 23:09:01 -070044 if (base::WriteFile(file_path, "", 0) != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -080045 LOG(ERROR) << "Unable to create shutdown check file";
Ken Mixter03403162010-08-18 15:23:16 -070046 return false;
47 }
48 return true;
49}
50
Simon Que3f7ed5d2010-11-30 17:10:54 -080051bool UncleanShutdownCollector::DeleteUncleanShutdownFiles() {
Mike Frysingera557c112014-02-05 22:55:39 -050052 if (!base::DeleteFile(FilePath(unclean_shutdown_file_), false)) {
Ken Mixtera3249322011-03-03 08:47:38 -080053 LOG(ERROR) << "Failed to delete unclean shutdown file "
54 << unclean_shutdown_file_;
Ken Mixter03403162010-08-18 15:23:16 -070055 return false;
56 }
Daniel Erat9c9e1c42013-07-18 15:57:11 -070057 // Delete power manager state file if it exists.
Mike Frysingera557c112014-02-05 22:55:39 -050058 base::DeleteFile(powerd_suspended_file_, false);
Ken Mixter03403162010-08-18 15:23:16 -070059 return true;
60}
61
62bool UncleanShutdownCollector::Collect() {
63 FilePath unclean_file_path(unclean_shutdown_file_);
Mike Frysingera557c112014-02-05 22:55:39 -050064 if (!base::PathExists(unclean_file_path)) {
Ken Mixter03403162010-08-18 15:23:16 -070065 return false;
66 }
Ken Mixtera3249322011-03-03 08:47:38 -080067 LOG(WARNING) << "Last shutdown was not clean";
Simon Que3f7ed5d2010-11-30 17:10:54 -080068 if (DeadBatteryCausedUncleanShutdown()) {
69 DeleteUncleanShutdownFiles();
70 return false;
71 }
72 DeleteUncleanShutdownFiles();
Ken Mixter03403162010-08-18 15:23:16 -070073
74 if (is_feedback_allowed_function_()) {
75 count_crash_function_();
76 }
77 return true;
78}
79
80bool UncleanShutdownCollector::Disable() {
Ken Mixtera3249322011-03-03 08:47:38 -080081 LOG(INFO) << "Clean shutdown signalled";
Simon Que3f7ed5d2010-11-30 17:10:54 -080082 return DeleteUncleanShutdownFiles();
83}
84
Daniel Erat9c9e1c42013-07-18 15:57:11 -070085bool UncleanShutdownCollector::DeadBatteryCausedUncleanShutdown() {
Simon Que3f7ed5d2010-11-30 17:10:54 -080086 // Check for case of battery running out while suspended.
Mike Frysingera557c112014-02-05 22:55:39 -050087 if (base::PathExists(powerd_suspended_file_)) {
Ken Mixtera3249322011-03-03 08:47:38 -080088 LOG(INFO) << "Unclean shutdown occurred while suspended. Not counting "
89 << "toward unclean shutdown statistic.";
Simon Que3f7ed5d2010-11-30 17:10:54 -080090 return true;
91 }
Simon Que3f7ed5d2010-11-30 17:10:54 -080092 return false;
Ken Mixter03403162010-08-18 15:23:16 -070093}