OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[linuxjm/LDP_man-pages.git] / original / man2 / execve.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
4 .\" and Copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Modified by Michael Haardt <michael@moria.de>
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>:
29 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 1999-11-12 by Urs Thuermann <urs@isnogud.escape.de>
31 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" 2006-09-04 Michael Kerrisk <mtk.manpages@gmail.com>
33 .\"     Added list of process attributes that are not preserved on exec().
34 .\" 2007-09-14 Ollie Wild <aaw@google.com>, mtk
35 .\"     Add text describing limits on command-line arguments + environment
36 .\"
37 .TH EXECVE 2 2011-09-14 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 execve \- execute program
40 .SH SYNOPSIS
41 .B #include <unistd.h>
42 .sp
43 .BI "int execve(const char *" filename ", char *const " argv "[], "
44 .br
45 .BI "           char *const " envp []);
46 .SH DESCRIPTION
47 .BR execve ()
48 executes the program pointed to by \fIfilename\fP.
49 \fIfilename\fP must be either a binary executable, or a script
50 starting with a line of the form:
51
52 .in +4n
53 .nf
54 \fB#!\fP \fIinterpreter \fP[optional-arg]
55 .fi
56 .in
57
58 For details of the latter case, see "Interpreter scripts" below.
59
60 \fIargv\fP is an array of argument strings passed to the new program.
61 By convention, the first of these strings should contain the filename
62 associated with the file being executed.
63 \fIenvp\fP is an array of strings, conventionally of the form
64 \fBkey=value\fP, which are passed as environment to the new program.
65 Both \fIargv\fP and \fIenvp\fP must be terminated by a null pointer.
66 The argument vector and environment can be accessed by the
67 called program's main function, when it is defined as:
68
69 .in +4n
70 .nf
71 int main(int argc, char *argv[], char *envp[])
72 .fi
73 .in
74
75 .BR execve ()
76 does not return on success, and the text, data, bss, and
77 stack of the calling process are overwritten by that of the program
78 loaded.
79
80 If the current program is being ptraced, a \fBSIGTRAP\fP is sent to it
81 after a successful
82 .BR execve ().
83
84 If the set-user-ID bit is set on the program file pointed to by
85 \fIfilename\fP,
86 and the underlying file system is not mounted
87 .I nosuid
88 (the
89 .B MS_NOSUID
90 flag for
91 .BR mount (2)),
92 and the calling process is not being ptraced,
93 then the effective user ID of the calling process is changed
94 to that of the owner of the program file.
95 Similarly, when the set-group-ID
96 bit of the program file is set the effective group ID of the calling
97 process is set to the group of the program file.
98
99 The effective user ID of the process is copied to the saved set-user-ID;
100 similarly, the effective group ID is copied to the saved set-group-ID.
101 This copying takes place after any effective ID changes that occur
102 because of the set-user-ID and set-group-ID permission bits.
103
104 If the executable is an a.out dynamically linked
105 binary executable containing
106 shared-library stubs, the Linux dynamic linker
107 .BR ld.so (8)
108 is called at the start of execution to bring
109 needed shared libraries into memory
110 and link the executable with them.
111
112 If the executable is a dynamically linked ELF executable, the
113 interpreter named in the PT_INTERP segment is used to load the needed
114 shared libraries.
115 This interpreter is typically
116 \fI/lib/ld-linux.so.1\fP for binaries linked with the
117 Linux libc 5, or \fI/lib/ld-linux.so.2\fP for binaries linked with the
118 glibc 2.
119
120 All process attributes are preserved during an
121 .BR execve (),
122 except the following:
123 .IP *
124 The dispositions of any signals that are being caught are
125 reset to the default
126 .RB ( signal (7)).
127 .IP *
128 Any alternate signal stack is not preserved
129 .RB ( sigaltstack (2)).
130 .IP *
131 Memory mappings are not preserved
132 .RB ( mmap (2)).
133 .IP *
134 Attached System V shared memory segments are detached
135 .RB ( shmat (2)).
136 .IP *
137 POSIX shared memory regions are unmapped
138 .RB ( shm_open (3)).
139 .IP *
140 Open POSIX message queue descriptors are closed
141 .RB ( mq_overview (7)).
142 .IP *
143 Any open POSIX named semaphores are closed
144 .RB ( sem_overview (7)).
145 .IP *
146 POSIX timers are not preserved
147 .RB ( timer_create (2)).
148 .IP *
149 Any open directory streams are closed
150 .RB ( opendir (3)).
151 .IP *
152 Memory locks are not preserved
153 .RB ( mlock (2),
154 .BR mlockall (2)).
155 .IP *
156 Exit handlers are not preserved
157 .RB ( atexit (3),
158 .BR on_exit (3)).
159 .IP *
160 The floating-point environment is reset to the default (see
161 .BR fenv (3)).
162 .PP
163 The process attributes in the preceding list are all specified
164 in POSIX.1-2001.
165 The following Linux-specific process attributes are also
166 not preserved during an
167 .BR execve ():
168 .IP * 3
169 The
170 .BR prctl (2)
171 .B PR_SET_DUMPABLE
172 flag is set,
173 unless a set-user-ID or set-group ID program is being executed,
174 in which case it is cleared.
175 .IP *
176 The
177 .BR prctl (2)
178 .B PR_SET_KEEPCAPS
179 flag is cleared.
180 .IP *
181 The process name, as set by
182 .BR prctl (2)
183 .B PR_SET_NAME
184 (and displayed by
185 .IR "ps\ \-o comm" ),
186 is reset to the name of the new executable file.
187 .IP *
188 The termination signal is reset to
189 .B SIGCHLD
190 (see
191 .BR clone (2)).
192 .PP
193 Note the following further points:
194 .IP * 3
195 All threads other than the calling thread are destroyed during an
196 .BR execve ().
197 Mutexes, condition variables, and other pthreads objects are not preserved.
198 .IP *
199 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
200 is executed at program start-up.
201 .IP *
202 POSIX.1-2001 specifies that the dispositions of any signals that
203 are ignored or set to the default are left unchanged.
204 POSIX.1-2001 specifies one exception: if
205 .B SIGCHLD
206 is being ignored,
207 then an implementation may leave the disposition unchanged or
208 reset it to the default; Linux does the former.
209 .IP *
210 Any outstanding asynchronous I/O operations are canceled
211 .RB ( aio_read (3),
212 .BR aio_write (3)).
213 .IP *
214 For the handling of capabilities during
215 .BR execve (),
216 see
217 .BR capabilities (7).
218 .IP *
219 By default, file descriptors remain open across an
220 .BR execve ().
221 File descriptors that are marked close-on-exec are closed;
222 see the description of
223 .B FD_CLOEXEC
224 in
225 .BR fcntl (2).
226 (If a file descriptor is closed, this will cause the release
227 of all record locks obtained on the underlying file by this process.
228 See
229 .BR fcntl (2)
230 for details.)
231 POSIX.1-2001 says that if file descriptors 0, 1, and 2 would
232 otherwise be closed after a successful
233 .BR execve (),
234 and the process would gain privilege because the set-user_ID or
235 set-group_ID permission bit was set on the executed file,
236 then the system may open an unspecified file for each of these
237 file descriptors.
238 As a general principle, no portable program, whether privileged or not,
239 can assume that these three file descriptors will remain
240 closed across an
241 .BR execve ().
242 .\" On Linux it appears that these file descriptors are
243 .\" always open after an execve(), and it looks like
244 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
245 .SS Interpreter scripts
246 An interpreter script is a text file that has execute
247 permission enabled and whose first line is of the form:
248
249 .in +4n
250 .nf
251 \fB#!\fP \fIinterpreter \fP[optional-arg]
252 .fi
253 .in
254
255 The
256 .I interpreter
257 must be a valid pathname for an
258 executable which is not itself a script.
259 If the
260 .I filename
261 argument of
262 .BR execve ()
263 specifies an interpreter script, then
264 .I interpreter
265 will be invoked with the following arguments:
266
267 .in +4n
268 .nf
269 \fIinterpreter\fP [optional-arg] \fIfilename\fP arg...
270 .fi
271 .in
272
273 where
274 .I arg...
275 is the series of words pointed to by the
276 .I argv
277 argument of
278 .BR execve ().
279
280 For portable use,
281 .I optional-arg
282 should either be absent, or be specified as a single word (i.e., it
283 should not contain white space); see NOTES below.
284 .SS "Limits on size of arguments and environment"
285 Most UNIX implementations impose some limit on the total size
286 of the command-line argument
287 .RI ( argv )
288 and environment
289 .RI ( envp )
290 strings that may be passed to a new program.
291 POSIX.1 allows an implementation to advertise this limit using the
292 .B ARG_MAX
293 constant (either defined in
294 .I <limits.h>
295 or available at run time using the call
296 .IR "sysconf(_SC_ARG_MAX)" ).
297
298 On Linux prior to kernel 2.6.23, the memory used to store the
299 environment and argument strings was limited to 32 pages
300 (defined by the kernel constant
301 .BR MAX_ARG_PAGES ).
302 On architectures with a 4-kB page size,
303 this yields a maximum size of 128 kB.
304
305 On kernel 2.6.23 and later, most architectures support a size limit
306 derived from the soft
307 .B RLIMIT_STACK
308 resource limit (see
309 .BR getrlimit (2))
310 that is in force at the time of the
311 .BR execve ()
312 call.
313 (Architectures with no memory management unit are excepted:
314 they maintain the limit that was in effect before kernel 2.6.23.)
315 This change allows programs to have a much larger
316 argument and/or environment list.
317 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
318 .\" 2.6.25, see:
319 .\"     http://sourceware.org/bugzilla/show_bug.cgi?id=5786
320 .\"     http://bugzilla.kernel.org/show_bug.cgi?id=10095
321 .\"     http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
322 .\"     checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
323 For these architectures, the total size is limited to 1/4 of the allowed
324 stack size.
325 (Imposing the 1/4-limit
326 ensures that the new program always has some stack space.)
327 .\" Ollie: That doesn't include the lists of pointers, though,
328 .\" so the actual usage is a bit higher (1 pointer per argument).
329 Since Linux 2.6.25,
330 the kernel places a floor of 32 pages on this size limit,
331 so that, even when
332 .BR RLIMIT_STACK
333 is set very low,
334 applications are guaranteed to have at least as much argument and
335 environment space as was provided by Linux 2.6.23 and earlier.
336 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
337 Additionally, the limit per string is 32 pages (the kernel constant
338 .BR MAX_ARG_STRLEN ),
339 and the maximum number of strings is 0x7FFFFFFF.
340 .SH "RETURN VALUE"
341 On success,
342 .BR execve ()
343 does not return, on error \-1 is returned, and
344 .I errno
345 is set appropriately.
346 .SH ERRORS
347 .TP
348 .B E2BIG
349 The total number of bytes in the environment
350 .RI ( envp )
351 and argument list
352 .RI ( argv )
353 is too large.
354 .TP
355 .B EACCES
356 Search permission is denied on a component of the path prefix of
357 .I filename
358 or the name of a script interpreter.
359 (See also
360 .BR path_resolution (7).)
361 .TP
362 .B EACCES
363 The file or a script interpreter is not a regular file.
364 .TP
365 .B EACCES
366 Execute permission is denied for the file or a script or ELF interpreter.
367 .TP
368 .B EACCES
369 The file system is mounted
370 .IR noexec .
371 .TP
372 .B EFAULT
373 .I filename
374 points outside your accessible address space.
375 .TP
376 .B EINVAL
377 An ELF executable had more than one PT_INTERP segment (i.e., tried to
378 name more than one interpreter).
379 .TP
380 .B EIO
381 An I/O error occurred.
382 .TP
383 .B EISDIR
384 An ELF interpreter was a directory.
385 .TP
386 .B ELIBBAD
387 An ELF interpreter was not in a recognized format.
388 .TP
389 .B ELOOP
390 Too many symbolic links were encountered in resolving
391 .I filename
392 or the name of a script or ELF interpreter.
393 .TP
394 .B EMFILE
395 The process has the maximum number of files open.
396 .TP
397 .B ENAMETOOLONG
398 .I filename
399 is too long.
400 .TP
401 .B ENFILE
402 The system limit on the total number of open files has been reached.
403 .TP
404 .B ENOENT
405 The file
406 .I filename
407 or a script or ELF interpreter does not exist, or a shared library
408 needed for file or interpreter cannot be found.
409 .TP
410 .B ENOEXEC
411 An executable is not in a recognized format, is for the wrong
412 architecture, or has some other format error that means it cannot be
413 executed.
414 .TP
415 .B ENOMEM
416 Insufficient kernel memory was available.
417 .TP
418 .B ENOTDIR
419 A component of the path prefix of
420 .I filename
421 or a script or ELF interpreter is not a directory.
422 .TP
423 .B EPERM
424 The file system is mounted
425 .IR nosuid ,
426 the user is not the superuser,
427 and the file has the set-user-ID or set-group-ID bit set.
428 .TP
429 .B EPERM
430 The process is being traced, the user is not the superuser and the
431 file has the set-user-ID or set-group-ID bit set.
432 .TP
433 .B ETXTBSY
434 Executable was open for writing by one or more processes.
435 .SH "CONFORMING TO"
436 SVr4, 4.3BSD, POSIX.1-2001.
437 POSIX.1-2001 does not document the #!  behavior
438 but is otherwise compatible.
439 .\" SVr4 documents additional error
440 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
441 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
442 .\" EISDIR or ELIBBAD error conditions.
443 .SH NOTES
444 Set-user-ID and set-group-ID processes can not be
445 .BR ptrace (2)d.
446
447 Linux ignores the set-user-ID and set-group-ID bits on scripts.
448
449 The result of mounting a file system
450 .I nosuid
451 varies across Linux kernel versions:
452 some will refuse execution of set-user-ID and set-group-ID
453 executables when this would
454 give the user powers she did not have already (and return
455 .BR EPERM ),
456 some will just ignore the set-user-ID and set-group-ID bits and
457 .BR exec ()
458 successfully.
459
460 A maximum line length of 127 characters is allowed for the first line in
461 a #! executable shell script.
462
463 The semantics of the
464 .I optional-arg
465 argument of an interpreter script vary across implementations.
466 On Linux, the entire string following the
467 .I interpreter
468 name is passed as a single argument to the interpreter,
469 and this string can include white space.
470 However, behavior differs on some other systems.
471 Some systems
472 .\" e.g., Solaris 8
473 use the first white space to terminate
474 .IR optional-arg .
475 On some systems,
476 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
477 an interpreter script can have multiple arguments,
478 and white spaces in
479 .I optional-arg
480 are used to delimit the arguments.
481
482 On Linux,
483 .I argv
484 can be specified as NULL,
485 which has the same effect as specifying this argument
486 as a pointer to a list containing a single NULL pointer.
487 .B "Do not take advantage of this misfeature!"
488 It is nonstandard and nonportable:
489 on most other UNIX systems doing this will result in an error
490 .RB ( EFAULT ).
491 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
492 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
493 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
494 .\" Bug rejected (because fix would constitute an ABI change).
495 .\"
496
497 POSIX.1-2001 says that values returned by
498 .BR sysconf (3)
499 should be invariant over the lifetime of a process.
500 However, since Linux 2.6.23, if the
501 .BR RLIMIT_STACK
502 resource limit changes, then the value reported by
503 .B _SC_ARG_MAX
504 will also change,
505 to reflect the fact that the limit on space for holding
506 command-line arguments and environment variables has changed.
507 .\"
508 .\" .SH BUGS
509 .\" Some Linux versions have failed to check permissions on ELF
510 .\" interpreters.  This is a security hole, because it allows users to
511 .\" open any file, such as a rewinding tape device, for reading.  Some
512 .\" Linux versions have also had other security holes in
513 .\" .BR execve ()
514 .\" that could be exploited for denial of service by a suitably crafted
515 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
516 .SS Historical
517 With UNIX V6 the argument list of an
518 .BR exec ()
519 call was ended by 0,
520 while the argument list of
521 .I main
522 was ended by \-1.
523 Thus, this argument list was not directly usable in a further
524 .BR exec ()
525 call.
526 Since UNIX V7 both are NULL.
527 .SH EXAMPLE
528 The following program is designed to be execed by the second program below.
529 It just echoes its command-line one per line.
530
531 .in +4n
532 .nf
533 /* myecho.c */
534
535 #include <stdio.h>
536 #include <stdlib.h>
537
538 int
539 main(int argc, char *argv[])
540 {
541     int j;
542
543     for (j = 0; j < argc; j++)
544         printf("argv[%d]: %s\\n", j, argv[j]);
545
546     exit(EXIT_SUCCESS);
547 }
548 .fi
549 .in
550
551 This program can be used to exec the program named in its command-line
552 argument:
553 .in +4n
554 .nf
555
556 /* execve.c */
557
558 #include <stdio.h>
559 #include <stdlib.h>
560 #include <unistd.h>
561
562 int
563 main(int argc, char *argv[])
564 {
565     char *newargv[] = { NULL, "hello", "world", NULL };
566     char *newenviron[] = { NULL };
567
568     if (argc != 2) {
569         fprintf(stderr, "Usage: %s <file-to-exec>\\n", argv[0]);
570         exit(EXIT_FAILURE);
571     }
572
573     newargv[0] = argv[1];
574
575     execve(argv[1], newargv, newenviron);
576     perror("execve");   /* execve() only returns on error */
577     exit(EXIT_FAILURE);
578 }
579 .fi
580 .in
581
582 We can use the second program to exec the first as follows:
583
584 .in +4n
585 .nf
586 .RB "$" " cc myecho.c \-o myecho"
587 .RB "$" " cc execve.c \-o execve"
588 .RB "$" " ./execve ./myecho"
589 argv[0]: ./myecho
590 argv[1]: hello
591 argv[2]: world
592 .fi
593 .in
594
595 We can also use these programs to demonstrate the use of a script
596 interpreter.
597 To do this we create a script whose "interpreter" is our
598 .I myecho
599 program:
600
601 .in +4n
602 .nf
603 .RB "$" " cat > script.sh"
604 .B #! ./myecho script-arg
605 .B ^D
606 .RB "$" " chmod +x script.sh"
607 .fi
608 .in
609
610 We can then use our program to exec the script:
611
612 .in +4n
613 .nf
614 .RB "$" " ./execve ./script.sh"
615 argv[0]: ./myecho
616 argv[1]: script-arg
617 argv[2]: ./script.sh
618 argv[3]: hello
619 argv[4]: world
620 .fi
621 .in
622 .SH "SEE ALSO"
623 .BR chmod (2),
624 .BR fork (2),
625 .BR ptrace (2),
626 .BR execl (3),
627 .BR fexecve (3),
628 .BR getopt (3),
629 .BR credentials (7),
630 .BR environ (7),
631 .BR path_resolution (7),
632 .BR ld.so (8)