File: execve.2

package info (click to toggle)
manpages-ja 0.5.0.0.20221215%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 29,304 kB
  • sloc: perl: 161; makefile: 61
file content (586 lines) | stat: -rw-r--r-- 31,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
.\" and Copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" Modified by Michael Haardt <michael@moria.de>
.\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>:
.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 1999-11-12 by Urs Thuermann <urs@isnogud.escape.de>
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
.\" 2006-09-04 Michael Kerrisk <mtk.manpages@gmail.com>
.\"     Added list of process attributes that are not preserved on exec().
.\" 2007-09-14 Ollie Wild <aaw@google.com>, mtk
.\"     Add text describing limits on command-line arguments + environment
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 1996 TABATA Tomohira
.\"         all rights reserved.
.\" Translated 1996-07-04, TABATA Tomohira <loba@k2.t.u-tokyo.ac.jp>
.\" Updated 1997-12-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated 2001-08-17, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated 2005-02-05, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2005-11-19, Akihiro MOTOKI
.\" Updated 2006-08-13, Akihiro MOTOKI, LDP v2.39
.\" Updated 2007-01-09, Akihiro MOTOKI, LDP v2.43
.\" Updated 2007-06-03, Akihiro MOTOKI, LDP v2.51
.\" Updated 2007-10-12, Akihiro MOTOKI, LDP v2.66
.\" Updated 2008-04-04, Akihiro MOTOKI, LDP v2.79
.\" Updated 2008-11-05, Akihiro MOTOKI, LDP v3.12
.\" Updated 2012-05-29, Akihiro MOTOKI <amotoki@gmail.com>
.\" Updated 2013-03-22, Akihiro MOTOKI, LDP v3.50
.\" Updated 2013-07-22, Akihiro MOTOKI, LDP v3.52
.\"
.TH EXECVE 2 " 2020\-08\-13" Linux "Linux Programmer's Manual"
.SH 名前
execve \- プログラムを実行する
.SH 書式
\fB#include <unistd.h>\fP
.PP
 \fBint execve(const char *\fP\fIpathname\fP\fB, char *const \fP\fIargv\fP\fB[],\fP
.br
\fB char *const \fP\fIenvp\fP\fB[]);\fP
.SH 説明
\fBexecve\fP()  executes the program referred to by \fIpathname\fP.  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.
.PP
\fIpathname\fP は、バイナリ実行形式か、 以下の形式の行で始まるスクリプトでなければならない。
.PP
.in +4n
.EX
\fB#!\fP\fIinterpreter \fP[optional\-arg]
.EE
.in
.PP
後者の詳細は、後ろの「インタープリタースクリプト」の節を参照のこと。
.PP
\fIargv\fP 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.,
\fIargv[0]\fP)  should contain the filename associated with the file being
executed.  The \fIargv\fP array must be terminated by a NULL pointer.  (Thus,
in the new program, \fIargv[argc]\fP will be NULL.)
.PP
\fIenvp\fP is an array of pointers to strings, conventionally of the form
\fBkey=value\fP, which are passed as the environment of the new program.  The
\fIenvp\fP array must be terminated by a NULL pointer.
.PP
The argument vector and environment can be accessed by the new program's
main function, when it is defined as:
.PP
.in +4n
.EX
int main(int argc, char *argv[], char *envp[])
.EE
.in
.PP
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 \fBenviron\fP(7).
.PP
\fBexecve\fP()  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.
.PP
元のプログラムが ptrace されている場合、 \fBexecve\fP()  が成功した後に そのプログラムに \fBSIGTRAP\fP シグナルが送られる。
.PP
\fIpathname\fP で参照されるプログラムファイルに set\-user\-ID ビットが設定されている場合、呼び出したプロセスの実効
(effective) ユーザー ID は プログラムファイルの所有者 (owner) に変更される。 同様に、プログラムファイルに
set\-group\-ID ビットが設定されていた場合、 呼び出したプロセスの有効グループ ID は プログラムファイルのグループに変更される。
.PP
The aforementioned transformations of the effective IDs are \fInot\fP performed
(i.e., the set\-user\-ID and set\-group\-ID bits are ignored)  if any of the
following is true:
.IP * 3
the \fIno_new_privs\fP attribute is set for the calling thread (see
\fBprctl\fP(2));
.IP *
the underlying filesystem is mounted \fInosuid\fP (the \fBMS_NOSUID\fP flag for
\fBmount\fP(2)); or
.IP *
the calling process is being ptraced.
.PP
The capabilities of the program file (see \fBcapabilities\fP(7))  are also
ignored if any of the above are true.
.PP
プロセスの実効ユーザー ID は保存 (saved) set\-user\-ID にコピーされる。 同様に、実効グループ ID は保存
set\-group\-ID にコピーされる。 このコピーは、set\-user\-ID / set\-group\-ID モードビットにより発生する 実効 ID
の変更後に行われる。
.PP
The process's real UID and real GID, as well its supplementary group IDs,
are unchanged by a call to \fBexecve\fP().
.PP
実行ファイルが動的リンクされた a.out 実行形式で、共有ライブラリの スタブを含むものだった場合、実行の開始時に Linux の
ダイナミックリンカー \fBld.so\fP(8)  が呼び出され、必要な共有オブジェクトをメモリーに読み込んでリンクを行う。
.PP
.\"
実行ファイルがダイナミックリンクされた ELF 実行形式だった場合、
PT_INTERP セグメントに指定されたインタープリターが必要な 共有オブジェクト
(shared library) を読み込むのに使用される。
通常、インタープリターは glibc をリンクしたバイナリでは
\fI/lib/ld\-linux.so.2\fP である。 (\fBld\-linux.so\fP(8) を参照)
.SS "Effect on process attributes"
以下に示す以外のすべてのプロセス属性は \fBexecve\fP()  の前後で保持される。
.IP * 3
捕捉されたシグナルの処理方法 (disposition) は デフォルト動作にリセットされる (\fBsignal\fP(7))。
.IP *
代替シグナルスタックはどれも保持されない (\fBsigaltstack\fP(2))。
.IP *
メモリーマッピングは保持されない (\fBmmap\fP(2))。
.IP *
付加された (attached) System\ V 共有メモリーセグメントは分離される (\fBshmat\fP(2))。
.IP *
POSIX 共有メモリー領域はマッピングを解除される (\fBshm_open\fP(3))。
.IP *
オープンされた POSIX メッセージキューディスクリプターはクローズされる (\fBmq_overview\fP(7))。
.IP *
オープンされた POSIX 名前付きセマフォはいずれもクローズされる (\fBsem_overview\fP(7))。
.IP *
POSIX タイマーは保持されない (\fBtimer_create\fP(2))。
.IP *
オープンされたディレクトリストリームはいずれもクローズされる (\fBopendir\fP(3))。
.IP *
メモリーロックは保持されない (\fBmlock\fP(2), \fBmlockall\fP(2))。
.IP *
終了 (exit) ハンドラーは保持されない (\fBatexit\fP(3), \fBon_exit\fP(3))。
.IP *
浮動小数点関連の環境はデフォルトにリセットされる (\fBfenv\fP(3)  参照)。
.PP
上記のリストのプロセス属性はいずれも POSIX.1 で規定されている。 以下に示す Linux 固有のプロセス属性も \fBexecve\fP()
の前後で保持されない。
.IP * 3
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 \fI/proc/sys/fs/suid_dumpable\fP, in the circumstances described
under \fBPR_SET_DUMPABLE\fP in \fBprctl\fP(2).  Note that changes to the
"dumpable" attribute may cause ownership of files in the process's
\fI/proc/[pid]\fP directory to change to \fIroot:root\fP, as described in
\fBproc\fP(5).
.IP *
\fBprctl\fP(2)  の \fBPR_SET_KEEPCAPS\fP フラグはクリアされる。
.IP *
(Linux 2.4.36 以降 / 2.6.23 以降)  set\-user\-ID や set\-group\-ID されたプログラムが実行された場合、
\fBprctl\fP(2) の \fBPR_SET_PDEATHSIG\fP フラグで設定された parent death シグナルはクリアされる。
.IP *
プロセス名は新しい実行ファイルの名前にリセットされる。 プロセス名は \fBprctl\fP(2)  の \fBPR_SET_NAME\fP で設定でき、
\fIps\ \-o comm\fP で表示できる。
.IP *
\fBSECBIT_KEEP_CAPS\fP の \fIsecurebits\fP フラグはクリアされる。 \fBcapabilities\fP(7) 参照。
.IP *
終了シグナル (termination signal) は \fBSIGCHLD\fP にリセットされる (\fBclone\fP(2)  参照)。
.IP *
The file descriptor table is unshared, undoing the effect of the
\fBCLONE_FILES\fP flag of \fBclone\fP(2).
.PP
以下の点についても注意すること:
.IP * 3
呼び出し元スレッド以外の全てのスレッドは \fBexecve\fP()  中に破棄される。 mutex、条件変数、その他の pthread
オブジェクトは保持されない。
.IP *
\fIsetlocale(LC_ALL, "C")\fP 相当の処理がプログラム開始時に実行される。
.IP *
POSIX.1 は、動作が無視かデフォルトに設定されている全てのシグナル の処理方法は変更せずそのままにする、と規定している。
但し、POSIX.1\-2001 には一つ例外があり、 \fBSIGCHLD\fP が無視になっている場合、
その処理方法を変更せずにそのままにするか、デフォルト動作にリセットするかは 実装依存となっている。 Linux では前者 (変更しない) となっている。
.IP *
完了していない非同期 I/O 操作はキャンセルされる (\fBaio_read\fP(3), \fBaio_write\fP(3))。
.IP *
\fBexecve\fP(2)  時のケーパビリティの扱いについては、 \fBcapabilities\fP(7)  を参照。
.IP *
.\" On Linux it appears that these file descriptors are
.\" always open after an execve(), and it looks like
.\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
デフォルトでは、ファイルディスクリプターは \fBexecve\fP()  を行った後でもオープンされたままである。 close\-on\-exec
の印が付いているファイルディスクリプターはクローズされる。 \fBfcntl\fP(2)  の \fBFD_CLOEXEC\fP の説明を参照。
(ファイルディスクリプターがクローズされると、このプロセスが ファイルディスクリプターに対応するファイルに対して獲得していた
レコードのロックが全て解放されることになる。)  POSIX.1 では、 ファイルディスクリプター 0, 1, 2 が \fBexecve\fP()
成功後にどこかでクローズされ、かつ 実行されるファイルに set\-user_ID か set\-group_ID のモードビットが
セットされていてプロセスが特権を獲得した場合、 システムは何らかのファイルをオープンする際に これらの番号のディスクリプターのどれかを使うことがある、
とされている。 原則として、移植性が必要なプログラムでは、 特権の有無に関わらず、 \fBexecve\fP()  の前後でこれら
3つのファイルディスクリプターがクローズされたままで あることを前提にすることはできない。
.SS インタープリタースクリプト
インタープリタースクリプトとは、実行許可が有効になっていて、 最初の行が以下の形になっているテキストファイルのことである。
.PP
.in +4n
.EX
\fB#!\fP\fIinterpreter \fP[optional\-arg]
.EE
.in
.PP
The \fIinterpreter\fP must be a valid pathname for an executable file.
.PP
\fBexecve\fP()  の \fIpathname\fP 引数がインタープリタースクリプトを指定している場合、 \fIinterpreter\fP
は以下の引数で起動される。
.PP
.in +4n
.EX
\fIinterpreter\fP [optional\-arg] \fIpathname\fP arg...
.EE
.in
.PP
.\" See the P - preserve-argv[0] option.
.\" Documentation/admin-guide/binfmt-misc.rst
.\" https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
where \fIpathname\fP is the absolute pathname of the file specified as the
first argument of \fBexecve\fP(), and \fIarg...\fP is the series of words pointed
to by the \fIargv\fP argument of \fBexecve\fP(), starting at \fIargv[1]\fP.  Note
that there is no way to get the \fIargv[0]\fP that was passed to the
\fBexecve\fP()  call.
.PP
移植性を持たすには、 \fIoptional\-arg\fP は空か 1ワードだけにすべきである (つまり、ホワイトスペースを含めるべきではない)。
下記の「注意」の節を参照。
.PP
.\" commit bf2a9a39639b8b51377905397a5005f444e9a892
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.
.SS 引数と環境変数の合計サイズの上限
ほとんどの UNIX の実装は、新しいプログラムに渡すことができる コマンドライン引数 (\fIargv\fP)  と環境変数 (\fIenvp\fP)
の文字列群の合計サイズに何らかの上限を設けている。 POSIX.1 は、 \fBARG_MAX\fP 定数を使ってこの上限を決める実装を認めている
(\fBARG_MAX\fP は \fI<limits.h>\fP で定義されるか、実行時に \fIsysconf(_SC_ARG_MAX)\fP
の呼び出しで入手できるかのいずれかである)。
.PP
カーネル 2.6.23 より前の Linux では、環境変数と引数の文字列群を 格納するのに使用されるメモリーは 32 ページに制限されていた (32
ページというのはカーネル定数 \fBMAX_ARG_PAGES\fP で定義される)。したがって、 ページサイズが 4\ kB のアーキテクチャーでは、
最大サイズは 128\ kB ということになる。
.PP
.\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
.\" 2.6.25, see:
.\"     http://sourceware.org/bugzilla/show_bug.cgi?id=5786
.\"     http://bugzilla.kernel.org/show_bug.cgi?id=10095
.\"     http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
.\"     checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
.\" Ollie: That doesn't include the lists of pointers, though,
.\" so the actual usage is a bit higher (1 pointer per argument).
On kernel 2.6.23 and later, most architectures support a size limit derived
from the soft \fBRLIMIT_STACK\fP resource limit (see \fBgetrlimit\fP(2))  that is
in force at the time of the \fBexecve\fP()  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
\fB_STK_LIM\fP (8 Mibibytes).  Since Linux 2.6.25, the kernel also places a
floor of 32 pages on this size limit, so that, even when \fBRLIMIT_STACK\fP 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 \fBMAX_ARG_STRLEN\fP), and
the maximum number of strings is 0x7FFFFFFF.
.SH 返り値
成功すると \fBexecve\fP()  は返らない。エラーの場合は \-1 を返し、 \fIerrno\fP を適切に設定する。
.SH エラー
.TP 
\fBE2BIG\fP
環境変数 (\fIenvp\fP)  と引数リスト (\fIargv\fP)  の合計バイト数が大き過ぎる。
.TP 
\fBEACCES\fP
\fIpathname\fP やスクリプトインタープリター名の構成要素に検索許可 (search permission)  が与えられていない
(\fBpath_resolution\fP(7)  も参照すること)。
.TP 
\fBEACCES\fP
ファイルもしくはスクリプトのインタープリターが通常ファイル (regular file)  でない。
.TP 
\fBEACCES\fP
ファイルやスクリプトや ELF インタープリターに 実行許可 (execute permission) が与えられていない。
.TP 
\fBEACCES\fP
ファイルシステムが \fInoexec\fP でマウントされている。
.TP 
\fBEAGAIN\fP (Linux 3.1 以降)
.\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
\fBset*uid\fP() のいずれかの呼び出しでプロセスの実 UID が変更されたとすると、呼び出し元の \fBRLIMIT_NPROC\fP リソース上限
(\fBsetrlimit\fP(2) 参照) を超えてしまう、 現在もまだ超えている。 このエラーの詳細な説明については「注意」の節を参照。
.TP 
\fBEFAULT\fP
\fIpathname\fP または配列 \fIargv\fP か \fIenvp\fP のポインターの一つがアクセス可能なアドレス空間の外を指している。
.TP 
\fBEINVAL\fP
ELF 実行形式で複数の PT_INTERP セグメントが存在する。 (すなわち複数のインタープリターを指定した。)
.TP 
\fBEIO\fP
I/O エラーが発生した。
.TP 
\fBEISDIR\fP
ELF インタープリターがディレクトリだった。
.TP 
\fBELIBBAD\fP
ELF インタープリターが理解できるフォーマットでなかった。
.TP 
\fBELOOP\fP
\fIpathname\fP やスクリプトや ELF のインタープリターを解決する際に遭遇した シンボリックリンクが多過ぎる。
.TP 
\fBELOOP\fP
.\" commit d740269867021faf4ce38a449353d2b986c34a67
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 \fBENOEXEC\fP.
.TP 
\fBEMFILE\fP
The per\-process limit on the number of open file descriptors has been
reached.
.TP 
\fBENAMETOOLONG\fP
\fIpathname\fP が長過ぎる。
.TP 
\fBENFILE\fP
オープンされたファイルの総数がシステム全体の上限に達していた。
.TP 
\fBENOENT\fP
The file \fIpathname\fP or a script or ELF interpreter does not exist.
.TP 
\fBENOEXEC\fP
実行ファイルが理解できない形式であるか、違うアーキテクチャーのものか、 その他のフォーマットエラーにより実行ができなかった。
.TP 
\fBENOMEM\fP
カーネルに十分なメモリーがない。
.TP 
\fBENOTDIR\fP
\fIpathname\fP やスクリプトや ELF のインタープリターの構成要素がディレクトリでない。
.TP 
\fBEPERM\fP
ファイルシステムが \fInosuid\fP でマウントされ、ユーザーがスーパーユーザーでなく、 ファイルに set\-user\-ID あるいは
set\-group\-ID ビットが設定されている。
.TP 
\fBEPERM\fP
プロセスがトレースされ、ユーザーがスーパーユーザーでなく、 ファイルに set\-user\-ID あるいは set\-group\-ID
ビットが設定されている。
.TP 
\fBEPERM\fP
A "capability\-dumb" applications would not obtain the full set of permitted
capabilities granted by the executable file.  See \fBcapabilities\fP(7).
.TP 
\fBETXTBSY\fP
指定された実行ファイルを書き込み用にオープンしているプロセスがある。
.SH 準拠
.\" SVr4 documents additional error
.\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
.\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
.\" EISDIR or ELIBBAD error conditions.
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.
.SH 注意
One sometimes sees \fBexecve\fP()  (and the related functions described in
\fBexec\fP(3))  described as "executing a \fInew\fP 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
\fBexecve\fP()  does is arrange for an existing process (the calling process)
to execute a new program.
.PP
set\-user\-id プロセスと set\-group\-ID プロセスは \fBptrace\fP(2)  できない。
.PP
ファイルシステムを \fInosuid\fP でマウントした場合に set\-user\-ID/set\-group\-ID の実行ファイルを
どの様に扱うかは、Linux カーネルのバージョンによって異なる: あるバージョンでは、すでに必要な権限を持っている場合を除いて、 その実行を拒否する
(そして \fBEPERM\fP を返す)。別のあるバージョンでは set\-user\-ID/set\-group\-ID ビットのみを無視し \fBexec\fP()
は成功する。
.PP
.\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
.\" HP-UX 11 is like Linux -- mtk, Apr 2007
.\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
.\" Bug rejected (because fix would constitute an ABI change).
.\"
On Linux, \fIargv\fP and \fIenvp\fP 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.  \fBDo not take advantage of this
nonstandard and nonportable misfeature!\fP On many other UNIX systems,
specifying \fIargv\fP as NULL will result in an error (\fBEFAULT\fP).  \fISome\fP
other UNIX systems treat the \fIenvp==NULL\fP case the same as Linux.
.PP
POSIX.1 は、 \fBsysconf\fP(3)  が返す値はプロセスの生存中は変化しないべきだとしている。 しかしながら、Linux 2.6.23
以降では、リソース上限 \fBRLIMIT_STACK\fP が変化した場合、 コマンドライン引数と環境変数を保持するための空間に対する上限が
変化したことを反映して、 \fB_SC_ARG_MAX\fP が返す値も変化する。
.PP
.\" commit 19d860a140beac48a1377f179e693abe86a9dac9
.\"
\fBexecve\fP() が失敗するほとんどの場合、 制御は元の実行可能イメージに戻り、 \fBexecve\fP()
の呼び出し元がエラーを処理することができる。 しかしながら、 (リソース枯渇が原因となった場合など、まれに)
呼び出し元に制御が戻る時点を過ぎてからエラーが発生する場合がある。 元の実行可能イメージはすでに破棄されているが、
新しいイメージが完全には構築されていないという状況である。 このような場合、カーネルはそのプロセスをシグナル \fBSIGSEGV\fP (Linux
3.17 までは \fBSIGKILL\fP) で停止 (kill) する。
.SS インタープリタースクリプト
.\" commit 6eb3c3d0a52dca337e327ae8868ca1f44a712e02
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.
.PP
.\" e.g., Solaris 8
.\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
インタープリタースクリプトの \fIoptional\-arg\fP 引数の解釈方法は実装により異なる。 Linux では、インタープリター名
\fIinterpreter\fP に続く文字列全体がインタープリターに 1個の引数として渡される。 しかし、動作が異なるシステムもある。 あるシステムでは、
\fIoptional\-arg\fP のうち最初のホワイトスペースまでが 引数として渡される。 また、別のシステムでは
インタープリタースクリプトは複数の引数を持つことができ、 \fIoptional\-arg\fP 内のホワイトスペースが引数の区切りとなる。
.PP
.\"
.\" .SH BUGS
.\" Some Linux versions have failed to check permissions on ELF
.\" interpreters.  This is a security hole, because it allows users to
.\" open any file, such as a rewinding tape device, for reading.  Some
.\" Linux versions have also had other security holes in
.\" .BR execve ()
.\" that could be exploited for denial of service by a suitably crafted
.\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
Linux (like most other modern UNIX systems)  ignores the set\-user\-ID and
set\-group\-ID bits on scripts.
.SS "execve() と EAGAIN"
\fBexecve\fP() を呼び出した際に (Linux 3.1 以降で) 起こり得る \fBEAGAIN\fP エラーの詳細な説明を以下で行う。
.PP
.\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
\fI直前の\fP \fBsetuid\fP(2), \fBsetreuid\fP(2), \fBsetresuid\fP(2) の呼び出しで、 そのプロセスの実ユーザー ID
が変更され、 その変更によりそのプロセスが \fBRLIMIT_NPROC\fP リソース上限を超過してしまった場合 (すなわち、新しい実ユーザー ID
に属するプロセス数が \fBRLIMIT_NPROC\fP リソース上限を超過した場合) に、 \fBEAGAIN\fP エラーが発生する。 Linux 2.6.0
以上 3.0 以下では、これにより \fBset*uid\fP() の呼び出しが失敗していた。 (Linux 2.6 より前では、このリソース上限はユーザー
ID を変更したプロセスには適用されていなかった。)
.PP
Linux 3.1 以降では、上で説明したシナリオでは \fBset*uid\fP() の呼び出しは失敗しない。 なぜなら、
返されたステータスの確認を行わず「呼び出し元が特権を持っている場合には」呼び出しは必ず成功するとみなしているバグがあるアプリケーションでは、セキュリティホールにつながることが非常によくあるからだ。
その代わり、 \fBset*uid\fP() の呼び出しによる実 UID の変更は成功するが、 カーネルは \fBPF_NPROC_EXCEEDED\fP
という名前の内部フラグをセットする。 このフラグは \fBRLIMIT_NPROC\fP リソース上限が超過したことを示す。
\fBPF_NPROC_EXCEEDED\fP フラグがセットされていて、その後で \fBexecve\fP() が呼ばれた際にリソース上限がまだ超過していれば、
その \fBexecve\fP() の呼び出しは \fBEAGAIN\fP エラーで失敗する。 このカーネルのロジックにより、
特権デーモンでよく行われる処理フロー、 すなわち \fBfork\fP(2)  + \fBset*uid\fP()  + \fBexecve\fP()
に対して、前と変わらず \fBRLIMIT_NPROC\fP リソース上限を適用できることが保証される。
.PP
(\fBset*uid\fP() と \fBexecve\fP() の呼び出しの間に、この実 UID に属する他のプロセスが終了して) 次に \fBexecve\fP()
が呼び出された際にこのリソース上限が超過してなければ、 \fBexecve\fP() の呼び出しは成功し、カーネルは \fBPF_NPROC_EXCEEDED\fP
プロセスフラグをクリアする。 同じプロセスによって \fBfork\fP(2) の呼び出しが後で行われた場合にも、このフラグはクリアされる。
.SS 歴史
.\"
.\" .SH BUGS
.\" Some Linux versions have failed to check permissions on ELF
.\" interpreters.  This is a security hole, because it allows users to
.\" open any file, such as a rewinding tape device, for reading.  Some
.\" Linux versions have also had other security holes in
.\" .BR execve ()
.\" that could be exploited for denial of service by a suitably crafted
.\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
UNIX\ V6 では \fBexec\fP()  コールの引数リストは 0 で終端され、 \fImain\fP の引数リストは \-1 で終端されていた。
そのため、 \fImain\fP の引数リストは、その後の \fBexec\fP()  コールには直接使用できなかった。 UNIX\ V7 以降では、ともに
NULL で終端される。
.SH 例
このプログラムは、以下の二つ目のプログラムから実行するためのものである。 コマンドライン引数を 1行に 1個ずつ表示するだけのプログラムである。
.PP
.in +4n
.EX
/* 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\en", j, argv[j]);

    exit(EXIT_SUCCESS);
}
.EE
.in
.PP
以下のプログラムは、コマンドライン引数で指定した名前のプログラムを 実行するのに使う。
.PP
.in +4n
.EX
/* 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>\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    newargv[0] = argv[1];

    execve(argv[1], newargv, newenviron);
    perror("execve");   /* execve() returns only on error */
    exit(EXIT_FAILURE);
}
.EE
.in
.PP
二つ目のプログラムを使って一つ目のプログラムを実行するには 以下のようにする。
.PP
.in +4n
.EX
$\fB cc myecho.c \-o myecho\fP
$\fB cc execve.c \-o execve\fP
$\fB ./execve ./myecho\fP
argv[0]: ./myecho
argv[1]: hello
argv[2]: world
.EE
.in
.PP
さらに、これらのプログラムを使って、スクリプトインタープリターの例を示す。 このために、「インタープリター」として先ほど作成したプログラム
\fImyecho\fP を使うスクリプトを作成する。
.PP
.in +4n
.EX
$\fB cat > script\fP
\fB#!./myecho script\-arg\fP
\fB\(haD\fP
$\fB chmod +x script\fP
.EE
.in
.PP
作成しておいたプログラムを使ってスクリプトを実行する。
.PP
.in +4n
.EX
$\fB ./execve ./script\fP
argv[0]: ./myecho
argv[1]: script\-arg
argv[2]: ./script
argv[3]: hello
argv[4]: world
.EE
.in
.SH 関連項目
\fBchmod\fP(2), \fBexecveat\fP(2), \fBfork\fP(2), \fBget_robust_list\fP(2),
\fBptrace\fP(2), \fBexec\fP(3), \fBfexecve\fP(3), \fBgetopt\fP(3), \fBsystem\fP(3),
\fBcapabilities\fP(7), \fBcredentials\fP(7), \fBenviron\fP(7), \fBpath_resolution\fP(7),
\fBld.so\fP(8)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
\%https://www.kernel.org/doc/man\-pages/ に書かれている。