1EXECVE(2)                  Linux Programmer's Manual                 EXECVE(2)
2
3
4

NAME

6       execve - execute program
7

SYNOPSIS

9       #include <unistd.h>
10
11       int execve(const char *filename, char *const argv[],
12                  char *const envp[]);
13

DESCRIPTION

15       execve() executes the program pointed to by filename.  filename must be
16       either a binary executable, or a script starting with  a  line  of  the
17       form:
18
19           #! interpreter [optional-arg]
20
21       For details of the latter case, see "Interpreter scripts" below.
22
23       argv  is  an  array  of argument strings passed to the new program.  By
24       convention, the first of these  strings  should  contain  the  filename
25       associated  with the file being executed.  envp is an array of strings,
26       conventionally of the form key=value, which are passed  as  environment
27       to  the  new  program.  Both argv and envp must be terminated by a NULL
28       pointer.  The argument vector and environment can be  accessed  by  the
29       called program's main function, when it is defined as:
30
31           int main(int argc, char *argv[], char *envp[])
32
33       execve() does not return on success, and the text, data, bss, and stack
34       of the calling process are overwritten by that of the program loaded.
35
36       If the current program is being ptraced, a SIGTRAP is sent to it  after
37       a successful execve().
38
39       If  the  set-user-ID bit is set on the program file pointed to by file‐
40       name, and the  underlying  file  system  is  not  mounted  nosuid  (the
41       MS_NOSUID  flag  for  mount(2)),  and  the calling process is not being
42       ptraced, then the effective user ID of the calling process  is  changed
43       to  that  of  the  owner of the program file.  Similarly, when the set-
44       group-ID bit of the program file is set the effective group ID  of  the
45       calling process is set to the group of the program file.
46
47       The  effective  user ID of the process is copied to the saved set-user-
48       ID; similarly, the effective group ID is copied to the saved set-group-
49       ID.  This copying takes place after any effective ID changes that occur
50       because of the set-user-ID and set-group-ID permission bits.
51
52       If the executable is an a.out dynamically linked binary executable con‐
53       taining  shared-library  stubs,  the  Linux  dynamic linker ld.so(8) is
54       called at the start of execution to bring needed shared libraries  into
55       memory and link the executable with them.
56
57       If  the  executable  is a dynamically linked ELF executable, the inter‐
58       preter named in the PT_INTERP segment is used to load the needed shared
59       libraries.   This interpreter is typically /lib/ld-linux.so.2 for bina‐
60       ries linked with glibc 2.  (For binaries  linked  with  the  old  Linux
61       libc5, the interpreter was typically /lib/ld-linux.so.1.)
62
63       All  process  attributes  are  preserved during an execve(), except the
64       following:
65
66       *  The dispositions of any signals that are being caught are  reset  to
67          the default (signal(7)).
68
69       *  Any alternate signal stack is not preserved (sigaltstack(2)).
70
71       *  Memory mappings are not preserved (mmap(2)).
72
73       *  Attached System V shared memory segments are detached (shmat(2)).
74
75       *  POSIX shared memory regions are unmapped (shm_open(3)).
76
77       *  Open POSIX message queue descriptors are closed (mq_overview(7)).
78
79       *  Any open POSIX named semaphores are closed (sem_overview(7)).
80
81       *  POSIX timers are not preserved (timer_create(2)).
82
83       *  Any open directory streams are closed (opendir(3)).
84
85       *  Memory locks are not preserved (mlock(2), mlockall(2)).
86
87       *  Exit handlers are not preserved (atexit(3), on_exit(3)).
88
89       *  The   floating-point  environment  is  reset  to  the  default  (see
90          fenv(3)).
91
92       The process attributes in the  preceding  list  are  all  specified  in
93       POSIX.1-2001.  The following Linux-specific process attributes are also
94       not preserved during an execve():
95
96       *  The prctl(2) PR_SET_DUMPABLE flag is set, unless  a  set-user-ID  or
97          set-group ID program is being executed, in which case it is cleared.
98
99       *  The prctl(2) PR_SET_KEEPCAPS flag is cleared.
100
101       *  (Since  Linux 2.4.36 / 2.6.23) If a set-user-ID or set-group-ID pro‐
102          gram is being executed, then the parent death signal set by prctl(2)
103          PR_SET_PDEATHSIG flag is cleared.
104
105       *  The  process  name, as set by prctl(2) PR_SET_NAME (and displayed by
106          ps -o comm), is reset to the name of the new executable file.
107
108       *  The SECBIT_KEEP_CAPS securebits  flag  is  cleared.   See  capabili‐
109          ties(7).
110
111       *  The termination signal is reset to SIGCHLD (see clone(2)).
112
113       Note the following further points:
114
115       *  All  threads  other  than the calling thread are destroyed during an
116          execve().  Mutexes, condition variables, and other pthreads  objects
117          are not preserved.
118
119       *  The  equivalent  of  setlocale(LC_ALL,  "C")  is executed at program
120          start-up.
121
122       *  POSIX.1-2001 specifies that the dispositions of any signals that are
123          ignored  or  set  to  the  default are left unchanged.  POSIX.1-2001
124          specifies one exception: if SIGCHLD is being ignored, then an imple‐
125          mentation  may  leave  the  disposition unchanged or reset it to the
126          default; Linux does the former.
127
128       *  Any   outstanding   asynchronous   I/O   operations   are   canceled
129          (aio_read(3), aio_write(3)).
130
131       *  For  the  handling  of  capabilities  during execve(), see capabili‐
132          ties(7).
133
134       *  By default, file descriptors remain open across an  execve().   File
135          descriptors  that  are  marked  close-on-exec  are  closed;  see the
136          description of FD_CLOEXEC in fcntl(2).  (If  a  file  descriptor  is
137          closed,  this will cause the release of all record locks obtained on
138          the underlying file by this process.   See  fcntl(2)  for  details.)
139          POSIX.1-2001  says that if file descriptors 0, 1, and 2 would other‐
140          wise be closed after a successful execve(), and  the  process  would
141          gain  privilege  because  the set-user_ID or set-group_ID permission
142          bit was set on the executed  file,  then  the  system  may  open  an
143          unspecified  file  for each of these file descriptors.  As a general
144          principle, no portable  program,  whether  privileged  or  not,  can
145          assume  that  these three file descriptors will remain closed across
146          an execve().
147
148   Interpreter scripts
149       An interpreter script is  a  text  file  that  has  execute  permission
150       enabled and whose first line is of the form:
151
152           #! interpreter [optional-arg]
153
154       The interpreter must be a valid pathname for an executable which is not
155       itself a script.  If the filename argument  of  execve()  specifies  an
156       interpreter script, then interpreter will be invoked with the following
157       arguments:
158
159           interpreter [optional-arg] filename arg...
160
161       where arg...  is the series of words pointed to by the argv argument of
162       execve().
163
164       For portable use, optional-arg should either be absent, or be specified
165       as a single word (i.e., it should not contain white space);  see  NOTES
166       below.
167
168   Limits on size of arguments and environment
169       Most  UNIX  implementations  impose some limit on the total size of the
170       command-line argument (argv) and environment (envp) strings that may be
171       passed to a new program.  POSIX.1 allows an implementation to advertise
172       this limit using the ARG_MAX constant (either defined in <limits.h>  or
173       available at run time using the call sysconf(_SC_ARG_MAX)).
174
175       On  Linux prior to kernel 2.6.23, the memory used to store the environ‐
176       ment and argument strings was limited to 32 pages (defined by the  ker‐
177       nel  constant  MAX_ARG_PAGES).  On architectures with a 4-kB page size,
178       this yields a maximum size of 128 kB.
179
180       On kernel 2.6.23 and later, most architectures  support  a  size  limit
181       derived  from  the  soft RLIMIT_STACK resource limit (see getrlimit(2))
182       that is in force at the time of the execve() call.  (Architectures with
183       no  memory  management  unit are excepted: they maintain the limit that
184       was in effect before kernel 2.6.23.)  This change  allows  programs  to
185       have  a much larger argument and/or environment list.  For these archi‐
186       tectures, the total size is limited to 1/4 of the allowed  stack  size.
187       (Imposing  the  1/4-limit  ensures that the new program always has some
188       stack space.)  Since Linux 2.6.25, the kernel  places  a  floor  of  32
189       pages  on  this size limit, so that, even when RLIMIT_STACK is set very
190       low, applications are guaranteed to have at least as much argument  and
191       environment  space  as was provided by Linux 2.6.23 and earlier.  (This
192       guarantee was not provided in Linux 2.6.23 and 2.6.24.)   Additionally,
193       the  limit per string is 32 pages (the kernel constant MAX_ARG_STRLEN),
194       and the maximum number of strings is 0x7FFFFFFF.
195

