systemのヘルプ・マニュアル
日本語 英語
system --help
man system
SYSTEM(3) Linux Programmer’s Manual SYSTEM(3)
名前
system - シェルコマンドの実行
書式
#include
int system(const char *command);
説明
system() は command で指定したコマンドを /bin/sh -c command の形で実行
する。指定したコマンドが終了すればこの関数も終了する。コマンド実行中 は
、 SIGCHLD はブロックされ、 SIGINT と SIGQUIT は無視される。
返り値
エ ラーが発生した場合 (fork(2) に失敗した場合など)、-1 を返す。そうでな
ければ、コマンドのステータスを返す。後者の場合、ステータスは wait(2) で
定 義されているフォーマットで返ってくる。従って、コマンドの終了コードは
WEXITSTATUS(status) で得ることが出来る。 /bin/sh が実行出来なかった場合
、終了ステータスはコマンドが exit(127) を実行した場合と同じになる。
command の値が NULL のときは、 system() はシェルが利用可能ならゼロ以外
の値を返し、利用不可ならゼロを返す。
system() は他の子プロセスのウエイトステータスには影響を与えない。
準拠
C89, C99, POSIX.1-2001.
注意
機能検査マクロである _XOPEN_SOURCE が定義された場合には、 wait(2) で 説
明 されているマクロ群 (WEXITSTATUS() 等) が をインクルードす
ると利用可能になる。
既に述べたように、 system() は SIGINT と SIGQUIT を無視する。よってルー
プ から system() を呼ぶプログラムは、以下の例のように子プロセスの終了状
態を自分でチェックしておかないと、中断できなくなるかもしれない。
while (something) {
int ret = system("foo");
if (WIFSIGNALED(ret) &&
(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
break;
}
set-user-ID や set-group-ID の特権をもつプログラムの中では system() を
使 ってはいけない。なぜなら、ある環境変数の未知の値によってシステムの安
全が損なわれるからである。代わりに exec(3) 関連の関数群の中で execlp(3)
と execvp(3) 以外の関数を使用すべきである。実際のところ、 system() は
/bin/sh が bash バージョン 2 であるシステムでは、 set-user-ID や set-
group-ID の特権を持つプログラムからは正しく動作しない。なぜなら、bash
バージョン 2 はスタートアップ時に特権を落とすからである。 (Debian で は
、sh として起動された時にはこのような動作を行なわないように修正された
bash を用いている)
glibc 2.1.3 より前のバージョンでは、 command が NULL の場合 に /bin/sh
が 利用可能かどうかのチェックは実際には行わず、いつでも利用可能であると
みなしていた。 system() はこの場合に常に 1 を返していた。 POSIX.1-2001
で は シ ェ ル が提供されているという標準に準拠した実装を要求しているが
、glibc 2.1.3 以降ではシェルのチェックを実行している。なぜなら、呼び 出
し元のプログラムが system() を呼び出すより前に (POSIX.1-2001 では規定さ
れていない) chroot(2) を呼び出していた時には、シェルが利用可能でない 場
合や実行可能ファイルでない場合があるからである。
実 行 したシェルコマンドが 127 (/bin/sh の呼び出しに失敗した時に返す値)
を返すことも考えられる。そのため、プログラムは (リターンコードを見る だ
けでは) execve(2) の呼び出しが失敗したことを確実に知ることはできない。
関連項目
sh(1), signal(2), wait(2), exec(3)
2004-12-20 SYSTEM(3)
SYSTEM(3) Linux Programmer’s Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During exe-
cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
will be ignored.
RETURN VALUE
The value returned is -1 on error (e.g. fork(2) failed), and the
return status of the command otherwise. This latter return status is
in the format specified in wait(2). Thus, the exit code of the command
will be WEXITSTATUS(status). In case /bin/sh could not be executed,
the exit status will be that of a command that does exit(127).
If the value of command is NULL, system() returns non-zero if the shell
is available, and zero if not.
system() does not affect the wait status of any other children.
CONFORMING TO
C89, C99, POSIX.1-2001.
NOTES
If the _XOPEN_SOURCE feature test macro is defined, then the macros
described in wait(2) (WEXITSTATUS(), etc.) are made available when
including .
As mentioned, system() ignores SIGINT and SIGQUIT. This may make pro-
grams that call it from a loop uninterruptible, unless they take care
themselves to check the exit status of the child. E.g.
while (something) {
int ret = system("foo");
if (WIFSIGNALED(ret) &&
(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
break;
}
Do not use system() from a program with set-user-ID or set-group-ID
privileges, because strange values for some environment variables might
be used to subvert system integrity. Use the exec(3) family of func-
tions instead, but not execlp(3) or execvp(3). 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, since bash 2
drops privileges on startup. (Debian uses a modified bash which does
not do this when invoked as sh.)
In versions of glibc before 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).
It is possible for the shell command to return 127, so that code is not
a sure indication that the execve(2) call failed.
SEE ALSO
sh(1), signal(2), wait(2), exec(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2004-12-20 SYSTEM(3)