System函数返回值 和popen

 

曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入。只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值、它所执行命令的返回值以及命令执行失败原因如何定位,这才是重点。当初因为这个函数风险较多,故抛弃不用,改用其他的方法。这里先不说我用了什么方法,这里必须要搞懂system()函数,因为还是有很多人用了system()函数,有时你不得不面对它。

 

 

 

先来看一下system()函数的简单介绍:

 

#include <stdlib.h>
int system(const char *command);

 

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

 

system()函数调用/bin/sh来执行参数指定的命令,/bin/sh 一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令;

 

在该command执行期间,SIGCHLD是被阻塞的,好比在说:hi,内核,这会不要给我送SIGCHLD信号,等我忙完再说;

 

在该command执行期间,SIGINT和SIGQUIT是被忽略的,意思是进程收到这两个信号后没有任何动作。

 

 

 

再来看一下system()函数返回值:

 

The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).
If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.

 

为了更好的理解system()函数返回值,需要了解其执行过程,实际上system()函数执行了三步操作:

 

1.fork一个子进程;

 

2.在子进程中调用exec函数去执行command;

 

3.在父进程中调用wait去等待子进程结束。

 

对于fork失败,system()函数返回-1。

 

如果exec执行成功,也即command顺利执行完毕,则返回command通过exit或return返回的值。

 

(注意,command顺利执行不代表执行成功,比如command:"rm debuglog.txt",不管文件存不存在该command都顺利执行了)

 

如果exec执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127.

 

如果command为NULL,则system()函数返回非0值,一般为1.

 

 

 

看一下system()函数的源码

 

看完这些,我想肯定有人对system()函数返回值还是不清楚,看源码最清楚,下面给出一个system()函数的实现:

 

int system(const char * cmdstring)
{
    pid_t pid;
    int status;

if(cmdstring == NULL)
{
    return (1); //如果cmdstring为空,返回非零值,一般为1
}

if((pid = fork())<0)
{
    status = -1; //fork失败,返回-1
}
else if(pid == 0)
{
    execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
    _exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~
}
else //父进程
{
    while(waitpid(pid, &status, 0) < 0)
    {
        if(errno != EINTR)
        {
            status = -1; //如果waitpid被信号中断,则返回-1
            break;
        }
    }
}

    return status; //如果waitpid成功,则返回子进程的返回状态
}

 

仔细看完这个system()函数的简单实现,那么该函数的返回值就清晰了吧,那么什么时候system()函数返回0呢?只在command命令返回0时。

 

 

 

看一下该怎么监控system()函数执行状态

 

这里给我出的做法:

 

int status;
if(NULL == cmdstring) //如果cmdstring为空趁早闪退吧,尽管system()函数也能处理空指针
{
    return XXX;
}
status = system(cmdstring);
if(status < 0)
{
    printf("cmd: %s\t error: %s", cmdstring, strerror(errno)); // 这里务必要把errno信息输出或记入Log
    return XXX;
}

if(WIFEXITED(status))
{
    printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
}
else if(WIFSIGNALED(status))
{
    printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断,取得信号值
}
else if(WIFSTOPPED(status))
{
    printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行,取得信号值
}

 

 

到于取得子进程返回值的相关介绍可以参考另一篇文章:http://my.oschina.net/renhc/blog/35116

 

 

 

system()函数用起来很容易出错,返回值太多,而且返回值很容易跟command的返回值混淆。这里推荐使用popen()函数替代,关于popen()函数的简单使用也可以通过上面的链接查看。

 

popen()函数较于system()函数的优势在于使用简单,popen()函数只返回两个值:
成功返回子进程的status,使用WIFEXITED相关宏就可以取得command的返回结果;
失败返回-1,我们可以使用perro()函数或strerror()函数得到有用的错误信息。

 

这篇文章只涉及了system()函数的简单使用,还没有谈及SIGCHLD、SIGINT和SIGQUIT对system()函数的影响,事实上,之所以今天写这篇文章,是因为项目中因有人使用了system()函数而造成了很严重的事故。现像是system()函数执行时会产生一个错误:“No child processes”。

 

关于这个错误的分析,感兴趣的朋友可以看一下:http://my.oschina.net/renhc/blog/54582

 

 