RETURN VALUE

197       On success, execve() does not return, on  error  -1  is  returned,  and
198       errno is set appropriately.
199

ERRORS

201       E2BIG  The total number of bytes in the environment (envp) and argument
202              list (argv) is too large.
203
204       EACCES Search permission is denied on a component of the path prefix of
205              filename  or  the  name  of  a  script  interpreter.   (See also
206              path_resolution(7).)
207
208       EACCES The file or a script interpreter is not a regular file.
209
210       EACCES Execute permission is denied for the file or  a  script  or  ELF
211              interpreter.
212
213       EACCES The file system is mounted noexec.
214
215       EFAULT filename points outside your accessible address space.
216
217       EINVAL An  ELF  executable  had  more than one PT_INTERP segment (i.e.,
218              tried to name more than one interpreter).
219
220       EIO    An I/O error occurred.
221
222       EISDIR An ELF interpreter was a directory.
223
224       ELIBBAD
225              An ELF interpreter was not in a recognized format.
226
227       ELOOP  Too many symbolic links were encountered in  resolving  filename
228              or the name of a script or ELF interpreter.
229
230       EMFILE The process has the maximum number of files open.
231
232       ENAMETOOLONG
233              filename is too long.
234
235       ENFILE The  system  limit  on  the  total number of open files has been
236              reached.
237
238       ENOENT The file filename or a script or ELF interpreter does not exist,
239              or  a  shared  library  needed for file or interpreter cannot be
240              found.
241
242       ENOEXEC
243              An executable is not in a recognized format, is  for  the  wrong
244              architecture,  or has some other format error that means it can‐
245              not be executed.
246
247       ENOMEM Insufficient kernel memory was available.
248
249       ENOTDIR
250              A component of the path prefix of filename or a  script  or  ELF
251              interpreter is not a directory.
252
253       EPERM  The  file  system  is  mounted nosuid, the user is not the supe‐
254              ruser, and the file has the set-user-ID or set-group-ID bit set.
255
256       EPERM  The process is being traced, the user is not the  superuser  and
257              the file has the set-user-ID or set-group-ID bit set.
258
259       ETXTBSY
260              Executable was open for writing by one or more processes.
261

