2020-06-15 07:18:57 -07: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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-27 17:18:44 -08: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 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08: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 07:18:57 -07:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-08-25 04:23:25 -07:00
|
|
|
#include "libc/calls/calls.h"
|
2022-11-06 15:29:47 +08:00
|
|
|
#include "libc/calls/metalfile.internal.h"
|
2023-08-16 07:54:40 -07:00
|
|
|
#include "libc/calls/struct/stat.h"
|
2023-03-29 21:16:46 -04:00
|
|
|
#include "libc/fmt/conv.h"
|
2022-06-11 01:59:26 -07:00
|
|
|
#include "libc/intrin/cmpxchg.h"
|
2023-08-15 18:24:53 -07:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-07-23 06:47:01 -07:00
|
|
|
#include "libc/intrin/promises.internal.h"
|
2022-10-03 08:17:37 -07:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2021-10-08 08:11:51 -07:00
|
|
|
#include "libc/macros.internal.h"
|
2020-08-25 04:23:25 -07:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-15 18:24:53 -07:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
2023-08-16 07:54:40 -07:00
|
|
|
#include "libc/sysv/consts/f.h"
|
2020-08-25 04:23:25 -07:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2022-10-03 08:17:37 -07:00
|
|
|
#include "libc/thread/thread.h"
|
2023-06-09 18:02:06 -07:00
|
|
|
#include "libc/zip.internal.h"
|
2020-06-15 07:18:57 -07:00
|
|
|
|
2023-05-10 01:10:28 -07:00
|
|
|
#ifdef __x86_64__
|
2023-07-26 13:54:49 -07:00
|
|
|
__static_yoink(APE_COM_NAME);
|
2023-05-10 01:10:28 -07:00
|
|
|
#endif
|
2022-11-06 15:29:47 +08:00
|
|
|
|
2021-10-08 08:11:51 -07:00
|
|
|
static uint64_t __zipos_get_min_offset(const uint8_t *base,
|
|
|
|
const uint8_t *cdir) {
|
|
|
|
uint64_t i, n, c, r, o;
|
|
|
|
c = GetZipCdirOffset(cdir);
|
|
|
|
n = GetZipCdirRecords(cdir);
|
|
|
|
for (r = c, i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(base + c)) {
|
|
|
|
o = GetZipCfileOffset(base + c);
|
|
|
|
if (o < r) r = o;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __zipos_munmap_unneeded(const uint8_t *base, const uint8_t *cdir,
|
|
|
|
const uint8_t *map) {
|
|
|
|
uint64_t n;
|
|
|
|
n = __zipos_get_min_offset(base, cdir);
|
|
|
|
n += base - map;
|
|
|
|
n = ROUNDDOWN(n, FRAMESIZE);
|
|
|
|
if (n) munmap(map, n);
|
|
|
|
}
|
|
|
|
|
2020-06-15 07:18:57 -07:00
|
|
|
/**
|
2020-08-25 04:23:25 -07:00
|
|
|
* Returns pointer to zip central directory of current executable.
|
2022-06-15 16:19:50 -07:00
|
|
|
* @asyncsignalsafe
|
2022-04-20 09:56:53 -07:00
|
|
|
* @threadsafe
|
2020-06-15 07:18:57 -07:00
|
|
|
*/
|
2020-08-25 04:23:25 -07:00
|
|
|
struct Zipos *__zipos_get(void) {
|
2023-06-16 20:05:24 -07:00
|
|
|
char *endptr;
|
2023-08-16 07:54:40 -07:00
|
|
|
struct stat st;
|
2022-04-20 09:56:53 -07:00
|
|
|
static bool once;
|
|
|
|
struct Zipos *res;
|
2023-08-16 07:54:40 -07:00
|
|
|
int x, fd, err, msg;
|
2023-06-17 04:20:16 -07:00
|
|
|
uint8_t *map, *cdir;
|
2022-04-20 09:56:53 -07:00
|
|
|
const char *progpath;
|
|
|
|
static struct Zipos zipos;
|
2023-06-16 20:05:24 -07:00
|
|
|
__zipos_lock();
|
|
|
|
if (!once) {
|
2023-08-16 07:54:40 -07:00
|
|
|
// this environment variable may be a filename or file descriptor
|
|
|
|
if ((progpath = getenv("COSMOPOLITAN_INIT_ZIPOS")) &&
|
|
|
|
(x = strtol(progpath, &endptr, 10)) >= 0 && !*endptr) {
|
|
|
|
fd = x;
|
2023-06-16 20:05:24 -07:00
|
|
|
} else {
|
|
|
|
fd = -1;
|
2023-03-29 21:16:46 -04:00
|
|
|
}
|
2023-06-16 20:05:24 -07:00
|
|
|
if (fd != -1 || PLEDGED(RPATH)) {
|
|
|
|
if (fd == -1) {
|
2023-08-15 18:24:53 -07:00
|
|
|
if (!progpath) {
|
|
|
|
progpath = GetProgramExecutableName();
|
|
|
|
}
|
2023-06-16 20:05:24 -07:00
|
|
|
fd = open(progpath, O_RDONLY);
|
|
|
|
}
|
|
|
|
if (fd != -1) {
|
2023-08-16 07:54:40 -07:00
|
|
|
if (!fstat(fd, &st) && (map = mmap(0, st.st_size, PROT_READ,
|
|
|
|
MAP_PRIVATE, fd, 0)) != MAP_FAILED) {
|
|
|
|
if ((cdir = GetZipEocd(map, st.st_size, &err))) {
|
2023-06-17 04:20:16 -07:00
|
|
|
__zipos_munmap_unneeded(map, cdir, map);
|
|
|
|
zipos.map = map;
|
2023-06-16 20:05:24 -07:00
|
|
|
zipos.cdir = cdir;
|
2023-08-16 07:54:40 -07:00
|
|
|
zipos.dev = st.st_ino;
|
2023-06-16 20:05:24 -07:00
|
|
|
msg = kZipOk;
|
|
|
|
} else {
|
2023-08-16 07:54:40 -07:00
|
|
|
munmap(map, st.st_size);
|
2023-06-16 20:05:24 -07:00
|
|
|
msg = !cdir ? err : kZipErrorRaceCondition;
|
|
|
|
}
|
2020-11-28 12:01:51 -08:00
|
|
|
} else {
|
2023-06-16 20:05:24 -07:00
|
|
|
msg = kZipErrorMapFailed;
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|
2023-06-16 20:05:24 -07:00
|
|
|
close(fd);
|
2022-06-15 16:19:50 -07:00
|
|
|
} else {
|
2023-06-16 20:05:24 -07:00
|
|
|
msg = kZipErrorOpenFailed;
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|
2022-03-23 06:31:55 -07:00
|
|
|
} else {
|
2023-06-16 20:05:24 -07:00
|
|
|
msg = -666;
|
2022-04-20 09:56:53 -07:00
|
|
|
}
|
2023-06-08 23:44:03 -07:00
|
|
|
STRACE("__zipos_get(%#s) → %d% m", progpath, msg);
|
2023-06-16 20:05:24 -07:00
|
|
|
once = true;
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|
2023-06-16 20:05:24 -07:00
|
|
|
__zipos_unlock();
|
2022-04-20 09:56:53 -07:00
|
|
|
if (zipos.cdir) {
|
|
|
|
res = &zipos;
|
|
|
|
} else {
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
return res;
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|