/* 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;95break;96case ENOENT:97case ESTALE:98case ENOTDIR:99/* Those errors indicate the file is missing or not executable100by us, in which case we want to just try the next path101directory. */102case ENODEV:103case ETIMEDOUT:104/* Some strange filesystems like AFS return even105stranger error numbers. They cannot reasonably mean106anything else so ignore those, too. */107case ENOEXEC:108/* We won't go searching for the shell109* if it is not executable - the Linux110* kernel already handles this enough,111* for us. */112break;113114default:115/* Some other error means we found an executable file, but116something went wrong executing it; return the error to our117caller. */118return -errno;119}120} while (*p++ != '\0');121122/* We tried every element and none of them worked. */123if (got_eacces)124/* At least one failure was due to permissions, so report that125error. */126return -EACCES;127}128129/* Return the error from the last attempt (probably ENOENT). */130return -errno;131}132#ifdef TEST133int main(int argc, char**argv)134{135char buf[PATH_MAX];136int ret;137argc--;138if (!argc) {139os_warn("Not enough arguments\n");140return 1;141}142argv++;143if (ret = execvp_noalloc(buf, argv[0], argv)) {144errno = -ret;145perror("execvp_noalloc");146}147return 0;148}149#endif150151152