Implement raw system call for redbean lua code

You can now call functions like fork() from Lua and it'll work across
all supported platforms, including Windows. This gives you a level of
control of the system that Lua traditionally hasn't been able to have
due to its focus on old portable stdio rather modern POSIX APIs. Demo
code has been added to redbean-demo.com to show how it works.

This change also modifies Lua so that integer literals with a leading
zero will be interpreted as octal. That should help avoid shooting in
the foot with POSIX APIs that frequently use octal mode bits.

This change fixes a bug in opendir(".") on New Technology.

Lastly, redbean will now serve crash reports to private network IPs.
This is consistent with other frameworks. However that isn't served
to public IPs unless the -E flag is passed to redbean at startup.
This commit is contained in:
Justine Tunney 2022-04-13 08:49:17 -07:00
parent f684e348d4
commit 281a0f2730
39 changed files with 2044 additions and 84 deletions

View file

@ -229,7 +229,7 @@ uint32_t getgid(void) nosideeffect;
uint32_t getpgrp(void) nosideeffect;
uint32_t getsid(int) nosideeffect;
uint32_t getuid(void) nosideeffect;
uint32_t umask(int32_t);
uint32_t umask(uint32_t);
void rewinddir(DIR *);
void sync(void);
int getloadavg(double *, int);

View file

@ -32,7 +32,7 @@
* @param oldfd isn't closed afterwards
* @param newfd if already assigned, is silently closed beforehand;
* unless it's equal to oldfd, in which case dup2() is a no-op
* @flags can have O_CLOEXEC
* @param flags can have O_CLOEXEC
* @see dup(), dup2()
*/
int dup3(int oldfd, int newfd, int flags) {

View file

@ -17,19 +17,23 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
/**
* Returns parent process id.
* @asyncsignalsafe
*/
int getppid(void) {
int rc;
if (!IsWindows()) {
if (!IsNetbsd()) {
return sys_getppid();
rc = sys_getppid();
} else {
return sys_getpid().dx;
rc = sys_getpid().dx;
}
} else {
return sys_getppid_nt();
rc = sys_getppid_nt();
}
STRACE("getppid() → %d% m", rc);
return rc;
}

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
/**
* Returns nice value of thing.
@ -28,9 +29,12 @@
* @see setpriority(), nice()
*/
int getpriority(int which, unsigned who) {
int rc;
if (!IsWindows()) {
return sys_getpriority(which, who) - 20;
rc = sys_getpriority(which, who) - 20;
} else {
return sys_getsetpriority_nt(which, who, 0, sys_getpriority_nt);
rc = sys_getsetpriority_nt(which, who, 0, sys_getpriority_nt);
}
STRACE("getpriority(%d, %u) → %d% m", which, who, rc);
return rc;
}

View file

@ -220,6 +220,7 @@ u32 sys_getgid(void) hidden;
u32 sys_getsid(int) hidden;
u32 sys_gettid(void) hidden;
u32 sys_getuid(void) hidden;
u32 sys_umask(u32) hidden;
void *__sys_mmap(void *, u64, u32, u32, i64, i64, i64) hidden;
void *sys_mremap(void *, u64, u64, i32, void *) hidden;
void sys_exit(int) hidden;

View file

@ -17,10 +17,14 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
/**
* Sets effective group ID.
*/
int setegid(unsigned gid) {
return setregid(-1, gid);
int setegid(unsigned egid) {
int rc;
rc = setregid(-1, egid);
STRACE("%s(%u) → %d% m", "setegid", egid, rc);
return rc;
}

View file

@ -17,10 +17,14 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
/**
* Sets effective user ID.
*/
int seteuid(unsigned euid) {
return setreuid(-1, euid);
int rc;
rc = setreuid(-1, euid);
STRACE("%s(%u) → %d% m", "seteuid", euid, rc);
return rc;
}

View file

@ -18,10 +18,15 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
/**
* Creates session and sets the process group id.
* @return new session id, or -1 w/ errno
*/
int setsid(void) {
return sys_setsid();
int rc;
rc = sys_setsid();
STRACE("setsid() → %d% m", rc);
return rc;
}

40
libc/calls/umask.c Normal file
View file

@ -0,0 +1,40 @@
/*-*- 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/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
/**
* Sets file mode creation mask.
*
* @return previous mask
* @note always succeeds
*/
unsigned umask(unsigned newmask) {
unsigned oldmask;
if (!IsWindows()) {
oldmask = sys_umask(newmask);
} else {
// TODO(jart): what should we do with this?
oldmask = newmask;
}
STRACE("umask(%#o) → %#o", oldmask);
return oldmask;
}