162306a36Sopenharmony_ci/* Copyright (C) 2006 by Paolo Giarrusso - modified from glibc' execvp.c. 262306a36Sopenharmony_ci Original copyright notice follows: 362306a36Sopenharmony_ci 462306a36Sopenharmony_ci Copyright (C) 1991,92,1995-99,2002,2004 Free Software Foundation, Inc. 562306a36Sopenharmony_ci This file is part of the GNU C Library. 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci The GNU C Library is free software; you can redistribute it and/or 862306a36Sopenharmony_ci modify it under the terms of the GNU Lesser General Public 962306a36Sopenharmony_ci License as published by the Free Software Foundation; either 1062306a36Sopenharmony_ci version 2.1 of the License, or (at your option) any later version. 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci The GNU C Library is distributed in the hope that it will be useful, 1362306a36Sopenharmony_ci but WITHOUT ANY WARRANTY; without even the implied warranty of 1462306a36Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1562306a36Sopenharmony_ci Lesser General Public License for more details. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci You should have received a copy of the GNU Lesser General Public 1862306a36Sopenharmony_ci License along with the GNU C Library; if not, write to the Free 1962306a36Sopenharmony_ci Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 2062306a36Sopenharmony_ci 02111-1307 USA. */ 2162306a36Sopenharmony_ci#include <unistd.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include <stdbool.h> 2462306a36Sopenharmony_ci#include <stdlib.h> 2562306a36Sopenharmony_ci#include <string.h> 2662306a36Sopenharmony_ci#include <errno.h> 2762306a36Sopenharmony_ci#include <limits.h> 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#ifndef TEST 3062306a36Sopenharmony_ci#include <um_malloc.h> 3162306a36Sopenharmony_ci#else 3262306a36Sopenharmony_ci#include <stdio.h> 3362306a36Sopenharmony_ci#define um_kmalloc malloc 3462306a36Sopenharmony_ci#endif 3562306a36Sopenharmony_ci#include <os.h> 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci/* Execute FILE, searching in the `PATH' environment variable if it contains 3862306a36Sopenharmony_ci no slashes, with arguments ARGV and environment from `environ'. */ 3962306a36Sopenharmony_ciint execvp_noalloc(char *buf, const char *file, char *const argv[]) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci if (*file == '\0') { 4262306a36Sopenharmony_ci return -ENOENT; 4362306a36Sopenharmony_ci } 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci if (strchr (file, '/') != NULL) { 4662306a36Sopenharmony_ci /* Don't search when it contains a slash. */ 4762306a36Sopenharmony_ci execv(file, argv); 4862306a36Sopenharmony_ci } else { 4962306a36Sopenharmony_ci int got_eacces; 5062306a36Sopenharmony_ci size_t len, pathlen; 5162306a36Sopenharmony_ci char *name, *p; 5262306a36Sopenharmony_ci char *path = getenv("PATH"); 5362306a36Sopenharmony_ci if (path == NULL) 5462306a36Sopenharmony_ci path = ":/bin:/usr/bin"; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci len = strlen(file) + 1; 5762306a36Sopenharmony_ci pathlen = strlen(path); 5862306a36Sopenharmony_ci /* Copy the file name at the top. */ 5962306a36Sopenharmony_ci name = memcpy(buf + pathlen + 1, file, len); 6062306a36Sopenharmony_ci /* And add the slash. */ 6162306a36Sopenharmony_ci *--name = '/'; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci got_eacces = 0; 6462306a36Sopenharmony_ci p = path; 6562306a36Sopenharmony_ci do { 6662306a36Sopenharmony_ci char *startp; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci path = p; 6962306a36Sopenharmony_ci //Let's avoid this GNU extension. 7062306a36Sopenharmony_ci //p = strchrnul (path, ':'); 7162306a36Sopenharmony_ci p = strchr(path, ':'); 7262306a36Sopenharmony_ci if (!p) 7362306a36Sopenharmony_ci p = strchr(path, '\0'); 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci if (p == path) 7662306a36Sopenharmony_ci /* Two adjacent colons, or a colon at the beginning or the end 7762306a36Sopenharmony_ci of `PATH' means to search the current directory. */ 7862306a36Sopenharmony_ci startp = name + 1; 7962306a36Sopenharmony_ci else 8062306a36Sopenharmony_ci startp = memcpy(name - (p - path), path, p - path); 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci /* Try to execute this name. If it works, execv will not return. */ 8362306a36Sopenharmony_ci execv(startp, argv); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci /* 8662306a36Sopenharmony_ci if (errno == ENOEXEC) { 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci switch (errno) { 9162306a36Sopenharmony_ci case EACCES: 9262306a36Sopenharmony_ci /* Record the we got a `Permission denied' error. If we end 9362306a36Sopenharmony_ci up finding no executable we can use, we want to diagnose 9462306a36Sopenharmony_ci that we did find one but were denied access. */ 9562306a36Sopenharmony_ci got_eacces = 1; 9662306a36Sopenharmony_ci break; 9762306a36Sopenharmony_ci case ENOENT: 9862306a36Sopenharmony_ci case ESTALE: 9962306a36Sopenharmony_ci case ENOTDIR: 10062306a36Sopenharmony_ci /* Those errors indicate the file is missing or not executable 10162306a36Sopenharmony_ci by us, in which case we want to just try the next path 10262306a36Sopenharmony_ci directory. */ 10362306a36Sopenharmony_ci case ENODEV: 10462306a36Sopenharmony_ci case ETIMEDOUT: 10562306a36Sopenharmony_ci /* Some strange filesystems like AFS return even 10662306a36Sopenharmony_ci stranger error numbers. They cannot reasonably mean 10762306a36Sopenharmony_ci anything else so ignore those, too. */ 10862306a36Sopenharmony_ci case ENOEXEC: 10962306a36Sopenharmony_ci /* We won't go searching for the shell 11062306a36Sopenharmony_ci * if it is not executable - the Linux 11162306a36Sopenharmony_ci * kernel already handles this enough, 11262306a36Sopenharmony_ci * for us. */ 11362306a36Sopenharmony_ci break; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci default: 11662306a36Sopenharmony_ci /* Some other error means we found an executable file, but 11762306a36Sopenharmony_ci something went wrong executing it; return the error to our 11862306a36Sopenharmony_ci caller. */ 11962306a36Sopenharmony_ci return -errno; 12062306a36Sopenharmony_ci } 12162306a36Sopenharmony_ci } while (*p++ != '\0'); 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci /* We tried every element and none of them worked. */ 12462306a36Sopenharmony_ci if (got_eacces) 12562306a36Sopenharmony_ci /* At least one failure was due to permissions, so report that 12662306a36Sopenharmony_ci error. */ 12762306a36Sopenharmony_ci return -EACCES; 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci /* Return the error from the last attempt (probably ENOENT). */ 13162306a36Sopenharmony_ci return -errno; 13262306a36Sopenharmony_ci} 13362306a36Sopenharmony_ci#ifdef TEST 13462306a36Sopenharmony_ciint main(int argc, char**argv) 13562306a36Sopenharmony_ci{ 13662306a36Sopenharmony_ci char buf[PATH_MAX]; 13762306a36Sopenharmony_ci int ret; 13862306a36Sopenharmony_ci argc--; 13962306a36Sopenharmony_ci if (!argc) { 14062306a36Sopenharmony_ci os_warn("Not enough arguments\n"); 14162306a36Sopenharmony_ci return 1; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci argv++; 14462306a36Sopenharmony_ci if (ret = execvp_noalloc(buf, argv[0], argv)) { 14562306a36Sopenharmony_ci errno = -ret; 14662306a36Sopenharmony_ci perror("execvp_noalloc"); 14762306a36Sopenharmony_ci } 14862306a36Sopenharmony_ci return 0; 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci#endif 151