Introduce COSMOPOLITAN_DISABLE_ZIPOS environ var

This commit is contained in:
Justine Tunney 2023-08-16 21:04:16 -07:00
parent 8d1c81ac9f
commit 04bd488da7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 41 additions and 33 deletions

View file

@ -65,6 +65,7 @@ static void __zipos_munmap_unneeded(const uint8_t *base, const uint8_t *cdir,
*/ */
struct Zipos *__zipos_get(void) { struct Zipos *__zipos_get(void) {
char *endptr; char *endptr;
const char *s;
struct stat st; struct stat st;
static bool once; static bool once;
struct Zipos *res; struct Zipos *res;
@ -74,42 +75,48 @@ struct Zipos *__zipos_get(void) {
static struct Zipos zipos; static struct Zipos zipos;
__zipos_lock(); __zipos_lock();
if (!once) { if (!once) {
// this environment variable may be a filename or file descriptor if (!(s = getenv("COSMOPOLITAN_DISABLE_ZIPOS"))) {
if ((progpath = getenv("COSMOPOLITAN_INIT_ZIPOS")) && // this environment variable may be a filename or file descriptor
(x = strtol(progpath, &endptr, 10)) >= 0 && !*endptr) { if ((progpath = getenv("COSMOPOLITAN_INIT_ZIPOS")) &&
fd = x; (x = strtol(progpath, &endptr, 10)) >= 0 && !*endptr) {
} else { fd = x;
fd = -1;
}
if (fd != -1 || PLEDGED(RPATH)) {
if (fd == -1) {
if (!progpath) {
progpath = GetProgramExecutableName();
}
fd = open(progpath, O_RDONLY);
}
if (fd != -1) {
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))) {
__zipos_munmap_unneeded(map, cdir, map);
zipos.map = map;
zipos.cdir = cdir;
zipos.dev = st.st_ino;
msg = kZipOk;
} else {
munmap(map, st.st_size);
msg = !cdir ? err : kZipErrorRaceCondition;
}
} else {
msg = kZipErrorMapFailed;
}
close(fd);
} else { } else {
msg = kZipErrorOpenFailed; fd = -1;
}
if (fd != -1 || PLEDGED(RPATH)) {
if (fd == -1) {
if (!progpath) {
progpath = GetProgramExecutableName();
}
fd = open(progpath, O_RDONLY);
}
if (fd != -1) {
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))) {
__zipos_munmap_unneeded(map, cdir, map);
zipos.map = map;
zipos.cdir = cdir;
zipos.dev = st.st_ino;
msg = kZipOk;
} else {
munmap(map, st.st_size);
msg = !cdir ? err : kZipErrorRaceCondition;
}
} else {
msg = kZipErrorMapFailed;
}
close(fd);
} else {
msg = kZipErrorOpenFailed;
}
} else {
msg = -666;
} }
} else { } else {
msg = -666; progpath = 0;
msg = -777;
} }
STRACE("__zipos_get(%#s) → %d% m", progpath, msg); STRACE("__zipos_get(%#s) → %d% m", progpath, msg);
once = true; once = true;

View file

@ -29,6 +29,7 @@ ssize_t __zipos_parseuri(const char *uri, struct ZiposUri *out) {
uri[2] == 'i' && // uri[2] == 'i' && //
uri[3] == 'p' && // uri[3] == 'p' && //
(!uri[4] || uri[4] == '/')) && (!uri[4] || uri[4] == '/')) &&
__zipos_get() &&
(len = __zipos_normpath(out->path, uri + 4 + !!uri[4], (len = __zipos_normpath(out->path, uri + 4 + !!uri[4],
sizeof(out->path))) < sizeof(out->path)) { sizeof(out->path))) < sizeof(out->path)) {
return (out->len = len); return (out->len = len);