1 /*
2  * Copyright (C) 2021 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 "checkpoint_handling.h"
18 #include "log.h"
19 
20 #include <fstab/fstab.h>
21 #include <unistd.h>
22 #include <cstring>
23 #include <string>
24 
25 #include <libgsi/libgsi.h>
26 
27 namespace {
28 
29 bool checkpointingDoneForever = false;
30 
31 }  // namespace
32 
is_data_checkpoint_active(bool * active)33 int is_data_checkpoint_active(bool* active) {
34     if (!active) {
35         ALOGE("active out parameter is null");
36         return 0;
37     }
38 
39     *active = false;
40 
41     if (checkpointingDoneForever) {
42         return 0;
43     }
44 
45     android::fs_mgr::Fstab procMounts;
46     bool success = android::fs_mgr::ReadFstabFromFile("/proc/mounts", &procMounts);
47     if (!success) {
48         ALOGE("Could not parse /proc/mounts\n");
49         /* Really bad. Tell the caller to abort the write. */
50         return -1;
51     }
52 
53     android::fs_mgr::FstabEntry* dataEntry =
54             android::fs_mgr::GetEntryForMountPoint(&procMounts, "/data");
55     if (dataEntry == NULL) {
56         ALOGE("/data is not mounted yet\n");
57         return 0;
58     }
59 
60     /* We can't handle e.g., ext4. Nothing we can do about it for now. */
61     if (dataEntry->fs_type != "f2fs") {
62         ALOGW("Checkpoint status not supported for filesystem %s\n", dataEntry->fs_type.c_str());
63         checkpointingDoneForever = true;
64         return 0;
65     }
66 
67     /*
68      * The data entry looks like "... blah,checkpoint=disable:0,blah ...".
69      * checkpoint=disable means checkpointing is on (yes, arguably reversed).
70      */
71     size_t checkpointPos = dataEntry->fs_options.find("checkpoint=disable");
72     if (checkpointPos == std::string::npos) {
73         /* Assumption is that once checkpointing turns off, it stays off */
74         checkpointingDoneForever = true;
75     } else {
76         *active = true;
77     }
78 
79     return 0;
80 }
81 
82 /**
83  * is_gsi_running() - Check if a GSI image is running via DSU.
84  *
85  * This function is equivalent to android::gsi::IsGsiRunning(), but this API is
86  * not yet vendor-accessible although the underlying metadata file is.
87  *
88  */
is_gsi_running()89 bool is_gsi_running() {
90     /* TODO(b/210501710): Expose GSI image running state to vendor storageproxyd */
91     return !access(android::gsi::kGsiBootedIndicatorFile, F_OK);
92 }
93