2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-01-25 21:08:05 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-08-18 05:01:42 +00:00
|
|
|
#include "ape/ape.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/atomic.h"
|
2022-11-06 02:49:41 +00:00
|
|
|
#include "libc/calls/blockcancel.internal.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/calls/blocksigs.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/calls/cp.internal.h"
|
|
|
|
#include "libc/calls/execve.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/cosmo.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/dce.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/errno.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/intrin/safemacros.internal.h"
|
2023-09-11 20:51:37 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/limits.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/mem/alloca.h"
|
|
|
|
#include "libc/paths.h"
|
2022-05-25 18:31:08 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-05-25 18:31:08 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/ok.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2023-09-11 20:51:37 +00:00
|
|
|
#define ELIBBAD_LINUX 80
|
|
|
|
#define EBADEXEC_XNU 85
|
|
|
|
#define EBADARCH_XNU 86
|
2022-05-25 18:31:08 +00:00
|
|
|
|
2023-09-11 20:51:37 +00:00
|
|
|
static struct {
|
|
|
|
atomic_uint once;
|
|
|
|
const char *home;
|
|
|
|
const char *tmpdir;
|
|
|
|
} g_execve;
|
2023-02-23 02:58:23 +00:00
|
|
|
|
2023-09-11 20:51:37 +00:00
|
|
|
static bool IsApeFile(const char *path) {
|
|
|
|
if (!endswith(path, ".com")) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
bool res = false;
|
|
|
|
BLOCK_CANCELLATIONS;
|
|
|
|
BEGIN_CANCELLATION_POINT;
|
|
|
|
int fd;
|
|
|
|
char buf[8];
|
|
|
|
int flags = O_RDONLY | O_NOCTTY | O_NONBLOCK | O_CLOEXEC;
|
|
|
|
if ((fd = sys_openat(AT_FDCWD, path, flags, 0)) != -1) {
|
|
|
|
res = sys_pread(fd, buf, 8, 0, 0) == 8 && IsApeLoadable(buf);
|
|
|
|
sys_close(fd);
|
|
|
|
}
|
|
|
|
END_CANCELLATION_POINT;
|
|
|
|
ALLOW_CANCELLATIONS;
|
|
|
|
return res;
|
2022-05-25 18:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *Join(const char *a, const char *b, char buf[PATH_MAX]) {
|
|
|
|
size_t n, m;
|
2023-09-11 20:51:37 +00:00
|
|
|
if (a && *a) {
|
|
|
|
n = strlen(a);
|
|
|
|
m = strlen(b);
|
|
|
|
if (n + m + 1 < PATH_MAX) {
|
|
|
|
stpcpy(stpcpy(buf, a), b);
|
|
|
|
return buf;
|
|
|
|
}
|
2022-05-25 18:31:08 +00:00
|
|
|
}
|
2023-09-11 20:51:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RetryExecve(const char *prog, char **argv, char *const *envp) {
|
|
|
|
if ((argv[0] = (char *)prog)) {
|
|
|
|
STRACE("execve(%#s, %s) due to %s", prog, DescribeStringList(argv),
|
|
|
|
_strerrno(errno));
|
|
|
|
__sys_execve(prog, argv, envp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupExecve(void) {
|
|
|
|
g_execve.home = getenv("HOME");
|
|
|
|
g_execve.tmpdir = getenv("TMPDIR");
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__constructor__)) static void InitExecve(void) {
|
|
|
|
cosmo_once(&g_execve.once, SetupExecve);
|
2022-05-25 18:31:08 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
int sys_execve(const char *prog, char *const argv[], char *const envp[]) {
|
2023-09-11 20:51:37 +00:00
|
|
|
|
|
|
|
// try kernel
|
|
|
|
// this also checks execute bit
|
2021-02-07 14:11:44 +00:00
|
|
|
__sys_execve(prog, argv, envp);
|
2023-09-11 20:51:37 +00:00
|
|
|
if (!(errno == ENOEXEC || (IsLinux() && errno == ELIBBAD_LINUX))) {
|
|
|
|
return -1;
|
2021-02-05 17:44:54 +00:00
|
|
|
}
|
2023-09-11 20:51:37 +00:00
|
|
|
|
|
|
|
// allocate memory
|
|
|
|
int argc;
|
|
|
|
for (argc = 0; argv[argc];) ++argc;
|
|
|
|
char **shargs = alloca((argc + 4) * sizeof(char *));
|
|
|
|
|
|
|
|
// try ape loader
|
|
|
|
if (IsApeFile(prog)) {
|
|
|
|
shargs[1] = (char *)"-";
|
|
|
|
shargs[2] = (char *)prog;
|
|
|
|
memcpy(shargs + 3, argv, (argc + 1) * sizeof(char *));
|
|
|
|
RetryExecve("/usr/bin/ape", shargs, envp);
|
|
|
|
char *buf = alloca(PATH_MAX);
|
|
|
|
const char *name = "/.ape-" APE_VERSION_STR;
|
|
|
|
InitExecve();
|
|
|
|
RetryExecve(Join(g_execve.tmpdir, name, buf), shargs, envp);
|
|
|
|
RetryExecve(Join(g_execve.home, name, buf), shargs, envp);
|
|
|
|
RetryExecve(Join(".", name, buf), shargs, envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// try bourne shell
|
|
|
|
shargs[0] = _PATH_BSHELL;
|
|
|
|
shargs[1] = (char *)prog;
|
|
|
|
memcpy(shargs + 2, argv + 1, argc * sizeof(char *));
|
|
|
|
RetryExecve(shargs[0], shargs, envp);
|
|
|
|
|
|
|
|
enoexec();
|
|
|
|
return -1;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|