• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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  */
16 
17 #include "boot_event_record_store.h"
18 
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <utime.h>
23 
24 #include <chrono>
25 #include <cstdlib>
26 #include <string>
27 #include <utility>
28 
29 #include <android-base/chrono_utils.h>
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 #include <android-base/parseint.h>
33 
34 namespace {
35 
36 const char BOOTSTAT_DATA_DIR[] = "/data/misc/bootstat/";
37 
38 // Given a boot even record file at |path|, extracts the event's relative time
39 // from the record into |uptime|.
ParseRecordEventTime(const std::string & path,int32_t * uptime)40 bool ParseRecordEventTime(const std::string& path, int32_t* uptime) {
41   DCHECK_NE(static_cast<int32_t*>(nullptr), uptime);
42 
43   struct stat file_stat;
44   if (stat(path.c_str(), &file_stat) == -1) {
45     PLOG(ERROR) << "Failed to read " << path;
46     return false;
47   }
48 
49   *uptime = file_stat.st_mtime;
50 
51   return true;
52 }
53 
54 }  // namespace
55 
BootEventRecordStore()56 BootEventRecordStore::BootEventRecordStore() {
57   SetStorePath(BOOTSTAT_DATA_DIR);
58 }
59 
AddBootEvent(const std::string & event)60 void BootEventRecordStore::AddBootEvent(const std::string& event) {
61     auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
62         android::base::boot_clock::now().time_since_epoch());
63     AddBootEventWithValue(event, uptime.count());
64 }
65 
66 // The implementation of AddBootEventValue makes use of the mtime file
67 // attribute to store the value associated with a boot event in order to
68 // optimize on-disk size requirements and small-file thrashing.
AddBootEventWithValue(const std::string & event,int32_t value)69 void BootEventRecordStore::AddBootEventWithValue(
70     const std::string& event, int32_t value) {
71   std::string record_path = GetBootEventPath(event);
72   int record_fd = creat(record_path.c_str(), S_IRUSR | S_IWUSR);
73   if (record_fd == -1) {
74     PLOG(ERROR) << "Failed to create " << record_path;
75     return;
76   }
77 
78   // Fill out the stat structure for |record_path| in order to get the atime to
79   // set in the utime() call.
80   struct stat file_stat;
81   if (stat(record_path.c_str(), &file_stat) == -1) {
82     PLOG(ERROR) << "Failed to read " << record_path;
83     close(record_fd);
84     return;
85   }
86 
87   // Set the |modtime| of the file to store the value of the boot event while
88   // preserving the |actime| (as read by stat).
89   struct utimbuf times = {/* actime */ file_stat.st_atime, /* modtime */ value};
90   if (utime(record_path.c_str(), &times) == -1) {
91     PLOG(ERROR) << "Failed to set mtime for " << record_path;
92     close(record_fd);
93     return;
94   }
95 
96   close(record_fd);
97 }
98 
GetBootEvent(const std::string & event,BootEventRecord * record) const99 bool BootEventRecordStore::GetBootEvent(
100     const std::string& event, BootEventRecord* record) const {
101   CHECK_NE(static_cast<BootEventRecord*>(nullptr), record);
102   CHECK(!event.empty());
103 
104   const std::string record_path = GetBootEventPath(event);
105   int32_t uptime;
106   if (!ParseRecordEventTime(record_path, &uptime)) {
107     LOG(ERROR) << "Failed to parse boot time record: " << record_path;
108     return false;
109   }
110 
111   *record = std::make_pair(event, uptime);
112   return true;
113 }
114 
115 std::vector<BootEventRecordStore::BootEventRecord> BootEventRecordStore::
GetAllBootEvents() const116     GetAllBootEvents() const {
117   std::vector<BootEventRecord> events;
118 
119   std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(store_path_.c_str()), closedir);
120 
121   // This case could happen due to external manipulation of the filesystem,
122   // so crash out if the record store doesn't exist.
123   CHECK_NE(static_cast<DIR*>(nullptr), dir.get());
124 
125   struct dirent* entry;
126   while ((entry = readdir(dir.get())) != NULL) {
127     // Only parse regular files.
128     if (entry->d_type != DT_REG) {
129       continue;
130     }
131 
132     const std::string event = entry->d_name;
133     BootEventRecord record;
134     if (!GetBootEvent(event, &record)) {
135       LOG(ERROR) << "Failed to parse boot time event: " << event;
136       continue;
137     }
138 
139     events.push_back(record);
140   }
141 
142   return events;
143 }
144 
SetStorePath(const std::string & path)145 void BootEventRecordStore::SetStorePath(const std::string& path) {
146   DCHECK_EQ('/', path.back());
147   store_path_ = path;
148 }
149 
GetBootEventPath(const std::string & event) const150 std::string BootEventRecordStore::GetBootEventPath(
151     const std::string& event) const {
152   DCHECK_EQ('/', store_path_.back());
153   return store_path_ + event;
154 }
155