system函数介绍
来源:互联网 发布:吃西瓜 网络意思是什么 编辑:程序博客网 时间:2024/06/02 02:34
system(执行shell 命令)
相关函数
fork,execve,waitpid,popen
表头文件
#i nclude<stdlib.h>
定义函数
int system(const char * string);
函数说明
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()会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例
#i nclude<stdlib.h>
main()
{
system("ls -al /etc/passwd /etc/shadow");
}
执行结果:
-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
-r--------- 1 root root 572 Sep 2 15 :34 /etc/shado
例2:
char tmp[];
sprintf(tmp,"/bin/mount -t vfat %s /mnt/usb",dev);
system(tmp);
其中dev是/dev/sda1。
system函数的源码
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
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)
{
if(status >= -1);
break;
}
}
}
return status;
}
那么如何获得system的返回值呢??
char buf[10];
char * ps="ps -ef|grep -c root";
FILE *ptr;
int i;
if((ptr = popen(ps, "r")) != NULL)
{
fgets(buf, 10 , ptr);
i = atoi(buf);
pclose(ptr);
}
可以man下waitpid查看下如何检查status的值
int ret = system("ls -al /etc/passwd /etc/shadow");
if(WIFSIGNALED(ret))
具体的这些宏查看man waitpid
相关函数
fork,execve,waitpid,popen
表头文件
#i nclude<stdlib.h>
定义函数
int system(const char * string);
函数说明
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()会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例
#i nclude<stdlib.h>
main()
{
system("ls -al /etc/passwd /etc/shadow");
}
执行结果:
-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
-r--------- 1 root root 572 Sep 2 15 :34 /etc/shado
例2:
char tmp[];
sprintf(tmp,"/bin/mount -t vfat %s /mnt/usb",dev);
system(tmp);
其中dev是/dev/sda1。
system函数的源码
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
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)
{
if(status >= -1);
break;
}
}
}
return status;
}
那么如何获得system的返回值呢??
char buf[10];
char * ps="ps -ef|grep -c root";
FILE *ptr;
int i;
if((ptr = popen(ps, "r")) != NULL)
{
fgets(buf, 10 , ptr);
i = atoi(buf);
pclose(ptr);
}
可以man下waitpid查看下如何检查status的值
int ret = system("ls -al /etc/passwd /etc/shadow");
if(WIFSIGNALED(ret))
具体的这些宏查看man waitpid
- system函数介绍
- system函数介绍
- linux下system函数介绍
- linux c system函数介绍
- linux下system函数介绍
- linux下system函数介绍
- linux下system函数介绍
- System介绍
- php中exec,system及passthru函数用法介绍
- Linux C编程--进程介绍5--system函数
- C#中system.object的函数方法功能介绍
- system函数
- System函数
- system()函数
- system函数
- system ()函数
- system()函数
- system()函数
- apache的ftpclient的使用
- C# ref的用法
- Android 后台Service : 向服务器发送心跳包
- SQLServer存储过程返回值总结
- XAMPP中配置多个网站
- system函数介绍
- ORACLE在AIX中产生SOFTWARE PROGRAM ABNORMALLY TERMINATED警告原因
- Gdiplus-Lock up your bits
- qt-读取和修改系统时间
- flex-设置简单的柱状图ColumnChart(一)
- shell脚本执行命令错误处理
- 解决PHP函数ip2long转换IP时数值太大而产生负数的办法
- MP3文件格式解析
- 成为一个完美的小娇妻!