=======================================================================================

 

  APUE这本书,对system这个函数已经将的比较明白了,只是它的相关知识稍显分散。最开始我是去网上找的资料,自己写的测试代码,可是还是有很多迷惑的地方。后来才拿起APUE ,好好读了第八章和第十章的相关章节。    

  1. #include <stdlib.h>
  2. int system(const char *command);
    system的作用是在shell终端执行command。简单的说就是在C中执行system("ls")这行代码的含义就相当于在shell执行ls一样。这么说还是比较笼统,下面详细描述之:
 
    system是个综合的操作,分解开来看就是相当于执行了
1 fork  生成一个子进程。
2 在子进程执行 execl("/bin/sh","sh","-c" command,(char*)0);
3 waitpid
 
下面进入正题,返回值:
1 如果fork失败了,或者waitpid返回除了EINTR之外的错误,system返回 -1;
2 execl执行失败,其返回值如同shell执行了"exit(127)" 一样。
3 如果上述三步都执行成功,那么,system返回值是shell的终止状态。
 
 
上面这些话是APUE的,很抽象,很不具体,很笼统,我现在结合手册和代码解释一下。
 
手册中有这么一段话:
  1. The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter  return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status)In case /bin/sh could not be executed, the exit status will be that of a command that does
  2.  exit(127).
1 如果/bin/sh拉起shell命令失败,或者是shell命令没有正常执行 (比如命令根本就是非法的命令),那么,将原因填入status的8~15位
  手册中也说如果命令执行不起来,那么相当于执行exit
  1. libin@libin:~/program/C/Linux/system$ ./tsys "nosuchcmd"
  2. sh: nosuchcmd: not found
  3. status = 32512
  4. normal termination,exit status = 127
们看到了,nosuchcmd不是shell支持的命令,所以,shell命令返回了127,对于system函数,返回值为127*256 = 32512;因为shell的返回值是 system返回值的8~15位
 
2  如果shell顺利执行完毕,那么将shell的返回值填到system返回值的8~15位
 这里需要强调以下,所谓顺利执行完毕,并不是说,命令command执行成功,而是指  /bin/sh顺利调用,执行期间没有被信号异常终止,这都算是顺利执行完毕。
  
看下面的例子:
 
  1. libin@libin:~/program/C/Linux/system$ ./tsys "ls /noexisted"
  2. ls: 无法访问/noexisted: 没有那个文件或目录
  3. status = 512
  4. normal termination,exit status = 2
  5.  
  6. libin@libin:~/program/C/Linux/system$ ls /noexist
  7. ls: 无法访问/noexist: 没有那个文件或目录
  8. libin@libin:~/program/C/Linux/system$ echo $?
  9. 2
  10. libin@libin:~/program/C/Linux/system$ 
    我们看到了,虽然/noexist文件并不存在,ls这条命令执行出了错,但是仍然属于shell顺利执行完毕。ls /noexist的错误吗是2,所以,system函数的返回值为 2*256 = 512.
 
    各位可能比较感兴趣的是,如果我知道system的返回值,如何知道我的命令的返回值呢?手册中有这么一句话:
  1. Thus, the exit code of the command will be WEXITSTATUS(status)
    看到了WEXITSTATUS(status),就是command的返回值。当然前提条件是shell命令顺利执行完毕。即
  1. WIFEXITED(status) ! =0
 
3 前面的讨论都没有考虑信号,如果shell在执行过程中收到了信号。
这个问题我存在有疑惑,因为APUE第十章讲到,system的实现要忽略SIGINT和SIGQUIT,但是我的实验结果并不是这样,如有高手知道,请不吝赐教。
 
