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

名前
execve - プログラムを実行する
書式
#include <unistd.h>
int execve(const char *pathname, char *const argv[],
char *const envp[]);
説明
execve() executes the program referred to by pathname. This causes the program that is currently being
run by the calling process to be replaced with a new program, with newly initialized stack, heap, and
(initialized and uninitialized) data segments.
pathname は、バイナリ実行形式か、 以下の形式の行で始まるスクリプトでなければならない。
#!interpreter [optional-arg]
後者の詳細は、後ろの「インタープリタースクリプト」の節を参照のこと。
argv is an array of pointers to strings passed to the new program as its command-line arguments. By
convention, the first of these strings (i.e., argv[0]) should contain the filename associated with the
file being executed. The argv array must be terminated by a NULL pointer. (Thus, in the new program,
argv[argc] will be NULL.)
envp is an array of pointers to strings, conventionally of the form key=value, which are passed as the
environment of the new program. The envp array must be terminated by a NULL pointer.
The argument vector and environment can be accessed by the new program's main function, when it is
defined as:
int main(int argc, char *argv[], char *envp[])
Note, however, that the use of a third argument to the main function is not specified in POSIX.1;
according to POSIX.1, the environment should be accessed via the external variable environ(7).
execve() does not return on success, and the text, initialized data, uninitialized data (bss), and stack
of the calling process are overwritten according to the contents of the newly loaded program.
元のプログラムが ptrace されている場合、 execve() が成功した後に そのプログラムに SIGTRAP シグナルが送ら
れる。
pathname で参照されるプログラムファイルに set-user-ID ビットが設定されている場合、呼び出したプロセスの実
効 (effective) ユーザー ID は プログラムファイルの所有者 (owner) に変更される。 同様に、プログラムファイ
ルに set-group-ID ビットが設定されていた場合、 呼び出したプロセスの有効グループ ID は プログラムファイル
のグループに変更される。
The aforementioned transformations of the effective IDs are not performed (i.e., the set-user-ID and
set-group-ID bits are ignored) if any of the following is true:
* the no_new_privs attribute is set for the calling thread (see prctl(2));
* the underlying filesystem is mounted nosuid (the MS_NOSUID flag for mount(2)); or
* the calling process is being ptraced.
The capabilities of the program file (see capabilities(7)) are also ignored if any of the above are
true.
プロセスの実効ユーザー ID は保存 (saved) set-user-ID にコピーされる。 同様に、実効グループ ID は保存
set-group-ID にコピーされる。 このコピーは、set-user-ID / set-group-ID モードビットにより発生する 実効 ID
の変更後に行われる。
The process's real UID and real GID, as well its supplementary group IDs, are unchanged by a call to
execve().
実行ファイルが動的リンクされた a.out 実行形式で、共有ライブラリの スタブを含むものだった場合、実行の開始
時に Linux の ダイナミックリンカー ld.so(8) が呼び出され、必要な共有オブジェクトをメモリーに読み込んでリ
ンクを行う。
実行ファイルがダイナミックリンクされた ELF 実行形式だった場合、 PT_INTERP セグメントに指定されたインター
プリターが必要な 共有オブジェクト (shared library) を読み込むのに使用される。 通常、インタープリターは
glibc をリンクしたバイナリでは /lib/ld-linux.so.2 である。 (ld-linux.so(8) を参照)
Effect on process attributes
以下に示す以外のすべてのプロセス属性は execve() の前後で保持される。
* 捕捉されたシグナルの処理方法 (disposition) は デフォルト動作にリセットされる (signal(7))。
* 代替シグナルスタックはどれも保持されない (sigaltstack(2))。
* メモリーマッピングは保持されない (mmap(2))。
* 付加された (attached) System V 共有メモリーセグメントは分離される (shmat(2))。
* POSIX 共有メモリー領域はマッピングを解除される (shm_open(3))。
* オープンされた POSIX メッセージキューディスクリプターはクローズされる (mq_overview(7))。
* オープンされた POSIX 名前付きセマフォはいずれもクローズされる (sem_overview(7))。
* POSIX タイマーは保持されない (timer_create(2))。
* オープンされたディレクトリストリームはいずれもクローズされる (opendir(3))。
* メモリーロックは保持されない (mlock(2), mlockall(2))。
* 終了 (exit) ハンドラーは保持されない (atexit(3), on_exit(3))。
* 浮動小数点関連の環境はデフォルトにリセットされる (fenv(3) 参照)。
上記のリストのプロセス属性はいずれも POSIX.1 で規定されている。 以下に示す Linux 固有のプロセス属性も
execve() の前後で保持されない。
* The process's "dumpable" attribute is set to the value 1, unless a set-user-ID program, a set-group-ID
program, or a program with capabilities is being executed, in which case the dumpable flag may instead
be reset to the value in /proc/sys/fs/suid_dumpable, in the circumstances described under
PR_SET_DUMPABLE in prctl(2). Note that changes to the "dumpable" attribute may cause ownership of
files in the process's /proc/[pid] directory to change to root:root, as described in proc(5).
* prctl(2) の PR_SET_KEEPCAPS フラグはクリアされる。
* (Linux 2.4.36 以降 / 2.6.23 以降) set-user-ID や set-group-ID されたプログラムが実行された場合、
prctl(2) の PR_SET_PDEATHSIG フラグで設定された parent death シグナルはクリアされる。
* プロセス名は新しい実行ファイルの名前にリセットされる。 プロセス名は prctl(2) の PR_SET_NAME で設定で
き、 ps -o comm で表示できる。
* SECBIT_KEEP_CAPS の securebits フラグはクリアされる。 capabilities(7) 参照。
* 終了シグナル (termination signal) は SIGCHLD にリセットされる (clone(2) 参照)。
* The file descriptor table is unshared, undoing the effect of the CLONE_FILES flag of clone(2).
以下の点についても注意すること:
* 呼び出し元スレッド以外の全てのスレッドは execve() 中に破棄される。 mutex、条件変数、その他の pthread
オブジェクトは保持されない。
* setlocale(LC_ALL, "C") 相当の処理がプログラム開始時に実行される。
* POSIX.1 は、動作が無視かデフォルトに設定されている全てのシグナル の処理方法は変更せずそのままにす
る、と規定している。 但し、POSIX.1-2001 には一つ例外があり、 SIGCHLD が無視になっている場合、 その処理
方法を変更せずにそのままにするか、デフォルト動作にリセットするかは 実装依存となっている。 Linux では前
者 (変更しない) となっている。
* 完了していない非同期 I/O 操作はキャンセルされる (aio_read(3), aio_write(3))。
* execve(2) 時のケーパビリティの扱いについては、 capabilities(7) を参照。
* デフォルトでは、ファイルディスクリプターは execve() を行った後でもオープンされたままである。
close-on-exec の印が付いているファイルディスクリプターはクローズされる。 fcntl(2) の FD_CLOEXEC の説
明を参照。 (ファイルディスクリプターがクローズされると、このプロセスが ファイルディスクリプターに対応
するファイルに対して獲得していた レコードのロックが全て解放されることになる。) POSIX.1 では、 ファイ
ルディスクリプター 0, 1, 2 が execve() 成功後にどこかでクローズされ、かつ 実行されるファイルに
set-user_ID か set-group_ID のモードビットが セットされていてプロセスが特権を獲得した場合、 システムは
何らかのファイルをオープンする際に これらの番号のディスクリプターのどれかを使うことがある、 とされてい
る。 原則として、移植性が必要なプログラムでは、 特権の有無に関わらず、 execve() の前後でこれら 3つの
ファイルディスクリプターがクローズされたままで あることを前提にすることはできない。
インタープリタースクリプト
インタープリタースクリプトとは、実行許可が有効になっていて、 最初の行が以下の形になっているテキストファイ
ルのことである。
#!interpreter [optional-arg]
The interpreter must be a valid pathname for an executable file.
execve() の pathname 引数がインタープリタースクリプトを指定している場合、 interpreter は以下の引数で起動
される。
interpreter [optional-arg] pathname arg...
where pathname is the absolute pathname of the file specified as the first argument of execve(), and
arg... is the series of words pointed to by the argv argument of execve(), starting at argv[1]. Note
that there is no way to get the argv[0] that was passed to the execve() call.
移植性を持たすには、 optional-arg は空か 1ワードだけにすべきである (つまり、ホワイトスペースを含めるべき
ではない)。 下記の「注意」の節を参照。
Since Linux 2.6.28, the kernel permits the interpreter of a script to itself be a script. This
permission is recursive, up to a limit of four recursions, so that the interpreter may be a script which
is interpreted by a script, and so on.
引数と環境変数の合計サイズの上限
ほとんどの UNIX の実装は、新しいプログラムに渡すことができる コマンドライン引数 (argv) と環境変数 (envp)
の文字列群の合計サイズに何らかの上限を設けている。 POSIX.1 は、 ARG_MAX 定数を使ってこの上限を決める実装
を認めている (ARG_MAX は <limits.h> で定義されるか、実行時に sysconf(_SC_ARG_MAX) の呼び出しで入手できる
かのいずれかである)。
カーネル 2.6.23 より前の Linux では、環境変数と引数の文字列群を 格納するのに使用されるメモリーは 32 ペー
ジに制限されていた (32 ページというのはカーネル定数 MAX_ARG_PAGES で定義される)。したがって、 ページサイ
ズが 4 kB のアーキテクチャーでは、 最大サイズは 128 kB ということになる。
On kernel 2.6.23 and later, most architectures support a size limit derived from the soft RLIMIT_STACK
resource limit (see getrlimit(2)) that is in force at the time of the execve() call. (Architectures
with no memory management unit are excepted: they maintain the limit that was in effect before kernel
2.6.23.) This change allows programs to have a much larger argument and/or environment list. For these
architectures, the total size is limited to 1/4 of the allowed stack size. (Imposing the 1/4-limit
ensures that the new program always has some stack space.) Additionally, the total size is limited to
3/4 of the value of the kernel constant _STK_LIM (8 Mibibytes). Since Linux 2.6.25, the kernel also
places a floor of 32 pages on this size limit, so that, even when RLIMIT_STACK is set very low,
applications are guaranteed to have at least as much argument and environment space as was provided by
Linux 2.6.23 and earlier. (This guarantee was not provided in Linux 2.6.23 and 2.6.24.) Additionally,
the limit per string is 32 pages (the kernel constant MAX_ARG_STRLEN), and the maximum number of strings
is 0x7FFFFFFF.
返り値
成功すると execve() は返らない。エラーの場合は -1 を返し、 errno を適切に設定する。
エラー
E2BIG 環境変数 (envp) と引数リスト (argv) の合計バイト数が大き過ぎる。
EACCES pathname やスクリプトインタープリター名の構成要素に検索許可 (search permission) が与えられていな
い (path_resolution(7) も参照すること)。
EACCES ファイルもしくはスクリプトのインタープリターが通常ファイル (regular file) でない。
EACCES ファイルやスクリプトや ELF インタープリターに 実行許可 (execute permission) が与えられていない。
EACCES ファイルシステムが noexec でマウントされている。
EAGAIN (Linux 3.1 以降)
set*uid() のいずれかの呼び出しでプロセスの実 UID が変更されたとすると、呼び出し元の RLIMIT_NPROC
リソース上限 (setrlimit(2) 参照) を超えてしまう、 現在もまだ超えている。 このエラーの詳細な説明に
ついては「注意」の節を参照。
EFAULT pathname または配列 argv か envp のポインターの一つがアクセス可能なアドレス空間の外を指している。
EINVAL ELF 実行形式で複数の PT_INTERP セグメントが存在する。 (すなわち複数のインタープリターを指定した。)
EIO I/O エラーが発生した。
EISDIR ELF インタープリターがディレクトリだった。
ELIBBAD
ELF インタープリターが理解できるフォーマットでなかった。
ELOOP pathname やスクリプトや ELF のインタープリターを解決する際に遭遇した シンボリックリンクが多過ぎ
る。
ELOOP The maximum recursion limit was reached during recursive script interpretation (see "Interpreter
scripts", above). Before Linux 3.8, the error produced for this case was ENOEXEC.
EMFILE The per-process limit on the number of open file descriptors has been reached.
ENAMETOOLONG
pathname が長過ぎる。
ENFILE オープンされたファイルの総数がシステム全体の上限に達していた。
ENOENT The file pathname or a script or ELF interpreter does not exist.
ENOEXEC
実行ファイルが理解できない形式であるか、違うアーキテクチャーのものか、 その他のフォーマットエラー
により実行ができなかった。
ENOMEM カーネルに十分なメモリーがない。
ENOTDIR
pathname やスクリプトや ELF のインタープリターの構成要素がディレクトリでない。
EPERM ファイルシステムが nosuid でマウントされ、ユーザーがスーパーユーザーでなく、 ファイルに
set-user-ID あるいは set-group-ID ビットが設定されている。
EPERM プロセスがトレースされ、ユーザーがスーパーユーザーでなく、 ファイルに set-user-ID あるいは
set-group-ID ビットが設定されている。
EPERM A "capability-dumb" applications would not obtain the full set of permitted capabilities granted
by the executable file. See capabilities(7).
ETXTBSY
指定された実行ファイルを書き込み用にオープンしているプロセスがある。
準拠
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD. POSIX does not document the #! behavior, but it exists (with
some variations) on other UNIX systems.
注意
One sometimes sees execve() (and the related functions described in exec(3)) described as "executing a
new process" (or similar). This is a highly misleading description: there is no new process; many
attributes of the calling process remain unchanged (in particular, its PID). All that execve() does is
arrange for an existing process (the calling process) to execute a new program.
set-user-id プロセスと set-group-ID プロセスは ptrace(2) できない。
ファイルシステムを nosuid でマウントした場合に set-user-ID/set-group-ID の実行ファイルを どの様に扱うか
は、Linux カーネルのバージョンによって異なる: あるバージョンでは、すでに必要な権限を持っている場合を除い
て、 その実行を拒否する (そして EPERM を返す)。別のあるバージョンでは set-user-ID/set-group-ID ビットのみ
を無視し exec() は成功する。
On Linux, argv and envp can be specified as NULL. In both cases, this has the same effect as specifying
the argument as a pointer to a list containing a single null pointer. Do not take advantage of this
nonstandard and nonportable misfeature! On many other UNIX systems, specifying argv as NULL will result
in an error (EFAULT). Some other UNIX systems treat the envp==NULL case the same as Linux.
POSIX.1 は、 sysconf(3) が返す値はプロセスの生存中は変化しないべきだとしている。 しかしながら、Linux
2.6.23 以降では、リソース上限 RLIMIT_STACK が変化した場合、 コマンドライン引数と環境変数を保持するための
空間に対する上限が 変化したことを反映して、 _SC_ARG_MAX が返す値も変化する。
execve() が失敗するほとんどの場合、 制御は元の実行可能イメージに戻り、 execve() の呼び出し元がエラーを処
理することができる。 しかしながら、 (リソース枯渇が原因となった場合など、まれに) 呼び出し元に制御が戻る時
点を過ぎてからエラーが発生する場合がある。 元の実行可能イメージはすでに破棄されているが、 新しいイメージ
が完全には構築されていないという状況である。 このような場合、カーネルはそのプロセスをシグナル SIGSEGV
(Linux 3.17 までは SIGKILL) で停止 (kill) する。
インタープリタースクリプト
The kernel imposes a maximum length on the text that follows the "#!" characters at the start of a
script; characters beyond the limit are ignored. Before Linux 5.1, the limit is 127 characters. Since
Linux 5.1, the limit is 255 characters.
インタープリタースクリプトの optional-arg 引数の解釈方法は実装により異なる。 Linux では、インタープリター
名 interpreter に続く文字列全体がインタープリターに 1個の引数として渡される。 しかし、動作が異なるシステ
ムもある。 あるシステムでは、 optional-arg のうち最初のホワイトスペースまでが 引数として渡される。 ま
た、別のシステムでは インタープリタースクリプトは複数の引数を持つことができ、 optional-arg 内のホワイトス
ペースが引数の区切りとなる。
Linux (like most other modern UNIX systems) ignores the set-user-ID and set-group-ID bits on scripts.
execve() と EAGAIN
execve() を呼び出した際に (Linux 3.1 以降で) 起こり得る EAGAIN エラーの詳細な説明を以下で行う。
直前の setuid(2), setreuid(2), setresuid(2) の呼び出しで、 そのプロセスの実ユーザー ID が変更され、 その
変更によりそのプロセスが RLIMIT_NPROC リソース上限を超過してしまった場合 (すなわち、新しい実ユーザー ID
に属するプロセス数が RLIMIT_NPROC リソース上限を超過した場合) に、 EAGAIN エラーが発生する。 Linux 2.6.0
以上 3.0 以下では、これにより set*uid() の呼び出しが失敗していた。 (Linux 2.6 より前では、このリソース上
限はユーザー ID を変更したプロセスには適用されていなかった。)
Linux 3.1 以降では、上で説明したシナリオでは set*uid() の呼び出しは失敗しない。 なぜなら、 返されたステー
タスの確認を行わず「呼び出し元が特権を持っている場合には」呼び出しは必ず成功するとみなしているバグがある
アプリケーションでは、セキュリティホールにつながることが非常によくあるからだ。 その代わり、 set*uid() の
呼び出しによる実 UID の変更は成功するが、 カーネルは PF_NPROC_EXCEEDED という名前の内部フラグをセットす
る。 このフラグは RLIMIT_NPROC リソース上限が超過したことを示す。 PF_NPROC_EXCEEDED フラグがセットされて
いて、その後で execve() が呼ばれた際にリソース上限がまだ超過していれば、 その execve() の呼び出しは
EAGAIN エラーで失敗する。 このカーネルのロジックにより、 特権デーモンでよく行われる処理フロー、 すなわち
fork(2) + set*uid() + execve() に対して、前と変わらず RLIMIT_NPROC リソース上限を適用できることが保証さ
れる。
(set*uid() と execve() の呼び出しの間に、この実 UID に属する他のプロセスが終了して) 次に execve() が呼び
出された際にこのリソース上限が超過してなければ、 execve() の呼び出しは成功し、カーネルは
PF_NPROC_EXCEEDED プロセスフラグをクリアする。 同じプロセスによって fork(2) の呼び出しが後で行われた場合
にも、このフラグはクリアされる。
歴史
UNIX V6 では exec() コールの引数リストは 0 で終端され、 main の引数リストは -1 で終端されていた。 そのた
め、 main の引数リストは、その後の exec() コールには直接使用できなかった。 UNIX V7 以降では、ともに NULL
で終端される。
例
このプログラムは、以下の二つ目のプログラムから実行するためのものである。 コマンドライン引数を 1行に 1個ず
つ表示するだけのプログラムである。
/* myecho.c */
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
for (int j = 0; j < argc; j++)
printf("argv[%d]: %s\n", j, argv[j]);
exit(EXIT_SUCCESS);
}
以下のプログラムは、コマンドライン引数で指定した名前のプログラムを 実行するのに使う。
/* execve.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
char *newargv[] = { NULL, "hello", "world", NULL };
char *newenviron[] = { NULL };
if (argc != 2) {
fprintf(stderr, "Usage: %s <file-to-exec>\n", argv[0]);
exit(EXIT_FAILURE);
}
newargv[0] = argv[1];
execve(argv[1], newargv, newenviron);
perror("execve"); /* execve() returns only on error */
exit(EXIT_FAILURE);
}
二つ目のプログラムを使って一つ目のプログラムを実行するには 以下のようにする。
$ cc myecho.c -o myecho
$ cc execve.c -o execve
$ ./execve ./myecho
argv[0]: ./myecho
argv[1]: hello
argv[2]: world
さらに、これらのプログラムを使って、スクリプトインタープリターの例を示す。 このために、「インタープリ
ター」として先ほど作成したプログラム myecho を使うスクリプトを作成する。
$ cat > script
#!./myecho script-arg
^D
$ chmod +x script
作成しておいたプログラムを使ってスクリプトを実行する。
$ ./execve ./script
argv[0]: ./myecho
argv[1]: script-arg
argv[2]: ./script
argv[3]: hello
argv[4]: world
関連項目
chmod(2), execveat(2), fork(2), get_robust_list(2), ptrace(2), exec(3), fexecve(3), getopt(3), system(3),
capabilities(7), credentials(7), environ(7), path_resolution(7), ld.so(8)
この文書について
この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告
に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。
Linux 2020-08-13 EXECVE(2)