system

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

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

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <stdlib.h>

int system(const char *command);
 

説明

The system() library function behaves as if it used fork(2) to create a child process that executed the shell command specified in command using execl(3) as follows:

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

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

コマンドの実行中は、 system() を呼び出したプロセスでは、 SIGCHLD はブロックされ、 SIGINT と SIGQUIT は無視される (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 safetyMT-Safe
 

標準規格

C11, POSIX.1-2008.  

歴史

POSIX.1-2001, C89.  

注意

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

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

既に述べたように、 system() は SIGINT と SIGQUIT を無視する。 よってループから 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.

Before glibc 2.1.3, the check for the availability of /bin/sh was not actually performed if command was NULL; instead it was always assumed to be available, and system() always returned 1 in this case. Since glibc 2.1.3, this check is performed because, even though POSIX.1-2001 requires a conforming implementation to provide a shell, that shell may not be available or executable if the calling program has previously called chroot(2) (which is not specified by POSIX.1-2001).

シェルコマンドがステータス 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.  

バグ

If the command name starts with a hyphen, sh(1) interprets the command name as an option, and the behavior is undefined. (See the -c option to sh(1).) To work around this problem, prepend the command with a space as in the following call:

    system(" -unfortunate-command-name");
 

関連項目

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


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
標準規格
歴史
注意
Caveats
バグ
関連項目

This document was created by man2html, using the manual pages.