[Feature][task-view-998]merge P56U10 version, ZXW code

Only Configure: No
Affected branch: master
Affected module: unknow
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: No
Doc Update: No

Change-Id: I466f2ab935c5ede0be1803c75518b2cc4f939c15
diff --git a/ap/app/busybox/src/init/halt.c b/ap/app/busybox/src/init/halt.c
index 7974adb..b7d439b 100644
--- a/ap/app/busybox/src/init/halt.c
+++ b/ap/app/busybox/src/init/halt.c
@@ -94,6 +94,81 @@
 #define write_wtmp() ((void)0)
 #endif
 
+/* Started by AICoder, pid:eecb340c7fw298714a9d0b3210b0288be1203207 */
+static void get_app_name_by_pid(int pid, char *app_name, int app_name_len)
+{
+	char file_comm[256];
+	FILE *pfile;
+	size_t len;
+	
+	memset(file_comm, 0, sizeof(file_comm));
+	snprintf(file_comm, sizeof(file_comm), "/proc/%d/comm", pid);
+
+	pfile = fopen(file_comm, "r");
+	if (pfile)
+	{
+		memset(app_name, 0, app_name_len);
+		fgets(app_name, app_name_len, pfile);
+		app_name[app_name_len-1] = '\0';
+		app_name[strlen(app_name) - 1] = '\0'; //last byte is \n
+		fclose(pfile);
+	}
+}
+
+static int get_ppid(int pid) {
+    char path[256];
+    snprintf(path, sizeof(path), "/proc/%d/stat", pid);
+    
+    FILE *fp = fopen(path, "r");
+    if (fp == NULL) {
+        perror("fopen");
+        return -1;
+    }
+    
+    int ppid = -1;
+    // 通过解析第4列(ppid)来获取父进程ID
+    fscanf(fp, "%*d %*s %*c %d", &ppid);
+    fclose(fp);
+    return ppid;
+}
+
+static int get_reboot_caller(char *applet_name)
+{
+	int pid = get_ppid(getpid());
+	char app_name[32];
+	int app_name_len = sizeof(app_name);
+	char cmd_str[256];
+	int try_cnt = 0;
+
+	while(1)
+	{
+		if (try_cnt > 5) {
+			strcpy(app_name, "unkown");
+			break;
+		}
+		try_cnt++;
+		if (pid == 1) {
+			get_app_name_by_pid(pid, app_name, app_name_len);
+			break; //init
+		}
+		get_app_name_by_pid(pid, app_name, app_name_len);
+		if ((strcmp(app_name, "sh") == 0) || (strcmp(app_name, "bash") == 0)) {
+			//printf("shell %s continue %d\n", app_name, strlen(app_name));
+			pid = get_ppid(pid); //sh continue
+		} else {
+			//printf("not sh break %s %d\n", app_name, strlen(app_name));
+			break; //not sh
+		}
+	}
+
+	memset(cmd_str, 0, sizeof(cmd_str));
+	snprintf(cmd_str, sizeof(cmd_str), "nv set ap_reset_app=%s_%d; nv save", app_name, pid);
+	system(cmd_str);
+	printf("call %s by %s(%d)\n", applet_name, app_name, pid);
+
+	return 0;
+}
+/* Ended by AICoder, pid:eecb340c7fw298714a9d0b3210b0288be1203207 */
 
 int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int halt_main(int argc UNUSED_PARAM, char **argv)
@@ -111,6 +186,10 @@
 	/* Figure out which applet we're running */
 	for (which = 0; "hpr"[which] != applet_name[0]; which++)
 		continue;
+#if ENABLE_INSTALL_TO_RECOVERY
+#else
+	get_reboot_caller(applet_name); //add by zxic, print parent proccess name and pid
+#endif
 
 	/* Parse and handle arguments */
 	opt_complementary = "d+"; /* -d N */