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