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