-
Notifications
You must be signed in to change notification settings - Fork 0
/
execvpe.c
85 lines (72 loc) · 1.59 KB
/
execvpe.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
/*
* Copyright 2021-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <paths.h>
#include <limits.h>
#include <unistd.h>
#include "iamroot.h"
#define getenv _getenv
extern int __execve(const char *, char * const [], char * const []);
/*
* Stolen and hacked from musl (src/process/execvp.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
static int __execvpe(const char *file, char * const argv[],
char * const envp[])
{
const char *p, *z, *path = getenv("PATH");
size_t l, k;
int seen_eacces = 0;
errno = ENOENT;
if (!*file) return -1;
if (strchr(file, '/'))
return __execve(file, argv, envp);
if (!path) path = _PATH_DEFPATH;
k = strnlen(file, NAME_MAX+1);
if (k > NAME_MAX) {
errno = ENAMETOOLONG;
return -1;
}
l = strnlen(path, PATH_MAX-1)+1;
for(p=path; ; p=z) {
char b[l+k+1];
z = __strchrnul(p, ':');
if ((size_t)(z-p) >= l) {
if (!*z++) break;
continue;
}
memcpy(b, p, z-p);
b[z-p] = '/';
memcpy(b+(z-p)+(z>p), file, k+1);
__execve(b, argv, envp);
switch (errno) {
case EACCES:
seen_eacces = 1;
case ENOENT:
case ENOTDIR:
break;
default:
return -1;
}
if (!*z++) break;
}
if (seen_eacces) errno = EACCES;
return -1;
}
#undef getenv
int execvpe(const char *file, char * const argv[], char * const envp[])
{
__debug("%s(file: '%s', argv: { '%s', '%s', ... }, envp: %p)\n",
__func__, file, argv[0], argv[1], envp);
/* Forward to local function */
return __execvpe(file, argv, envp);
}