]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/execve.2
execve.2: Rework text describing when effective IDs aren't transformed by execve()
[thirdparty/man-pages.git] / 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 2016-10-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 (i.e.,
62 .IR argv[0] )
63 should contain the filename associated with the file being executed.
64 \fIenvp\fP is an array of strings, conventionally of the form
65 \fBkey=value\fP, which are passed as environment to the new program.
66 The \fIargv\fP and \fIenvp\fP arrays must each include a null pointer
67 at the end of the array.
68
69 The argument vector and environment can be accessed by the
70 called program's main function, when it is defined as:
71
72 .in +4n
73 .nf
74 int main(int argc, char *argv[], char *envp[])
75 .fi
76 .in
77
78 Note, however, that the use of a third argument to the main function
79 is not specified in POSIX.1;
80 according to POSIX.1,
81 the environment should be accessed via the external variable
82 .BR environ (7).
83
84 .BR execve ()
85 does not return on success, and the text, initialized data,
86 uninitialized data (bss), and stack of the calling process are overwritten
87 according to the contents of the newly loaded program.
88
89 If the current program is being ptraced, a \fBSIGTRAP\fP signal is sent to it
90 after a successful
91 .BR execve ().
92
93 If the set-user-ID bit is set on the program file pointed to by
94 \fIfilename\fP,
95 then the effective user ID of the calling process is changed
96 to that of the owner of the program file.
97 Similarly, when the set-group-ID
98 bit of the program file is set the effective group ID of the calling
99 process is set to the group of the program file.
100 The aforementioned transformations of the effective IDs are
101 .I not
102 performed if any of the following is true:
103 .IP * 3
104 the underlying filesystem is mounted
105 .I nosuid
106 (the
107 .B MS_NOSUID
108 flag for
109 .BR mount (2));
110 or
111 .IP *
112 the calling process is being ptraced.
113 .PP
114 The effective user ID of the process is copied to the saved set-user-ID;
115 similarly, the effective group ID is copied to the saved set-group-ID.
116 This copying takes place after any effective ID changes that occur
117 because of the set-user-ID and set-group-ID mode bits.
118
119 The process's read UID and real GID, as well its supplementary group IDs,
120 are unchanged by a call to
121 .BR execve (2).
122
123 If the executable is an a.out dynamically linked
124 binary executable containing
125 shared-library stubs, the Linux dynamic linker
126 .BR ld.so (8)
127 is called at the start of execution to bring
128 needed shared objects into memory
129 and link the executable with them.
130
131 If the executable is a dynamically linked ELF executable, the
132 interpreter named in the PT_INTERP segment is used to load the needed
133 shared objects.
134 This interpreter is typically
135 .I /lib/ld-linux.so.2
136 for binaries linked with glibc (see
137 .BR ld-linux.so (8)).
138
139 All process attributes are preserved during an
140 .BR execve (),
141 except the following:
142 .IP * 3
143 The dispositions of any signals that are being caught are
144 reset to the default
145 .RB ( signal (7)).
146 .IP *
147 Any alternate signal stack is not preserved
148 .RB ( sigaltstack (2)).
149 .IP *
150 Memory mappings are not preserved
151 .RB ( mmap (2)).
152 .IP *
153 Attached System\ V shared memory segments are detached
154 .RB ( shmat (2)).
155 .IP *
156 POSIX shared memory regions are unmapped
157 .RB ( shm_open (3)).
158 .IP *
159 Open POSIX message queue descriptors are closed
160 .RB ( mq_overview (7)).
161 .IP *
162 Any open POSIX named semaphores are closed
163 .RB ( sem_overview (7)).
164 .IP *
165 POSIX timers are not preserved
166 .RB ( timer_create (2)).
167 .IP *
168 Any open directory streams are closed
169 .RB ( opendir (3)).
170 .IP *
171 Memory locks are not preserved
172 .RB ( mlock (2),
173 .BR mlockall (2)).
174 .IP *
175 Exit handlers are not preserved
176 .RB ( atexit (3),
177 .BR on_exit (3)).
178 .IP *
179 The floating-point environment is reset to the default (see
180 .BR fenv (3)).
181 .PP
182 The process attributes in the preceding list are all specified
183 in POSIX.1.
184 The following Linux-specific process attributes are also
185 not preserved during an
186 .BR execve ():
187 .IP * 3
188 The
189 .BR prctl (2)
190 .B PR_SET_DUMPABLE
191 flag is set,
192 unless a set-user-ID or set-group ID program is being executed,
193 in which case it is cleared.
194 .IP *
195 The
196 .BR prctl (2)
197 .B PR_SET_KEEPCAPS
198 flag is cleared.
199 .IP *
200 (Since Linux 2.4.36 / 2.6.23)
201 If a set-user-ID or set-group-ID program is being executed,
202 then the parent death signal set by
203 .BR prctl (2)
204 .B PR_SET_PDEATHSIG
205 flag is cleared.
206 .IP *
207 The process name, as set by
208 .BR prctl (2)
209 .B PR_SET_NAME
210 (and displayed by
211 .IR "ps\ \-o comm" ),
212 is reset to the name of the new executable file.
213 .IP *
214 The
215 .B SECBIT_KEEP_CAPS
216 .I securebits
217 flag is cleared.
218 See
219 .BR capabilities (7).
220 .IP *
221 The termination signal is reset to
222 .B SIGCHLD
223 (see
224 .BR clone (2)).
225 .IP *
226 The file descriptor table is unshared, undoing the effect of the
227 .B CLONE_FILES
228 flag of
229 .BR clone (2).
230 .PP
231 Note the following further points:
232 .IP * 3
233 All threads other than the calling thread are destroyed during an
234 .BR execve ().
235 Mutexes, condition variables, and other pthreads objects are not preserved.
236 .IP *
237 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
238 is executed at program start-up.
239 .IP *
240 POSIX.1 specifies that the dispositions of any signals that
241 are ignored or set to the default are left unchanged.
242 POSIX.1 specifies one exception: if
243 .B SIGCHLD
244 is being ignored,
245 then an implementation may leave the disposition unchanged or
246 reset it to the default; Linux does the former.
247 .IP *
248 Any outstanding asynchronous I/O operations are canceled
249 .RB ( aio_read (3),
250 .BR aio_write (3)).
251 .IP *
252 For the handling of capabilities during
253 .BR execve (),
254 see
255 .BR capabilities (7).
256 .IP *
257 By default, file descriptors remain open across an
258 .BR execve ().
259 File descriptors that are marked close-on-exec are closed;
260 see the description of
261 .B FD_CLOEXEC
262 in
263 .BR fcntl (2).
264 (If a file descriptor is closed, this will cause the release
265 of all record locks obtained on the underlying file by this process.
266 See
267 .BR fcntl (2)
268 for details.)
269 POSIX.1 says that if file descriptors 0, 1, and 2 would
270 otherwise be closed after a successful
271 .BR execve (),
272 and the process would gain privilege because the set-user-ID or
273 set-group_ID mode bit was set on the executed file,
274 then the system may open an unspecified file for each of these
275 file descriptors.
276 As a general principle, no portable program, whether privileged or not,
277 can assume that these three file descriptors will remain
278 closed across an
279 .BR execve ().
280 .\" On Linux it appears that these file descriptors are
281 .\" always open after an execve(), and it looks like
282 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
283 .SS Interpreter scripts
284 An interpreter script is a text file that has execute
285 permission enabled and whose first line is of the form:
286
287 .in +4n
288 .nf
289 \fB#!\fP \fIinterpreter \fP[optional-arg]
290 .fi
291 .in
292
293 The
294 .I interpreter
295 must be a valid pathname for an executable file.
296 If the
297 .I filename
298 argument of
299 .BR execve ()
300 specifies an interpreter script, then
301 .I interpreter
302 will be invoked with the following arguments:
303
304 .in +4n
305 .nf
306 \fIinterpreter\fP [optional-arg] \fIfilename\fP arg...
307 .fi
308 .in
309
310 where
311 .I arg...
312 is the series of words pointed to by the
313 .I argv
314 argument of
315 .BR execve (),
316 starting at
317 .IR argv [1].
318
319 For portable use,
320 .I optional-arg
321 should either be absent, or be specified as a single word (i.e., it
322 should not contain white space); see NOTES below.
323
324 Since Linux 2.6.28,
325 .\" commit bf2a9a39639b8b51377905397a5005f444e9a892
326 the kernel permits the interpreter of a script to itself be a script.
327 This permission is recursive, up to a limit of four recursions,
328 so that the interpreter may be a script which is interpreted by a script,
329 and so on.
330 .SS Limits on size of arguments and environment
331 Most UNIX implementations impose some limit on the total size
332 of the command-line argument
333 .RI ( argv )
334 and environment
335 .RI ( envp )
336 strings that may be passed to a new program.
337 POSIX.1 allows an implementation to advertise this limit using the
338 .B ARG_MAX
339 constant (either defined in
340 .I <limits.h>
341 or available at run time using the call
342 .IR "sysconf(_SC_ARG_MAX)" ).
343
344 On Linux prior to kernel 2.6.23, the memory used to store the
345 environment and argument strings was limited to 32 pages
346 (defined by the kernel constant
347 .BR MAX_ARG_PAGES ).
348 On architectures with a 4-kB page size,
349 this yields a maximum size of 128 kB.
350
351 On kernel 2.6.23 and later, most architectures support a size limit
352 derived from the soft
353 .B RLIMIT_STACK
354 resource limit (see
355 .BR getrlimit (2))
356 that is in force at the time of the
357 .BR execve ()
358 call.
359 (Architectures with no memory management unit are excepted:
360 they maintain the limit that was in effect before kernel 2.6.23.)
361 This change allows programs to have a much larger
362 argument and/or environment list.
363 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
364 .\" 2.6.25, see:
365 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
366 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
367 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
368 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
369 For these architectures, the total size is limited to 1/4 of the allowed
370 stack size.
371 (Imposing the 1/4-limit
372 ensures that the new program always has some stack space.)
373 .\" Ollie: That doesn't include the lists of pointers, though,
374 .\" so the actual usage is a bit higher (1 pointer per argument).
375 Since Linux 2.6.25,
376 the kernel places a floor of 32 pages on this size limit,
377 so that, even when
378 .BR RLIMIT_STACK
379 is set very low,
380 applications are guaranteed to have at least as much argument and
381 environment space as was provided by Linux 2.6.23 and earlier.
382 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
383 Additionally, the limit per string is 32 pages (the kernel constant
384 .BR MAX_ARG_STRLEN ),
385 and the maximum number of strings is 0x7FFFFFFF.
386 .SH RETURN VALUE
387 On success,
388 .BR execve ()
389 does not return, on error \-1 is returned, and
390 .I errno
391 is set appropriately.
392 .SH ERRORS
393 .TP
394 .B E2BIG
395 The total number of bytes in the environment
396 .RI ( envp )
397 and argument list
398 .RI ( argv )
399 is too large.
400 .TP
401 .B EACCES
402 Search permission is denied on a component of the path prefix of
403 .I filename
404 or the name of a script interpreter.
405 (See also
406 .BR path_resolution (7).)
407 .TP
408 .B EACCES
409 The file or a script interpreter is not a regular file.
410 .TP
411 .B EACCES
412 Execute permission is denied for the file or a script or ELF interpreter.
413 .TP
414 .B EACCES
415 The filesystem is mounted
416 .IR noexec .
417 .TP
418 .BR EAGAIN " (since Linux 3.1)"
419 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
420 Having changed its real UID using one of the
421 .BR set*uid ()
422 calls, the caller was\(emand is now still\(emabove its
423 .BR RLIMIT_NPROC
424 resource limit (see
425 .BR setrlimit (2)).
426 For a more detailed explanation of this error, see NOTES.
427 .TP
428 .B EFAULT
429 .I filename
430 or one of the pointers in the vectors
431 .I argv
432 or
433 .I envp
434 points outside your accessible address space.
435 .TP
436 .B EINVAL
437 An ELF executable had more than one PT_INTERP segment (i.e., tried to
438 name more than one interpreter).
439 .TP
440 .B EIO
441 An I/O error occurred.
442 .TP
443 .B EISDIR
444 An ELF interpreter was a directory.
445 .TP
446 .B ELIBBAD
447 An ELF interpreter was not in a recognized format.
448 .TP
449 .B ELOOP
450 Too many symbolic links were encountered in resolving
451 .I filename
452 or the name of a script or ELF interpreter.
453 .TP
454 .B ELOOP
455 The maximum recursion limit was reached during recursive script
456 interpretation (see "Interpreter scripts", above).
457 Before Linux 3.8,
458 .\" commit d740269867021faf4ce38a449353d2b986c34a67
459 the error produced for this case was
460 .BR ENOEXEC .
461 .TP
462 .B EMFILE
463 The per-process limit on the number of open file descriptors has been reached.
464 .TP
465 .B ENAMETOOLONG
466 .I filename
467 is too long.
468 .TP
469 .B ENFILE
470 The system-wide limit on the total number of open files has been reached.
471 .TP
472 .B ENOENT
473 The file
474 .I filename
475 or a script or ELF interpreter does not exist, or a shared library
476 .\" FIXME but see http://sourceware.org/bugzilla/show_bug.cgi?id=12241
477 needed for the file or interpreter cannot be found.
478 .TP
479 .B ENOEXEC
480 An executable is not in a recognized format, is for the wrong
481 architecture, or has some other format error that means it cannot be
482 executed.
483 .TP
484 .B ENOMEM
485 Insufficient kernel memory was available.
486 .TP
487 .B ENOTDIR
488 A component of the path prefix of
489 .I filename
490 or a script or ELF interpreter is not a directory.
491 .TP
492 .B EPERM
493 The filesystem is mounted
494 .IR nosuid ,
495 the user is not the superuser,
496 and the file has the set-user-ID or set-group-ID bit set.
497 .TP
498 .B EPERM
499 The process is being traced, the user is not the superuser and the
500 file has the set-user-ID or set-group-ID bit set.
501 .TP
502 .B EPERM
503 A "capability-dumb" applications would not obtain the full set of
504 permitted capabilities granted by the executable file.
505 See
506 .BR capabilities (7).
507 .TP
508 .B ETXTBSY
509 The specified executable was open for writing by one or more processes.
510 .SH CONFORMING TO
511 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
512 POSIX does not document the #! behavior, but it exists
513 (with some variations) on other UNIX systems.
514 .\" SVr4 documents additional error
515 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
516 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
517 .\" EISDIR or ELIBBAD error conditions.
518 .SH NOTES
519 Set-user-ID and set-group-ID processes can not be
520 .BR ptrace (2)d.
521
522 The result of mounting a filesystem
523 .I nosuid
524 varies across Linux kernel versions:
525 some will refuse execution of set-user-ID and set-group-ID
526 executables when this would
527 give the user powers she did not have already (and return
528 .BR EPERM ),
529 some will just ignore the set-user-ID and set-group-ID bits and
530 .BR exec ()
531 successfully.
532
533 On Linux,
534 .I argv
535 and
536 .I envp
537 can be specified as NULL.
538 In both cases, this has the same effect as specifying the argument
539 as a pointer to a list containing a single null pointer.
540 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
541 On many other UNIX systems, specifying
542 .I argv
543 as NULL will result in an error
544 .RB ( EFAULT ).
545 .I Some
546 other UNIX systems treat the
547 .I envp==NULL
548 case the same as Linux.
549 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
550 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
551 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
552 .\" Bug rejected (because fix would constitute an ABI change).
553 .\"
554
555 POSIX.1 says that values returned by
556 .BR sysconf (3)
557 should be invariant over the lifetime of a process.
558 However, since Linux 2.6.23, if the
559 .BR RLIMIT_STACK
560 resource limit changes, then the value reported by
561 .B _SC_ARG_MAX
562 will also change,
563 to reflect the fact that the limit on space for holding
564 command-line arguments and environment variables has changed.
565
566 In most cases where
567 .BR execve ()
568 fails, control returns to the original executable image,
569 and the caller of
570 .BR execve ()
571 can then handle the error.
572 However, in (rare) cases (typically caused by resource exhaustion),
573 failure may occur past the point of no return:
574 the original executable image has been torn down,
575 but the new image could not be completely built.
576 In such cases, the kernel kills the process with a
577 .BR SIGKILL
578 signal.
579 .\"
580 .SS Interpreter scripts
581 A maximum line length of 127 characters is allowed for the first line in
582 an interpreter scripts.
583
584 The semantics of the
585 .I optional-arg
586 argument of an interpreter script vary across implementations.
587 On Linux, the entire string following the
588 .I interpreter
589 name is passed as a single argument to the interpreter,
590 and this string can include white space.
591 However, behavior differs on some other systems.
592 Some systems
593 .\" e.g., Solaris 8
594 use the first white space to terminate
595 .IR optional-arg .
596 On some systems,
597 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
598 an interpreter script can have multiple arguments,
599 and white spaces in
600 .I optional-arg
601 are used to delimit the arguments.
602
603 Linux ignores the set-user-ID and set-group-ID bits on scripts.
604 .\"
605 .\" .SH BUGS
606 .\" Some Linux versions have failed to check permissions on ELF
607 .\" interpreters. This is a security hole, because it allows users to
608 .\" open any file, such as a rewinding tape device, for reading. Some
609 .\" Linux versions have also had other security holes in
610 .\" .BR execve ()
611 .\" that could be exploited for denial of service by a suitably crafted
612 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
613 .SS execve() and EAGAIN
614 A more detailed explanation of the
615 .BR EAGAIN
616 error that can occur (since Linux 3.1) when calling
617 .BR execve ()
618 is as follows.
619
620 The
621 .BR EAGAIN
622 error can occur when a
623 .I preceding
624 call to
625 .BR setuid (2),
626 .BR setreuid (2),
627 or
628 .BR setresuid (2)
629 caused the real user ID of the process to change,
630 and that change caused the process to exceed its
631 .BR RLIMIT_NPROC
632 resource limit (i.e., the number of processes belonging
633 to the new real UID exceeds the resource limit).
634 From Linux 2.6.0 to 3.0, this caused the
635 .BR set*uid ()
636 call to fail.
637 (Prior to 2.6,
638 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
639 the resource limit was not imposed on processes that
640 changed their user IDs.)
641
642 Since Linux 3.1, the scenario just described no longer causes the
643 .BR set*uid ()
644 call to fail,
645 because it too often led to security holes where buggy applications
646 didn't check the return status and assumed
647 that\(emif the caller had root privileges\(emthe call would always succeed.
648 Instead, the
649 .BR set*uid ()
650 calls now successfully change the real UID,
651 but the kernel sets an internal flag, named
652 .BR PF_NPROC_EXCEEDED ,
653 to note that the
654 .BR RLIMIT_NPROC
655 resource limit has been exceeded.
656 If the
657 .BR PF_NPROC_EXCEEDED
658 flag is set and the resource limit is still
659 exceeded at the time of a subsequent
660 .BR execve ()
661 call, that call fails with the error
662 .BR EAGAIN .
663 This kernel logic ensures that the
664 .BR RLIMIT_NPROC
665 resource limit is still enforced for the
666 common privileged daemon workflow\(emnamely,
667 .BR fork (2)
668 +
669 .BR set*uid ()
670 +
671 .BR execve ().
672
673 If the resource limit was not still exceeded at the time of the
674 .BR execve ()
675 call
676 (because other processes belonging to this real UID terminated between the
677 .BR set*uid ()
678 call and the
679 .BR execve ()
680 call), then the
681 .BR execve ()
682 call succeeds and the kernel clears the
683 .BR PF_NPROC_EXCEEDED
684 process flag.
685 The flag is also cleared if a subsequent call to
686 .BR fork (2)
687 by this process succeeds.
688 .SS Historical
689 With UNIX\ V6, the argument list of an
690 .BR exec ()
691 call was ended by 0,
692 while the argument list of
693 .I main
694 was ended by \-1.
695 Thus, this argument list was not directly usable in a further
696 .BR exec ()
697 call.
698 Since UNIX\ V7, both are NULL.
699 .\"
700 .\" .SH BUGS
701 .\" Some Linux versions have failed to check permissions on ELF
702 .\" interpreters. This is a security hole, because it allows users to
703 .\" open any file, such as a rewinding tape device, for reading. Some
704 .\" Linux versions have also had other security holes in
705 .\" .BR execve ()
706 .\" that could be exploited for denial of service by a suitably crafted
707 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
708 .SH EXAMPLE
709 The following program is designed to be execed by the second program below.
710 It just echoes its command-line arguments, one per line.
711
712 .in +4n
713 .nf
714 /* myecho.c */
715
716 #include <stdio.h>
717 #include <stdlib.h>
718
719 int
720 main(int argc, char *argv[])
721 {
722 int j;
723
724 for (j = 0; j < argc; j++)
725 printf("argv[%d]: %s\\n", j, argv[j]);
726
727 exit(EXIT_SUCCESS);
728 }
729 .fi
730 .in
731
732 This program can be used to exec the program named in its command-line
733 argument:
734 .in +4n
735 .nf
736
737 /* execve.c */
738
739 #include <stdio.h>
740 #include <stdlib.h>
741 #include <unistd.h>
742
743 int
744 main(int argc, char *argv[])
745 {
746 char *newargv[] = { NULL, "hello", "world", NULL };
747 char *newenviron[] = { NULL };
748
749 if (argc != 2) {
750 fprintf(stderr, "Usage: %s <file\-to\-exec>\\n", argv[0]);
751 exit(EXIT_FAILURE);
752 }
753
754 newargv[0] = argv[1];
755
756 execve(argv[1], newargv, newenviron);
757 perror("execve"); /* execve() returns only on error */
758 exit(EXIT_FAILURE);
759 }
760 .fi
761 .in
762
763 We can use the second program to exec the first as follows:
764
765 .in +4n
766 .nf
767 .RB "$" " cc myecho.c \-o myecho"
768 .RB "$" " cc execve.c \-o execve"
769 .RB "$" " ./execve ./myecho"
770 argv[0]: ./myecho
771 argv[1]: hello
772 argv[2]: world
773 .fi
774 .in
775
776 We can also use these programs to demonstrate the use of a script
777 interpreter.
778 To do this we create a script whose "interpreter" is our
779 .I myecho
780 program:
781
782 .in +4n
783 .nf
784 .RB "$" " cat > script"
785 .B #!./myecho script-arg
786 .B ^D
787 .RB "$" " chmod +x script"
788 .fi
789 .in
790
791 We can then use our program to exec the script:
792
793 .in +4n
794 .nf
795 .RB "$" " ./execve ./script"
796 argv[0]: ./myecho
797 argv[1]: script-arg
798 argv[2]: ./script
799 argv[3]: hello
800 argv[4]: world
801 .fi
802 .in
803 .SH SEE ALSO
804 .BR chmod (2),
805 .BR execveat (2),
806 .BR fork (2),
807 .BR ptrace (2),
808 .BR execl (3),
809 .BR fexecve (3),
810 .BR getopt (3),
811 .BR system (3),
812 .BR credentials (7),
813 .BR environ (7),
814 .BR path_resolution (7),
815 .BR ld.so (8)