CONFORMING TO

263       SVr4,  4.3BSD,  POSIX.1-2001.   POSIX.1-2001  does  not document the #!
264       behavior but is otherwise compatible.
265

NOTES

267       Set-user-ID and set-group-ID processes can not be ptrace(2)d.
268
269       Linux ignores the set-user-ID and set-group-ID bits on scripts.
270
271       The result of mounting a file system nosuid varies across Linux  kernel
272       versions:  some  will  refuse execution of set-user-ID and set-group-ID
273       executables when this would give the  user  powers  she  did  not  have
274       already  (and  return EPERM), some will just ignore the set-user-ID and
275       set-group-ID bits and exec() successfully.
276
277       A maximum line length of 127 characters is allowed for the  first  line
278       in a #! executable shell script.
279
280       The  semantics  of  the  optional-arg argument of an interpreter script
281       vary across implementations.  On Linux, the entire string following the
282       interpreter name is passed as a single argument to the interpreter, and
283       this string can include white space.  However, behavior differs on some
284       other  systems.   Some  systems  use the first white space to terminate
285       optional-arg.  On some systems, an interpreter script can have multiple
286       arguments,  and  white  spaces  in optional-arg are used to delimit the
287       arguments.
288
289       On Linux, either argv or envp can be specified as NULL, which  has  the
290       same  effect  as specifying these arguments as a pointer to a list con‐
291       taining a single NULL pointer.  Do not take advantage of  this  misfea‐
292       ture!   It  is  nonstandard and nonportable: on most other UNIX systems
293       doing this will result in an error (EFAULT).
294
295       POSIX.1-2001 says that values returned by sysconf(3) should be  invari‐
296       ant  over  the  lifetime of a process.  However, since Linux 2.6.23, if
297       the RLIMIT_STACK resource limit changes, then  the  value  reported  by
298       _SC_ARG_MAX  will  also  change,  to reflect the fact that the limit on
299       space for holding command-line arguments and environment variables  has
300       changed.
301
302   Historical
303       With  UNIX V6 the argument list of an exec() call was ended by 0, while
304       the argument list of main was ended by -1.  Thus,  this  argument  list
305       was  not  directly usable in a further exec() call.  Since UNIX V7 both
306       are NULL.
307

