linux C 语言的 system(执行shell 命令)
2016-02-01 00:00
531 查看
http://blog.chinaunix.net/uid-22150747-id-189252.html
相关函数 fork,execve,waitpid,popen
表头文件
代码:
#include
定义函数
代码:
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命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因
此最好能再检查errno 来确认执行成功。
附加说明
在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例
代码:
#include
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/shadow
__________________
相关函数 fork,execve,waitpid,popen
表头文件
代码:
#include
定义函数
代码:
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命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因
此最好能再检查errno 来确认执行成功。
附加说明
在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例
代码:
#include
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/shadow
__________________
相关文章推荐
- 百度MP3 SHELL脚本
- Android—adb shell—/system/bin/sh: ./executer: No such file or directory
- HBase 常用Shell命令
- Trap in bash
- shell脚本编程(四)
- shell脚本编程(三)
- shell脚本编程(二)
- shell脚本编程(一)
- Shell的GUI编程
- 常用算法——希尔排序(Shell Sort)
- Linux15_Shell实例(翻译_1)
- Shell将文件后缀重命名
- Shell脚本使用TinyPng批量压缩图片New
- Shell解析Json字符串
- shell脚本读取ini配置文件的值
- 实录: .bashrc配置,让shell前缀显色
- 第五章、bash功能
- Linuxshell脚本基础学习
- bash 之for循环
- erlang shell 命令中文使用说明