相關函數 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”);
}
}
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).
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().