{"id":1630,"date":"2015-11-12T12:13:30","date_gmt":"2015-11-12T04:13:30","guid":{"rendered":"http:\/\/www.51cos.com\/?p=1630"},"modified":"2015-11-12T12:13:30","modified_gmt":"2015-11-12T04:13:30","slug":"linux-system-%e5%87%bd%e6%95%b0%e6%ba%90%e4%bb%a3%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=1630","title":{"rendered":"Linux system() \u51fd\u6570\u6e90\u4ee3\u7801\u5206\u6790"},"content":{"rendered":"<p>inux \u4e0bsystem\u51fd\u6570\u539f\u578b\uff1a<\/p>\n<p>int system(const char *command);<br \/>\nsystem() executes a command specified in command by calling \/bin\/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.<br \/>\n     system\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6bd4\u8f83\u591a\uff0c\u4e14\u5b58\u5728\u76f8\u540c\u7684\u503c\u5374\u4ee3\u8868\u7740\u4e0d\u540c\u7684\u610f\u601d\uff0c\u9488\u5bf9\u4ee5\u4e0a\u95ee\u9898\u5bf9\u6e90\u7801\u8fdb\u884c\u5206\u6790\u3002<br \/>\nLibc-2.9 \u4e0bsysdeps\\posi\\system.c\u6e90\u7801\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\n<p>    int system (const char *line)<br \/>\n    {<br \/>\n        return __libc_system (line);<br \/>\n    }<br \/>\n    int __libc_system (const char *line) \/*system\u51fd\u6570\u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u662fdo_system\u51fd\u6570*\/<br \/>\n    {<br \/>\n        if (line == NULL)<br \/>\n            \/* Check that we have a command processor available. It might<br \/>\n               ?not be available after a chroot(), for example. *\/<br \/>\n            return do_system (\"exit 0\") == 0; \/* \u5f53line\u4e3aNULL\u65f6\uff0c\u8fd4\u56de\u503c\u4e3a0\uff0c\u53ca\u6267\u884cbash \u2013c exit 0*\/<br \/>\n        if (SINGLE_THREAD_P)<br \/>\n            return do_system (line);<\/p>\n<p>        \/*GCC cleanup exception range can cover the<\/p>\n<p>          LIBC_CANCEL_ASYNC() and LIBC_CANCEL_RESET():http:\/\/sourceware.org\/ml\/libc-alpha\/2011-08\/msg00063.html *\/<\/p>\n<p>        int oldtype = LIBC_CANCEL_ASYNC ();<br \/>\n        int result = do_system (line);<br \/>\n        LIBC_CANCEL_RESET (oldtype);<br \/>\n        return result;<br \/>\n    }<br \/>\n    weak_alias (__libc_system, system) \/* weak_alias \u522b\u540d *\/<br \/>\n        \/*\u901a\u8fc7\u4ee5\u4e0a\u5206\u6790system\u51fd\u6570\u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u662fdo_system\u51fd\u6570\uff0c \u4ee5\u4e0b\u5bf9do_system\u51fd\u6570\u8fdb\u884c\u5206\u6790\uff0c\u4ee5\u4e0b\u5217\u51fa\u4e3b\u8981\u51fd\u6570\uff1a*\/<br \/>\n        \/* Execute LINE as a shell command, returning its status. *\/<br \/>\n    do_system (const char *line)<br \/>\n    {<br \/>\n        int status, save;<br \/>\n        pid_t pid;<br \/>\n        struct sigaction sa;<br \/>\n    #ifndef _LIBC_REENTRANT<br \/>\n        struct sigaction intr, quit;<br \/>\n    #endif<br \/>\n        sigset_t omask;<br \/>\n        sa.sa_handler = SIG_IGN;<br \/>\n        sa.sa_flags = 0;<br \/>\n        __sigemptyset (&sa.sa_mask);<br \/>\n        DO_LOCK (); \/* mutex lock*\/<br \/>\n        if (ADD_REF () == 0)<br \/>\n        {<br \/>\n            if (__sigaction (SIGINT, &sa, &intr) < 0) \/*\u6267\u884c\u65f6 SIGINT\u88ab\u5ffd\u7565*\/\n            {\n                SUB_REF ();\n                goto out;\n            }\n            if (__sigaction (SIGQUIT, &#038;sa, &#038;quit) < 0) \/*\u6267\u884c\u65f6 SIGQUIT\u88ab\u5ffd\u7565*\/\n            {\n                save = errno;\n                SUB_REF ();\n                goto out_restore_sigint;\n            }\n        }\n        DO_UNLOCK ();\n        \/* We reuse the bitmap in the 'sa' structure. *\/\n        __sigaddset (&#038;sa.sa_mask, SIGCHLD);\n        save = errno;\n        if (__sigprocmask (SIG_BLOCK, &#038;sa.sa_mask, &#038;omask) < 0) \/*\u6267\u884c\u65f6\u8bbe\u7f6eSIG_BLOCK\u6807\u5fd7\u4f4d\uff0cSIGCHLD\u88ab\u963b\u585e\uff0c\u6267\u884c\u5931\u8d25\uff0c\u5219\u6062\u590d\u4e4b\u524d\u7684\u4fe1\u53f7\u7684bitmap*\/\n        {\n    #ifndef _LIBC\n            if (errno == ENOSYS)\n                __set_errno (save);\n            else\n    #endif\n            {\n                DO_LOCK ();\n                if (SUB_REF () == 0)\n                {\n                    save = errno;\n                    (void) __sigaction (SIGQUIT, &#038;quit, (struct sigaction *) NULL);\n    out_restore_sigint:\n                    (void) __sigaction (SIGINT, &#038;intr, (struct sigaction *) NULL);\n                    __set_errno (save);\n                }\n    out:\n                DO_UNLOCK ();\n                return -1;\n            }\n        }\n    #ifdef CLEANUP_HANDLER\n        CLEANUP_HANDLER;\n    #endif\n        \/*\u6267\u884c\u6210\u529f\uff0c\u8c03\u7528fork\uff0c\u751f\u6210\u5b50\u8fdb\u7a0b\u6267\u884ccommand\u547d\u4ee4*\/\n    #ifdef FORK\n        pid = FORK (); \/*\u8c03\u7528SYS_CALL\u751f\u6210\u5b50\u8fdb\u7a0b*\/\n    #else\n        pid = __fork ();\n    #endif\n        if (pid == (pid_t) 0) \/\/\n        {\n            \/* Child side. *\/\n            const char *new_argv[4];\n            new_argv[0] = SHELL_NAME;\n            new_argv[1] = \"-c\";\n            new_argv[2] = line;\n            new_argv[3] = NULL;\n            \/* Restore the signals. *\/\n            (void) __sigaction (SIGINT, &#038;intr, (struct sigaction *) NULL);\n            (void) __sigaction (SIGQUIT, &#038;quit, (struct sigaction *) NULL);\n            (void) __sigprocmask (SIG_SETMASK, &#038;omask, (sigset_t *) NULL);\n            INIT_LOCK ();\n\n            \/* Exec the shell. *\/\n            (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);\n            _exit (127); \/*exec\u6267\u884c\u5931\u8d25\u5219\u8fd4\u56de127*\/\n        }\n        else if (pid < (pid_t) 0)\n            \/* The fork failed. *\/\n            status = -1;\n        else \/*\u7236\u8fdb\u7a0b\uff0cwaitpid*\/\n            \/* Parent side. *\/\n        {\n            \/* Note the system() is a cancellation point. But since we call\n               waitpid() which itself is a cancellation point we do not\n               have to do anything here. *\/\n            if (TEMP_FAILURE_RETRY (__waitpid (pid, &#038;status, 0)) != pid)\n                status = -1; \/*waitpid \u5931\u8d25\u8fd4\u56de1*\/\n        }\n    #ifdef CLEANUP_HANDLER\n        CLEANUP_RESET;\n    #endif\n        save = errno;\n        DO_LOCK ();\n        if ((SUB_REF () == 0\n                    &#038;&#038; (__sigaction (SIGINT, &#038;intr, (struct sigaction *) NULL)\n                        | __sigaction (SIGQUIT, &#038;quit, (struct sigaction *) NULL)) != 0)\n                || __sigprocmask (SIG_SETMASK, &#038;omask, (sigset_t *) NULL) != 0)\n        {\n    #ifndef _LIBC\n            \/* glibc cannot be used on systems without waitpid. *\/\n            if (errno == ENOSYS)\n                __set_errno (save);\n            else\n    #endif\n                status = -1;\n        }\n        DO_UNLOCK ();\n        return status;\n    }\n\n    \u603b\u7ed3\uff1asystem\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1a\u52062\u5927\u7c7b\uff1a\n    \uff081\uff09 \u5f53\u53c2\u6570\u4e3a\u7a7a\u65f6\uff0c\u8c03\u7528do_system (\"exit 0\"),\u8fd4\u56de\u503c\u4e3aNULL \/*\u7f51\u4e0a\u90fd\u8bf4\u662f\u8fd4\u56de\u975e0\u503c\uff0c\u4f46\u6211\u6d4b\u7684\u662fNULL \uff01\uff01\uff01\uff01*\/\n    \uff082\uff09 \u8c03\u7528result = do_system (line)\uff1b\n    \u5f53\u6267\u884c\u5ffd\u7565SIGINT\u548cSIGQUIT\u4fe1\u53f7\u65f6\uff0c\u9519\u8bef\u8fd4\u56de-1\n    fork\u5b50\u8fdb\u7a0b\u65f6\u51fa\u73b0\u9519\u8bef \u8fd4\u56de\u503c\u4e3a-1\n    \u5f53exec\u6267\u884c\u9519\u8bef\u6216\u547d\u4ee4\u65e0\u6548\u65f6\u8fd4\u56de\u503c\u4e3a127\n    \u5f53waitpid\u9519\u8bef\u65f6 \u8fd4\u56de\u503c\u4e3a-1\n    if(NULL == cmdstring) \/\/\u5982\u679ccmdstring\u4e3a\u7a7a\u8d81\u65e9\u95ea\u9000\u5427\uff0c\u5c3d\u7ba1system()\u51fd\u6570\u4e5f\u80fd\u5904\u7406\u7a7a\u6307\u9488\n        \u4ee5\u4e0b\u53ef\u4ee5\u5f97\u5230\u8fd4\u56de\u503c\u9519\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff0c\u76f8\u5173\u77e5\u8bc6\u8be6\u89c1APUE\uff08\u6458\u5f55\u7684\u5177\u4f53\u4f4d\u7f6e\u5fd8\u4e86\uff0c\u4f5c\u8005\u770b\u5230\u540e\u8bf7\u7559\u8a00\uff09\n    if(NULL == cmdstring) \/\/\u5982\u679ccmdstring\u4e3a\u7a7a\u8d81\u65e9\u95ea\u9000\u5427\uff0c\u5c3d\u7ba1system()\u51fd\u6570\u4e5f\u80fd\u5904\u7406\u7a7a\u6307\u9488\n    {\n        return XXX;\n    }\n    status = system(cmdstring);\n    if(status < 0)\n    {\n        printf(\"cmd: %s\\t error: %s\", cmdstring, strerror(errno)); \/\/ \u8fd9\u91cc\u52a1\u5fc5\u8981\u628aerrno\u4fe1\u606f\u8f93\u51fa\u6216\u8bb0\u5165Log\n        return XXX;\n    }\n\n    if(WIFEXITED(status))\n    {\n        printf(\"normal termination, exit status = %d\\n\", WEXITSTATUS(status)); \/\/\u53d6\u5f97cmdstring\u6267\u884c\u7ed3\u679c\n    }\n    else if(WIFSIGNALED(status))\n    {\n        printf(\"abnormal termination,signal number =%d\\n\", WTERMSIG(status)); \/\/\u5982\u679ccmdstring\u88ab\u4fe1\u53f7\u4e2d\u65ad\uff0c\u53d6\u5f97\u4fe1\u53f7\u503c\n    }\n    else if(WIFSTOPPED(status))\n    {\n        printf(\"process stopped, signal number =%d\\n\", WSTOPSIG(status)); \/\/\u5982\u679ccmdstring\u88ab\u4fe1\u53f7\u6682\u505c\u6267\u884c\uff0c\u53d6\u5f97\u4fe1\u53f7\u503c\n    }\n\n\n \n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>inux \u4e0bsystem\u51fd\u6570\u539f\u578b\uff1a int system(const char *command); syst [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1630","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1630"}],"version-history":[{"count":1,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1630\/revisions"}],"predecessor-version":[{"id":1631,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1630\/revisions\/1631"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1630"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}