Android, CMDTool, stdout redirect, "Fatal signal 11 (SIGSEGV)"
Android, CMDTool, stdout redirect, "Fatal signal 11 (SIGSEGV)"
用例
Android C++命令行工具lunching"/system/bin/getprop"并将其stdout重定向到套接字
问题
Lunching'getprop'失败,返回"F/libc(20694):致命信号11(SIGSEGV),代码1,tid 20694(getprop)中的错误地址0x0",代码&下面是logcat片段。
为什么我得到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
}
...
logcat
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)
您滥用execvp()的方式不是getprop
所要处理的。引用OSX/BSD(但实际上是通用的)手册页中的execvp()
execv()、execvp()和execvp()函数提供了一个指针数组,指向以null结尾的字符串,这些字符串表示可用的参数列表到新程序。按照惯例,第一个论点应该指向与正在执行的文件相关联的文件名。的数组指针必须以NULL指针结束。
通过传递一个NULL元素的数组,您打破了这一惯例,当时,您应该将程序的名称作为第一个元素,然后传递一个终止的NULL元素
这将使getprop
失败,因为它考虑到只有一个参数(自身)的可能性,在这种情况下,它将显示所有内容。
if (argc == 1) {
list_properties();
但是,如果它没有1个参数,则会假设必须有两个或多个参数,并尝试取消引用第二个参数,应该是要获取的属性的名称。
property_get(argv[1], value, default_value);
将一个无效且可能为null的指针代替属性名传递给property_get()最终在仿生libc内部失败,可能是在__system_property_fild()的实现调用strlen()的地方。
如果您按照fadden的建议检查了堆栈跟踪,那么您可能会在libc中看到一个地址,很可能是strlen()。
相关文章:
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- SIGSEGV 使用shared_ptr引用时
- SIGSEGV, 分段错误. 而 printf() 数组索引的值
- SIGSEGV on Boost UDP 套接字关闭 - tcache_get at malloc.c.
- 分段故障 运行C++代码时出现 SIGSEGV
- 为什么我的递归代码给出 sigsegv 错误?
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- 调用 raise(signal) 会终止 c++ 程序
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 未调用 signal() 指定的处理程序
- 为什么在环路条件中使用'<='而不是'<'会产生分段错误 [SIGSEGV]?
- C++ - 程序接收信号SIGSEGV,分段错误.In msvcrt!memcpy () (C:\Windows\S
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 使用 dlopen 在 Mac 上加载 libjvm.dylib 时发出信号 SIGSEGV
- SIGSEGV(signal 11) 'segmentation fault' in FCTRL2 codechef
- Android, CMDTool, stdout redirect, "Fatal signal 11 (SIGSEGV)"
- 当我尝试从结构列表中访问内存时出现错误:" Program received signal SIGSEGV, Segmentation fault."
- "Program received signal SIGSEGV, Segmentation fault."
- 08-27 08:26:11.330: A/libc(23962): Fatal signal 11 (SIGSEGV)
- 在代码中调试我的C++项目时"Program received signal SIGSEGV, Segmentation fault. In ?? () ()":块