Android OTA升級(jí)包制作腳本詳解(五,升級(jí)腳本updater-script的執(zhí)行<1>)
來源:程序員人生 發(fā)布時(shí)間:2015-03-17 08:39:51 閱讀次數(shù):5036次
寫在前面:
首先當(dāng)我們履行升級(jí)腳本updater-script的時(shí)候,就表示我們已進(jìn)入了升級(jí)安裝狀態(tài)。那末在我們就從實(shí)際的安假裝為入口開始分析。也就是說我們從install.cpp中的install_package函數(shù)開始1步步來分析。
這里主要分析與腳本相干的部份,其他的請(qǐng)參考這位朋友的博文http://blog.chinaunix.net/uid⑵2028566-id⑶533856.html,我也很受啟發(fā)。這里也借用1張圖來幫助流程上的分析。

下面是調(diào)用的流程:install_package()-->really_install_package(),那末在really_install_package()函數(shù)中真正開始安裝的邏輯以下:
/* Verify and install the contents of the package.
*/
ui->Print("Installing update...
");
err = try_update_binary(path, &zip, wipe_cache);
if(err != INSTALL_SUCCESS)
return err;
try_update_binary是真正實(shí)現(xiàn)讀取升級(jí)包中的腳本文件并履行相應(yīng)的函數(shù)。在此函數(shù)中,通過調(diào)用fork函數(shù)創(chuàng)建出1個(gè)子進(jìn)程,在子進(jìn)程中開始讀取并履行升級(jí)腳本文件。在此需要注意的是函數(shù)fork的用法,fork被調(diào)用1次,將做兩次返回,在父進(jìn)程中返回的是子進(jìn)程的進(jìn)程ID,為正數(shù);而在子進(jìn)程中,則返回0。子進(jìn)程中所進(jìn)行的操作,即execv(binary, args)。子進(jìn)程創(chuàng)建成功后,開始履行升級(jí)代碼,并通過管道與父進(jìn)程交互(創(chuàng)建管道,并將pipefd[1]作為參數(shù)傳遞給子進(jìn)程,子進(jìn)程則將相干信息寫入到此管道描寫符中);而父進(jìn)程則通過讀取子進(jìn)程傳遞過來的信息更新UI。
那末下面我們來具體來分析1下這個(gè)方法具體的邏輯。
static int
try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
//定義1個(gè)常量用來封裝查找相zip關(guān)信息的結(jié)果如“META-INF/com/google/android/update-binary”,便于后面進(jìn)行解壓,由于之前mzOpenZipArchive函數(shù)并沒有對(duì)更新包進(jìn)行解壓操作,*zip是之前mzOpenZipArchive方法返回的1個(gè)ZipArchive對(duì)象。mzOpenZipArchive是打開升級(jí)包,并將相干的信息拷貝到1個(gè)臨時(shí)的ZipArchinve變量中。
//ASSUMED_UPDATE_BINARY_NAME="META-INF/com/google/android/update-binary"
const ZipEntry* binary_entry =
mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
if (binary_entry == NULL) {
mzCloseZipArchive(zip);
return INSTALL_CORRUPT;
}
const char* binary = "/tmp/update_binary";
unlink(binary);
//創(chuàng)建“/tmp/update_binary”文件
int fd = creat(binary, 0755);
if (fd < 0) {
mzCloseZipArchive(zip);
LOGE("Can't make %s
", binary);
return INSTALL_ERROR;
}
//將META-INF/com/google/android/update-binary中的內(nèi)容解緊縮到"/tmp/update_binary"下面。其實(shí)這個(gè)方法中主要是對(duì)update_binary操作的,如解壓和履行等。
bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
close(fd);
mzCloseZipArchive(zip);
if (!ok) {
LOGE("Can't copy %s
", ASSUMED_UPDATE_BINARY_NAME);
return INSTALL_ERROR;
}
int pipefd[2];
//創(chuàng)建管道,用于下面的子進(jìn)程和父進(jìn)程之間的通訊
pipe(pipefd);
// When executing the update binary contained in the package, the
// arguments passed are:
//
// - the version number for this interface
//
// - an fd to which the program can write in order to update the
// progress bar. The program can write single-line commands:
//
// progress <frac> <secs>
// fill up the next <frac> part of of the progress bar
// over <secs> seconds. If <secs> is zero, use
// set_progress commands to manually control the
// progress of this segment of the bar
//
// set_progress <frac>
// <frac> should be between 0.0 and 1.0; sets the
// progress bar within the segment defined by the most
// recent progress command.
//
// firmware <"hboot"|"radio"> <filename>
// arrange to install the contents of <filename> in the
// given partition on reboot.
//
// (API v2: <filename> may start with "PACKAGE:" to
// indicate taking a file from the OTA package.)
//
// (API v3: this command no longer exists.)
//
// ui_print <string>
// display <string> on the screen.
//
// - the name of the package zip file.
//
const char** args = (const char**)malloc(sizeof(char*) * 5);
args[0] = binary;
args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
char* temp = (char*)malloc(10);
sprintf(temp, "%d", pipefd[1]);
args[2] = temp;
args[3] = (char*)path;
args[4] = NULL;
//創(chuàng)建子進(jìn)程。其中的子進(jìn)程主要負(fù)責(zé)履行binary(execv(binary,args),即履行我們的安裝命令腳本),父進(jìn)程負(fù)責(zé)接受子進(jìn)程發(fā)送的命令去更新ui顯示(顯示當(dāng)前的進(jìn)度)。子父進(jìn)程間通訊依托管道。
pid_t pid = fork();
if (pid == 0) {
close(pipefd[0]);
//履行<span style="font-family: Arial, Helvetica, sans-serif;">update-binary</span><span style="font-family: Arial, Helvetica, sans-serif;">,這個(gè)程序的實(shí)質(zhì)就是去解析update.zip包中的updater-script腳本中的命令并履行。由此,Recovery服務(wù)就進(jìn)入了實(shí)際安裝update.zip包的進(jìn)程。</span><span style="font-family: Arial, Helvetica, sans-serif;">在履行execv函數(shù)時(shí),execv會(huì)停止履行當(dāng)前的進(jìn)程,并且以progname利用進(jìn)程替換被停止履行的進(jìn)程,進(jìn)程ID沒有改變。這里的progname就是指</span><span style="font-family: Arial, Helvetica, sans-serif;">update-binary</span><span style="font-family: Arial, Helvetica, sans-serif;">,也就是被履行的利用程序。第2個(gè)參數(shù)argv,是指運(yùn)行</span><span style="font-family: Arial, Helvetica, sans-serif;">update-binary</span><span style="font-family: Arial, Helvetica, sans-serif;">t時(shí),傳遞給履行程序的參數(shù)列表, 注意,這個(gè)數(shù)組的第1個(gè)參數(shù)應(yīng)當(dāng)是利用程序名字本身,并且最后1個(gè)參數(shù)應(yīng)當(dāng)為NULL,不參將多個(gè)參數(shù)合并為1個(gè)參數(shù)放入數(shù)組。</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span>另外,如果利用程序正常履行終了,那末execv是永久不會(huì)返回的;當(dāng)execv在調(diào)用進(jìn)程中返回時(shí),那末這個(gè)利用程序應(yīng)當(dāng)出錯(cuò)了(多是程序本身沒找到,權(quán)限不夠...),此時(shí)它的返回值應(yīng)當(dāng)是⑴,具體的毛病代碼可以通過全局變量errno查看,還可以通過stderr得到具體的毛病描寫字符串。
execv(binary, (char* const*)args);//其實(shí)我的理解就是<span style="font-family: Arial, Helvetica, sans-serif;">update-binary相當(dāng)1個(gè)exe可履行程序,這里就是1個(gè)雙擊可履行程序的動(dòng)作</span>
fprintf(stdout, "E:Can't run %s (%s)
", binary, strerror(errno));
_exit(⑴);
}
close(pipefd[1]);
*wipe_cache = 0;
char buffer[1024];
FILE* from_child = fdopen(pipefd[0], "r");
while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
char* command = strtok(buffer, "
");
if (command == NULL) {
continue;
} else if (strcmp(command, "progress") == 0) {
char* fraction_s = strtok(NULL, "
");
char* seconds_s = strtok(NULL, "
");
float fraction = strtof(fraction_s, NULL);
int seconds = strtol(seconds_s, NULL, 10);
ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
} else if (strcmp(command, "set_progress") == 0) {
char* fraction_s = strtok(NULL, "
");
float fraction = strtof(fraction_s, NULL);
ui->SetProgress(fraction);
} else if (strcmp(command, "ui_print") == 0) {
char* str = strtok(NULL, "
");
if (str) {
ui->Print("%s", str);
} else {
ui->Print("
");
}
fflush(stdout);
} else if (strcmp(command, "wipe_cache") == 0) {
*wipe_cache = 1;
#if 1 //wschen 2012-07⑵5
} else if (strcmp(command, "special_factory_reset") == 0) {
*wipe_cache = 2;
#endif
} else if (strcmp(command, "clear_display") == 0) {
ui->SetBackground(RecoveryUI::NONE);
} else {
LOGE("unknown command [%s]
", command);
}
}
fclose(from_child);
int status;
waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
LOGE("Error in %s
(Status %d)
", path, WEXITSTATUS(status));
return INSTALL_ERROR;
}
#ifdef SUPPORT_DATA_BACKUP_RESTORE //wschen 2011-03-09
//Skip userdata restore if updating from /data with no /data layout change
if(!usrdata_changed && update_from_data){
ui->Print("/data offset remains the same no need to restore usrdata
");
}else{
if (part_size_changed) {
if (ensure_path_mounted("/sdcard") != 0) {
LOGE("Can't mount %s
", path);
return INSTALL_NO_SDCARD;
}
if (userdata_restore(backup_path, 1)) {
return INSTALL_FILE_SYSTEM_ERROR;
}
}
}
#endif //SUPPORT_DATA_BACKUP_RESTORE
/* ----------------------------- */
/* SECURE BOOT UPDATE */
/* ----------------------------- */
#ifdef SUPPORT_SBOOT_UPDATE
sec_update(false);
#endif
return INSTALL_SUCCESS;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)