Blame sysdeps/unix/sysv/linux/fexecve.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <sys/syscall.h>
Packit 6c4009
#include <kernel-features.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Execute the file FD refers to, overlaying the running program image.
Packit 6c4009
   ARGV and ENVP are passed to the new program, as for `execve'.  */
Packit 6c4009
int
Packit 6c4009
fexecve (int fd, char *const argv[], char *const envp[])
Packit 6c4009
{
Packit 6c4009
  if (fd < 0 || argv == NULL || envp == NULL)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef __NR_execveat
Packit 6c4009
  /* Avoid implicit array coercion in syscall macros.  */
Packit 6c4009
  INLINE_SYSCALL (execveat, 5, fd, "", &argv[0], &envp[0], AT_EMPTY_PATH);
Packit 6c4009
# ifndef __ASSUME_EXECVEAT
Packit 6c4009
  if (errno != ENOSYS)
Packit 6c4009
    return -1;
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef __ASSUME_EXECVEAT
Packit 6c4009
  /* We use the /proc filesystem to get the information.  If it is not
Packit 6c4009
     mounted we fail.  */
Packit 6c4009
  char buf[sizeof "/proc/self/fd/" + sizeof (int) * 3];
Packit 6c4009
  __snprintf (buf, sizeof (buf), "/proc/self/fd/%d", fd);
Packit 6c4009
Packit 6c4009
  /* We do not need the return value.  */
Packit 6c4009
  __execve (buf, argv, envp);
Packit 6c4009
Packit 6c4009
  int save = errno;
Packit 6c4009
Packit 6c4009
  /* We come here only if the 'execve' call fails.  Determine whether
Packit 6c4009
     /proc is mounted.  If not we return ENOSYS.  */
Packit 6c4009
  struct stat st;
Packit 6c4009
  if (stat ("/proc/self/fd", &st) != 0 && errno == ENOENT)
Packit 6c4009
    save = ENOSYS;
Packit 6c4009
Packit 6c4009
  __set_errno (save);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return -1;
Packit 6c4009
}