Files
glibc/posix/execvp.c
T

199 lines
5.2 KiB
C
Raw Normal View History

2005-01-20 20:10:02 +00:00
/* Copyright (C) 1991,92,1995-99,2002,2004,2005 Free Software Foundation, Inc.
1997-02-15 04:31:36 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
1995-02-18 01:27:10 +00:00
#include <unistd.h>
#include <stdarg.h>
2005-01-27 18:49:47 +00:00
#include <stdbool.h>
1995-02-18 01:27:10 +00:00
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <paths.h>
1995-02-18 01:27:10 +00:00
1997-10-12 04:05:44 +00:00
1999-04-28 23:13:52 +00:00
/* The file is accessible but it is not an executable file. Invoke
the shell to interpret it as a script. */
2005-01-20 20:10:02 +00:00
static char **
1997-10-12 04:05:44 +00:00
internal_function
2005-01-20 20:10:02 +00:00
allocate_scripts_argv (const char *file, char *const argv[])
1997-10-12 04:05:44 +00:00
{
/* Count the arguments. */
int argc = 0;
while (argv[argc++])
;
1999-04-28 23:13:52 +00:00
/* Construct an argument list for the shell. */
2005-01-20 20:10:02 +00:00
char **new_argv = (char **) malloc ((argc + 1) * sizeof (char *));
if (new_argv != NULL)
{
new_argv[0] = (char *) _PATH_BSHELL;
new_argv[1] = (char *) file;
while (argc > 1)
{
new_argv[argc] = argv[argc - 1];
--argc;
}
}
2005-01-20 20:10:02 +00:00
return new_argv;
1997-10-12 04:05:44 +00:00
}
1995-02-18 01:27:10 +00:00
/* Execute FILE, searching in the `PATH' environment variable if it contains
no slashes, with arguments ARGV and environment from `environ'. */
int
execvp (file, argv)
const char *file;
char *const argv[];
1995-02-18 01:27:10 +00:00
{
1998-10-22 11:49:29 +00:00
if (*file == '\0')
{
/* We check the simple case first. */
__set_errno (ENOENT);
return -1;
}
2005-01-20 20:10:02 +00:00
char **script_argv = NULL;
if (strchr (file, '/') != NULL)
1999-04-28 23:13:52 +00:00
{
/* Don't search when it contains a slash. */
1999-08-20 17:01:09 +00:00
__execve (file, argv, __environ);
1999-04-28 23:13:52 +00:00
if (errno == ENOEXEC)
2005-01-20 20:10:02 +00:00
{
script_argv = allocate_scripts_argv (file, argv);
if (script_argv != NULL)
{
__execve (script_argv[0], script_argv, __environ);
free (script_argv);
}
}
1999-04-28 23:13:52 +00:00
}
else
1995-02-18 01:27:10 +00:00
{
2005-01-20 20:10:02 +00:00
char *path = getenv ("PATH");
2005-07-24 21:41:49 +00:00
char *path_malloc = NULL;
1995-02-18 01:27:10 +00:00
if (path == NULL)
{
/* There is no `PATH' in the environment.
The default search path is the current directory
followed by the path `confstr' returns for `_CS_PATH'. */
2005-01-20 20:10:02 +00:00
size_t len = confstr (_CS_PATH, (char *) NULL, 0);
path = (char *) malloc (1 + len);
if (path == NULL)
return -1;
1995-02-18 01:27:10 +00:00
path[0] = ':';
(void) confstr (_CS_PATH, path + 1, len);
2005-07-24 21:41:49 +00:00
path_malloc = path;
1995-02-18 01:27:10 +00:00
}
2005-01-20 20:10:02 +00:00
size_t len = strlen (file) + 1;
size_t pathlen = strlen (path);
char *name = malloc (pathlen + len + 1);
if (name == NULL)
{
2005-07-24 21:41:49 +00:00
free (path_malloc);
2005-01-20 20:10:02 +00:00
return -1;
}
1999-08-20 17:01:09 +00:00
/* Copy the file name at the top. */
1999-08-23 22:20:34 +00:00
name = (char *) memcpy (name + pathlen + 1, file, len);
1999-08-20 17:01:09 +00:00
/* And add the slash. */
*--name = '/';
2005-01-20 20:10:02 +00:00
bool got_eacces = false;
char *p = path;
1995-02-18 01:27:10 +00:00
do
{
1999-08-20 17:01:09 +00:00
char *startp;
1995-02-18 01:27:10 +00:00
path = p;
1999-04-28 23:13:52 +00:00
p = __strchrnul (path, ':');
1995-02-18 01:27:10 +00:00
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
of `PATH' means to search the current directory. */
1999-08-20 17:01:09 +00:00
startp = name + 1;
1995-02-18 01:27:10 +00:00
else
1999-08-20 17:01:09 +00:00
startp = (char *) memcpy (name - (p - path), path, p - path);
/* Try to execute this name. If it works, execve will not return. */
1999-08-20 17:01:09 +00:00
__execve (startp, argv, __environ);
1999-04-28 23:13:52 +00:00
if (errno == ENOEXEC)
2005-01-20 20:10:02 +00:00
{
if (script_argv == NULL)
{
script_argv = allocate_scripts_argv (startp, argv);
2005-01-20 20:10:02 +00:00
if (script_argv == NULL)
{
/* A possible EACCES error is not as important as
the ENOMEM. */
got_eacces = false;
break;
}
}
__execve (script_argv[0], script_argv, __environ);
}
switch (errno)
{
case EACCES:
/* Record the we got a `Permission denied' error. If we end
up finding no executable we can use, we want to diagnose
that we did find one but were denied access. */
2005-01-20 20:10:02 +00:00
got_eacces = true;
case ENOENT:
1997-07-11 01:19:07 +00:00
case ESTALE:
1998-09-22 19:08:42 +00:00
case ENOTDIR:
/* Those errors indicate the file is missing or not executable
by us, in which case we want to just try the next path
directory. */
2004-10-22 06:27:54 +00:00
case ENODEV:
case ETIMEDOUT:
/* Some strange filesystems like AFS return even
stranger error numbers. They cannot reasonably mean
anything else so ignore those, too. */
break;
default:
/* Some other error means we found an executable file, but
something went wrong executing it; return the error to our
caller. */
return -1;
}
1995-02-18 01:27:10 +00:00
}
while (*p++ != '\0');
1999-04-28 23:13:52 +00:00
/* We tried every element and none of them worked. */
if (got_eacces)
/* At least one failure was due to permissions, so report that
error. */
1999-04-28 23:13:52 +00:00
__set_errno (EACCES);
2005-01-20 20:10:02 +00:00
free (script_argv);
2005-02-22 15:36:13 +00:00
free (name - pathlen);
2005-07-24 21:41:49 +00:00
free (path_malloc);
1995-02-18 01:27:10 +00:00
}
/* Return the error from the last attempt (probably ENOENT). */
return -1;
1995-02-18 01:27:10 +00:00
}
libc_hidden_def (execvp)