Blame man2/execveat.2

Packit 7cfc04
.\" Copyright (c) 2014 Google, Inc., written by David Drysdale
Packit 7cfc04
.\" and Copyright (c) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH EXECVEAT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
execveat \- execute program relative to a directory file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int execveat(int " dirfd ", const char *" pathname ","
Packit 7cfc04
.br
Packit 7cfc04
.BI "             char *const " argv "[], char *const " envp "[],"
Packit 7cfc04
.br
Packit 7cfc04
.BI "             int " flags );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.\" commit 51f39a1f0cea1cacf8c787f652f26dfee9611874
Packit 7cfc04
The
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
system call executes the program referred to by the combination of
Packit 7cfc04
.I dirfd
Packit 7cfc04
and
Packit 7cfc04
.IR pathname .
Packit 7cfc04
It operates in exactly the same way as
Packit 7cfc04
.BR execve (2),
Packit 7cfc04
except for the differences described in this manual page.
Packit 7cfc04
.PP
Packit 7cfc04
If the pathname given in
Packit 7cfc04
.I pathname
Packit 7cfc04
is relative, then it is interpreted relative to the directory
Packit 7cfc04
referred to by the file descriptor
Packit 7cfc04
.I dirfd
Packit 7cfc04
(rather than relative to the current working directory of
Packit 7cfc04
the calling process, as is done by
Packit 7cfc04
.BR execve (2)
Packit 7cfc04
for a relative pathname).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is relative and
Packit 7cfc04
.I dirfd
Packit 7cfc04
is the special value
Packit 7cfc04
.BR AT_FDCWD ,
Packit 7cfc04
then
Packit 7cfc04
.I pathname
Packit 7cfc04
is interpreted relative to the current working
Packit 7cfc04
directory of the calling process (like
Packit 7cfc04
.BR execve (2)).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is absolute, then
Packit 7cfc04
.I dirfd
Packit 7cfc04
is ignored.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is an empty string and the
Packit 7cfc04
.BR AT_EMPTY_PATH
Packit 7cfc04
flag is specified, then the file descriptor
Packit 7cfc04
.I dirfd
Packit 7cfc04
specifies the file to be executed (i.e.,
Packit 7cfc04
.IR dirfd
Packit 7cfc04
refers to an executable file, rather than a directory).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument is a bit mask that can include zero or more of the following flags:
Packit 7cfc04
.TP
Packit 7cfc04
.BR AT_EMPTY_PATH
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is an empty string, operate on the file referred to by
Packit 7cfc04
.IR dirfd
Packit 7cfc04
(which may have been obtained using the
Packit 7cfc04
.BR open (2)
Packit 7cfc04
.B O_PATH
Packit 7cfc04
flag).
Packit 7cfc04
.TP
Packit 7cfc04
.B AT_SYMLINK_NOFOLLOW
Packit 7cfc04
If the file identified by
Packit 7cfc04
.I dirfd
Packit 7cfc04
and a non-NULL
Packit 7cfc04
.I pathname
Packit 7cfc04
is a symbolic link, then the call fails with the error
Packit 7cfc04
.BR ELOOP .
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
does not return.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The same errors that occur for
Packit 7cfc04
.BR execve (2)
Packit 7cfc04
can also occur for
Packit 7cfc04
.BR execveat ().
Packit 7cfc04
The following additional errors can occur for
Packit 7cfc04
.BR execveat ():
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I dirfd
Packit 7cfc04
is not a valid file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Invalid flag specified in
Packit 7cfc04
.IR flags .
Packit 7cfc04
.TP
Packit 7cfc04
.B ELOOP
Packit 7cfc04
.I flags
Packit 7cfc04
includes
Packit 7cfc04
.BR AT_SYMLINK_NOFOLLOW
Packit 7cfc04
and the file identified by
Packit 7cfc04
.I dirfd
Packit 7cfc04
and a non-NULL
Packit 7cfc04
.I pathname
Packit 7cfc04
is a symbolic link.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
The program identified by
Packit 7cfc04
.I dirfd
Packit 7cfc04
and
Packit 7cfc04
.I pathname
Packit 7cfc04
requires the use of an interpreter program
Packit 7cfc04
(such as a script starting with "#!"), but the file descriptor
Packit 7cfc04
.I dirfd
Packit 7cfc04
was opened with the
Packit 7cfc04
.B O_CLOEXEC
Packit 7cfc04
flag, with the result that
Packit 7cfc04
the program file is inaccessible to the launched interpreter.
Packit 7cfc04
See BUGS.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTDIR
Packit 7cfc04
.I pathname
Packit 7cfc04
is relative and
Packit 7cfc04
.I dirfd
Packit 7cfc04
is a file descriptor referring to a file other than a directory.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
was added to Linux in kernel 3.19.
Packit 7cfc04
GNU C library support is pending.
Packit 7cfc04
.\" FIXME . check for glibc support in a future release
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
The
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
system call is Linux-specific.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
In addition to the reasons explained in
Packit 7cfc04
.BR openat (2),
Packit 7cfc04
the
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
system call is also needed to allow
Packit 7cfc04
.BR fexecve (3)
Packit 7cfc04
to be implemented on systems that do not have the
Packit 7cfc04
.I /proc
Packit 7cfc04
filesystem mounted.
Packit 7cfc04
.PP
Packit 7cfc04
When asked to execute a script file, the
Packit 7cfc04
.IR argv[0]
Packit 7cfc04
that is passed to the script interpreter is a string of the form
Packit 7cfc04
.IR /dev/fd/N
Packit 7cfc04
or
Packit 7cfc04
.IR /dev/fd/N/P ,
Packit 7cfc04
where
Packit 7cfc04
.I N
Packit 7cfc04
is the number of the file descriptor passed via the
Packit 7cfc04
.IR dirfd
Packit 7cfc04
argument.
Packit 7cfc04
A string of the first form occurs when
Packit 7cfc04
.BR AT_EMPTY_PATH
Packit 7cfc04
is employed.
Packit 7cfc04
A string of the second form occurs when the script is specified via both
Packit 7cfc04
.IR dirfd
Packit 7cfc04
and
Packit 7cfc04
.IR pathname ;
Packit 7cfc04
in this case,
Packit 7cfc04
.IR P
Packit 7cfc04
is the value given in
Packit 7cfc04
.IR pathname .
Packit 7cfc04
.PP
Packit 7cfc04
For the same reasons described in
Packit 7cfc04
.BR fexecve (3),
Packit 7cfc04
the natural idiom when using
Packit 7cfc04
.BR execveat ()
Packit 7cfc04
is to set the close-on-exec flag on
Packit 7cfc04
.IR dirfd .
Packit 7cfc04
(But see BUGS.)
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The
Packit 7cfc04
.B ENOENT
Packit 7cfc04
error described above means that it is not possible to set the
Packit 7cfc04
close-on-exec flag on the file descriptor given to a call of the form:
Packit 7cfc04
.PP
Packit 7cfc04
    execveat(fd, "", argv, envp, AT_EMPTY_PATH);
Packit 7cfc04
.PP
Packit 7cfc04
However, the inability to set the close-on-exec flag means that a file
Packit 7cfc04
descriptor referring to the script leaks through to the script itself.
Packit 7cfc04
As well as wasting a file descriptor,
Packit 7cfc04
this leakage can lead to file-descriptor exhaustion in scenarios
Packit 7cfc04
where scripts recursively employ
Packit 7cfc04
.BR execveat ().
Packit 7cfc04
.\" For an example, see Michael Kerrisk's 2015-01-10 reply in this LKML
Packit 7cfc04
.\" thread (http://thread.gmane.org/gmane.linux.kernel/1836105/focus=20229):
Packit 7cfc04
.\"
Packit 7cfc04
.\"     Subject: [PATCHv10 man-pages 5/5] execveat.2: initial man page.\"                        for execveat(2
Packit 7cfc04
.\"     Date: Mon, 24 Nov 2014 11:53:59 +0000
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR execve (2),
Packit 7cfc04
.BR openat (2),
Packit 7cfc04
.BR fexecve (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.