system调用总是返回-1

 在一个daemon程序中调用system来执行一段脚本,返回值总是-1.脚本简单的不能再简单了,只有一句exit 0,但还是返回-1.
查了很多资料,也没能找出原因,最后经过我反复的调试,终于发现了一个惊天动地的秘密!!

返回-1的原因是因为进程将SIGCHLD屏蔽了!!

确实我的程序在main函数里面就调用signal(SIGCHLD, SIG_IGN);将SIGCHLD屏蔽了!
system的调用过程主要是调三个函数
1.fork()
2.execl()
3.wait()
wait需要依赖SIGCHLD信号,所以屏蔽了SIGCHLD信号就永远得不到system正确的返回值了!

另外附一段正确判断脚本执行的方法:
system的返回值是按不同的字节置位的,0~7位和8~15位置的是不同的内容,判断脚本是否正确执行的正确做法是:

(1)-1 != status
(2)WIFEXITED(status)为真
(3)0 == WEXITSTATUS(status)

下面来解释一下后两个返回值的含义:

1,WIFEXITED(status) 这个宏用来指出子进程是否为正常退出的,如果是,它会返回一个非零值。

2,WEXITSTATUS(status) 当WIFEXITED返回非零值时,我们可以用这个宏来提取子进程的返回值,如果子进程调用exit(5)退出,WEXITSTATUS(status)就会返回5;如果子进程调用exit(7),WEXITSTATUS(status)就会返回7。


最后写了一段小的测试程序。下面 这段代码在2.6内核的系统上总是返回-1,但是奇怪的是在2.4内核的系统上返回0.
#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 
int main()
{
    cout<<"pid: "<<getpid()<<endl;
     int ret = 0;
    signal(SIGCHLD, SIG_IGN);
    ret = system("./wyf.sh");
    cout<<"ret: "<<ret<<endl;
      return 0;
}
原文链接:http://blog.chinaunix.net/uid-12274566-id-3508850.html

另外,贴上一段自己封装的,能够获取system正确返回值的函数
int my_system(const char *cmd)
{
int status = 0;
int ret = NGLOG_SUCCESS;
struct sigaction act, old_act;

act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;

sigaction(SIGCHLD, &act, &old_act);
    status = system(cmd); 
sigaction(SIGCHLD, &old_act, NULL);

if (-1 == status) {
_LOG_ERR("system: %s", strerror(errno));
ret = -1;
} else {
if (WIFEXITED(status)) {
ret =  WEXITSTATUS(status);
if (0 != ret) {
 printf("run shell script fail, script exit code: %d", ret);
}
}else{
printf("exit status = %d", WEXITSTATUS(status)); 
ret = -1;
}
}
return ret;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <sys/socket.h>
//#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <linux/kernel.h>

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,你可以使用 System Process、HW Binder 和 Vendor Process 的方式来调用 lib。这种方式可以提高系统的性能和安全性。下面是具体的方法: 1. 编写 lib 库代码 首先,你需要编写你的 lib 库代码,并将其打包成一个 .so 文件。你可以使用 C 或 C++ 编写这个库,然后使用 Android NDK 工具链来编译它。 2. 将 lib 库文件放到 vendor partition 中 将编译后的 .so 文件复制到 /vendor/lib 或 /vendor/lib64 目录中。这些目录是专门为供应商分区中的共享库而设计的。 3. 编写 System Process 代码 编写 System Process 代码,以便它可以使用 HW Binder 与 Vendor Process 通信。你可以使用 Android 开发工具包中的 Aidl 工具来定义接口。然后,你需要将接口实现添加到你的 System Process 代码中,以便它可以调用供应商分区中的 lib 库。 4. 编写 Vendor Process 代码 编写 Vendor Process 代码,以便它可以使用 HW Binder 与 System Process 通信。你需要使用 Aidl 工具来定义接口,然后将接口实现添加到你的 Vendor Process 代码中。 5. 连接 System Process 和 Vendor Process 使用 HW Binder 连接 System Process 和 Vendor Process。你可以使用 IBinder 接口来实现这个连接。当 System Process 调用供应商分区中的 lib 库时,它将通过 HW Binder 与 Vendor Process 进行通信,并返回结果。 总之,这种方式需要你有一定的 Android 开发经验。如果你不熟悉 Android 开发,建议先学习一下 Android 的基础知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值