execvp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright (C) 2006 by Paolo Giarrusso - modified from glibc' execvp.c.
  2. Original copyright notice follows:
  3. Copyright (C) 1991,92,1995-99,2002,2004 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <unistd.h>
  18. #include <stdbool.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #ifndef TEST
  24. #include "um_malloc.h"
  25. #else
  26. #include <stdio.h>
  27. #define um_kmalloc malloc
  28. #endif
  29. #include "os.h"
  30. /* Execute FILE, searching in the `PATH' environment variable if it contains
  31. no slashes, with arguments ARGV and environment from `environ'. */
  32. int execvp_noalloc(char *buf, const char *file, char *const argv[])
  33. {
  34. if (*file == '\0') {
  35. return -ENOENT;
  36. }
  37. if (strchr (file, '/') != NULL) {
  38. /* Don't search when it contains a slash. */
  39. execv(file, argv);
  40. } else {
  41. int got_eacces;
  42. size_t len, pathlen;
  43. char *name, *p;
  44. char *path = getenv("PATH");
  45. if (path == NULL)
  46. path = ":/bin:/usr/bin";
  47. len = strlen(file) + 1;
  48. pathlen = strlen(path);
  49. /* Copy the file name at the top. */
  50. name = memcpy(buf + pathlen + 1, file, len);
  51. /* And add the slash. */
  52. *--name = '/';
  53. got_eacces = 0;
  54. p = path;
  55. do {
  56. char *startp;
  57. path = p;
  58. //Let's avoid this GNU extension.
  59. //p = strchrnul (path, ':');
  60. p = strchr(path, ':');
  61. if (!p)
  62. p = strchr(path, '\0');
  63. if (p == path)
  64. /* Two adjacent colons, or a colon at the beginning or the end
  65. of `PATH' means to search the current directory. */
  66. startp = name + 1;
  67. else
  68. startp = memcpy(name - (p - path), path, p - path);
  69. /* Try to execute this name. If it works, execv will not return. */
  70. execv(startp, argv);
  71. /*
  72. if (errno == ENOEXEC) {
  73. }
  74. */
  75. switch (errno) {
  76. case EACCES:
  77. /* Record the we got a `Permission denied' error. If we end
  78. up finding no executable we can use, we want to diagnose
  79. that we did find one but were denied access. */
  80. got_eacces = 1;
  81. case ENOENT:
  82. case ESTALE:
  83. case ENOTDIR:
  84. /* Those errors indicate the file is missing or not executable
  85. by us, in which case we want to just try the next path
  86. directory. */
  87. case ENODEV:
  88. case ETIMEDOUT:
  89. /* Some strange filesystems like AFS return even
  90. stranger error numbers. They cannot reasonably mean
  91. anything else so ignore those, too. */
  92. case ENOEXEC:
  93. /* We won't go searching for the shell
  94. * if it is not executable - the Linux
  95. * kernel already handles this enough,
  96. * for us. */
  97. break;
  98. default:
  99. /* Some other error means we found an executable file, but
  100. something went wrong executing it; return the error to our
  101. caller. */
  102. return -errno;
  103. }
  104. } while (*p++ != '\0');
  105. /* We tried every element and none of them worked. */
  106. if (got_eacces)
  107. /* At least one failure was due to permissions, so report that
  108. error. */
  109. return -EACCES;
  110. }
  111. /* Return the error from the last attempt (probably ENOENT). */
  112. return -errno;
  113. }
  114. #ifdef TEST
  115. int main(int argc, char**argv)
  116. {
  117. char buf[PATH_MAX];
  118. int ret;
  119. argc--;
  120. if (!argc) {
  121. fprintf(stderr, "Not enough arguments\n");
  122. return 1;
  123. }
  124. argv++;
  125. if (ret = execvp_noalloc(buf, argv[0], argv)) {
  126. errno = -ret;
  127. perror("execvp_noalloc");
  128. }
  129. return 0;
  130. }
  131. #endif