Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Steve Fung | 129bea5 | 2015-07-23 13:11:15 -0700 | [diff] [blame] | 17 | #include "unclean_shutdown_collector.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 18 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 19 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 21 | |
| 22 | static const char kUncleanShutdownFile[] = |
| 23 | "/var/lib/crash_reporter/pending_clean_shutdown"; |
| 24 | |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 25 | // Files created by power manager used for crash reporting. |
| 26 | static const char kPowerdTracePath[] = "/var/lib/power_manager"; |
| 27 | // Presence of this file indicates that the system was suspended |
| 28 | static const char kPowerdSuspended[] = "powerd_suspended"; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 29 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 30 | using base::FilePath; |
| 31 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 32 | UncleanShutdownCollector::UncleanShutdownCollector() |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 33 | : unclean_shutdown_file_(kUncleanShutdownFile), |
| 34 | powerd_trace_path_(kPowerdTracePath), |
Daniel Erat | 9c9e1c4 | 2013-07-18 15:57:11 -0700 | [diff] [blame] | 35 | powerd_suspended_file_(powerd_trace_path_.Append(kPowerdSuspended)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | UncleanShutdownCollector::~UncleanShutdownCollector() { |
| 39 | } |
| 40 | |
| 41 | bool UncleanShutdownCollector::Enable() { |
| 42 | FilePath file_path(unclean_shutdown_file_); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 43 | base::CreateDirectory(file_path.DirName()); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 44 | if (base::WriteFile(file_path, "", 0) != 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 45 | LOG(ERROR) << "Unable to create shutdown check file"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 51 | bool UncleanShutdownCollector::DeleteUncleanShutdownFiles() { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 52 | if (!base::DeleteFile(FilePath(unclean_shutdown_file_), false)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 53 | LOG(ERROR) << "Failed to delete unclean shutdown file " |
| 54 | << unclean_shutdown_file_; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
Daniel Erat | 9c9e1c4 | 2013-07-18 15:57:11 -0700 | [diff] [blame] | 57 | // Delete power manager state file if it exists. |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 58 | base::DeleteFile(powerd_suspended_file_, false); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool UncleanShutdownCollector::Collect() { |
| 63 | FilePath unclean_file_path(unclean_shutdown_file_); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 64 | if (!base::PathExists(unclean_file_path)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 65 | return false; |
| 66 | } |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 67 | LOG(WARNING) << "Last shutdown was not clean"; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 68 | if (DeadBatteryCausedUncleanShutdown()) { |
| 69 | DeleteUncleanShutdownFiles(); |
| 70 | return false; |
| 71 | } |
| 72 | DeleteUncleanShutdownFiles(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 73 | |
| 74 | if (is_feedback_allowed_function_()) { |
| 75 | count_crash_function_(); |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool UncleanShutdownCollector::Disable() { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 81 | LOG(INFO) << "Clean shutdown signalled"; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 82 | return DeleteUncleanShutdownFiles(); |
| 83 | } |
| 84 | |
Daniel Erat | 9c9e1c4 | 2013-07-18 15:57:11 -0700 | [diff] [blame] | 85 | bool UncleanShutdownCollector::DeadBatteryCausedUncleanShutdown() { |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 86 | // Check for case of battery running out while suspended. |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 87 | if (base::PathExists(powerd_suspended_file_)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 88 | LOG(INFO) << "Unclean shutdown occurred while suspended. Not counting " |
| 89 | << "toward unclean shutdown statistic."; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 90 | return true; |
| 91 | } |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 92 | return false; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 93 | } |