首先看我的终端信号配置:
  1. libin@libin:~/program/C/Linux/system$ stty -a
  2. speed 38400 baud; rows 36; columns 134; line = 0;
  3. intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S;susp = ^Z;
  4. rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
  5. -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
  6. -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8
  7. opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
  8. isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
    Ctrl+C产生SIGINT,Ctrl+\ 产生 SIGQUIT。
    看下测试结果
  1. libin@libin:~/program/C/Linux/system$ ./tsys "sleep 7"
  2. ^Cstatus = 2
  3. abnormal termination,signal number =2
  4. libin@libin:~/program/C/Linux/system$ sleep 7
  5. ^C
  6. libin@libin:~/program/C/Linux/system$ echo $?
  7. 130
    我们可以看到,直接在终端执行sleep 7,然后用Ctrl+C发送SIGINT信号,异常退出,shell返回值为130,130的原因,APUE上解释了,因为SIGINT 等于2,终止状态是128+信号编号,所以为130.
 
    按照APUE上,我们的system调用应该将SIGINT忽略掉,然后正常返回,同时返回值为130,但是实际上LINUX下并不是这样的。实际上system的返回值为2,并且异常退出了。
 
    SIGQUIT信号也是一样的,看我在我的Ubuntu 上做的测试:
  1. libin@libin:~/program/C/Linux/system$ ./tsys "sleep 7"
  2. ^\status = 3
  3. abnormal termination,signal number =3
  4. libin@libin:~/program/C/Linux/system$ sleep 7
  5. ^\退出
  6. libin@libin:~/program/C/Linux/system$ echo $?
  7. 131
 
    2012年1月1日,我做了下面的实验测试,下面是实验的过程。
  1. root@libin:~/program/C/Linux/system# ./tsys "sleep 50" &
  2. [1] 2518
  3. root@libin:~/program/C/Linux/system# ps -ef
  4. root 2359 2343 0 12:42 pts/0 00:00:00 /bin/bash
  5. root 2518 2359 0 12:55 pts/0 00:00:00 ./tsys sleep 50
  6. root 2519 2518 0 12:55 pts/0 00:00:00 sh -c sleep 50
  7. root 2520 2519 0 12:55 pts/0 00:00:00 sleep 50
  8. root 2521 2359 0 12:56 pts/0 00:00:00 ps -ef
  9. root@libin:~/program/C/Linux/system# kill -3 2520
  10. Quit
  11. status = 33536
  12. normal termination,exit status = 131
  13. root@libin:~/program/C/Linux/system# ./tsys "sleep 50" &
  14. [1] 2568
  15. root@libin:~/program/C/Linux/system# ps -ef
  16. root 2568 2359 0 13:01 pts/0 00:00:00 ./tsys sleep 50
  17. root 2569 2568 0 13:01 pts/0 00:00:00 sh -c sleep 50
  18. root 2570 2569 0 13:01 pts/0 00:00:00 sleep 50
  19. root 2571 2359 0 13:01 pts/0 00:00:00 ps -ef
  20. root@libin:~/program/C/Linux/system# kill -3 2569
  21. status = 3
  22. abnormal termination,signal number =3
 
    从测试结果上看,基本上进程关系如下,system的返回值,其实是shell的终止状态,
而不是sleep的返回值。所谓屏蔽INTR信号和QUIT信号,指的是tsys这个进程,忽略了INT信号的QUIT信号,但是sh进程,和sleep进程,都没有忽略这两个信号。
 
    如果给sleep进程发送SIGQUIT信号,杀死sleep进程,那么sleep会返回131,告诉shell进程我被SIGQUIT杀死了。而sh进程,则是安然退出,所以,依然打印出正常退出的打印,通过调用WEXITSTATUS,看以看出,sleep进程的遗言是131,表明收到QUIT信号。
 
 
    下面是测试程序,基本是照抄的APUE。
  1. #define _XOPEN_SOURCE
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<signal.h>
  6. #include<sys/wait.h>
  7. void pr_exit(int status)
  8. {
  9.         printf("status = %d\n",status);
  10.     if(WIFEXITED(status))
  11.         {
  12.          printf("normal termination,exit status = %d\n",WEXITSTATUS(status));
  13.         }
  14.         else if(WIFSIGNALED(status))
  15.         {
  16.          printf("abnormal termination,signal number =%d%s\n",
  17.                                 WTERMSIG(status),
  18. #ifdef WCOREDUMP
  19.                                 WCOREDUMP(status)?"core file generated" : "");
  20. #else
  21.                 "");
  22. #endif
  23.         }
  24. }
  25. int main(int argc,char* argv[])
  26. {
  27.         int status;
  28.         if(argc<2)
  29.         {
  30.          fprintf(stderr,"usage:tsys cmd\n");
  31.                  return -1;
  32.         }
  33.         if((status = system(argv[1]) )<0)
  34.         {
  35.          fprintf(stderr,"system error\n");
  36.                 return -2;
  37.         }
  38.         pr_exit(status);
  39.     return 0;
  40. }
十分感谢Heartwork的回复,本应该及早致谢,但是由于本周加班太多,机会每天都是10点以后到家,所以没有时间细细揣摩Heartwork的恢复。
 
参考文献:
1 APUE 
 
 ================================================================
 
 

一,system()理解

功能:system()函数调用“/bin/sh -c command”执行特定的命令,阻塞当前进程直到command命令执行完毕

原型:

int system(const char *command);

返回值:

