execveのヘルプ・マニュアル
日本語 英語
execve --help
man execve
EXECVE(2) Linux Programmer’s Manual EXECVE(2)
名前
execve - プログラムを実行する
書式
#include
int execve(const char *filename, char *const argv[],
char *const envp[]);
説明
execve() は、filename によって指定されたプログラムを実行する。 filename
は、バイナリ実行形式か、以下の形式の行で始まるスクリプトでなければな ら
ない。
#! interpreter [optional-arg]
後者の詳細は、後ろの「インタプリタ・スクリプト」の節を参照のこと。
argv は新しいプログラムに渡される引き数文字列の配列である。 envp は文字
列の配列であり、伝統的に key=value の形式をしており、新しいプログラムの
環 境変数として渡される。 argv と envp はいずれもの NULL ポインタで終わ
っている必要がある。引き数配列と環境変数は、呼び出されたプログ ラ ム の
main 関数を以下のように定義することによってアクセス可能になる。
int main(int argc, char *argv[], char *envp[])
成 功 し た 場 合、 execve() は返らない。そして、呼び出し元のプロセスの
text, data, bss, スタックは、読み込まれたプログラムによって上書きされる
。
元 のプログラムが ptrace されている場合、 execve() が成功した後にそのプ
ログラムに SIGTRAP が送られる。
filename で指定されたプログラムファイルに set-user-ID ビットが設定さ れ
て お り 、 フ ァ イ ルが存在するファイルシステムが nosuid (mount(2) の
MS_NOSUID フラグ) でマウントされておらず、呼び出したプロセス が ptrace
されていない場合、呼び出したプロセスの実効 (effective) ユーザ ID はプロ
グラムファイルの所有者 (owner) に変更される。同様に、プログラムファイル
に set-group-ID ビットが設定されていた場合、呼び出したプロセスの有効グ
ループ ID はプログラムファイルのグループに変更される。
プロセスの実効ユーザ ID は保存 (saved) set-user-ID にコピーされる。同様
に 、 実 効グループ ID は保存 set-group-ID にコピーされる。このコピーは
、set-user-ID / set-group-ID 許可ビットにより発生する実効 ID の変更後に
行われる。
実行ファイルが動的リンクされた a.out 実行形式で、共有ライブラリのスタブ
を含むものだった場合、実行の開始時に Linux のダイナミ ッ ク ・ リ ン カ
ld.so(8) が呼び出され、必要な共有ライブラリをメモリに読み込んでリンクを
行う。
実行ファイルがダイナミック・リンクされた ELF 実行形式 だ っ た 場 合 、
PT_INTERP セ グ メ ントに指定されたインタプリタが必要な共有ライブラリ
(shared library) を読み込むのに使用される。通常、インタプリタとしては、
Linux libc 5 をリンクしたバイナリの場合には /lib/ld-linux.so.1 が、
glibc 2 をリンクしたバイナリの場合には /lib/ld-linux.so.2 が使用され る
。
以下に示す以外のすべてのプロセス属性は execve() の前後で保持される。
* 処理待ち (pending) のシグナル集合はクリアされる (sigpending(2))。
* 捕捉されたシグナルの処理方法 (disposition) は「無視」にリセットされ
る。
* 代替シグナルスタックはどれも保持されない (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-2001 で規定されている。 以
下に示す Linux 固有のプロセス属性も execve() の前後で保持されない。
* set-user-ID か set-group-ID されたプログラムが実行されている場合、
prctl(2) の PR_SET_DUMPABLE フラグはクリアされる。それ以外の場合、こ
のフラグはセットされる。
* prctl(2) の PR_SET_KEEPCAPS フラグはクリアされる。
* プ ロセス名は新しい実行ファイルの名前にリセットされる。プロセス名は
prctl(2) の PR_SET_NAME で設定でき、 ps -o comm で表示できる。
* 終了シグナル (termination signal) は SIGCHLD に リ セ ッ ト さ れ る
(clone(2) 参照)。
以下の点についても注意すること:
* 呼 び出し元スレッド以外の全てのスレッドは execve() 中に破棄される。
mutex、条件変数、その他の pthread オブジェクトは保持されない。
* setlocale(LC_ALL, "C") 相当の処理がプログラム開始時に実行される。
* POSIX.1-2001 は、動作が無視かデフォルトに設定されている全てのシグ ナ
ル の 処 理 方 法 は 変 更 せ ずそのままにする、と規定している。但し
、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-2001 では、ファイルディスクリプタ 0,
1, 2 が execve() 成功後にどこかでクローズされ、かつ実行されるファイ
ルに set-user_ID か set-group_ID の許可ビットがセットされていてプ ロ
セスが特権を獲得した場合、システムは何らかのファイルをオープンする際
にこれらの番号のディスクリプタのどれかを使うことがある、とされている
。 原 則として、移植性が必要なプログラムでは、特権の有無に関わらず、
execve() の前後でこれら 3つのファイルディスクリプタがクローズされ た
ままであることを前提にすることはできない。
インタプリタ・スクリプト
イ ンタプリタ・スクリプトとは、実行許可が有効になっていて、最初の行が以
下の形になっているテキストファイルのことである。
#! interpreter [optional-arg]
interpreter は有効な実行ファイルのパス名でなければならず、それ自身が ス
ク リプトであってはならない。 execve() の filename 引き数がインタプリタ
スクリプトを指定している場合、 interpreter は以下の引き数で起動される。
interpreter [optional-arg] filename arg...
arg... は execve() の argv 引き数が指すワード列である。
移 植性を持たすには、 optional-arg は空か 1ワードだけにすべきである (つ
まり、ホワイト・スペースを含めるべきではない)。下記の「注意」の節を参照
。
引き数と環境変数の合計サイズの上限
ほ とんどの Unix の実装は、新しいプログラムに渡すことができるコマンドラ
イン引き数 (argv) と環境変数 (envp) の文字列群の合計サイズに何らかの 上
限 を設けている。 POSIX.1 は、 ARG_MAX 定数を使ってこの上限を決める実装
を認めている (ARG_MAX は で 定 義 さ れ る か 、 実 行 時 に
sysconf(_SC_ARG_MAX) の呼び出しで入手できるかのいずれかである)。
カーネル 2.6.23 より前の Linux では、環境変数と引き数の文字列群を格納す
るのに使用されるメモリは 32 ページに制限されていた (32 ページというのは
カ ーネル定数 MAX_ARG_PAGES で定義される)。したがって、ページサイズが 4
kB のアーキテクチャでは、最大サイズは 128 kB ということになる。
カーネル 2.6.23 以降では、ほとんどのアーキテクチャにおいて 、 execve()
が 呼び出された時点で適用されているリソースのソフト上限 RLIMIT_STACK に
基づいたサイズ上限が使われる (メモリ管理ユニット (MMU) を持たないアーキ
テ ク チ ャは上記の変更の例外であり、これらのアーキテクチャではカーネル
2.6.23 より前と同じ上限がそのまま使用される)。これらのアーキテクチャ で
は、合計サイズは許可されたスタックサイズの 1/4 に制限されている (1/4 の
上限を設けているのは、新しいプログラムが必ずある程度のスタック空間を 持
てることを保証するためである)。 Linux 2.6.25 以降では、カーネルはこのサ
イズ上限に 32 ページの下限を設けている。これにより、 RLIMIT_STACK が 非
常に小さく設定された場合でも、アプリケーションが少なくとも Linux 2.6.23
以前で提供されていたのと同じ大きさの引き数と環境変数の空間と同じだけ は
確 保 で き る ことが保証されている (この最低限の保証は Linux 2.6.23 と
2.6.24 では提供されていない)。また、各文字列の上限は 32 ページ (カー ネ
ル定数 MAX_ARG_STRLEN) で、文字列数の最大値は 0x7FFFFFFF である。
返り値
成功すると execve() は返らない。エラーの場合は -1 を返し、 errno を適切
に設定する。
エラー
E2BIG 環境変数 (envp) と引き数リスト (argv) の合計バイト数が大き過ぎる
。
EACCES filename やスクリプトインタプリタ名の構成要素に検索許可 (search
permission) が与えられていない (path_resolution(7) も参照する こ
と)。
EACCES ファイルもしくはスクリプトのインタプリタが通常ファイル (regular
file) でない。
EACCES ファイルやスクリプトや ELF インタプリタに実行許可 (execute per-
mission) が与えられていない。
EACCES ファイル・システムが noexec でマウントされている。
EFAULT filename がアクセス可能なアドレス空間の外を指している。
EINVAL ELF 実行形式で複数の PT_INTERP セグメントが存在する。 (すなわち
複数のインタプリタを指定した。)
EIO I/O エラーが発生した。
EISDIR ELF インタプリタがディレクトリだった。
ELIBBAD
ELF インタプリタが理解できるフォーマットでなかった。
ELOOP filename やスクリプトや ELF のインタプリタを解決する際に遭遇した
シンボリック・リンクが多過ぎる。
EMFILE そのプロセスがオープンできるファイル数の上限まで既にオープンして
いる。
ENAMETOOLONG
filename が長過ぎる。
ENFILE そのシステムでオープンできるファイル数の制限に達した。
ENOENT ファイル filename かスクリプトや ELF のインタプリタが存在しな い
。
ENOEXEC
実行ファイルが理解できない形式であるか、違うアーキテクチャのもの
か、その他のフォーマット・エラーにより実行ができなかった。
ENOMEM カーネルに十分なメモリがない。
ENOTDIR
filename やスクリプトや ELF のインタプリタの構成要素がディレクト
リでない。
EPERM ファイル・システムが nosuid でマウントされ、ユーザがスーパーユー
ザでなく、ファイルに set-user-ID あるいは set-group-ID ビット が
設定されている。
EPERM プロセスがトレースされ、ユーザがスーパーユーザでなく、ファイルに
set-user-ID あるいは set-group-ID ビットが設定されている。
ETXTBSY
実行ファイルを書き込み用にオープンしているプロセスがある。
準拠
SVr4, 4.3BSD, POSIX.1-2001. POSIX.1-2001 には #! 動作についての記述 は
ないが、他は互換性がある。
注意
set-user-id プロセスと set-group-ID プロセスは ptrace(2) できない。
Linux はスクリプトの set-user-ID と set-group-ID ビットを無視する。
ファイルシステムを nosuid でマウントした場合に set-user-ID/set-group-ID
の実行ファイルをどの様に扱うかは、Linux カーネルのバージョンによって 異
な る: あるバージョンでは、すでに必要な権限を持っている場合を除いて、そ
の実行を拒否する (そして EPERM を返す)。別のあるバージョ ン で は set-
user-ID/set-group-ID ビットのみを無視し exec() は成功する。
#! 実行形式のシェル・スクリプトの 1行目に許されている文字数は、最大 127
文字である。
インタプリタ・スクリプトの optional-arg 引き数の解釈方法は実装により 異
な る。 Linux では、インタプリタ名 interpreter に続く文字列全体がインタ
プリタに 1個の引き数として渡される。しかし、動作が異なるシステムもあ る
。 あるシステムでは、 optional-arg のうち最初のホワイト・スペースまでが
引き数として渡される。また、別のシステムではインタプリタ・スクリプト は
複 数の引き数を持つことができ、 optional-arg 内のホワイト・スペースが引
き数の区切りとなる。
Linux では、 argv に NULL を指定することができる。これは、この引き数 に
NULL ポインタ 1個だけを含むリストへのポインタを指定したのと同じ効果を持
つ。 「この間違った機能を利用しないこと」。これは非標準で、移植性もない
。 他のほとんどの Unix システムでは、これを行うとエラー (EFAULT) になる
。
POSIX.1-2001 は、 sysconf(3) が返す値はプロセスの生存中は変化しないべき
だ と し て い る 。 し かしながら、Linux 2.6.23 以降では、リソース上限
RLIMIT_STACK が変化した場合、コマンドライン引き数と環境変数を保持するた
めの空間に対する上限が変化したことを反映して、 _SC_ARG_MAX が返す値も変
化する。
歴史
Unix V6 では exec() コールの引き数リストは 0 で終端され、 main の引き数
リ ストは -1 で終端されていた。そのため、 main の引き数リストは、その後
の exec() コールには直接使用できなかった。 Unix V7 以降では 、 と も に
NULL で終端される。
例
こ のプログラムは、以下の二つ目のプログラムから実行するためのものである
。コマンドラインを 1行に 1個ずつ表示するだけのプログラムである。
/* myecho.c */
#include
#include
int
main(int argc, char *argv[])
{
int j;
for (j = 0; j < argc; j++)
printf("argv[%d]: %s\n", j, argv[j]);
exit(EXIT_SUCCESS);
}
以下のプログラムは、コマンドライン引き数で指定した名前のプログラムを 実
行するのに使う。
/* execve.c */
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
char *newargv[] = { NULL, "hello", "world", NULL };
char *newenviron[] = { NULL };
assert(argc == 2); /* argv[1] identifies
program to exec */
newargv[0] = argv[1];
execve(argv[1], newargv, newenviron);
perror("execve"); /* execve() only returns 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.sh
#! ./myecho script-arg
^D
$ chmod +x script.sh
作成しておいたプログラムを使ってスクリプトを実行する。
$ ./execve ./script.sh
argv[0]: ./myecho
argv[1]: script-arg
argv[2]: ./script.sh
argv[3]: hello
argv[4]: world
関連項目
chmod(2), fork(2), ptrace(2), execl(3), fexecve(3), getopt(3), creden-
tials(7), environ(7), path_resolution(7), ld.so(8)
Linux 2008-11-28 EXECVE(2)
EXECVE(2) Linux Programmer’s Manual EXECVE(2)
NAME
execve - execute program
SYNOPSIS
#include
int execve(const char *filename, char *const argv[],
char *const envp[]);
DESCRIPTION
execve() executes the program pointed to by filename. filename must be
either a binary executable, or a script starting with a line of the
form:
#! interpreter [optional-arg]
For details of the latter case, see "Interpreter scripts" below.
argv is an array of argument strings passed to the new program. envp
is an array of strings, conventionally of the form key=value, which are
passed as environment to the new program. Both argv and envp must be
terminated by a null pointer. The argument vector and environment can
be accessed by the called program’s main function, when it is defined
as:
int main(int argc, char *argv[], char *envp[])
execve() does not return on success, and the text, data, bss, and stack
of the calling process are overwritten by that of the program loaded.
If the current program is being ptraced, a SIGTRAP is sent to it after
a successful execve().
If the set-user-ID bit is set on the program file pointed to by file-
name, and the underlying file system is not mounted nosuid (the
MS_NOSUID flag for mount(2)), and the calling process is not being
ptraced, then the effective user ID of the calling process is changed
to that of the owner of the program file. Similarly, when the set-
group-ID bit of the program file is set the effective group ID of the
calling process is set to the group of the program file.
The effective user ID of the process is copied to the saved set-user-
ID; similarly, the effective group ID is copied to the saved set-group-
ID. This copying takes place after any effective ID changes that occur
because of the set-user-ID and set-group-ID permission bits.
If the executable is an a.out dynamically linked binary executable con-
taining shared-library stubs, the Linux dynamic linker ld.so(8) is
called at the start of execution to bring needed shared libraries into
memory and link the executable with them.
If the executable is a dynamically linked ELF executable, the inter-
preter named in the PT_INTERP segment is used to load the needed shared
libraries. This interpreter is typically /lib/ld-linux.so.1 for bina-
ries linked with the Linux libc 5, or /lib/ld-linux.so.2 for binaries
linked with the glibc 2.
All process attributes are preserved during an execve(), except the
following:
* The dispositions of any signals that are being caught are reset
to being ignored.
* Any alternate signal stack is not preserved (sigaltstack(2)).
* Memory mappings are not preserved (mmap(2)).
* Attached System V shared memory segments are detached
(shmat(2)).
* POSIX shared memory regions are unmapped (shm_open(3)).
* Open POSIX message queue descriptors are closed
(mq_overview(7)).
* Any open POSIX named semaphores are closed (sem_overview(7)).
* POSIX timers are not preserved (timer_create(2)).
* Any open directory streams are closed (opendir(3)).
* Memory locks are not preserved (mlock(2), mlockall(2)).
* Exit handlers are not preserved (atexit(3), on_exit(3)).
* The floating-point environment is reset to the default (see
fenv(3)).
The process attributes in the preceding list are all specified in
POSIX.1-2001. The following Linux-specific process attributes are also
not preserved during an execve():
* The prctl(2) PR_SET_DUMPABLE flag is set, unless a set-user-ID or
set-group ID program is being executed, in which case it is cleared.
* The prctl(2) PR_SET_KEEPCAPS flag is cleared.
* The process name, as set by prctl(2) PR_SET_NAME (and displayed by
ps -o comm), is reset to the name of the new executable file.
* The termination signal is reset to SIGCHLD (see clone(2)).
Note the following further points:
* All threads other than the calling thread are destroyed during an
execve(). Mutexes, condition variables, and other pthreads objects
are not preserved.
* The equivalent of setlocale(LC_ALL, "C") is executed at program
start-up.
* POSIX.1-2001 specifies that the dispositions of any signals that are
ignored or set to the default are left unchanged. POSIX.1-2001
specifies one exception: if SIGCHLD is being ignored, then an imple-
mentation may leave the disposition unchanged or reset it to the
default; Linux does the former.
* Any outstanding asynchronous I/O operations are canceled
(aio_read(3), aio_write(3)).
* For the handling of capabilities during execve(), see capabili-
ties(7).
* By default, file descriptors remain open across an execve(). File
descriptors that are marked close-on-exec are closed; see the
description of FD_CLOEXEC in fcntl(2). (If a file descriptor is
closed, this will cause the release of all record locks obtained on
the underlying file by this process. See fcntl(2) for details.)
POSIX.1-2001 says that if file descriptors 0, 1, and 2 would other-
wise be closed after a successful execve(), and the process would
gain privilege because the set-user_ID or set-group_ID permission
bit was set on the executed file, then the system may open an
unspecified file for each of these file descriptors. As a general
principle, no portable program, whether privileged or not, can
assume that these three file descriptors will remain closed across
an execve().
Interpreter scripts
An interpreter script is a text file that has execute permission
enabled and whose first line is of the form:
#! interpreter [optional-arg]
The interpreter must be a valid pathname for an executable which is not
itself a script. If the filename argument of execve() specifies an
interpreter script, then interpreter will be invoked with the following
arguments:
interpreter [optional-arg] filename arg...
where arg... is the series of words pointed to by the argv argument of
execve().
For portable use, optional-arg should either be absent, or be specified
as a single word (i.e., it should not contain white space); see NOTES
below.
Limits on size of arguments and environment
Most Unix implementations impose some limit on the total size of the
command-line argument (argv) and environment (envp) strings that may be
passed to a new program. POSIX.1 allows an implementation to advertise
this limit using the ARG_MAX constant (either defined in or
available at run time using the call sysconf(_SC_ARG_MAX)).
On Linux prior to kernel 2.6.23, the memory used to store the environ-
ment and argument strings was limited to 32 pages (defined by the ker-
nel constant MAX_ARG_PAGES). On architectures with a 4-kB page size,
this yields a maximum size of 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 archi-
tectures, 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.) Since Linux 2.6.25, the kernel 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.
RETURN VALUE
On success, execve() does not return, on error -1 is returned, and
errno is set appropriately.
ERRORS
E2BIG The total number of bytes in the environment (envp) and argument
list (argv) is too large.
EACCES Search permission is denied on a component of the path prefix of
filename or the name of a script interpreter. (See also
path_resolution(7).)
EACCES The file or a script interpreter is not a regular file.
EACCES Execute permission is denied for the file or a script or ELF
interpreter.
EACCES The file system is mounted noexec.
EFAULT filename points outside your accessible address space.
EINVAL An ELF executable had more than one PT_INTERP segment (i.e.,
tried to name more than one interpreter).
EIO An I/O error occurred.
EISDIR An ELF interpreter was a directory.
ELIBBAD
An ELF interpreter was not in a recognized format.
ELOOP Too many symbolic links were encountered in resolving filename
or the name of a script or ELF interpreter.
EMFILE The process has the maximum number of files open.
ENAMETOOLONG
filename is too long.
ENFILE The system limit on the total number of open files has been
reached.
ENOENT The file filename or a script or ELF interpreter does not exist,
or a shared library needed for file or interpreter cannot be
found.
ENOEXEC
An executable is not in a recognized format, is for the wrong
architecture, or has some other format error that means it can-
not be executed.
ENOMEM Insufficient kernel memory was available.
ENOTDIR
A component of the path prefix of filename or a script or ELF
interpreter is not a directory.
EPERM The file system is mounted nosuid, the user is not the supe-
ruser, and the file has the set-user-ID or set-group-ID bit set.
EPERM The process is being traced, the user is not the superuser and
the file has the set-user-ID or set-group-ID bit set.
ETXTBSY
Executable was open for writing by one or more processes.
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001. POSIX.1-2001 does not document the #!
behavior but is otherwise compatible.
NOTES
Set-user-ID and set-group-ID processes can not be ptrace(2)d.
Linux ignores the set-user-ID and set-group-ID bits on scripts.
The result of mounting a file system nosuid varies across Linux kernel
versions: some will refuse execution of set-user-ID and set-group-ID
executables when this would give the user powers she did not have
already (and return EPERM), some will just ignore the set-user-ID and
set-group-ID bits and exec() successfully.
A maximum line length of 127 characters is allowed for the first line
in a #! executable shell script.
The semantics of the optional-arg argument of an interpreter script
vary across implementations. On Linux, the entire string following the
interpreter name is passed as a single argument to the interpreter, and
this string can include white space. However, behavior differs on some
other systems. Some systems use the first white space to terminate
optional-arg. On some systems, an interpreter script can have multiple
arguments, and white spaces in optional-arg are used to delimit the
arguments.
On Linux, argv can be specified as NULL, which has the same effect as
specifying this argument as a pointer to a list containing a single
NULL pointer. Do not take advantage of this misfeature! It is non-
standard and non-portable: on most other Unix systems doing this will
result in an error (EFAULT).
POSIX.1-2001 says that values returned by sysconf(3) should be invari-
ant over the lifetime of a process. However, since Linux 2.6.23, if
the RLIMIT_STACK resource limit changes, then the value reported by
_SC_ARG_MAX will also change, to reflect the fact that the limit on
space for holding command-line arguments and environment variables has
changed.
Historical
With Unix V6 the argument list of an exec() call was ended by 0, while
the argument list of main was ended by -1. Thus, this argument list
was not directly usable in a further exec() call. Since Unix V7 both
are NULL.
EXAMPLE
The following program is designed to be execed by the second program
below. It just echoes its command-line one per line.
/* myecho.c */
#include
#include
int
main(int argc, char *argv[])
{
int j;
for (j = 0; j < argc; j++)
printf("argv[%d]: %s\n", j, argv[j]);
exit(EXIT_SUCCESS);
}
This program can be used to exec the program named in its command-line
argument:
/* execve.c */
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
char *newargv[] = { NULL, "hello", "world", NULL };
char *newenviron[] = { NULL };
assert(argc == 2); /* argv[1] identifies
program to exec */
newargv[0] = argv[1];
execve(argv[1], newargv, newenviron);
perror("execve"); /* execve() only returns on error */
exit(EXIT_FAILURE);
}
We can use the second program to exec the first as follows:
$ cc myecho.c -o myecho
$ cc execve.c -o execve
$ ./execve ./myecho
argv[0]: ./myecho
argv[1]: hello
argv[2]: world
We can also use these programs to demonstrate the use of a script
interpreter. To do this we create a script whose "interpreter" is our
myecho program:
$ cat > script.sh
#! ./myecho script-arg
^D
$ chmod +x script.sh
We can then use our program to exec the script:
$ ./execve ./script.sh
argv[0]: ./myecho
argv[1]: script-arg
argv[2]: ./script.sh
argv[3]: hello
argv[4]: world
SEE ALSO
chmod(2), fork(2), ptrace(2), execl(3), fexecve(3), getopt(3), creden-
tials(7), environ(7), path_resolution(7), ld.so(8)
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/.
Linux 2009-04-21 EXECVE(2)