/
execlp.c
192 lines (143 loc) · 6.13 KB
/
execlp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**********************************************************************\
* Copyright (C) Michael Kerrisk, 2010. *
* *
* This program is free software. You may use, modify, and redistribute *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 or (at *
* your option) any later version. This program is distributed without *
* any warranty. See the file COPYING for details. *
\**********************************************************************/
/* execlp.c
An implementation of execlp() using execve().
For the full details, see the SUSv3 specification of the exec functions
(XSH:exec). For an alternate (and more complete) implementation, see the
posix/exec*.c sources in glibc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
extern char **environ;
#define max(x,y) ((x) > (y) ? (x) : (y))
#define SHELL_PATH "/bin/sh" /* pathname for the standard shell */
/* Exec a script file using the standard shell */
static void
execShScript(int argc, char *argv[], char *envp[])
{
char *shArgv[argc + 1];
int j;
shArgv[0] = SHELL_PATH;
for (j = 0; j <= argc; j++)
shArgv[j + 1] = argv[j];
execve(SHELL_PATH, shArgv, envp);
/* Only get here, if execve() fails, in which case we return
to our caller. */
}
int
execlp(const char *filename, const char *arg, ...)
{
char **argv; /* Argument vector for new program */
int argc; /* Number of items used in argv */
int argvSize; /* Currently allocated size of argv */
va_list argList; /* For variable argument list parsing */
char **envp; /* Environment for new program */
char *PATH; /* Value of PATH environment variable */
char *pathname; /* Path prefix + '/' + filename */
char *prStart, *prEnd; /* Start and end of prefix currently
being processed from PATH */
int savedErrno;
int morePrefixes; /* True if there are more prefixes in PATH */
char *p;
int j;
int fndEACCES; /* True if any execve() returned EACCES */
fndEACCES = 0;
/***** Build argument vector from variable length argument list *****/
argvSize = 100;
argv = calloc(argvSize, sizeof(void *));
if (argv == NULL) return -1;
argv[0] = (char *) arg;
argc = 1;
/* Walk variable length argument list until NULL terminator is found,
building argv as we go. */
va_start(argList, arg);
while (argv[argc - 1] != NULL) {
if (argc + 1 >= argvSize) { /* Resize argv if required */
argvSize += 100;
argv = realloc(argv, sizeof(void *));
if (argv == NULL) return -1;
}
argv[argc] = va_arg(argList, char *);
argc++;
}
va_end(argList);
/***** Use caller's environment to create envp vector *****/
for (j = 0; environ[j] != NULL; ) /* Calculate size of environ */
j++;
envp = calloc(sizeof(void *), j + 1);
if (envp == NULL) return -1;
for (j = 0; environ[j] != NULL; j++) /* Duplicate environ in envp */
envp[j] = strdup(environ[j]);
envp[j] = NULL; /* List must be terminated by NULL pointer */
/***** Now try to exec filename *****/
if (strchr(filename, '/') != NULL) {
/* If file contains a slash, it's a pathname and we don't do
a search using PATH */
pathname = strdup(filename);
execve(pathname, argv, envp);
savedErrno = errno; /* So we can return correct errno */
if (errno == ENOEXEC)
execShScript(argc, argv, envp);
free(pathname); /* Avoid memory leaks */
} else { /* Use PATH */
/* Treat undefined PATH as "." */
p = getenv("PATH");
PATH = (p == NULL || strlen(p) == 0) ? strdup(".") : strdup(p);
/* For each prefix in PATH, try to exec 'filename' in that
directory. The loop will terminate either when we successfully
exec or when we run out of path prefixes. */
prStart = PATH;
morePrefixes = 1;
while (morePrefixes) {
/* Locate end of prefix */
prEnd = strchr(prStart, ':');
if (prEnd == NULL) /* Handle last prefix */
prEnd = prStart + strlen(prStart);
/* Build complete pathname from path prefix and filename */
pathname = malloc(max(1, prEnd - prStart) + strlen(filename)
+ 2);
pathname[0] = '\0';
if (prEnd == prStart) /* Last prefix */
strcat(pathname, ".");
else
strncat(pathname, prStart, prEnd - prStart);
strcat(pathname, "/");
strcat(pathname, filename);
if (*prEnd == '\0') /* No more prefixes */
morePrefixes = 0;
else
prStart = prEnd + 1; /* Move to start of next prefix */
/* Try to exec pathname. Execve() only returns if we failed. */
execve(pathname, argv, envp);
savedErrno = errno; /* So we can return correct errno */
if (errno == EACCES)
fndEACCES = 1;
else if (errno == ENOEXEC)
execShScript(argc, argv, envp);
/* Avoid memory leaks, in case no execve() succeeds! */
free(pathname);
}
free(PATH);
}
/* If we get here, execve() failed; clean up and return -1, ensuring
that errno contains the value returned by (the last) execve() */
free(argv);
for (j = 0; envp[j] != NULL; j++)
free(envp[j]);
free(envp);
/* SUSv3 says that if any execve failed with EACCES then that
is the error that should be returned by exec[lv]p() */
errno = fndEACCES ? EACCES : savedErrno;
return -1;
}