EXAMPLE

309       The following program is designed to be execed by  the  second  program
310       below.  It just echoes its command-line one per line.
311
312           /* myecho.c */
313
314           #include <stdio.h>
315           #include <stdlib.h>
316
317           int
318           main(int argc, char *argv[])
319           {
320               int j;
321
322               for (j = 0; j < argc; j++)
323                   printf("argv[%d]: %s\n", j, argv[j]);
324
325               exit(EXIT_SUCCESS);
326           }
327
328       This  program can be used to exec the program named in its command-line
329       argument:
330
331           /* execve.c */
332
333           #include <stdio.h>
334           #include <stdlib.h>
335           #include <unistd.h>
336
337           int
338           main(int argc, char *argv[])
339           {
340               char *newargv[] = { NULL, "hello", "world", NULL };
341               char *newenviron[] = { NULL };
342
343               if (argc != 2) {
344                fprintf(stderr, "Usage: %s <file-to-exec>\n", argv[0]);
345                exit(EXIT_FAILURE);
346               }
347
348               newargv[0] = argv[1];
349
350               execve(argv[1], newargv, newenviron);
351               perror("execve");   /* execve() only returns on error */
352               exit(EXIT_FAILURE);
353           }
354
355       We can use the second program to exec the first as follows:
356
357           $ cc myecho.c -o myecho
358           $ cc execve.c -o execve
359           $ ./execve ./myecho
360           argv[0]: ./myecho
361           argv[1]: hello
362           argv[2]: world
363
364       We can also use these programs to  demonstrate  the  use  of  a  script
365       interpreter.   To do this we create a script whose "interpreter" is our
366       myecho program:
367
368           $ cat > script.sh
369           #! ./myecho script-arg
370           ^D
371           $ chmod +x script.sh
372
373       We can then use our program to exec the script:
374
375           $ ./execve ./script.sh
376           argv[0]: ./myecho
377           argv[1]: script-arg
378           argv[2]: ./script.sh
379           argv[3]: hello
380           argv[4]: world
381

SEE ALSO

383       chmod(2), fork(2), ptrace(2), execl(3), fexecve(3), getopt(3),  creden‐
384       tials(7), environ(7), path_resolution(7), ld.so(8)
385

COLOPHON

387       This  page  is  part of release 3.53 of the Linux man-pages project.  A
388       description of the project, and information about reporting  bugs,  can
389       be found at http://www.kernel.org/doc/man-pages/.
390
391
392
393Linux                             2013-07-04                         EXECVE(2)
Impressum