/* Copyright (C) 2006 by Paolo Giarrusso - modified from glibc' execvp.c.1Original copyright notice follows:23Copyright (C) 1991,92,1995-99,2002,2004 Free Software Foundation, Inc.4This file is part of the GNU C Library.56The GNU C Library is free software; you can redistribute it and/or7modify it under the terms of the GNU Lesser General Public8License as published by the Free Software Foundation; either9version 2.1 of the License, or (at your option) any later version.1011The GNU C Library is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14Lesser General Public License for more details.1516You should have received a copy of the GNU Lesser General Public17License along with the GNU C Library; if not, write to the Free18Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA1902111-1307 USA. */20#include <unistd.h>2122#include <stdbool.h>23#include <stdlib.h>24#include <string.h>25#include <errno.h>26#include <limits.h>2728#ifndef TEST29#include "um_malloc.h"30#else31#include <stdio.h>32#define um_kmalloc malloc33#endif34#include "os.h"3536/* Execute FILE, searching in the `PATH' environment variable if it contains37no slashes, with arguments ARGV and environment from `environ'. */38int execvp_noalloc(char *buf, const char *file, char *const argv[])39{40if (*file == '\0') {41return -ENOENT;42}4344if (strchr (file, '/') != NULL) {45/* Don't search when it contains a slash. */46execv(file, argv);47} else {48int got_eacces;49size_t len, pathlen;50char *name, *p;51char *path = getenv("PATH");52if (path == NULL)53path = ":/bin:/usr/bin";5455len = strlen(file) + 1;56pathlen = strlen(path);57/* Copy the file name at the top. */58name = memcpy(buf + pathlen + 1, file, len);59/* And add the slash. */60*--name = '/';6162got_eacces = 0;63p = path;64do {65char *startp;6667path = p;68//Let's avoid this GNU extension.69//p = strchrnul (path, ':');70p = strchr(path, ':');71if (!p)72p = strchr(path, '\0');7374if (p == path)75/* Two adjacent colons, or a colon at the beginning or the end76of `PATH' means to search the current directory. */77startp = name + 1;78else79startp = memcpy(name - (p - path), path, p - path);8081/* Try to execute this name. If it works, execv will not return. */82execv(startp, argv);8384/*85if (errno == ENOEXEC) {86}87*/8889switch (errno) {90case EACCES:91/* Record the we got a `Permission denied' error. If we end92up finding no executable we can use, we want to diagnose93that we did find one but were denied access. */94got_eacces = 1;95case ENOENT:96case ESTALE:97case ENOTDIR:98/* Those errors indicate the file is missing or not executable99by us, in which case we want to just try the next path100directory. */101case ENODEV:102case ETIMEDOUT:103/* Some strange filesystems like AFS return even104stranger error numbers. They cannot reasonably mean105anything else so ignore those, too. */106case ENOEXEC:107/* We won't go searching for the shell108* if it is not executable - the Linux109* kernel already handles this enough,110* for us. */111break;112113default:114/* Some other error means we found an executable file, but115something went wrong executing it; return the error to our116caller. */117return -errno;118}119} while (*p++ != '\0');120121/* We tried every element and none of them worked. */122if (got_eacces)123/* At least one failure was due to permissions, so report that124error. */125return -EACCES;126}127128/* Return the error from the last attempt (probably ENOENT). */129return -errno;130}131#ifdef TEST132int main(int argc, char**argv)133{134char buf[PATH_MAX];135int ret;136argc--;137if (!argc) {138fprintf(stderr, "Not enough arguments\n");139return 1;140}141argv++;142if (ret = execvp_noalloc(buf, argv[0], argv)) {143errno = -ret;144perror("execvp_noalloc");145}146return 0;147}148#endif149150151