相關函數 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命令後的返回值,但是此返回值也有可能爲system()調用/bin/sh失敗所返回的127,因此最好能再檢查errno 來確認執行成功。
附加說明 在編寫具有SUID/SGID權限的程序時請勿使用system(),system()會繼承環境變量,通過環境變量可能會造成系統安全的問題。
範例 #include<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/shadow
Upon successful completion, the system subroutine returns the exit status of the shell. The exit status of the shell
is returned in the same manner as a call to the wait or waitpid subroutine, using the structures in the sys/wait.h
file.
If the String parameter is a null pointer and a command processor is available, the system subroutine returns a
nonzero value. If the fork subroutine fails or if the exit status of the shell cannot be obtained, the system
subroutine returns a value of -1. If the exec l subroutine fails, the system subroutine returns a value of 127. In
all cases, the errno global variable is set to indicate the error.
----------------------------------------------------------------------------------------------------------------------------------------------------
linux中system函數的返回值
引自:原文
要分成兩部分來說:
1,在程序中,用exit來設置進程的退出值時,雖然該函數的參數類型爲int型,但再父進程中只能取到其值的低8位.所以用exit返回值時,高於255的值是沒有意義的.
2,對於system函數,返回值是由兩部分組成的,低8位值表示所執行的腳本在執行過程中所接收到的信號值,其餘的位表示的腳本exit退出時所設置的值,
即腳本內exit退出是的值的低8位,在system返回值的低9-16位
這樣我們就可以通過右移操作來得到exit的值了。
----------------------------------------------------------------------------------------------------------------------------------------------------
以下轉自:http://zhangwenxin82.blog.163.com/blog/static/1145959562010323103241387/
1,在程序中,用exit來設置進程的退出值時,雖然該函數的參數類型爲int型,但再父進程中只能取到其值的低8位.所以用exit返回值時,高於255的值是沒有意義的.
2,對於system函數,返回值是由兩部分組成的,低8位值表示所執行的腳本在執行過程中所接收到的信號值,其餘的位表示的腳本exit退出時所設置的值,
即腳本內exit退出是的值的低8位,在system返回值的低9-16位.
Linux中調用 system的返回值
包含文件
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
先寫一個被調用的函數
==================================
#include <iostream>
int main()
{
printf("Return 10./n");
return 10;
}
==================================
編譯後生成一個"rt"的可執行文件
運行結果
==================================
Return 10.
==================================
再寫一個調用system的程序
==================================
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;
int main()
{
pid_t status ;
int errno = 0 ;
status = system("./rt") ;
printf("wifexited(status):%d/n",WIFEXITED(status));
printf("WEXITSTATUS(status):%d/n",WEXITSTATUS(status));
if (status == -1)
printf("system error!") ;
if (WIFEXITED(status)){
printf("cp exit normal![%d]/n", errno) ;
printf("exit staus = [%X]/n", WEXITSTATUS(status)) ;
}else
printf("cp exit illegal![%d]/n", errno) ;
}
~
[tiantao@probe sys_test]$ cat sys_test.cpp
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;
int main()
{
pid_t status ;
int errno = 0 ;
status = system("./rt") ;
printf("wifexited(status):%d/n",WIFEXITED(status));
printf("WEXITSTATUS(status):%d/n",WEXITSTATUS(status));
if (status == -1)
printf("system error!") ;
if (WIFEXITED(status)){
printf("cp exit normal![%d]/n", errno) ;
printf("exit staus = [%X]/n", WEXITSTATUS(status)) ;
}else
printf("cp exit illegal![%d]/n", errno) ;
}
==================================
編譯後運行結果
==================================
Return 10.
wifexited(status):1
WEXITSTATUS(status):10
cp exit normal![0]
exit staus = [A]
==================================
可以看到:
WEXITSTATUS(status)可以得到調用程序的返回值。