Fix Redbean when file is read-only (#365)

This commit is contained in:
Paul Kulchenko 2022-03-14 17:13:28 -07:00 committed by GitHub
parent 22409b2b5e
commit 38112aeb20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View file

@ -41,6 +41,7 @@ OpenExecutable:
pushq MAP_PRIVATE(%rip) # -0x30(%rbp)
pushq MAP_FIXED(%rip) # -0x38(%rbp)
pushq __NR_mprotect(%rip) # -0x40(%rbp)
pushq O_RDONLY(%rip) # -0x48(%rbp)
push %rbx # code buffer
push %r12 # data buffer
push %r14 # filename
@ -120,8 +121,20 @@ OpenExecutable:
mov -0x08(%rbp),%eax # __NR_open
mov %r14,%rdi
mov -0x20(%rbp),%esi # O_RDWR
clc # clear carry flag
syscall
mov %eax,%r15d
jc .Lohno # bsd error
cmp $-4095,%eax
jae .Lohno # linux error
jmp .Lok
// Open executable in read-only mode.
.Lohno: mov -0x08(%rbp),%eax # __NR_open
mov %r14,%rdi
mov -0x48(%rbp),%esi # O_RDONLY
syscall
.Lok: mov %eax,%r15d
// Map code segment.
mov -0x10(%rbp),%eax # __NR_mmap

View file

@ -6884,8 +6884,8 @@ static void RestoreApe(void) {
if (endswith(zpath, ".com.dbg")) return;
if ((a = GetAssetZip("/.ape", 5)) && (p = LoadAsset(a, &n))) {
close(zfd);
zfd = OpenExecutable();
write(zfd, p, n);
if ((zfd = OpenExecutable()) == -1 || write(zfd, p, n) == -1)
WARNF("(srvr) can't restore .ape");
free(p);
} else {
WARNF("(srvr) /.ape not found");