man(1) Manual page archive


     EXEC(2)                                                   EXEC(2)

     NAME
          execl, execv, execle, execve, execlp, execvp, exect -
          execute a file

     SYNOPSIS
          int execl(name, arg0, arg1, ..., argn, (char *)0)
          char *name, *arg0, *arg1, ..., *argn;

          int execv(name, argv)
          char *name, *argv[];

          int execle(name, arg0, arg1, ..., argn, (char *)0, envp)
          char *name, *arg0, *arg1, ..., *argn, *envp[];

          int execve(name, argv, envp)
          char *name, *argv[], *envp[];

          int execlp(name, arg0, arg1, ..., argn, (char *)0)
          char *name, *arg0, *arg1, ..., *argn;

          int execvp(name, argv)
          char *name, *argv[];

          int exect(name, argv, envp)
          char *name, *argv[], *envp[];

     DESCRIPTION
          Exec in all its forms overlays the calling process with the
          named file, then transfers to the entry point of the image
          of the file.  There can be no return from a successful exec;
          the calling image is lost.

          Files remain open across exec unless explicit arrangement
          has been made; see ioctl(2). Signals that are caught (see
          signal(2)) are reset to their default values.  Other signal
          settings are unchanged.

          Each user has a real userid and groupid and an effective
          userid and groupid.  The real userid (groupid) identifies
          the person using the system; the effective userid (groupid)
          determines access privileges.  Exec changes the effective
          userid and groupid to the owner of the executed file if the
          file has the set-userid or set-groupid modes.  The real
          userid is not affected.

          Name points to the name of the file to be executed.  It must
          be a regular file (type S_IFREG, see stat(2)) and its per-
          missions must allow execution.  Arg0, arg1, ... or the
          pointers in argv address null-terminated argument strings to
          be made available when the new image starts.  Argv must end

     EXEC(2)                                                   EXEC(2)

          with a 0 pointer.  Conventionally argument 0 is the name of
          the program.

          In execle, execve, and exect, the envp array contains point-
          ers to a set of null-terminated strings composing the envi-
          ronment of the process.  Envp must end with a 0 pointer.
          The other calls copy the present environment from the global
          cell environ; see environ(5).

          The execl and execv forms differ only in argument syntax;
          one is more convenient when the number of arguments is known
          in advance, the other when arguments are assembled on the
          fly.

          If the first two bytes of the file are the characters `#!',
          subsequent text up to a newline is examined.  The first
          word, up to a blank or tab, names an interpreter program;
          anything left over is a single supplemental argument.  The
          original name, preceded by the supplemental argument if any,
          is inserted in the argument list between arg0 and arg1 (or
          between the first pair of argv pointers).  The interpreter
          is executed with the modified argument list.

          If the file doesn't start with `#!', a standard header for a
          binary image is expected; see a.out(5). If the file doesn't
          begin with a valid header either, ENOEXEC is returned.  The
          shell sh(1) takes this to mean that the file contains shell
          commands.

          When a C program is executed, it is called as follows:

               main(argc, argv, envp)
               int argc;
               char **argv, **envp;

          Argv is the array of argument pointers passed to exec; argc
          is the number of arguments.  Argv is directly usable in a
          subsequent execv because argv[argc]==0.  Envp is the envi-
          ronment array; the same value has already been stored in
          environ.

          Execlp and execvp take the same arguments as execl and
          execv, but search the directories listed in the PATH envi-
          ronment variable for an executable file called name, mimick-
          ing the shell's path search.

          Exect is the same as execve, except that it arranges for the
          process to stop just before the first instruction of the new
          image; see proc(4).

     FILES
          shell, invoked if command file found

     EXEC(2)                                                   EXEC(2)

                   by execlp or execvp

     EXAMPLES
          This file, if created with execute permissions and run by
          exec, calls awk(1) to count the lines in all the files named
          in its arguments:
          #!/usr/bin/awk -f
          END  { print NR }

     SEE ALSO
          sh(1), fork(2), ioctl(2), signal(2), proc(4), environ(5)

     DIAGNOSTICS
          E2BIG, EACCES, EFAULT, EIO, ELOOP, ENOENT, ENOEXEC, ENOMEM,
          ENOTDIR, ENXIO, ETXTBSY

     BUGS
          If execvp is called to execute a file that turns out to be a
          shell command file, and the shell cannot be executed, some
          of the values in argv may be modified before return.
          Neither the shell's path search nor that of execlp and
          execvp extends to the interpreter named after `#!'.  The
          interpreter file may not itself begin with `#!'.  The text
          after `#!'  may be no more than 30 characters long, includ-
          ing the newline.