如果无法启动shell运行命令,system将返回127;出现不能执行system调用的其他错误时返回-1。如果system能够顺利执行,返回那个命令的退出码。

说明:

man帮助:

       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION 
       system()  executes a command specified in command by calling /bin/sh -c 
       command, and returns after the command has been completed.
  During exe- 
       cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT 
       will be ignored.

RETURN VALUE 
       The value returned is -1 on  error  (e.g.   fork(2)  failed),  and  the 
       return  status  of the command otherwise.  This latter return status is 
       in the format specified in wait(2).  Thus, the exit code of the command 
       will  be  WEXITSTATUS(status).   In case /bin/sh could not be executed, 
       the exit status will be that of a command that does exit(127).

       If the value of command is NULL, system() returns non-zero if the shell 
       is available, and zero if not.

       system() does not affect the wait status of any other children.

二,system()函数原理

system函数执行时,会调用fork、execve、waitpid等函数。

linux版system函数的源码:

复制代码
int system(const char * cmdstring)
 {
     pid_t pid;
     int status;
     if(cmdstring == NULL){        
          return (1);
     }
     if((pid = fork())<0){
             status = -1;
     }
     else if(pid == 0){
         execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
         _exit(127); //子进程正常执行则不会执行此语句
        }
     else{
             while(waitpid(pid, &status, 0) < 0){
                 if(errno != EINTER){
                     status = -1;
                     break;
                 }
             }
         }
         return status;
 }
复制代码
  • 函数说明 
    system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命>令执行完后随即返回原调用的进程。 
    在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。 
    返回值 
    =-1:出现错误  
    =0:调用成功但是没有出现子进程  
    >0:成功退出的子进程的id 
    如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值>。如果system()调用成功则最后会返回 
    执行shell命令后的返回值,但是此返回值也有可能为 system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。 
    附加说明 
    在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。

system函数对返回值的处理,涉及3个阶段:


阶段1:创建子进程等准备工作。如果失败,返回-1。 
阶段2:调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1),原因值被写入到status的低8~15比特位中。system的man中只说明了会写了127这个值,但实测发现还会写126等值。 
阶段3:如果shell脚本正常执行结束,将shell返回值填到status的低8~15比特位中。 
备注1: 
只要能够调用到/bin/sh,并且执行shell过程中没有被其他信号异常中断,都算正常结束。 
比如:不管shell脚本中返回什么原因值,是0还是非0,都算正常执行结束。即使shell脚本不存在或没有执行权限,也都算正常执行结束。 
如果shell脚本执行过程中被强制kill掉等情况则算异常结束。

如何判断阶段2中,shell脚本子进程是否正常执行结束呢?系统提供了宏:WIFEXITED(status)。如果WIFEXITED(status)为真,则说明正常结束。 
如何取得阶段3中的shell返回值?你可以直接通过右移8bit来实现,但安全的做法是使用系统提供的宏:WEXITSTATUS(status)。

由于我们一般在shell脚本中会通过返回值判断本脚本是否正常执行,如果成功返回0,失败返回正数。 
所以综上,判断一个system函数调用shell脚本是否正常结束的方法应该是如下3个条件同时成立: 
(1)-1 != status 
(2)WIFEXITED(status)为真 
(3)0 == WEXITSTATUS(status) 
注意: 
根据以上分析,当shell脚本不存在、没有执行权限等场景下时,以上前2个条件仍会成立,此时WEXITSTATUS(status)为127,126等数值。 
所以,我们在shell脚本中不能将127,126等数值定义为返回值,否则无法区分中是shell的返回值,还是调用shell脚本异常的原因值。shell脚本中的返回值最好多1开始递增。

示例程序:

复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define EXIT_ERR(m) \
do\
{\
    perror(m);\
    exit(EXIT_FAILURE);\
}\
while (0);\

int main(void)
{
    int status ;
    status = system("ls -l|wc -l");

    if(status == -1){
        EXIT_ERR("system error");
    }

    else{
        if(WIFEXITED(status))
        {
            if(WEXITSTATUS(status) == 0)
                printf("run command successful\n");
            else
                printf("run command fail and exit code is %d\n",WEXITSTATUS(status));
        }
        else
            printf("exit status = %d\n",WEXITSTATUS(status));
    }
    return 0;
}
复制代码

结果:

QQ截图20130713131149

 
posted @ 2016-07-30 01:53  crazy_machine  阅读(10101)  评论(0编辑  收藏  举报