Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include <android/log.h> |
michael@0 | 8 | #include <cutils/android_reboot.h> |
michael@0 | 9 | #include <errno.h> |
michael@0 | 10 | #include <stdlib.h> |
michael@0 | 11 | #include <sys/mount.h> |
michael@0 | 12 | #include <sys/reboot.h> |
michael@0 | 13 | #include <sys/types.h> |
michael@0 | 14 | #include <unistd.h> |
michael@0 | 15 | |
michael@0 | 16 | #include "automounter_gonk.h" |
michael@0 | 17 | #include "updatedefines.h" |
michael@0 | 18 | #include "updatelogging.h" |
michael@0 | 19 | |
michael@0 | 20 | #define LOG_TAG "GonkAutoMounter" |
michael@0 | 21 | |
michael@0 | 22 | #define GONK_LOG(level, format, ...) \ |
michael@0 | 23 | LOG((LOG_TAG ": " format "\n", ##__VA_ARGS__)); \ |
michael@0 | 24 | __android_log_print(level, LOG_TAG, format, ##__VA_ARGS__) |
michael@0 | 25 | |
michael@0 | 26 | #define LOGI(format, ...) GONK_LOG(ANDROID_LOG_INFO, format, ##__VA_ARGS__) |
michael@0 | 27 | #define LOGE(format, ...) GONK_LOG(ANDROID_LOG_ERROR, format, ##__VA_ARGS__) |
michael@0 | 28 | |
michael@0 | 29 | const char *kGonkMountsPath = "/proc/mounts"; |
michael@0 | 30 | const char *kGonkSystemPath = "/system"; |
michael@0 | 31 | |
michael@0 | 32 | GonkAutoMounter::GonkAutoMounter() : mDevice(nullptr), mAccess(Unknown) |
michael@0 | 33 | { |
michael@0 | 34 | if (!RemountSystem(ReadWrite)) { |
michael@0 | 35 | LOGE("Could not remount %s as read-write.", kGonkSystemPath); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | GonkAutoMounter::~GonkAutoMounter() |
michael@0 | 40 | { |
michael@0 | 41 | bool result = RemountSystem(ReadOnly); |
michael@0 | 42 | free(mDevice); |
michael@0 | 43 | |
michael@0 | 44 | if (!result) { |
michael@0 | 45 | // Don't take any chances when remounting as read-only fails, just reboot. |
michael@0 | 46 | Reboot(); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void |
michael@0 | 51 | GonkAutoMounter::Reboot() |
michael@0 | 52 | { |
michael@0 | 53 | // The android_reboot wrapper provides more safety, doing fancier read-only |
michael@0 | 54 | // remounting and attempting to sync() the filesystem first. If this fails |
michael@0 | 55 | // our only hope is to force a reboot directly without these protections. |
michael@0 | 56 | // For more, see system/core/libcutils/android_reboot.c |
michael@0 | 57 | LOGE("Could not remount %s as read-only, forcing a system reboot.", |
michael@0 | 58 | kGonkSystemPath); |
michael@0 | 59 | LogFlush(); |
michael@0 | 60 | |
michael@0 | 61 | if (android_reboot(ANDROID_RB_RESTART, 0, nullptr) != 0) { |
michael@0 | 62 | LOGE("Safe system reboot failed, attempting to force"); |
michael@0 | 63 | LogFlush(); |
michael@0 | 64 | |
michael@0 | 65 | if (reboot(RB_AUTOBOOT) != 0) { |
michael@0 | 66 | LOGE("CRITICAL: Failed to force restart"); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | static const char * |
michael@0 | 72 | MountAccessToString(MountAccess access) |
michael@0 | 73 | { |
michael@0 | 74 | switch (access) { |
michael@0 | 75 | case ReadOnly: return "read-only"; |
michael@0 | 76 | case ReadWrite: return "read-write"; |
michael@0 | 77 | default: return "unknown"; |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | bool |
michael@0 | 82 | GonkAutoMounter::RemountSystem(MountAccess access) |
michael@0 | 83 | { |
michael@0 | 84 | if (!UpdateMountStatus()) { |
michael@0 | 85 | return false; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | if (mAccess == access) { |
michael@0 | 89 | return true; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | unsigned long flags = MS_REMOUNT; |
michael@0 | 93 | if (access == ReadOnly) { |
michael@0 | 94 | flags |= MS_RDONLY; |
michael@0 | 95 | // Give the system a chance to flush file buffers |
michael@0 | 96 | sync(); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | if (!MountSystem(flags)) { |
michael@0 | 100 | return false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Check status again to verify /system has been properly remounted |
michael@0 | 104 | if (!UpdateMountStatus()) { |
michael@0 | 105 | return false; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | if (mAccess != access) { |
michael@0 | 109 | LOGE("Updated mount status %s should be %s", |
michael@0 | 110 | MountAccessToString(mAccess), |
michael@0 | 111 | MountAccessToString(access)); |
michael@0 | 112 | return false; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | return true; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | bool |
michael@0 | 119 | GonkAutoMounter::UpdateMountStatus() |
michael@0 | 120 | { |
michael@0 | 121 | FILE *mountsFile = NS_tfopen(kGonkMountsPath, "r"); |
michael@0 | 122 | |
michael@0 | 123 | if (mountsFile == nullptr) { |
michael@0 | 124 | LOGE("Error opening %s: %s", kGonkMountsPath, strerror(errno)); |
michael@0 | 125 | return false; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // /proc/mounts returns a 0 size from fstat, so we use the same |
michael@0 | 129 | // pre-allocated buffer size that ADB does here |
michael@0 | 130 | const int mountsMaxSize = 4096; |
michael@0 | 131 | char mountData[mountsMaxSize]; |
michael@0 | 132 | size_t read = fread(mountData, 1, mountsMaxSize - 1, mountsFile); |
michael@0 | 133 | mountData[read + 1] = '\0'; |
michael@0 | 134 | |
michael@0 | 135 | if (ferror(mountsFile)) { |
michael@0 | 136 | LOGE("Error reading %s, %s", kGonkMountsPath, strerror(errno)); |
michael@0 | 137 | fclose(mountsFile); |
michael@0 | 138 | return false; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | char *token, *tokenContext; |
michael@0 | 142 | bool foundSystem = false; |
michael@0 | 143 | |
michael@0 | 144 | for (token = strtok_r(mountData, "\n", &tokenContext); |
michael@0 | 145 | token; |
michael@0 | 146 | token = strtok_r(nullptr, "\n", &tokenContext)) |
michael@0 | 147 | { |
michael@0 | 148 | if (ProcessMount(token)) { |
michael@0 | 149 | foundSystem = true; |
michael@0 | 150 | break; |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | fclose(mountsFile); |
michael@0 | 155 | |
michael@0 | 156 | if (!foundSystem) { |
michael@0 | 157 | LOGE("Couldn't find %s mount in %s", kGonkSystemPath, kGonkMountsPath); |
michael@0 | 158 | } |
michael@0 | 159 | return foundSystem; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | bool |
michael@0 | 163 | GonkAutoMounter::ProcessMount(const char *mount) |
michael@0 | 164 | { |
michael@0 | 165 | const int strSize = 256; |
michael@0 | 166 | char mountDev[strSize]; |
michael@0 | 167 | char mountDir[strSize]; |
michael@0 | 168 | char mountAccess[strSize]; |
michael@0 | 169 | |
michael@0 | 170 | int rv = sscanf(mount, "%255s %255s %*s %255s %*d %*d\n", |
michael@0 | 171 | mountDev, mountDir, mountAccess); |
michael@0 | 172 | mountDev[strSize - 1] = '\0'; |
michael@0 | 173 | mountDir[strSize - 1] = '\0'; |
michael@0 | 174 | mountAccess[strSize - 1] = '\0'; |
michael@0 | 175 | |
michael@0 | 176 | if (rv != 3) { |
michael@0 | 177 | return false; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | if (strcmp(kGonkSystemPath, mountDir) != 0) { |
michael@0 | 181 | return false; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | free(mDevice); |
michael@0 | 185 | mDevice = strdup(mountDev); |
michael@0 | 186 | mAccess = Unknown; |
michael@0 | 187 | |
michael@0 | 188 | char *option, *optionContext; |
michael@0 | 189 | for (option = strtok_r(mountAccess, ",", &optionContext); |
michael@0 | 190 | option; |
michael@0 | 191 | option = strtok_r(nullptr, ",", &optionContext)) |
michael@0 | 192 | { |
michael@0 | 193 | if (strcmp("ro", option) == 0) { |
michael@0 | 194 | mAccess = ReadOnly; |
michael@0 | 195 | break; |
michael@0 | 196 | } else if (strcmp("rw", option) == 0) { |
michael@0 | 197 | mAccess = ReadWrite; |
michael@0 | 198 | break; |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | return true; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | bool |
michael@0 | 206 | GonkAutoMounter::MountSystem(unsigned long flags) |
michael@0 | 207 | { |
michael@0 | 208 | if (!mDevice) { |
michael@0 | 209 | LOGE("No device was found for %s", kGonkSystemPath); |
michael@0 | 210 | return false; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | const char *readOnly = flags & MS_RDONLY ? "read-only" : "read-write"; |
michael@0 | 214 | int result = mount(mDevice, kGonkSystemPath, "none", flags, nullptr); |
michael@0 | 215 | |
michael@0 | 216 | if (result != 0) { |
michael@0 | 217 | LOGE("Error mounting %s as %s: %s", kGonkSystemPath, readOnly, |
michael@0 | 218 | strerror(errno)); |
michael@0 | 219 | return false; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | LOGI("Mounted %s partition as %s", kGonkSystemPath, readOnly); |
michael@0 | 223 | return true; |
michael@0 | 224 | } |