popen失败时如何解释pclose状态?

问题描述

我在嵌入式Linux(busybox)上运行的应用程序尝试通过popen(cmd,"r")执行脚本。
cmd = "sh /tmp/DF1_05/update.sh DF1_05"

我可以执行该脚本,而无需从sh手动启动它,但是在由应用程序启动时失败。
update.sh脚本的第一行是:

#!/bin/sh
echo "Starting update script"
....

我什至看不到echo的输出。

我的应用程序代码为:

sprintf(cmd,"sh /tmp/%s/update.sh %s",version_ihm_usb,version_ihm_usb);
printf("Executing script %s\n",cmd);
pipe_cmd = popen(cmd,"r");
if (pipe_cmd != NULL) {
    int ret = pclose(pipe_cmd);
    printf("pclose returned %d:%d,%d\n",ret,WIFEXITED(ret),WEXITSTATUS(ret));
    return 0;
} else {
    printf("Error executing script : %s\n",strerror(errno));
}

应用程序输出为:

Executing script sh /tmp/DF1_05/update.sh DF1_05
pclose returned 36096:1,141

因此,根据popen man pagepclose输出的状态类似于wait4的输出,并且由于WIFEXITED(ret)为真,WEXITSTATUS(ret)是孩子.......
好的,但是那之后对我来说是一次寻宝活动,实际上,我无法解释141是什么代码。

有人有更准确的信息吗?

解决方法

我无法解释代码141是什么。

摘自man popen我的重点:

popen()函数通过创建管道,分叉并调用外壳程序来打开进程。

来自bash manual,这是一个常见约定:

当命令终止于编号为N的致命信号时,Bash使用值128 + N作为退出状态。

来自man signal

   Signal        x86/ARM     Alpha/   MIPS   PARISC   Notes
               most others   SPARC
   ─────────────────────────────────────────────────────────────────
   SIGHUP           1           1       1       1
   .... other signals ....
   SIGPIPE         13          13      13      13

该过程已由SIGPIPE终止。 SIGPIPE在您的系统上为13。因此,您的外壳将退出状态返回为128 + 13 = 141

,

检查WIFSIGNALED(ret)WTERMSIG(ret)。您可能会发现该孩子已被SIGPIPE终止。

此外,pclose在这里不太可能失败,但是我会在将其返回值传递给等待宏之前检查其返回值。如果pclose返回

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...