Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <vector> |
| 6 | #include <dirent.h> |
| 7 | #include <time.h> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 8 | #include <errno.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <sys/mount.h> |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 11 | #include <sys/reboot.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 12 | #include <sys/sendfile.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/vfs.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/wait.h> |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 17 | #ifdef ANDROID_RB_POWEROFF |
| 18 | #include "cutils/android_reboot.h" |
| 19 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <fstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 22 | #include <sstream> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 23 | #include "twrp-functions.hpp" |
| 24 | #include "partitions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 25 | #include "twcommon.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 26 | #include "data.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 27 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 28 | #include "bootloader.h" |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 29 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 30 | #include "openaes/inc/oaes_lib.h" |
| 31 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 32 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 33 | extern "C" { |
| 34 | #include "libcrecovery/common.h" |
| 35 | } |
| 36 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | /* Execute a command */ |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 38 | int TWFunc::Exec_Cmd(const string& cmd, string &result) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 39 | FILE* exec; |
| 40 | char buffer[130]; |
| 41 | int ret = 0; |
| 42 | exec = __popen(cmd.c_str(), "r"); |
| 43 | if (!exec) return -1; |
| 44 | while(!feof(exec)) { |
| 45 | memset(&buffer, 0, sizeof(buffer)); |
| 46 | if (fgets(buffer, 128, exec) != NULL) { |
| 47 | buffer[128] = '\n'; |
| 48 | buffer[129] = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 49 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 50 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 51 | } |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 52 | ret = __pclose(exec); |
| 53 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 56 | int TWFunc::Exec_Cmd(const string& cmd) { |
| 57 | pid_t pid; |
| 58 | int status; |
| 59 | switch(pid = fork()) |
| 60 | { |
| 61 | case -1: |
| 62 | LOGERR("Exec_Cmd(): vfork failed!\n"); |
| 63 | return -1; |
| 64 | case 0: // child |
| 65 | execl("/sbin/sh", "sh", "-c", cmd.c_str(), NULL); |
| 66 | _exit(127); |
| 67 | break; |
| 68 | default: |
| 69 | { |
| 70 | if (TWFunc::Wait_For_Child(pid, &status, cmd) != 0) |
| 71 | return -1; |
| 72 | else |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 78 | // Returns "file.name" from a full /path/to/file.name |
| 79 | string TWFunc::Get_Filename(string Path) { |
| 80 | size_t pos = Path.find_last_of("/"); |
| 81 | if (pos != string::npos) { |
| 82 | string Filename; |
| 83 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 84 | return Filename; |
| 85 | } else |
| 86 | return Path; |
| 87 | } |
| 88 | |
| 89 | // Returns "/path/to/" from a full /path/to/file.name |
| 90 | string TWFunc::Get_Path(string Path) { |
| 91 | size_t pos = Path.find_last_of("/"); |
| 92 | if (pos != string::npos) { |
| 93 | string Pathonly; |
| 94 | Pathonly = Path.substr(0, pos + 1); |
| 95 | return Pathonly; |
| 96 | } else |
| 97 | return Path; |
| 98 | } |
| 99 | |
| 100 | // Returns "/path" from a full /path/to/file.name |
| 101 | string TWFunc::Get_Root_Path(string Path) { |
| 102 | string Local_Path = Path; |
| 103 | |
| 104 | // Make sure that we have a leading slash |
| 105 | if (Local_Path.substr(0, 1) != "/") |
| 106 | Local_Path = "/" + Local_Path; |
| 107 | |
| 108 | // Trim the path to get the root path only |
| 109 | size_t position = Local_Path.find("/", 2); |
| 110 | if (position != string::npos) { |
| 111 | Local_Path.resize(position); |
| 112 | } |
| 113 | return Local_Path; |
| 114 | } |
| 115 | |
| 116 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 117 | int need_libs = 0; |
| 118 | |
| 119 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 120 | return; |
| 121 | |
| 122 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 123 | return; |
| 124 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 125 | gui_print("Installing HTC Dumlock to system...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 126 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 127 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 128 | gui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 129 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 130 | need_libs = 1; |
| 131 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 132 | gui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 133 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 134 | gui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 135 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 136 | need_libs = 1; |
| 137 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 138 | gui_print("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 139 | if (need_libs) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 140 | gui_print("Installing libs needed for flash_image and dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 141 | copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755); |
| 142 | copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755); |
| 143 | copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755); |
| 144 | copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 145 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 146 | gui_print("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 147 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 149 | copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 150 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 151 | gui_print("HTC Dumlock is installed.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 155 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 156 | return; |
| 157 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 158 | gui_print("Restoring original boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 159 | Exec_Cmd("htcdumlock restore"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | gui_print("Original boot restored.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 164 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 165 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 166 | gui_print("Reflashing recovery to boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 167 | Exec_Cmd("htcdumlock recovery noreboot"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 168 | gui_print("Recovery is flashed to boot.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 169 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 170 | |
| 171 | int TWFunc::Recursive_Mkdir(string Path) { |
| 172 | string pathCpy = Path; |
| 173 | string wholePath; |
| 174 | size_t pos = pathCpy.find("/", 2); |
| 175 | |
| 176 | while (pos != string::npos) |
| 177 | { |
| 178 | wholePath = pathCpy.substr(0, pos); |
| 179 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 180 | LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | |
| 184 | pos = pathCpy.find("/", pos + 1); |
| 185 | } |
| 186 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 187 | return false; |
| 188 | return true; |
| 189 | } |
| 190 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 191 | unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 192 | DIR* d; |
| 193 | struct dirent* de; |
| 194 | struct stat st; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 195 | unsigned long long dusize = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 196 | unsigned long long dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 197 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 198 | d = opendir(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 199 | if (d == NULL) |
| 200 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 201 | LOGERR("error opening '%s'\n", Path.c_str()); |
| 202 | LOGERR("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | while ((de = readdir(d)) != NULL) |
| 207 | { |
bigbiff bigbiff | 616afed | 2013-07-11 18:50:29 -0400 | [diff] [blame] | 208 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, "lost+found") != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 209 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 210 | dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | dusize += dutemp; |
| 212 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 213 | } |
| 214 | else if (de->d_type == DT_REG) |
| 215 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 216 | stat((Path + "/" + de->d_name).c_str(), &st); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 217 | dusize += (unsigned long long)(st.st_size); |
| 218 | } |
| 219 | } |
| 220 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 221 | return dusize; |
| 222 | } |
| 223 | |
| 224 | bool TWFunc::Path_Exists(string Path) { |
| 225 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 226 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 227 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 228 | return false; |
| 229 | else |
| 230 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 234 | string Display_Text; |
| 235 | |
| 236 | DataManager::GetValue(Read_Value, Display_Text); |
| 237 | if (Display_Text.empty()) |
| 238 | Display_Text = Default_Text; |
| 239 | |
| 240 | DataManager::SetValue("tw_operation", Display_Text); |
| 241 | DataManager::SetValue("tw_partition", ""); |
| 242 | } |
| 243 | |
| 244 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 245 | string Display_Text; |
| 246 | |
| 247 | DataManager::GetValue(Read_Value, Display_Text); |
| 248 | if (Display_Text.empty()) |
| 249 | Display_Text = Default_Text; |
| 250 | |
| 251 | DataManager::SetValue("tw_operation", Display_Text); |
| 252 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 256 | struct stat st; |
| 257 | |
| 258 | if (stat(Path.c_str(), &st) != 0) |
| 259 | return 0; |
| 260 | return st.st_size; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 263 | void TWFunc::Copy_Log(string Source, string Destination) { |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 264 | PartitionManager.Mount_By_Path(Destination, false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 265 | FILE *destination_log = fopen(Destination.c_str(), "a"); |
| 266 | if (destination_log == NULL) { |
| 267 | LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 268 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 269 | FILE *source_log = fopen(Source.c_str(), "r"); |
| 270 | if (source_log != NULL) { |
| 271 | fseek(source_log, Log_Offset, SEEK_SET); |
| 272 | char buffer[4096]; |
| 273 | while (fgets(buffer, sizeof(buffer), source_log)) |
| 274 | fputs(buffer, destination_log); // Buffered write of log file |
| 275 | Log_Offset = ftell(source_log); |
| 276 | fflush(source_log); |
| 277 | fclose(source_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 278 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 279 | fflush(destination_log); |
| 280 | fclose(destination_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 281 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 284 | void TWFunc::Update_Log_File(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 285 | // Copy logs to cache so the system can find out what happened. |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 286 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 287 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 288 | LOGINFO("Recreating /cache/recovery folder.\n"); |
| 289 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 290 | LOGINFO("Unable to create /cache/recovery folder.\n"); |
| 291 | } |
| 292 | Copy_Log(TMP_LOG_FILE, "/cache/recovery/log"); |
| 293 | copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600); |
| 294 | chown("/cache/recovery/log", 1000, 1000); |
| 295 | chmod("/cache/recovery/log", 0600); |
| 296 | chmod("/cache/recovery/last_log", 0640); |
| 297 | } else { |
| 298 | LOGINFO("Failed to mount /cache for TWFunc::Update_Log_File\n"); |
| 299 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 300 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 301 | // Reset bootloader message |
| 302 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 303 | if (Part != NULL) { |
| 304 | struct bootloader_message boot; |
| 305 | memset(&boot, 0, sizeof(boot)); |
| 306 | if (Part->Current_File_System == "mtd") { |
| 307 | if (set_bootloader_message_mtd_name(&boot, Part->MTD_Name.c_str()) != 0) |
| 308 | LOGERR("Unable to set MTD bootloader message.\n"); |
| 309 | } else if (Part->Current_File_System == "emmc") { |
| 310 | if (set_bootloader_message_block_name(&boot, Part->Actual_Block_Device.c_str()) != 0) |
| 311 | LOGERR("Unable to set emmc bootloader message.\n"); |
| 312 | } else { |
| 313 | LOGERR("Unknown file system for /misc: '%s'\n", Part->Current_File_System.c_str()); |
| 314 | } |
| 315 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 316 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 317 | if (PartitionManager.Mount_By_Path("/cache", true)) { |
| 318 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 319 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 320 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 321 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 322 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 323 | sync(); |
| 324 | } |
| 325 | |
| 326 | void TWFunc::Update_Intent_File(string Intent) { |
| 327 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
| 328 | TWFunc::write_file("/cache/recovery/intent", Intent); |
| 329 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 333 | int TWFunc::tw_reboot(RebootCommand command) |
| 334 | { |
| 335 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 336 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 337 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 338 | switch (command) { |
| 339 | case rb_current: |
| 340 | case rb_system: |
| 341 | Update_Log_File(); |
| 342 | Update_Intent_File("s"); |
| 343 | sync(); |
| 344 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
| 345 | return reboot(RB_AUTOBOOT); |
| 346 | case rb_recovery: |
| 347 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
| 348 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 349 | case rb_bootloader: |
| 350 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
| 351 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 352 | case rb_poweroff: |
| 353 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 354 | #ifdef ANDROID_RB_POWEROFF |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 355 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 356 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 357 | return reboot(RB_POWER_OFF); |
| 358 | case rb_download: |
| 359 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
| 360 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 361 | default: |
| 362 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 363 | } |
| 364 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 368 | { |
| 369 | // Check for and run startup script if script exists |
| 370 | struct stat st; |
| 371 | if (stat(script_file, &st) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 372 | gui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 373 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 374 | TWFunc::Exec_Cmd(script_file); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 375 | gui_print("\nFinished running %s script.\n", display_name); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 376 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 377 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 378 | |
| 379 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 380 | DIR *d = opendir(path.c_str()); |
| 381 | int r = 0; |
| 382 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 383 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 384 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 385 | LOGERR("Error opening '%s'\n", path.c_str()); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 386 | return -1; |
| 387 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 388 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 389 | if (d) { |
| 390 | struct dirent *p; |
| 391 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 392 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 393 | continue; |
| 394 | new_path = path + "/"; |
| 395 | new_path.append(p->d_name); |
| 396 | if (p->d_type == DT_DIR) { |
| 397 | r = removeDir(new_path, true); |
| 398 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 399 | if (p->d_type == DT_DIR) |
| 400 | r = rmdir(new_path.c_str()); |
| 401 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 402 | LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 403 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 404 | } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 405 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 406 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 407 | LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 408 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 412 | |
| 413 | if (!r) { |
| 414 | if (skipParent) |
| 415 | return 0; |
| 416 | else |
| 417 | r = rmdir(path.c_str()); |
| 418 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 419 | } |
| 420 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int TWFunc::copy_file(string src, string dst, int mode) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 424 | LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 425 | ifstream srcfile(src.c_str(), ios::binary); |
| 426 | ofstream dstfile(dst.c_str(), ios::binary); |
| 427 | dstfile << srcfile.rdbuf(); |
| 428 | srcfile.close(); |
| 429 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 430 | if (chmod(dst.c_str(), mode) != 0) |
| 431 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 432 | return 0; |
| 433 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 434 | |
| 435 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 436 | struct stat st; |
| 437 | |
| 438 | stat(Path.c_str(), &st); |
| 439 | if (st.st_mode & S_IFDIR) |
| 440 | return DT_DIR; |
| 441 | else if (st.st_mode & S_IFBLK) |
| 442 | return DT_BLK; |
| 443 | else if (st.st_mode & S_IFCHR) |
| 444 | return DT_CHR; |
| 445 | else if (st.st_mode & S_IFIFO) |
| 446 | return DT_FIFO; |
| 447 | else if (st.st_mode & S_IFLNK) |
| 448 | return DT_LNK; |
| 449 | else if (st.st_mode & S_IFREG) |
| 450 | return DT_REG; |
| 451 | else if (st.st_mode & S_IFSOCK) |
| 452 | return DT_SOCK; |
| 453 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 454 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 455 | |
| 456 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 457 | ifstream file; |
| 458 | file.open(fn.c_str(), ios::in); |
| 459 | |
| 460 | if (file.is_open()) { |
| 461 | file >> results; |
| 462 | file.close(); |
| 463 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 464 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 465 | |
| 466 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 467 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 471 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 472 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 473 | file.open(fn.c_str(), ios::in); |
| 474 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 475 | while (getline(file, line)) |
| 476 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 477 | file.close(); |
| 478 | return 0; |
| 479 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 480 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | int TWFunc::write_file(string fn, string& line) { |
| 485 | FILE *file; |
| 486 | file = fopen(fn.c_str(), "w"); |
| 487 | if (file != NULL) { |
| 488 | fwrite(line.c_str(), line.size(), 1, file); |
| 489 | fclose(file); |
| 490 | return 0; |
| 491 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 492 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 496 | vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) { |
| 497 | vector<string> res; |
| 498 | |
| 499 | if (in.empty() || del == '\0') |
| 500 | return res; |
| 501 | |
| 502 | string field; |
| 503 | istringstream f(in); |
| 504 | if (del == '\n') { |
| 505 | while(getline(f, field)) { |
| 506 | if (field.empty() && skip_empty) |
| 507 | continue; |
| 508 | res.push_back(field); |
| 509 | } |
| 510 | } else { |
| 511 | while(getline(f, field, del)) { |
| 512 | if (field.empty() && skip_empty) |
| 513 | continue; |
| 514 | res.push_back(field); |
| 515 | } |
| 516 | } |
| 517 | return res; |
| 518 | } |
| 519 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 520 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 521 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 522 | timespec temp; |
| 523 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 524 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 525 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 526 | } else { |
| 527 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 528 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 529 | } |
| 530 | return temp; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 533 | int TWFunc::drop_caches(void) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 534 | string file = "/proc/sys/vm/drop_caches"; |
| 535 | string value = "3"; |
| 536 | if (write_file(file, value) != 0) |
| 537 | return -1; |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | int TWFunc::Check_su_Perms(void) { |
| 542 | struct stat st; |
| 543 | int ret = 0; |
| 544 | |
| 545 | if (!PartitionManager.Mount_By_Path("/system", false)) |
| 546 | return 0; |
| 547 | |
| 548 | // Check to ensure that perms are 6755 for all 3 file locations |
| 549 | if (stat("/system/bin/su", &st) == 0) { |
| 550 | if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) { |
| 551 | ret = 1; |
| 552 | } |
| 553 | } |
| 554 | if (stat("/system/xbin/su", &st) == 0) { |
| 555 | if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) { |
| 556 | ret += 2; |
| 557 | } |
| 558 | } |
| 559 | if (stat("/system/bin/.ext/.su", &st) == 0) { |
| 560 | if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) { |
| 561 | ret += 4; |
| 562 | } |
| 563 | } |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | bool TWFunc::Fix_su_Perms(void) { |
| 568 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 569 | return false; |
| 570 | |
| 571 | string file = "/system/bin/su"; |
| 572 | if (TWFunc::Path_Exists(file)) { |
| 573 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 574 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 578 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | file = "/system/xbin/su"; |
| 583 | if (TWFunc::Path_Exists(file)) { |
| 584 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 585 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 586 | return false; |
| 587 | } |
| 588 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 589 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 590 | return false; |
| 591 | } |
| 592 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 593 | file = "/system/xbin/daemonsu"; |
| 594 | if (TWFunc::Path_Exists(file)) { |
| 595 | if (chown(file.c_str(), 0, 0) != 0) { |
| 596 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 597 | return false; |
| 598 | } |
| 599 | if (tw_chmod(file, "6755") != 0) { |
| 600 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 601 | return false; |
| 602 | } |
| 603 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 604 | file = "/system/bin/.ext/.su"; |
| 605 | if (TWFunc::Path_Exists(file)) { |
| 606 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 607 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 611 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 612 | return false; |
| 613 | } |
| 614 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 615 | file = "/system/etc/install-recovery.sh"; |
| 616 | if (TWFunc::Path_Exists(file)) { |
| 617 | if (chown(file.c_str(), 0, 0) != 0) { |
| 618 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 619 | return false; |
| 620 | } |
| 621 | if (tw_chmod(file, "0755") != 0) { |
| 622 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 623 | return false; |
| 624 | } |
| 625 | } |
| 626 | file = "/system/etc/init.d/99SuperSUDaemon"; |
| 627 | if (TWFunc::Path_Exists(file)) { |
| 628 | if (chown(file.c_str(), 0, 0) != 0) { |
| 629 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 630 | return false; |
| 631 | } |
| 632 | if (tw_chmod(file, "0755") != 0) { |
| 633 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 634 | return false; |
| 635 | } |
| 636 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 637 | file = "/system/app/Superuser.apk"; |
| 638 | if (TWFunc::Path_Exists(file)) { |
| 639 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 640 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 641 | return false; |
| 642 | } |
| 643 | if (tw_chmod(file, "0644") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 644 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | } |
| 648 | sync(); |
| 649 | if (!PartitionManager.UnMount_By_Path("/system", true)) |
| 650 | return false; |
| 651 | return true; |
| 652 | } |
| 653 | |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 654 | int TWFunc::tw_chmod(const string& fn, const string& mode) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 655 | long mask = 0; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 656 | std::string::size_type n = mode.length(); |
| 657 | int cls = 0; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 658 | |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 659 | if(n == 3) |
| 660 | ++cls; |
| 661 | else if(n != 4) |
| 662 | { |
| 663 | LOGERR("TWFunc::tw_chmod used with %u long mode string (should be 3 or 4)!\n", mode.length()); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | for (n = 0; n < mode.length(); ++n, ++cls) { |
| 668 | if (cls == 0) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 669 | if (mode[n] == '0') |
| 670 | continue; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 671 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 672 | mask |= S_ISVTX; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 673 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 674 | mask |= S_ISGID; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 675 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 676 | mask |= S_ISUID; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 677 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 678 | mask |= S_ISVTX; |
| 679 | mask |= S_ISUID; |
| 680 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 681 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 682 | mask |= S_ISGID; |
| 683 | mask |= S_ISUID; |
| 684 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 685 | else if (mode[n] == '7') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 686 | mask |= S_ISVTX; |
| 687 | mask |= S_ISGID; |
| 688 | mask |= S_ISUID; |
| 689 | } |
| 690 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 691 | else if (cls == 1) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 692 | if (mode[n] == '7') { |
| 693 | mask |= S_IRWXU; |
| 694 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 695 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 696 | mask |= S_IRUSR; |
| 697 | mask |= S_IWUSR; |
| 698 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 699 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 700 | mask |= S_IRUSR; |
| 701 | mask |= S_IXUSR; |
| 702 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 703 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 704 | mask |= S_IRUSR; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 705 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 706 | mask |= S_IWUSR; |
| 707 | mask |= S_IRUSR; |
| 708 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 709 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 710 | mask |= S_IWUSR; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 711 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 712 | mask |= S_IXUSR; |
| 713 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 714 | else if (cls == 2) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 715 | if (mode[n] == '7') { |
| 716 | mask |= S_IRWXG; |
| 717 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 718 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 719 | mask |= S_IRGRP; |
| 720 | mask |= S_IWGRP; |
| 721 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 722 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 723 | mask |= S_IRGRP; |
| 724 | mask |= S_IXGRP; |
| 725 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 726 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 727 | mask |= S_IRGRP; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 728 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 729 | mask |= S_IWGRP; |
| 730 | mask |= S_IXGRP; |
| 731 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 732 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 733 | mask |= S_IWGRP; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 734 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 735 | mask |= S_IXGRP; |
| 736 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 737 | else if (cls == 3) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 738 | if (mode[n] == '7') { |
| 739 | mask |= S_IRWXO; |
| 740 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 741 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 742 | mask |= S_IROTH; |
| 743 | mask |= S_IWOTH; |
| 744 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 745 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 746 | mask |= S_IROTH; |
| 747 | mask |= S_IXOTH; |
| 748 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 749 | else if (mode[n] == '4') |
| 750 | mask |= S_IROTH; |
| 751 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 752 | mask |= S_IWOTH; |
| 753 | mask |= S_IXOTH; |
| 754 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 755 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 756 | mask |= S_IWOTH; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 757 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 758 | mask |= S_IXOTH; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (chmod(fn.c_str(), mask) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 763 | LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 764 | return -1; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | bool TWFunc::Install_SuperSU(void) { |
| 771 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 772 | return false; |
| 773 | |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 774 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su"); |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 775 | if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 776 | LOGERR("Failed to copy su binary to /system/bin\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 777 | return false; |
| 778 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 779 | if (!Path_Exists("/system/bin/.ext")) { |
| 780 | mkdir("/system/bin/.ext", 0777); |
| 781 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 782 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 783 | if (copy_file("/supersu/su", "/system/bin/.ext/su", 0755) != 0) { |
| 784 | LOGERR("Failed to copy su binary to /system/bin/.ext/su\n"); |
| 785 | return false; |
| 786 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 787 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 788 | if (copy_file("/supersu/su", "/system/xbin/daemonsu", 0755) != 0) { |
| 789 | LOGERR("Failed to copy su binary to /system/xbin/daemonsu\n"); |
| 790 | return false; |
| 791 | } |
| 792 | if (Path_Exists("/system/etc/init.d")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 793 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 794 | if (copy_file("/supersu/99SuperSUDaemon", "/system/etc/init.d/99SuperSUDaemon", 0755) != 0) { |
| 795 | LOGERR("Failed to copy 99SuperSUDaemon to /system/etc/init.d/99SuperSUDaemon\n"); |
| 796 | return false; |
| 797 | } |
| 798 | } else { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 799 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 800 | if (copy_file("/supersu/install-recovery.sh", "/system/etc/install-recovery.sh", 0755) != 0) { |
| 801 | LOGERR("Failed to copy install-recovery.sh to /system/etc/install-recovery.sh\n"); |
| 802 | return false; |
| 803 | } |
| 804 | } |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 805 | if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 806 | LOGERR("Failed to copy Superuser app to /system/app\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | if (!Fix_su_Perms()) |
| 810 | return false; |
| 811 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 812 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 813 | |
| 814 | int TWFunc::Get_File_Type(string fn) { |
| 815 | string::size_type i = 0; |
| 816 | int firstbyte = 0, secondbyte = 0; |
| 817 | char header[3]; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 818 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 819 | ifstream f; |
| 820 | f.open(fn.c_str(), ios::in | ios::binary); |
| 821 | f.get(header, 3); |
| 822 | f.close(); |
| 823 | firstbyte = header[i] & 0xff; |
| 824 | secondbyte = header[++i] & 0xff; |
| 825 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 826 | if (firstbyte == 0x1f && secondbyte == 0x8b) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 827 | return 1; // Compressed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 828 | else if (firstbyte == 0x4f && secondbyte == 0x41) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 829 | return 2; // Encrypted |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 830 | else |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 831 | return 0; // Unknown |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | int TWFunc::Try_Decrypting_File(string fn, string password) { |
| 837 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 838 | OAES_CTX * ctx = NULL; |
| 839 | uint8_t _key_data[32] = ""; |
| 840 | FILE *f; |
| 841 | uint8_t buffer[4096]; |
| 842 | uint8_t *buffer_out = NULL; |
| 843 | uint8_t *ptr = NULL; |
| 844 | size_t read_len = 0, out_len = 0; |
| 845 | int firstbyte = 0, secondbyte = 0, key_len; |
| 846 | size_t _j = 0; |
| 847 | size_t _key_data_len = 0; |
| 848 | |
| 849 | // mostly kanged from OpenAES oaes.c |
| 850 | for( _j = 0; _j < 32; _j++ ) |
| 851 | _key_data[_j] = _j + 1; |
| 852 | _key_data_len = password.size(); |
| 853 | if( 16 >= _key_data_len ) |
| 854 | _key_data_len = 16; |
| 855 | else if( 24 >= _key_data_len ) |
| 856 | _key_data_len = 24; |
| 857 | else |
| 858 | _key_data_len = 32; |
| 859 | memcpy(_key_data, password.c_str(), password.size()); |
| 860 | |
| 861 | ctx = oaes_alloc(); |
| 862 | if (ctx == NULL) { |
| 863 | LOGERR("Failed to allocate OAES\n"); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | oaes_key_import_data(ctx, _key_data, _key_data_len); |
| 868 | |
| 869 | f = fopen(fn.c_str(), "rb"); |
| 870 | if (f == NULL) { |
| 871 | LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str()); |
| 872 | return -1; |
| 873 | } |
| 874 | read_len = fread(buffer, sizeof(uint8_t), 4096, f); |
| 875 | if (read_len <= 0) { |
| 876 | LOGERR("Read size during try decrypt failed\n"); |
| 877 | fclose(f); |
| 878 | return -1; |
| 879 | } |
| 880 | if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) { |
| 881 | LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n"); |
| 882 | fclose(f); |
| 883 | return -1; |
| 884 | } |
| 885 | buffer_out = (uint8_t *) calloc(out_len, sizeof(char)); |
| 886 | if (buffer_out == NULL) { |
| 887 | LOGERR("Failed to allocate output buffer for try decrypt.\n"); |
| 888 | fclose(f); |
| 889 | return -1; |
| 890 | } |
| 891 | if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) { |
| 892 | LOGERR("Failed to decrypt file '%s'\n", fn.c_str()); |
| 893 | fclose(f); |
| 894 | free(buffer_out); |
| 895 | return 0; |
| 896 | } |
| 897 | fclose(f); |
| 898 | if (out_len < 2) { |
| 899 | LOGINFO("Successfully decrypted '%s' but read length %i too small.\n", fn.c_str(), out_len); |
| 900 | free(buffer_out); |
| 901 | return 1; // Decrypted successfully |
| 902 | } |
| 903 | ptr = buffer_out; |
| 904 | firstbyte = *ptr & 0xff; |
| 905 | ptr++; |
| 906 | secondbyte = *ptr & 0xff; |
| 907 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 908 | LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str()); |
| 909 | free(buffer_out); |
| 910 | return 3; // Compressed |
| 911 | } |
| 912 | if (out_len >= 262) { |
| 913 | ptr = buffer_out + 257; |
| 914 | if (strncmp((char*)ptr, "ustar", 5) == 0) { |
| 915 | LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str()); |
| 916 | free(buffer_out); |
| 917 | return 2; // Tar |
| 918 | } |
| 919 | } |
| 920 | free(buffer_out); |
| 921 | LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str()); |
| 922 | return 1; // Decrypted successfully |
| 923 | #else |
| 924 | LOGERR("Encrypted backup support not included.\n"); |
| 925 | return -1; |
| 926 | #endif |
| 927 | } |
| 928 | |
| 929 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 930 | DIR* d; |
| 931 | |
| 932 | string Filename; |
| 933 | Restore_Path += "/"; |
| 934 | d = opendir(Restore_Path.c_str()); |
| 935 | if (d == NULL) { |
| 936 | LOGERR("Error opening '%s'\n", Restore_Path.c_str()); |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | struct dirent* de; |
| 941 | while ((de = readdir(d)) != NULL) { |
| 942 | Filename = Restore_Path; |
| 943 | Filename += de->d_name; |
| 944 | if (TWFunc::Get_File_Type(Filename) == 2) { |
| 945 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 946 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 947 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 948 | closedir(d); |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | closedir(d); |
| 954 | return true; |
| 955 | } |
| 956 | |
| 957 | int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) { |
| 958 | pid_t rc_pid; |
| 959 | |
| 960 | rc_pid = waitpid(pid, status, 0); |
| 961 | if (rc_pid > 0) { |
| 962 | if (WEXITSTATUS(*status) == 0) |
| 963 | LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success |
| 964 | else if (WIFSIGNALED(*status)) { |
| 965 | LOGINFO("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination |
| 966 | return -1; |
| 967 | } else if (WEXITSTATUS(*status) != 0) { |
| 968 | LOGINFO("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error |
| 969 | return -1; |
| 970 | } |
| 971 | } else { // no PID returned |
| 972 | if (errno == ECHILD) |
| 973 | LOGINFO("%s no child process exist\n", Child_Name.c_str()); |
| 974 | else { |
| 975 | LOGINFO("%s Unexpected error\n", Child_Name.c_str()); |
| 976 | return -1; |
| 977 | } |
| 978 | } |
| 979 | return 0; |
| 980 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 981 | |
| 982 | string TWFunc::Get_Current_Date() { |
| 983 | string Current_Date; |
| 984 | time_t seconds = time(0); |
| 985 | struct tm *t = localtime(&seconds); |
| 986 | char timestamp[255]; |
| 987 | sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec); |
| 988 | Current_Date = timestamp; |
| 989 | return Current_Date; |
| 990 | } |
| 991 | |
| 992 | void TWFunc::Auto_Generate_Backup_Name() { |
| 993 | bool mount_state = PartitionManager.Is_Mounted_By_Path("/system"); |
| 994 | std::vector<string> buildprop; |
| 995 | if (!PartitionManager.Mount_By_Path("/system", true)) { |
| 996 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 997 | return; |
| 998 | } |
| 999 | if (TWFunc::read_file("/system/build.prop", buildprop) != 0) { |
| 1000 | LOGINFO("Unable to open /system/build.prop for getting backup name.\n"); |
Vojtech Bocek | a2e7016 | 2013-09-17 17:05:10 +0200 | [diff] [blame] | 1001 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1002 | if (!mount_state) |
| 1003 | PartitionManager.UnMount_By_Path("/system", false); |
| 1004 | return; |
| 1005 | } |
| 1006 | int line_count = buildprop.size(); |
| 1007 | int index; |
| 1008 | size_t start_pos = 0, end_pos; |
| 1009 | string propname, propvalue; |
| 1010 | for (index = 0; index < line_count; index++) { |
| 1011 | end_pos = buildprop.at(index).find("=", start_pos); |
| 1012 | propname = buildprop.at(index).substr(start_pos, end_pos); |
| 1013 | if (propname == "ro.build.display.id") { |
| 1014 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
| 1015 | string Backup_Name = Get_Current_Date(); |
| 1016 | Backup_Name += " " + propvalue; |
| 1017 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 1018 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 1019 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | if (propvalue.empty()) { |
| 1024 | LOGINFO("ro.build.display.id not found in build.prop\n"); |
Vojtech Bocek | a2e7016 | 2013-09-17 17:05:10 +0200 | [diff] [blame] | 1025 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1026 | } |
| 1027 | if (!mount_state) |
| 1028 | PartitionManager.UnMount_By_Path("/system", false); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1029 | } |