fexecve.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (C) 1994-2019 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 2.1 of the License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <https://www.gnu.org/licenses/>. */
  13. #include <errno.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. int
  20. fexecve (int fd, char *const argv[], char *const envp[])
  21. {
  22. if (fd < 0 || argv == NULL || envp == NULL)
  23. {
  24. __set_errno (EINVAL);
  25. return -1;
  26. }
  27. /* We use the /proc filesystem to get the information. If it is not
  28. mounted we fail. */
  29. char buf[sizeof "/proc/self/fd/" + sizeof (int) * 3];
  30. snprintf (buf, sizeof (buf), "/proc/self/fd/%d", fd);
  31. /* We do not need the return value. */
  32. execve (buf, argv, envp);
  33. int save = errno;
  34. /* We come here only if the 'execve' call fails. Determine whether
  35. /proc is mounted. If not we return ENOSYS. */
  36. struct stat st;
  37. if (stat ("/proc/self/fd", &st) != 0 && errno == ENOENT)
  38. save = ENOSYS;
  39. __set_errno (save);
  40. return -1;
  41. }
  42. libc_hidden_def(fexecve)