歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux中system函數的實現

Linux中system函數的實現

日期:2017/3/3 16:24:04   编辑:關於Linux
#include    <errno.h>  
#include    <signal.h>  
#include    <unistd.h>  
      
int
system(const char *cmdstring)   /* with appropriate signal handling */
{  
    pid_t               pid;  
    int                 status;  
    struct sigaction    ignore, saveintr, savequit;  
    sigset_t            chldmask, savemask;  
      
    if (cmdstring == NULL)  
        return(1);      /* always a command processor with UNIX */
      
    ignore.sa_handler = SIG_IGN;    /* ignore SIGINT and SIGQUIT */
    sigemptyset(&ignore.sa_mask);  
    ignore.sa_flags = 0;  
    if (sigaction(SIGINT, &ignore, &saveintr) < 0)  
        return(-1);  
    if (sigaction(SIGQUIT, &ignore, &savequit) < 0)  
        return(-1);  
    sigemptyset(&chldmask);         /* now block SIGCHLD */
    sigaddset(&chldmask, SIGCHLD);  
    if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)  
        return(-1);  
      
    if ((pid = fork()) < 0) {  
        status = -1;    /* probably out of processes */
    } else if (pid == 0) {          /* child */
        /* restore previous signal actions & reset signal mask */
        sigaction(SIGINT, &saveintr, NULL);  
        sigaction(SIGQUIT, &savequit, NULL);  
        sigprocmask(SIG_SETMASK, &savemask, NULL);  
      
        execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);  
        _exit(127);     /* exec error */
    } else {                        /* parent */
        while (waitpid(pid, &status, 0) < 0)  
            if (errno != EINTR) {  
                status = -1; /* error other than EINTR from waitpid() */
                break;  
            }  
    }  
      
    /* restore previous signal actions & reset signal mask */
    if (sigaction(SIGINT, &saveintr, NULL) < 0)  
        return(-1);  
    if (sigaction(SIGQUIT, &savequit, NULL) < 0)  
        return(-1);  
    if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0)  
        return(-1);  
      
    return(status);  
}

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 來確認執行成功。

Copyright © Linux教程網 All Rights Reserved