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