您的位置:首页 > 运维架构 > Shell

在C语言中调用shell命令的实现方法

2017-04-07 17:48 1016 查看
1、system(执行shell 命令)

相关函数
fork,execve,waitpid,popen
表头文件 #include<stdlib.h>
定义函数 int system(const char * string);
函数说明 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c

string来执行参数string字符串所代表的命令,此命令执行完后随

即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时

搁置,SIGINT和SIGQUIT 信号则会被忽略。

返回值 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-
1。若参数string为空指针(NULL),则返回非零值。如果system()调

用成功则最后会返回执行shell命令后的返回值,(存放在返回值的8~15位)但是此返回值也有

可能为system()调用/bin/sh失败所返回的127,因此最好能再检查

errno 来确认执行成功。
附加说明 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会

继承环境变量,通过环境变量可能会造成系统安全的问题。
范例:
复制代码 代码如下:

#include<stdlib.h>

main(){

system(“ls -al /etc/passwd /etc/shadow”);

}

2、popen(建立管道I/O)

相关函数
pipe,mkfifo,pclose,fork,system,fopen
表头文件 #include<stdio.h>
定义函数 FILE * popen( const char * command,const char * type);
函数说明 popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c

来执行参数command的指令。参数type可使用“r”代表读取,“w”

代表写入。依照此type值,popen()会建立管道连到子进程的标准输

出设备或标准输入设备,然后返回一个文件指针。随后进程便可利

用此文件指针来读取子进程的输出设备或是写入到子进程的标准输

入设备中。此外,所有使用文件指针(FILE*)操作的函数也都可以使

用,除了fclose()以外。
返回值 若成功则返回文件指针,否则返回NULL,错误原因存于errno中。

错误代码 EINVAL参数type不合法。
注意事项 在编写具SUID/SGID权限的程序时请尽量避免使用popen(),popen()

会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例:

复制代码 代码如下:

#include<stdio.h>

main()

{

FILE * fp;

char buffer[80];

fp=popen(“cat /etc/passwd”,”r”);

fgets(buffer,sizeof(buffer),fp);

printf(“%s”,buffer);

pclose(fp);

}

执行 root :x:0 0: root: /root: /bin/bash
3、使用vfork()新建子进程,然后调用exec函数族
复制代码 代码如下:

#include<unistd.h>

main()

{

    char * argv[ ]={“ls”,”-al”,”/etc/passwd”,(char*) };

    if(vfork() = =0)

    {

        execv(“/bin/ls”,argv);

    }else{       

        printf(“This is the parent process\n”);

    }
}

例:

[cpp] view
plain copy

status = system("./test.sh");  

1、先统一两个说法:

(1)system返回值:指调用system函数后的返回值,比如上例中status为system返回值

(2)shell返回值:指system所调用的shell命令的返回值,比如上例中,test.sh中返回的值为shell返回值。

2、如何正确判断test.sh是否正确执行?

仅判断status是否==0?或者仅判断status是否!=-1? 

都错!

3、man中对于system的说明

RETURN VALUE

       The value returned is -1 on error (e.g.  fork() 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).

看得很晕吧?

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开始递增。

判断shell脚本正常执行结束的健全代码如下:

[cpp] view
plain copy

#include <stdio.h>  

#include <stdlib.h>  

#include <sys/wait.h>  

#include <sys/types.h>  

  

int main()  

{  

    pid_t status;  

  

  

    status = system("./test.sh");  

  

    if (-1 == status)  

    {  

        printf("system error!");  

    }  

    else  

    {  

        printf("exit status value = [0x%x]\n", status);  

  

        if (WIFEXITED(status))  

        {  

            if (0 == WEXITSTATUS(status))  

            {  

                printf("run shell script successfully.\n");  

            }  

            else  

            {  

                printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));  

            }  

        }  

        else  

        {  

            printf("exit status = [%d]\n", WEXITSTATUS(status));  

        }  

    }  

  

    return 0;  

}  

WIFEXITED(stat_val) Evaluates to a non-zero value if status

was returned for a child process that

terminated normally.

WEXITSTATUS(stat_val) If the value of WIFEXITED(stat_val) is

non-zero, this macro evaluates to the

low-order 8 bits of the status argument

that the child process passed to _exit()

or exit(), or the value the child

process returned from main().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: