Work around Landlock output inode in compile.com

This change fixes Landlock Make so that only the output target file is
unveiled, rather than unveiling the directory that contains it. This
gives us a much stronger sandbox. It also helped identify problematic
build code in our repo that should have been using o/tmp instead.

Landlock isn't able to let us unveil files that don't exist. Even if
they do, then once a file is deleted, the sandboxing for it goes away.
This caused problems for Landlock Make because tools like GNU LD will
repeatedly delete and recreate the output file. This change uses the
compile.com wrapper to ensure on changes happen to the output inode.

New binary available on https://justine.lol/make/

Fixes #528
This commit is contained in:
Justine Tunney 2022-08-09 06:28:41 -07:00
parent c464f45692
commit 133c693650
33 changed files with 207 additions and 1346 deletions

View file

@ -360,18 +360,18 @@ o/cosmopolitan.h: \
libc/integral/normalize.inc \
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)) \
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_INCS))
$(file >$@.args,libc/integral/normalize.inc $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)))
@$(COMPILE) -AROLLUP -T$@ o/$(MODE)/tool/build/rollup.com @$@.args >$@
$(file >$(TMPDIR)/$(subst /,_,$@),libc/integral/normalize.inc $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)))
@$(COMPILE) -AROLLUP -T$@ o/$(MODE)/tool/build/rollup.com @$(TMPDIR)/$(subst /,_,$@) >$@
o/cosmopolitan.html: \
o/$(MODE)/third_party/chibicc/chibicc.com.dbg \
$(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))) \
$(SRCS) \
$(HDRS)
$(file >$@.args,$(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))))
$(file >$(TMPDIR)/$(subst /,_,$@),$(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))))
o/$(MODE)/third_party/chibicc/chibicc.com.dbg -J \
-fno-common -include libc/integral/normalize.inc -o $@ \
@$@.args
@$(TMPDIR)/$(subst /,_,$@)
$(SRCS): \
libc/integral/normalize.inc \

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -65,6 +65,7 @@ ZIPOBJ = build/bootstrap/zipobj.com
FIXUPOBJ = build/bootstrap/fixupobj.com
MKDIR = build/bootstrap/mkdir.com -p
COMPILE = build/bootstrap/compile.com -V9 -P4096 $(QUOTA)
SYMTAB = $(TMPDIR)/$(subst /,_,$@).symtab
COMMA := ,
PWD := $(shell build/bootstrap/pwd.com)

View file

@ -92,16 +92,16 @@ o/$(MODE)/%.o: o/$(MODE)/%.cc
@$(COMPILE) -AFIXUPOBJ -T$@ $(FIXUPOBJ) $@
o/%.a:
$(file >$@.args,$^)
@$(COMPILE) -AARCHIVE -T$@ $(AR) $(ARFLAGS) $@ @$@.args
$(file >$(TMPDIR)/$(subst /,_,$@),$^)
@$(COMPILE) -AARCHIVE -T$@ $(AR) $(ARFLAGS) $@ @$(TMPDIR)/$(subst /,_,$@)
o/%.pkg:
$(file >$@.args,$(filter %.o,$^))
@$(COMPILE) -APACKAGE -T$@ $(PKG) $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) @$@.args
$(file >$(TMPDIR)/$(subst /,_,$@).args,$(filter %.o,$^))
@$(COMPILE) -APACKAGE -T$@ $(PKG) $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) @$(TMPDIR)/$(subst /,_,$@)
o/$(MODE)/%.pkg:
$(file >$@.args,$(filter %.o,$^))
@$(COMPILE) -APACKAGE -T$@ $(PKG) $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) @$@.args
$(file >$(TMPDIR)/$(subst /,_,$@),$(filter %.o,$^))
@$(COMPILE) -APACKAGE -T$@ $(PKG) $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) @$(TMPDIR)/$(subst /,_,$@)
o/$(MODE)/%.o: %.py o/$(MODE)/third_party/python/pyobj.com
@$(COMPILE) -APYOBJ o/$(MODE)/third_party/python/pyobj.com $(PYFLAGS) -o $@ $<

View file

@ -155,16 +155,6 @@ o/$(MODE)/examples/picol.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/examples/nesemu1.com: \
o/$(MODE)/examples/nesemu1.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/examples/.nesemu1/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/examples/.nesemu1/.symtab
o/$(MODE)/examples/nesemu1.o: QUOTA += -M512m
o/$(MODE)/usr/share/dict/words.zip.o: ZIPOBJ_FLAGS += -C2

View file

@ -69,6 +69,7 @@ void *xloadzd(bool *, void **, const void *, size_t, size_t, size_t, size_t,
int rmrf(const char *);
int makedirs(const char *, unsigned);
char *xbasename(const char *) paramsnonnull() _XMAL;
char *xdirname(const char *) paramsnonnull() _XMAL;
char *xjoinpaths(const char *, const char *) paramsnonnull() _XMAL;
char *xreadlink(const char *) paramsnonnull() _XMAL;

28
libc/x/xbasename.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
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.
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.
*/
#include "libc/fmt/conv.h"
#include "libc/runtime/gc.internal.h"
#include "libc/x/x.h"
/**
* Returns base portion of path.
*/
char *xbasename(const char *path) {
return xstrdup(basename(gc(xstrdup(path))));
}

View file

@ -86,16 +86,6 @@ o/$(MODE)/third_party/chibicc/chibicc.com.dbg: \
$(THIRD_PARTY_CHIBICC_A).pkg
@$(APELINK)
o/$(MODE)/third_party/chibicc/chibicc.com: \
o/$(MODE)/third_party/chibicc/chibicc.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/chibicc/.chibicc/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/chibicc/.chibicc/.symtab
o/$(MODE)/third_party/chibicc/as.com.dbg: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC_A) \

View file

@ -250,16 +250,6 @@ o/$(MODE)/third_party/lua/lua.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/dbg/third_party/lua/lua.com: \
o/dbg/third_party/lua/lua.com.dbg \
o/dbg/third_party/zip/zip.com \
o/dbg/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/dbg/tool/build/symtab.com \
-o o/dbg/third_party/lua/.lua/.symtab $<
@$(COMPILE) -AZIP -T$@ o/dbg/third_party/zip/zip.com \
-9qj $@ o/dbg/third_party/lua/.lua/.symtab
################################################################################
# luac.com

View file

@ -47,6 +47,24 @@ this program. If not, see <http://www.gnu.org/licenses/>. */
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/map.h"
#include "libc/intrin/kprintf.h"
#include "libc/sysv/consts/o.h"
#include "libc/calls/struct/timeval.h"
#include "libc/x/x.h"
#include "libc/intrin/kprintf.h"
#include "libc/time/time.h"
#include "libc/calls/calls.h"
#include "libc/intrin/kprintf.h"
#include "libc/assert.h"
#include "libc/sysv/consts/pr.h"
#include "libc/calls/struct/bpf.h"
#include "libc/calls/struct/filter.h"
#include "libc/calls/struct/seccomp.h"
#include "libc/calls/struct/filter.h"
#include "libc/sysv/consts/pr.h"
#include "libc/sysv/consts/pr.h"
#include "libc/sysv/consts/audit.h"
#include "libc/sysv/consts/nrlinux.h"
#include "libc/macros.internal.h"
#include "third_party/make/dep.h"
#define GOTO_SLOW \
@ -1781,14 +1799,34 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
".runit.psk"),
"r");
/* unveil target output directory */
/*
* unveils target output file
*
* landlock operates per inode so it can't whitelist missing
* paths. so we create the output file manually, and prevent
* creation so that it can't be deleted by the command which
* must truncate when writing its output.
*/
if (strlen(c->file->name) < PATH_MAX)
{
const char *dir;
int fd, rc, err = errno;
strcpy (outpathbuf, c->file->name);
dir = dirname (outpathbuf);
makedirs (dir, 0755);
Unveil (dir, "rwc");
if (makedirs (dirname (outpathbuf), 0777) == -1)
errno = err;
fd = open (c->file->name, O_RDWR | O_CREAT, 0777);
if (fd != -1)
{
futimens (fd, (struct timespec[2]){0});
close (fd);
}
else if (errno == EEXIST)
errno = err;
else
OSS (error, NILF, "%s: touch target failed %s",
c->file->name, strerror (errno));
if (unveil (c->file->name, "rwx") && errno != ENOSYS)
OSS (error, NILF, "%s: unveil target failed %s",
c->file->name, strerror (errno));
}
/* unveil target prerequisites */
@ -1838,7 +1876,7 @@ exec_command (char **argv, char **envp)
/* Run the program. */
environ = envp;
execvp (argv[0], argv);
execv (argv[0], argv);
if(errno == ENOENT)
OSS (error, NILF, "%s: %s", argv[0], strerror (errno));

View file

@ -162,16 +162,6 @@ o/$(MODE)/third_party/make/hash.o: \
OVERRIDE_CFLAGS += \
-O3
o/$(MODE)/third_party/make/make.com: \
o/$(MODE)/third_party/make/make.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/make/.make/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/make/.make/.symtab
$(THIRD_PARTY_MAKE_OBJS): \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED \

View file

@ -4317,16 +4317,6 @@ o/$(MODE)/third_party/python/python.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/third_party/python/python.com: \
o/$(MODE)/third_party/python/python.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/python/.python/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/python/.python/.symtab
################################################################################
# FREEZE.COM

View file

@ -148,16 +148,6 @@ o/$(MODE)/third_party/quickjs/qjs.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/third_party/quickjs/qjs.com: \
o/$(MODE)/third_party/quickjs/qjs.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/quickjs/.qjs/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/quickjs/.qjs/.symtab
o/$(MODE)/third_party/quickjs/qjsc.com.dbg: \
$(THIRD_PARTY_QUICKJS) \
o/$(MODE)/third_party/quickjs/qjsc.o \

View file

@ -78,16 +78,6 @@ o/$(MODE)/third_party/sqlite3/sqlite3.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/third_party/sqlite3/sqlite3.com: \
o/$(MODE)/third_party/sqlite3/sqlite3.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/sqlite3/.sqlite3/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/sqlite3/.sqlite3/.symtab
$(THIRD_PARTY_SQLITE3_A): \
third_party/sqlite3/ \
$(THIRD_PARTY_SQLITE3_A).pkg \

View file

@ -54,16 +54,6 @@ o/$(MODE)/third_party/tidy/tidy.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/third_party/tidy/tidy.com: \
o/$(MODE)/third_party/tidy/tidy.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/third_party/tidy/.tidy/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/third_party/tidy/.tidy/.symtab
o/$(MODE)/third_party/tidy/.tidyrc.zip.o: third_party/tidy/.tidyrc
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -B $(OUTPUT_OPTION) $<

File diff suppressed because it is too large Load diff

View file

@ -22,6 +22,10 @@
#include "libc/stdio/temp.h"
#include "libc/time/time.h"
#include "libc/time/struct/tm.h"
#include "libc/fmt/fmt.h"
#include "libc/x/x.h"
#include "libc/x/x.h"
#include "libc/runtime/gc.internal.h"
#include "libc/limits.h"
#ifdef NO_MKTIME
@ -1322,6 +1326,11 @@ int a; /* attributes returned by getfileattr() */
#ifndef VMS /* VMS-specific function is in VMS.C. */
static char *EnsureDirs(char *path) {
makedirs(gc(xdirname(path)), 0755);
return path;
}
char *tempname(zip)
char *zip; /* path name of zip file to generate temp name for */
@ -1329,6 +1338,13 @@ char *tempname(zip)
{
char *t = zip; /* malloc'ed space for name (use zip to avoid warning) */
#ifdef __COSMOPOLITAN__
t = malloc(PATH_MAX);
snprintf(t, PATH_MAX, "%szip.XXXXXX", kTmpPath);
mkstemp(EnsureDirs(t));
return t;
#endif
# ifdef CMS_MVS
if ((t = malloc(strlen(tempath) + L_tmpnam + 2)) == NULL)
return NULL;

11
third_party/zip/zip.c vendored
View file

@ -35,10 +35,17 @@
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/gc.internal.h"
#include "libc/x/x.h"
#include "third_party/bzip2/bzlib.h"
#define MAXCOM 256 /* Maximum one-line comment size */
static char *EnsureDirs(char *path) {
makedirs(gc(xdirname(path)), 0755);
return path;
}
/* Local option flags */
#ifndef DELETE
#define DELETE 0
@ -3828,7 +3835,7 @@ char **argv; /* command line tokens */
}
strcat(tempzip, "ziXXXXXX");
if ((yd = mkstemp(tempzip)) == EOF) {
if ((yd = mkstemp(EnsureDirs(gc(xjoinpaths(kTmpPath, tempzip))))) == EOF) {
ZIPERR(ZE_TEMP, tempzip);
}
if ((y = fdopen(yd, FOPW_TMP)) == NULL) {
@ -4831,7 +4838,7 @@ char **argv; /* command line tokens */
}
strcat(tempzip, "ziXXXXXX");
if ((yd = mkstemp(tempzip)) == EOF) {
if ((yd = mkstemp(EnsureDirs(gc(xjoinpaths(kTmpPath, tempzip))))) == EOF) {
ZIPERR(ZE_TEMP, tempzip);
}
if ((y = fdopen(yd, FOPW_TMP)) == NULL) {

View file

@ -93,6 +93,7 @@ THIRD_PARTY_ZIP_DIRECTDEPS = \
LIBC_SYSV \
LIBC_TIME \
LIBC_UNICODE \
LIBC_X \
THIRD_PARTY_BZIP2
THIRD_PARTY_ZIP_DEPS := \

View file

@ -31,6 +31,7 @@
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/io.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
@ -322,13 +323,9 @@ int main(int argc, char *argv[]) {
goto fail;
}
outsize += (remain = sizes.p[i]);
while ((rc = copy_file_range(fd, 0, outfd, 0, remain, 0)) < remain) {
if (rc <= 0) {
reason = "copy_file_range failed";
goto fail;
} else {
remain -= rc;
}
if (_copyfd(fd, outfd, remain) == -1) {
reason = "copy_file_range failed";
goto fail;
}
close(fd);
}

View file

@ -85,17 +85,6 @@ o/$(MODE)/tool/build/%.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
.PRECIOUS: o/$(MODE)/tool/build/blinkenlights.com
o/$(MODE)/tool/build/blinkenlights.com: \
o/$(MODE)/tool/build/blinkenlights.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/build/.blinkenlights/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/build/.blinkenlights/.symtab
o/$(MODE)/tool/build/emulator.o: \
OVERRIDE_COPTS += \
-fno-sanitize=pointer-overflow

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
@ -38,6 +37,7 @@
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/io.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/nexgen32e/x86feature.h"
@ -171,6 +171,7 @@ char *target;
char *output;
char *outpath;
char *command;
char *movepath;
char *shortened;
char *colorflag;
char ccpath[PATH_MAX];
@ -188,6 +189,7 @@ struct timespec signalled;
sigset_t mask;
sigset_t savemask;
char buf[PAGESIZE];
char tmpout[PATH_MAX];
const char *const kSafeEnv[] = {
"ADDR2LINE", // needed by GetAddr2linePath
@ -707,6 +709,24 @@ void ReportResources(void) {
appendw(&output, '\n');
}
bool MovePreservingDestinationInode(const char *from, const char *to) {
bool res;
struct stat st;
int fdin, fdout;
if ((fdin = open(from, O_RDONLY)) == -1) {
return false;
}
fstat(fdin, &st);
if ((fdout = creat(to, st.st_mode)) == -1) {
close(fdin);
return false;
}
res = _copyfd(fdin, fdout, -1) != -1;
close(fdin);
close(fdout);
return res;
}
bool IsNativeExecutable(const char *path) {
bool res;
char buf[4];
@ -737,6 +757,7 @@ int main(int argc, char *argv[]) {
char *s, *p, *q, **envp;
mode = firstnonnull(getenv("MODE"), MODE);
ksnprintf(tmpout, sizeof(tmpout), "%scompile.%d", kTmpPath, getpid());
/*
* parse prefix arguments
@ -835,18 +856,26 @@ int main(int argc, char *argv[]) {
* ingest arguments
*/
for (i = optind; i < argc; ++i) {
if (!movepath && !outpath && target && !strcmp(target, argv[i])) {
outpath = argv[i];
AddArg(tmpout);
movepath = target;
MovePreservingDestinationInode(target, tmpout);
continue;
}
if (argv[i][0] != '-') {
AddArg(argv[i]);
continue;
}
if (startswith(argv[i], "-o")) {
if (!strcmp(argv[i], "-o")) {
AddArg(argv[i]);
AddArg((outpath = argv[++i]));
outpath = argv[++i];
} else {
AddArg(argv[i]);
outpath = argv[i] + 2;
}
AddArg("-o");
AddArg(tmpout);
movepath = outpath;
continue;
}
if (!iscc) {
@ -1091,6 +1120,19 @@ int main(int argc, char *argv[]) {
makedirs(xdirname(target), 0755);
touch(target, 0644);
}
if (movepath) {
if (!MovePreservingDestinationInode(tmpout, movepath)) {
unlink(tmpout);
exitcode = 90;
appends(&output, "\nfailed to move output file\n");
appends(&output, tmpout);
appends(&output, "\n");
appends(&output, movepath);
appends(&output, "\n");
} else {
unlink(tmpout);
}
}
} else {
appendw(&output, '\n');
PrintRed();

View file

@ -23,6 +23,7 @@
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/kprintf.h"
#include "libc/mem/io.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
@ -30,6 +31,7 @@
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/ex.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/s.h"
#include "libc/x/x.h"
@ -162,6 +164,24 @@ char *Join(const char *a, const char *b) {
return dstfile;
}
bool MovePreservingDestinationInode(const char *from, const char *to) {
bool res;
struct stat st;
int fdin, fdout;
if ((fdin = open(from, O_RDONLY)) == -1) {
return false;
}
fstat(fdin, &st);
if ((fdout = creat(to, st.st_mode)) == -1) {
close(fdin);
return false;
}
res = _copyfd(fdin, fdout, -1) != -1;
close(fdin);
close(fdout);
return res;
}
void Cp(char *src, char *dst) {
ssize_t rc;
const char *s;
@ -207,7 +227,7 @@ void Cp(char *src, char *dst) {
linkbuf[rc] = 0;
if (symlink(linkbuf, dst) == -1) goto OnFail;
} else {
if (copyfile(src, dst, flags) == -1) goto OnFail;
if (!MovePreservingDestinationInode(src, dst)) goto OnFail;
}
return;
OnFail:

View file

@ -62,9 +62,9 @@ static bool IsOverlappingIov(struct iovec *a, struct iovec *b) {
*/
void PersistObject(const char *path, size_t align,
const struct ObjectParam *obj) {
const char *pad;
struct iovec *iov;
int i, n, fd, iovlen;
const char *tmp, *pad;
long len, size, bytes, filesize;
unsigned char *hdr, *p1, *p2, **pp;
intptr_t arrayptroffset, arraydataoffset;
@ -79,7 +79,6 @@ void PersistObject(const char *path, size_t align,
pad = gc(xcalloc(align, 1));
hdr = gc(xmalloc(obj->size));
iov = gc(xcalloc(iovlen, sizeof(*iov)));
tmp = gc(xasprintf("%s.%d.%s", path, getpid(), "tmp"));
bytes = obj->size;
iov[0].iov_base = memcpy(hdr, obj->p, obj->size);
iov[0].iov_len = bytes;
@ -115,8 +114,7 @@ void PersistObject(const char *path, size_t align,
iov[(i + 0) * 2].iov_base, iov[(i + 0) * 2].iov_len, (i + 1) * 2,
iov[(i + 1) * 2].iov_base, iov[(i + 1) * 2].iov_len, path);
}
CHECK_NE(-1, (fd = open(tmp, O_CREAT | O_WRONLY | O_EXCL, 0644)), "%s", tmp);
CHECK_NE(-1, (fd = creat(path, 0644)), "%s", path);
CHECK_EQ(filesize, writev(fd, iov, iovlen));
CHECK_NE(-1, close(fd));
CHECK_NE(-1, rename(tmp, path), "%s", path);
}

View file

@ -425,7 +425,6 @@ void Explore(void) {
int main(int argc, char *argv[]) {
int i, fd;
char path[PATH_MAX];
ShowCrashReports();
if (argc == 2 && !strcmp(argv[1], "-n")) exit(0);
GetOpts(argc, argv);
@ -435,14 +434,12 @@ int main(int argc, char *argv[]) {
LoadRelationships(argc, argv);
Crunch();
Explore();
ksnprintf(path, sizeof(path), "%s.%d", out, getpid());
CHECK_NE(-1, (fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)),
"open(%#s)", path);
CHECK_NE(-1, (fd = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0644)),
"open(%#s)", out);
for (i = 0; i < threads; ++i) {
CHECK_NE(-1, xwrite(fd, bouts[i], appendz(bouts[i]).i));
}
CHECK_NE(-1, close(fd));
CHECK_NE(-1, rename(path, out));
for (i = 0; i < threads; ++i) {
free(bouts[i]);
}

View file

@ -141,9 +141,6 @@ struct Package *LoadPackage(const char *path) {
pkg->strings.p = (char *)((intptr_t)pkg->strings.p + (intptr_t)pkg);
pkg->objects.p = (struct Object *)((intptr_t)pkg->objects.p + (intptr_t)pkg);
pkg->symbols.p = (struct Symbol *)((intptr_t)pkg->symbols.p + (intptr_t)pkg);
CHECK(strcmp(path, &pkg->strings.p[pkg->path]) == 0,
"corrupt package: %`'s pkg=%012lx strings=%012lx", path, pkg,
pkg->strings.p);
pkg->addr = pkg;
pkg->size = st.st_size;
CHECK_NE(-1, mprotect(pkg, st.st_size, PROT_READ));

View file

@ -104,65 +104,20 @@ o/$(MODE)/tool/net/redbean.com.dbg: \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
o/$(MODE)/tool/net/net.pkg \
o/$(MODE)/tool/net/help.txt.zip.o \
o/$(MODE)/tool/net/.init.lua.zip.o \
o/$(MODE)/tool/net/favicon.ico.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
ifneq ($(MODE),tiny)
ifneq ($(MODE),tinylinux)
o/$(MODE)/tool/net/redbean.com: \
o/$(MODE)/tool/net/redbean.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/net.mk \
tool/net/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/net/.redbean
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean/.symtab \
tool/net/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
endif
endif
o/tiny/tool/net/redbean.com: \
o/tiny/tool/net/redbean.com.dbg \
o/tiny/third_party/zip/zip.com \
tool/net/net.mk \
tool/net/tiny/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/tiny/tool/net/.redbean
@$(COMPILE) -AZIP -T$@ o/tiny/third_party/zip/zip.com -9qj $@ \
tool/net/tiny/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
o/tinylinux/tool/net/redbean.com: \
o/tinylinux/tool/net/redbean.com.dbg \
o/tinylinux/third_party/zip/zip.com \
tool/net/net.mk \
tool/net/tiny/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/tinylinux/tool/net/.redbean
@$(COMPILE) -AZIP -T$@ o/tinylinux/third_party/zip/zip.com -9qj $@ \
tool/net/tiny/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com -o $(SYMTAB) $<
@$(COMPILE) -AZIP -T$@ $@ -A $(SYMTAB)
# REDBEAN-DEMO.COM
#
@ -170,6 +125,7 @@ o/tinylinux/tool/net/redbean.com: \
# bundles a bunch of example code and there's a live of it available
# online at http://redbean.justine.lol/
o/$(MODE)/tool/net/.init.lua.zip.o \
o/$(MODE)/tool/net/demo/.init.lua.zip.o \
o/$(MODE)/tool/net/demo/.reload.lua.zip.o \
o/$(MODE)/tool/net/demo/sql.lua.zip.o \
@ -196,6 +152,7 @@ o/$(MODE)/tool/net/demo/redbean-form.lua.zip.o \
o/$(MODE)/tool/net/demo/redbean-xhr.lua.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
o/$(MODE)/tool/net/favicon.ico.zip.o \
o/$(MODE)/tool/net/help.txt.zip.o \
o/$(MODE)/tool/net/demo/404.html.zip.o: \
ZIPOBJ_FLAGS += \
-B
@ -263,49 +220,30 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/demo/.lua/mymodule.lua.zip.o \
o/$(MODE)/tool/net/demo/.reload.lua.zip.o \
o/$(MODE)/tool/net/demo/.init.lua.zip.o \
o/$(MODE)/tool/net/help.txt.zip.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean-demo.com.dbg \
o/$(MODE)/tool/build/symtab.com \
o/$(MODE)/third_party/zip/zip.com \
tool/net/help.txt
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/net/.redbean-demo
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-demo/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-demo/.symtab \
tool/net/help.txt
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com -o $(SYMTAB) $<
@$(COMPILE) -AZIP -T$@ $@ -A $(SYMTAB)
# REDBEAN-STATIC.COM
#
# Passing the -DSTATIC causes Lua and SQLite to be removed. This reduces
# the binary size from roughly 1500 kb to 500 kb. It still supports SSL.
o/$(MODE)/tool/net/redbean-static.com: \
o/$(MODE)/tool/net/redbean-static.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/net/.redbean-static
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-static/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-static/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-static.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean-static.o \
o/$(MODE)/tool/net/net.pkg \
o/$(MODE)/tool/net/favicon.ico.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
o/$(MODE)/tool/net/help.txt.zip.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
@ -316,23 +254,6 @@ o/$(MODE)/tool/net/redbean-static.com.dbg: \
# That doesn't mean redbean becomes insecure. It just reduces complexity
# in situations where you'd rather have SSL be handled in an edge proxy.
o/$(MODE)/tool/net/redbean-unsecure.com: \
o/$(MODE)/tool/net/redbean-unsecure.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/net/.redbean-unsecure
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-unsecure/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-unsecure/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean-unsecure.o \
@ -345,6 +266,9 @@ o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
o/$(MODE)/tool/net/net.pkg \
o/$(MODE)/tool/net/favicon.ico.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
o/$(MODE)/tool/net/help.txt.zip.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
@ -355,57 +279,13 @@ o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
# produce 200kb binary that's very similar to redbean as it existed on
# Hacker News the day it went viral.
ifneq ($(MODE),tiny)
ifneq ($(MODE),tinylinux)
o/$(MODE)/tool/net/redbean-original.com: \
o/$(MODE)/tool/net/redbean-original.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/net/.redbean-original
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-original/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-original/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
endif
endif
o/tiny/tool/net/redbean-original.com: \
o/tiny/tool/net/redbean-original.com.dbg \
o/tiny/third_party/zip/zip.com \
tool/net/tiny/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/tiny/tool/net/.redbean-original
@$(COMPILE) -AZIP -T$@ o/tiny/third_party/zip/zip.com -9qj $@ \
tool/net/tiny/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/tinylinux/tool/net/redbean-original.com: \
o/tinylinux/tool/net/redbean-original.com.dbg \
o/tinylinux/third_party/zip/zip.com \
tool/net/tiny/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/tinylinux/tool/net/.redbean-original
@$(COMPILE) -AZIP -T$@ o/tinylinux/third_party/zip/zip.com -9qj $@ \
tool/net/tiny/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-original.com.dbg: \
$(TOOL_NET_DEPS) \
o/$(MODE)/tool/net/redbean-original.o \
o/$(MODE)/tool/net/net.pkg \
o/$(MODE)/tool/net/favicon.ico.zip.o \
o/$(MODE)/tool/net/redbean.png.zip.o \
o/$(MODE)/tool/net/help.txt.zip.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)

View file

@ -46,19 +46,6 @@ o/$(MODE)/tool/plinko/%.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
.PRECIOUS: o/$(MODE)/tool/plinko/plinko.com
o/$(MODE)/tool/plinko/plinko.com: \
o/$(MODE)/tool/plinko/plinko.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/plinko/plinko.mk
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ $(MKDIR) o/$(MODE)/tool/plinko/.redbean
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/plinko/.plinko/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/plinko/.plinko/.symtab
$(TOOL_PLINKO_OBJS): \
$(BUILD_FILES) \
tool/plinko/plinko.mk

View file

@ -82,26 +82,6 @@ o/$(MODE)/tool/viz/printimage.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/tool/viz/printimage.com: \
o/$(MODE)/tool/viz/printimage.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/viz/.printimage/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/viz/.printimage/.symtab
o/$(MODE)/tool/viz/printvideo.com: \
o/$(MODE)/tool/viz/printvideo.com.dbg \
o/$(MODE)/third_party/zip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/viz/.printvideo/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
o/$(MODE)/tool/viz/.printvideo/.symtab
o/$(MODE)/tool/viz/derasterize.o: \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED \