已关闭
fix(init_cmds): syncexec/DumpFdInfo waitpid 增加超时 (高-6) #4964
chenjinxiang3创建于 9月3日关闭于 29 天前
fix(init_cmds): syncexec/DumpFdInfo waitpid 增加超时 (高-6) #4964
已关闭
chenjinxiang3创建于 9月3日关闭于 29 天前
共 1 个文件变更+26-6
@@ -152,6 +152,28 @@ int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, u
152 return 0;152 return 0;
153}153}
154 154 
155+#define SYNC_EXEC_TIMEOUT_MS 30000
156+#define FD_DUMP_TIMEOUT_MS 5000
157+static int WaitPidTimeout(pid_t pid, int timeoutMs)
158+{
159+ int status = 0;
160+ int retry = timeoutMs / 100;
161+ while (retry-- > 0) {
162+ pid_t r = waitpid(pid, &status, WNOHANG);
163+ if (r == pid) {
164+ return status;
165+ }
166+ if (r < 0) {
167+ return -1;
168+ }
169+ usleep(100 * 1000);
170+ }
171+ INIT_LOGE("wait pid %d timeout, force kill", pid);
172+ kill(pid, SIGKILL);
173+ waitpid(pid, &status, 0);
174+ return -1;
175+}
176+ 
155static int SyncExecCommand(int argc, char * const *argv)177static int SyncExecCommand(int argc, char * const *argv)
156{178{
157 INIT_LOGI("Sync exec: %s", argv[0]);179 INIT_LOGI("Sync exec: %s", argv[0]);
@@ -161,9 +183,8 @@ static int SyncExecCommand(int argc, char * const *argv)
161 INIT_CHECK_ONLY_ELOG(execv(argv[0], argv) == 0, "execv %s failed! err %d.", argv[0], errno);183 INIT_CHECK_ONLY_ELOG(execv(argv[0], argv) == 0, "execv %s failed! err %d.", argv[0], errno);
162 exit(-1);184 exit(-1);
163 }185 }
164- int status;186+ int status = WaitPidTimeout(pid, SYNC_EXEC_TIMEOUT_MS);
165- pid_t ret = waitpid(pid, &status, 0);187+ if (status < 0) {
166- if (ret != pid) {
167 INIT_LOGE("failed wait pid %d, errno %d", pid, errno);188 INIT_LOGE("failed wait pid %d, errno %d", pid, errno);
168 return -1;189 return -1;
169 }190 }
@@ -821,9 +842,8 @@ static void DumpFdInfo()
821 INIT_LOGE("fork failed, err is %d", errno);842 INIT_LOGE("fork failed, err is %d", errno);
822 return;843 return;
823 }844 }
824- int status;845+ int status = WaitPidTimeout(pid, FD_DUMP_TIMEOUT_MS);
825- pid_t ret = waitpid(pid, &status, 0);846+ if (status < 0) {
826- if (ret != pid) {
827 INIT_LOGE("failed wait pid %d, errno is %d", pid, errno);847 INIT_LOGE("failed wait pid %d, errno is %d", pid, errno);
828 }848 }
829 INIT_LOGI("dump fd info end");849 INIT_LOGI("dump fd info end");