OSDN Git Service

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