linux的system () 函數詳解

來源:互聯網
上載者:User

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){

status = -1;

break;

}

}

}

return status;

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.