OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / man3 / system.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
25 .\" Modified 14 May 2001, 23 Sep 2001 by aeb
26 .\" 2004-12-20, mtk
27 .\"
28 .\" Japanese Version Copyright (c) 1996 Shoichi OZAWA
29 .\"     all right reserved.
30 .\" Translated 1996-09-01, Shoichi OZAWA <ozawa@omika.hitachi.co.jp>
31 .\" Updated 2001-03-19, Kentaro Shirakata <argrath@ub32.org>
32 .\" Updated 2001-10-16, Kentaro Shirakata <argrath@ub32.org>
33 .\" Updated 2005-02-27, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
34 .\"
35 .\"WORD:        feature test macro      機能検査マクロ
36 .\"
37 .TH SYSTEM 3  2010-09-10 "" "Linux Programmer's Manual"
38 .SH 名前
39 system \- シェルコマンドの実行
40 .SH 書式
41 .nf
42 .B #include <stdlib.h>
43 .sp
44 .BI "int system(const char *" "command" );
45 .fi
46 .SH 説明
47 .BR system ()
48
49 .I command
50 で指定したコマンドを
51 .B "/bin/sh \-c"
52 .I command
53 の形で実行する。指定したコマンドが終了すればこの関数も終了する。
54 コマンド実行中は、
55 .B SIGCHLD
56 はブロックされ、
57 .B SIGINT
58
59 .B SIGQUIT
60 は無視される。
61 .SH 返り値
62 エラーが発生した場合
63 .RB ( fork (2)
64 に失敗した場合など)、\-1 を返す。
65 そうでなければ、コマンドのステータスを返す。
66 後者の場合、ステータスは
67 .BR wait (2)
68 で定義されているフォーマットで返ってくる。
69 従って、コマンドの終了コードは
70 .I WEXITSTATUS(status)
71 で得ることが出来る。
72 .I /bin/sh
73 が実行出来なかった場合、
74 終了ステータスはコマンドが
75 .I exit(127)
76 を実行した場合と同じになる。
77 .PP
78 .I command
79 の値が NULL のときは、
80 .BR system ()
81 はシェルが利用可能ならゼロ以外の値を返し、利用不可ならゼロを返す。
82 .PP
83 .BR system ()
84 は他の子プロセスのウエイトステータスには影響を与えない。
85 .SH 準拠
86 C89, C99, POSIX.1-2001.
87 .SH 注意
88 .PP
89 (「どの」ヘッダファイルをインクルードするよりも前に)
90 機能検査マクロ
91 .B _XOPEN_SOURCE
92 が定義された場合には、
93 .BR wait (2)
94 で説明されているマクロ群
95 .RB ( WEXITSTATUS ()
96 等) が
97 .I <stdlib.h>
98 をインクルードすると利用可能になる。
99 .PP
100 既に述べたように、
101 .BR system ()
102
103 .B SIGINT
104
105 .B SIGQUIT
106 を無視する。
107 よってループから
108 .BR system ()
109 を呼ぶプログラムは、
110 以下の例のように子プロセスの終了状態を自分でチェックしておかないと、
111 中断できなくなるかもしれない。
112 .br
113 .nf
114
115     while (something) {
116         int ret = system("foo");
117
118         if (WIFSIGNALED(ret) &&
119             (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
120                 break;
121     }
122 .fi
123 .PP
124 set-user-ID や set-group-ID の特権をもつプログラムの中では
125 .BR system ()
126 を使ってはいけない。なぜなら、ある環境変数の未知の値によって
127 システムの安全が損なわれるからである。代わりに
128 .BR exec (3)
129 関連の関数群の中で
130 .BR execlp (3)
131
132 .BR execvp (3)
133 以外の関数を使用すべきである。
134 実際のところ、
135 .BR system ()
136
137 .I /bin/sh
138 が bash バージョン 2 であるシステムでは、
139 set-user-ID や set-group-ID の特権を持つプログラムからは正しく動作しない。
140 なぜなら、bash バージョン 2 はスタートアップ時に特権を落とすからである。
141 (Debian では、sh として起動された時にはこのような動作を行なわないように
142 修正された bash を用いている)
143 .PP
144 glibc 2.1.3 より前のバージョンでは、
145 .I command
146 が NULL の場合に
147 .I /bin/sh
148 が利用可能かどうかのチェックは実際には行わず、
149 いつでも利用可能であるとみなしていた。
150 .BR system ()
151 はこの場合に常に 1 を返していた。
152 POSIX.1-2001 ではシェルが提供されているという標準に準拠した実装を
153 要求しているが、glibc 2.1.3 以降ではシェルのチェックを実行している。
154 なぜなら、呼び出し元のプログラムが
155 .BR system ()
156 を呼び出すより前に (POSIX.1-2001 では規定されていない)
157 .BR chroot (2)
158 を呼び出していた時には、シェルが利用可能でない場合や実行可能ファイル
159 でない場合があるからである。
160 .PP
161 実行したシェルコマンドが 127
162 .RB ( /bin/sh
163 の呼び出しに失敗した時に返す値) を返すことも考えられる。
164 そのため、プログラムは (リターンコードを見るだけでは)
165 .BR execve (2)
166 の呼び出しが失敗したことを確実に知ることはできない。
167 .SH 関連項目
168 .BR sh (1),
169 .BR signal (2),
170 .BR wait (2),
171 .BR exec (3)