plucky (3) system.3.gz

Provided by: manpages-ja-dev_0.5.0.0.20221215+dfsg-1_all bug

名前

       system - シェルコマンドの実行

書式

       #include <stdlib.h>

       int system(const char *command);

説明

       system()  ライブラリ関数は、fork(2) を使って子プロセスを作成し、その子プロセスは以下のように command で指
       定されたシェルコマンドを execl(3) を使って実行する。

           execl("/bin/sh", "sh", "-c", command, (char *) NULL);

       system() が返るのはコマンドが完了した後である。

       コマンドの実行中は、 system() を呼び出したプロセスでは、 SIGCHLD はブロックされ、 SIGINTSIGQUIT  は無
       視される   (command  を実行する子プロセスでは、これらのシグナルはデフォルトの処理方法にしたがって処理され
       る)。

       command が NULL の場合、 system()  はそのシステムでシェルが利用可能かを示すステータスを返す。

返り値

       system() の返り値は以下のいずれかである。

       *  command が NULL の場合、 シェルが利用可能ならゼロ以外の値、利用不可なら 0。

       *  If a child process could not be created, or its status could not be retrieved, the return value is  -1
          and errno is set to indicate the error.

       *  子プロセスでシェルを実行できなかった場合、 返り値は子プロセスがステータス 127 で _exit(2) を呼び出して
          終了したのと同じになる。

       *  システムコールがすべて成功した場合、 返り値は command を実行するのに使用された子プロセスのシェルの終了
          ステータスとなる (シェルの終了ステータスはそのシェルが実行した最後のコマンドの終了ステータスである)。

       最後の 2 つの場合、返り値は "wait status" であり、 waitpid(2) に書かれているマクロ (つまり WIFEXITED() や
       WEXITSTATUS() などのマクロ) を使って検査することができる。

       system()  は他の子プロセスのウエイトステータスには影響を与えない。

エラー

       system()  can fail with any of the same errors as fork(2).

属性

       この節で使用されている用語の説明については、 attributes(7) を参照。

       ┌─────────────────┬───────────────┬─────────┐
       │インターフェース属性      │
       ├─────────────────┼───────────────┼─────────┤
       │system()         │ Thread safety │ MT-Safe │
       └─────────────────┴───────────────┴─────────┘

準拠

       POSIX.1-2001, POSIX.1-2008, C89, C99.

注意

       system() により簡便性と利便性がもたらされる。この関数は fork(2), execl(3), waitpid(2)  の呼び出しにおける
       詳細をすべて行うとともに、   シグナルの適切な処理も行う。   また、シェルは   command   に対して通常の置換
       (substitutions) と  I/O  リダイレクトを行う。  system()  を使用することによる主なコストは非効率性である。
       シェルを実行するプロセスを作成するためとそのシェルを実行するために、余計にシステムコールが必要となる。

       (「どの」ヘッダーファイルをインクルードするよりも前に)   機能検査マクロ  _XOPEN_SOURCE が定義された場合に
       は、 waitpid(2)  で説明されているマクロ群 (WEXITSTATUS()  等) が <stdlib.h>  をインクルードすると利用可能
       になる。

       既に述べたように、 system()  は SIGINTSIGQUIT を無視する。 よってループから system() を呼ぶプログラム
       は、 以下の例のように子プロセスの終了状態を自分でチェックしておかないと、 中断できなくなるかもしれない。

           while (something) {
               int ret = system("foo");

               if (WIFSIGNALED(ret) &&
                   (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
                       break;
           }

       According to POSIX.1, it is unspecified whether handlers registered using pthread_atfork(3)   are  called
       during the execution of system().  In the glibc implementation, such handlers are not called.

       glibc  2.1.3 より前のバージョンでは、 command が NULL の場合に /bin/sh が利用可能かどうかのチェックは実際
       には行わず、   いつでも利用可能であるとみなしていた。   system()    はこの場合に常に   1   を返していた。
       POSIX.1-2001  ではシェルが提供されているという標準に準拠した実装を  要求しているが、glibc  2.1.3 以降では
       シェルのチェックを実行している。    なぜなら、呼び出し元のプログラムが    system()     を呼び出すより前に
       (POSIX.1-2001  では規定されていない)  chroot(2)  を呼び出していた時には、シェルが利用可能でない場合や実行
       可能ファイル でない場合があるからである。

       シェルコマンドがステータス 127 で終了することもある。 この場合、system() の返り値は、子プロセスでシェルが
       実行できなかった場合と区別できない。

   Caveats
       Do  not use system()  from a privileged program (a set-user-ID or set-group-ID program, or a program with
       capabilities)  because strange values for some environment variables might  be  used  to  subvert  system
       integrity.   For  example,  PATH  could  be  manipulated  so  that  an arbitrary program is executed with
       privilege.  Use the exec(3)  family of functions instead, but not execlp(3)  or  execvp(3)   (which  also
       use the PATH environment variable to search for an executable).

       system()   will  not, in fact, work properly from programs with set-user-ID or set-group-ID privileges on
       systems on which /bin/sh is bash version 2: as a security measure, bash 2 drops  privileges  on  startup.
       (Debian uses a different shell, dash(1), which does not do this when invoked as sh.)

       Any  user  input  that  is  employed  as  part  of  command should be carefully sanitized, to ensure that
       unexpected shell commands or command options are not executed.  Such  risks  are  especially  grave  when
       using system()  from a privileged program.

関連項目

       sh(1), execve(2), fork(2), sigaction(2), sigprocmask(2), wait(2), exec(3), signal(7)

この文書について

       この  man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告
       に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

                                                   2019-03-06                                          SYSTEM(3)