【发布时间】:2015-04-17 22:29:34
【问题描述】:
用例
Android C++ 命令行工具午餐“/system/bin/getprop”并将其标准输出重定向到套接字
问题
午餐 'getprop' 失败并显示“F/libc (20694): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 20694 (getprop)”, code & logcat sn-p follow.
为什么我会得到 SIGSEGV?为什么分叉的进程会失败?
实施
HRESULT ServiceCore::CreateProcessAndRedirectStdout(IN char* pCmd, IN char** args, OUT SOCKET& stdoutstream) {
HRESULT hr = S_OK;
int fds[2] = { 0 };
pid_t pid = 0;
stdoutstream = 0;
if (SOCKET_ERROR == socketpair(PF_UNIX, SOCK_STREAM, 0, fds))
goto ErrExit;
stdoutstream = fds[1];
if((pid = fork()) < 0)
goto ErrExit;
if (pid == 0) {
// The newly created process
__android_log_print(ANDROID_LOG_INFO, "ServiceCore", "CreateProcessAndRedirectStdout>> '%s' started", pCmd);
int newfd = dup2(fds[0], STDOUT_FILENO);
__android_log_print(ANDROID_LOG_INFO, "ServiceCore", "CreateProcessAndRedirectStdout>> '%s' newfd:%d, oldfd:%d", pCmd, newfd, fds[0]);
_ASSERT(newfd == STDOUT_FILENO);
close(fds[0]);
close(fds[1]);
execvp(pCmd, args);
__android_log_print(ANDROID_LOG_INFO, "ServiceCore", "CreateProcessAndRedirectStdout>> '%s' FAILED", pCmd);
exit(1);
}
__android_log_print(ANDROID_LOG_INFO, "ServiceCore", "CreateProcessAndRedirectStdout>> '%s(%d)' Created", pCmd, pid);
return S_OK;
ErrExit:
if (0 != pid)
kill(pid, SIGTERM);
close(fds[0]);
close(fds[1]);
stdoutstream = 0;
return hr;
}
...
char* args[] = { 0 };
SOCKET soc;
if (SUCCEEDED(hr = CreateProcessAndRedirectStdout("/system/bin/getprop", args, soc))) {
errno = 0;
__android_log_print(ANDROID_LOG_INFO, "ServiceCore", "SendConfig>> Reading props");
char pTmp[256000];
int iRet = read(soc, pTmp, sizeof(pTmp));// Read the first few lines
}
...
日志猫
I/ServiceCore(20689): CreateProcessAndRedirectStdout>> '/system/bin/getprop(20694)' Created
I/ServiceCore(20689): SendConfig>> Reading props
I/ServiceCore(20694): CreateProcessAndRedirectStdout>> '/system/bin/getprop' started
I/ServiceCore(20694): CreateProcessAndRedirectStdout>> '/system/bin/getprop' newfd:1, oldfd:10
F/libc (20694): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 20694 (getprop)
【问题讨论】:
-
我不确定我是否看到了这个问题?
-
听起来像是在取消引用空指针。您需要查看 logcat 输出中的堆栈跟踪以找出位置,这应该会引导您了解什么/为什么。
标签: android c++ linux android-ndk android-source