OSDN Git Service

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