1EXEC(2) System Calls Manual EXEC(2)
2
3
4
6 execl, execv, execle, execve, execlp, execvp, exec, exece, environ -
7 execute a file
8
10 execl(name, arg0, arg1, ..., argn, 0)
11 char *name, *arg0, *arg1, ..., *argn;
12
13 execv(name, argv)
14 char *name, *argv[ ];
15
16 execle(name, arg0, arg1, ..., argn, 0, envp)
17 char *name, *arg0, *arg1, ..., *argn, *envp[ ];
18
19 execve(name, argv, envp);
20 char *name, *argv[ ], *envp[ ];
21
22 extern char **environ;
23
25 Exec in all its forms overlays the calling process with the named file,
26 then transfers to the entry point of the core image of the file. There
27 can be no return from a successful exec; the calling core image is
28 lost.
29
30 Files remain open across exec unless explicit arrangement has been
31 made; see ioctl(2). Ignored signals remain ignored across these calls,
32 but signals that are caught (see signal(2)) are reset to their default
33 values.
34
35 Each user has a real user ID and group ID and an effective user ID and
36 group ID. The real ID identifies the person using the system; the
37 effective ID determines his access privileges. Exec changes the effec‐
38 tive user and group ID to the owner of the executed file if the file
39 has the `set-user-ID' or `set-group-ID' modes. The real user ID is not
40 affected.
41
42 The name argument is a pointer to the name of the file to be executed.
43 The pointers arg[0], arg[1] ... address null-terminated strings. Con‐
44 ventionally arg[0] is the name of the file.
45
46 From C, two interfaces are available. Execl is useful when a known
47 file with known arguments is being called; the arguments to execl are
48 the character strings constituting the file and the arguments; the
49 first argument is conventionally the same as the file name (or its last
50 component). A 0 argument must end the argument list.
51
52 The execv version is useful when the number of arguments is unknown in
53 advance; the arguments to execv are the name of the file to be executed
54 and a vector of strings containing the arguments. The last argument
55 string must be followed by a 0 pointer.
56
57 When a C program is executed, it is called as follows:
58
59 main(argc, argv, envp)
60 int argc;
61 char **argv, **envp;
62
63 where argc is the argument count and argv is an array of character
64 pointers to the arguments themselves. As indicated, argc is conven‐
65 tionally at least one and the first member of the array points to a
66 string containing the name of the file.
67
68 Argv is directly usable in another execv because argv[argc] is 0.
69
70 Envp is a pointer to an array of strings that constitute the environ‐
71 ment of the process. Each string consists of a name, an ``='', and a
72 null-terminated value. The array of pointers is terminated by a null
73 pointer. The shell sh(1) passes an environment entry for each global
74 shell variable defined when the program is called. See environ(5) for
75 some conventionally used names. The C run-time start-off routine
76 places a copy of envp in the global cell environ, which is used by
77 execv and execl to pass the environment to any subprograms executed by
78 the current program. The exec routines use lower-level routines as
79 follows to pass an environment explicitly:
80 execle(file, arg0, arg1, . . . , argn, 0, environ);
81 execve(file, argv, environ);
82
83 Execlp and execvp are called with the same arguments as execl and
84 execv, but duplicate the shell's actions in searching for an executable
85 file in a list of directories. The directory list is obtained from the
86 environment.
87
89 /bin/sh shell, invoked if command file found by execlp or execvp
90
92 fork(2), environ(5)
93
95 If the file cannot be found, if it is not executable, if it does not
96 start with a valid magic number (see a.out(5)), if maximum memory is
97 exceeded, or if the arguments require too much space, a return consti‐
98 tutes the diagnostic; the return value is -1. Even for the super-user,
99 at least one of the execute-permission bits must be set for a file to
100 be executed.
101
103 If execvp is called to execute a file that turns out to be a shell com‐
104 mand file, and if it is impossible to execute the shell, the values of
105 argv[0] and argv[-1] will be modified before return.
106
108 (exec = 11.)
109 sys exec; name; argv
110
111 (exece = 59.)
112 sys exece; name; argv; envp
113
114 Plain exec is obsoleted by exece, but remains for historical reasons.
115
116 When the called file starts execution on the PDP11, the stack pointer
117 points to a word containing the number of arguments. Just above this
118 number is a list of pointers to the argument strings, followed by a
119 null pointer, followed by the pointers to the environment strings and
120 then another null pointer. The strings themselves follow; a 0 word is
121 left at the very top of memory.
122
123 sp→ nargs
124 arg0
125 ...
126 argn
127 0
128 env0
129 ...
130 envm
131 0
132
133 arg0: <arg0\0>
134 ...
135 env0: <env0\0>
136 0
137
138 On the Interdata 8/32, the stack begins at a conventional place (cur‐
139 rently 0xD0000) and grows upwards. After exec, the layout of data on
140 the stack is as follows.
141
142 int 0
143 arg0: byte ...
144 ...
145 argp0: int arg0
146 ...
147 int 0
148 envp0: int env0
149 ...
150 int 0
151 %2→ space 40
152 int nargs
153 int argp0
154 int envp0
155 %3→
156
157 This arrangement happens to conform well to C calling conventions.
158
159
160
161 EXEC(2)