OSDN Git Service

Update release for LDP 3.67
[linuxjm/LDP_man-pages.git] / release / man3 / system.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\" and Copyright (c) 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
28 .\" Modified 14 May 2001, 23 Sep 2001 by aeb
29 .\" 2004-12-20, mtk
30 .\"
31 .\"*******************************************************************
32 .\"
33 .\" This file was generated with po4a. Translate the source file.
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" Japanese Version Copyright (c) 1996 Shoichi OZAWA
38 .\"     all right reserved.
39 .\" Translated 1996-09-01, Shoichi OZAWA <ozawa@omika.hitachi.co.jp>
40 .\" Updated 2001-03-19, Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated 2001-10-16, Kentaro Shirakata <argrath@ub32.org>
42 .\" Updated 2005-02-27, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
43 .\"
44 .TH SYSTEM 3 2014\-05\-10 "" "Linux Programmer's Manual"
45 .SH 名前
46 system \- シェルコマンドの実行
47 .SH 書式
48 .nf
49 \fB#include <stdlib.h>\fP
50 .sp
51 \fBint system(const char *\fP\fIcommand\fP\fB);\fP
52 .fi
53 .SH 説明
54 \fBsystem\fP() ライブラリ関数は、\fBfork\fP(2) を使って子プロセスを作成し、その子プロセスは以下のように \fIcommand\fP
55 で指定されたシェルコマンドを \fBexecl\fP(3) を使って実行する。
56
57     execl("/bin/sh", "sh". "\-c", command, (char *) 0);
58
59 \fBsystem\fP() が返るのはコマンドが完了した後である。
60
61 コマンドの実行中は、 \fBsystem\fP() を呼び出したプロセスでは、 \fBSIGCHLD\fP はブロックされ、 \fBSIGINT\fP と
62 \fBSIGQUIT\fP は無視される (\fIcommand\fP を実行する子プロセスでは、これらのシグナルはデフォルトの処理方法にしたがって処理される)。
63
64 \fIcommand\fP が NULL の場合、 \fBsystem\fP()  はそのシステムでシェルが利用可能かを示すステータスを返す。
65 .SH 返り値
66 \fBsystem\fP() の返り値は以下のいずれかである。
67 .IP * 3
68 \fIcommand\fP が NULL の場合、 シェルが利用可能ならゼロ以外の値、利用不可なら 0。
69 .IP *
70 子プロセスを作成できなかった場合、または子プロセスのステータスを取得できなかった場合、 返り値は \-1 である。
71 .IP *
72 子プロセスでシェルを実行できなかった場合、 返り値は子プロセスがステータス 127 で \fB_exit\fP(2) を呼び出して終了したのと同じになる。
73 .IP *
74 システムコールがすべて成功した場合、 返り値は \fIcommand\fP を実行するのに使用された子プロセスのシェルの終了ステータスとなる
75 (シェルの終了ステータスはそのシェルが実行した最後のコマンドの終了ステータスである)。
76 .PP
77 最後の 2 つの場合、返り値は "wait status" であり、 \fBwaitpid\fP(2) に書かれているマクロ (つまり
78 \fBWIFEXITED\fP() や \fBWEXITSTATUS\fP() などのマクロ) を使って検査することができる。
79 .PP
80 \fBsystem\fP()  は他の子プロセスのウエイトステータスには影響を与えない。
81 .SH 準拠
82 C89, C99, POSIX.1\-2001.
83 .SH 注意
84 \fBsystem\fP()  provides simplicity and convenience: it handles all of the
85 details of calling \fBfork\fP(2), \fBexecl\fP(3), and \fBwaitpid\fP(2), as well as
86 the necessary manipulations of signals; in addition, the shell performs the
87 usual substitutions and I/O redirections for \fIcommand\fP.  The main cost of
88 \fBsystem\fP()  is inefficiency: additional system calls are required to create
89 the process that runs the shell and to execute the shell.
90
91 (「どの」ヘッダファイルをインクルードするよりも前に)  機能検査マクロ \fB_XOPEN_SOURCE\fP が定義された場合には、
92 \fBwaitpid\fP(2)  で説明されているマクロ群 (\fBWEXITSTATUS\fP()  等) が \fI<stdlib.h>\fP
93 をインクルードすると利用可能になる。
94 .PP
95 既に述べたように、 \fBsystem\fP()  は \fBSIGINT\fP と \fBSIGQUIT\fP を無視する。 よってループから \fBsystem\fP()
96 を呼ぶプログラムは、 以下の例のように子プロセスの終了状態を自分でチェックしておかないと、 中断できなくなるかもしれない。
97 .br
98 .nf
99
100     while (something) {
101         int ret = system("foo");
102
103         if (WIFSIGNALED(ret) &&
104             (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
105                 break;
106     }
107 .fi
108 .PP
109 set\-user\-ID や set\-group\-ID の特権をもつプログラムの中では \fBsystem\fP()
110 を使ってはいけない。なぜなら、ある環境変数の未知の値によって システムの安全が損なわれるからである。代わりに \fBexec\fP(3)  関連の関数群の中で
111 \fBexeclp\fP(3)  と \fBexecvp\fP(3)  以外の関数を使用すべきである。 実際のところ、 \fBsystem\fP()  は
112 \fI/bin/sh\fP が bash バージョン 2 であるシステムでは、 set\-user\-ID や set\-group\-ID
113 の特権を持つプログラムからは正しく動作しない。 なぜなら、bash バージョン 2 はスタートアップ時に特権を落とすからである。 (Debian
114 では、sh として起動された時にはこのような動作を行なわないように 修正された bash を用いている)
115 .PP
116 glibc 2.1.3 より前のバージョンでは、 \fIcommand\fP が NULL の場合に \fI/bin/sh\fP
117 が利用可能かどうかのチェックは実際には行わず、 いつでも利用可能であるとみなしていた。 \fBsystem\fP()  はこの場合に常に 1 を返していた。
118 POSIX.1\-2001 ではシェルが提供されているという標準に準拠した実装を 要求しているが、glibc 2.1.3
119 以降ではシェルのチェックを実行している。 なぜなら、呼び出し元のプログラムが \fBsystem\fP()  を呼び出すより前に (POSIX.1\-2001
120 では規定されていない)  \fBchroot\fP(2)  を呼び出していた時には、シェルが利用可能でない場合や実行可能ファイル でない場合があるからである。
121 .PP
122 It is possible for the shell command to terminate with a status of 127,
123 which yields a \fBsystem\fP()  return value that is indistinguishable from the
124 case where a shell could not be executed in the child process.
125 .SH 関連項目
126 \fBsh\fP(1), \fBsigaction\fP(2), \fBsigprocmask\fP(2), \fBfork\fP(2), \fBwait\fP(2),
127 \fBexec\fP(3), \fBsignal\fP(7)
128 .SH この文書について
129 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
130 である。プロジェクトの説明とバグ報告に関する情報は
131 http://www.kernel.org/doc/man\-pages/ に書かれている。