Linux下使用system()函数一定要谨慎

转载自:http://my.oschina.net/renhc/blog/53580

 

linux尽量避免使用system。

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

 

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

1 #include <stdlib.h>
2 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()函数的实现:

01 int system(const char * cmdstring)
02 {
03     pid_t pid;
04     int status;
05  
06 if(cmdstring == NULL)
07 {
08     return (1); //如果cmdstring为空,返回非零值,一般为1
09 }
10  
11 if((pid = fork())<0)
12 {
13     status = -1; //fork失败,返回-1
14 }
15 else if(pid == 0)
16 {
17     execl("/bin/sh""sh""-c", cmdstring, (char *)0);
18     _exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~
19 }
20 else //父进程
21 {
22     while(waitpid(pid, &status, 0) < 0)
23     {
24         if(errno != EINTR)
25         {
26             status = -1; //如果waitpid被信号中断,则返回-1
27             break;
28         }
29     }
30 }
31  
32     return status; //如果waitpid成功,则返回子进程的返回状态
33 }

 

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

 

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

这里给我出的做法:

01 int status;
02 if(NULL == cmdstring) //如果cmdstring为空趁早闪退吧,尽管system()函数也能处理空指针
03 {
04     return XXX;
05 }
06 status = system(cmdstring);
07 if(status < 0)
08 {
09     printf("cmd: %s\t error: %s", cmdstring, strerror(errno)); // 这里务必要把errno信息输出或记入Log
10     return XXX;
11 }
12  
13 if(WIFEXITED(status))
14 {
15     printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
16 }
17 else if(WIFSIGNALED(status))
18 {
19     printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断,取得信号值
20 }
21 else if(WIFSTOPPED(status))
22 {
23     printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行,取得信号值
24 }

 

到于取得子进程返回值的相关介绍可以参考另一篇文章: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

时间: 2024-07-30 00:44:17

Linux下使用system()函数一定要谨慎的相关文章

terminal-linux下用system函数打开的终端,能不能隐藏掉?

问题描述 linux下用system函数打开的终端,能不能隐藏掉? system("gnome-terminal -e ./应用程序名"); 这样会打开一个终端并运行指定的应用程序, 能不能把终端这个黑框隐藏掉?? 在运行的时候只打开应用程序. 解决方案 system(你的程序) 解决方案二: 楼上正解. 但是会等待要执行的程序的执行完成之后才会返回

linux下代替system的基于管道的popen和pclose函数

linux下使用system需要谨慎,那么代替它的方法是什么呢? 标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由popen启动的进程称为子进程. popen函数还创建一个管道用于父子进程间通信.父进程要么从管道读信息,要么向管道写信息,至于是读还是写取决于父进程调用popen时传递的参数.下在给出popen.pclose的定义:   01 #include <stdio.h> 02 /* 03 函数功能:pope

源代码-怎么样再linux下查看dump_stack()函数打印出来的信息?

问题描述 怎么样再linux下查看dump_stack()函数打印出来的信息? 本人Linux小白,刚学没多久,最近我想要研究下linux中打开文件操作的流程,于是我就在内核的filp open()函数的源代码中插入了dump_stack_()函数. 我重新编译内核之后,直接在终端调用cd 命令行,然后想在系统日志里面看看有没有 函数调用打印出来,但是翻来翻去好像什么都没有的样子. 但是我自己写一个简单的模块,里面有dump_stack_函数,在编译模块,再运行这个模块, 这样的话又可以在日志里

Linux下利用ioctl函数获取网卡信息

linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv]) 函数成功返回0,失败返回-1. 其相关命令接口如下:   类别 Request 说明 数据类型 套 接 口 SIOCATMARK SIOCSPGRP SIOCGPGRP 是否位于带外标记 设置套接口的进程ID 或进程组ID 获取套接口的进程ID 或进程组ID int int int 文 件 FIONBI

基于linux下获取时间函数的详解_C 语言

//-------------------------------------------------------------//asctime(将时间和日期以字符串格式表示)#include<time.h>定义函数char * asctime(const struct tm * timeptr);函数说明asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回.此函数已经由时区转换成当地时间,字符串格式为:"We

解析Linux下的时间函数:设置以及获取时间的方法_C 语言

一.时间函数 复制代码 代码如下: time_t time(time_t *t);char *asctime(const struct tm *tm);char *asctime_r(const struct tm *tm, char *buf);char *ctime(const time_t *timep);char *ctime_r(const time_t *timep, char *buf);struct tm *gmtime(const time_t *timep); //获取的为英

linux下练习 c++ 函数模版例子

//函数模版使用 //函数模版标准不支持参数默认值 #include<iostream> #include<cstring> using namespace std; template <typename T> void sort(T* a,int n)//普通冒泡排序 { bool changed; do { changed=false; for(int i=1;i<n;i++) { if(a[i]<a[i-1]) { swap(a[i],a[i-1]);

c语言中的system函数问题

问题描述 c语言中的system函数问题 在代码里面用system 函数 打开某一程序,结果导致打开这一程序后,下面的system函数用不了 . 请看图.......... 解决方案 求大神啊!!!!!!!!!!!!!!!!!!!! 解决方案二: system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码: #include <sys/types.h> #include <sys/wait.h> #include <errno.h>

udp-Linux下socket通信函数原型玄机?(UDP)

问题描述 Linux下socket通信函数原型玄机?(UDP) 编写UDP通讯的程序时发现:ssize_t sendto(int sockfd const void *buf size_t len int flagsconst struct sockaddr *dest_addr socklen_t addrlen);ssize_t recvfrom(int sockfd void *buf size_t len int flagsstruct sockaddr *src_addr sockle