Fix some bugs

This commit is contained in:
Justine Tunney 2022-08-14 13:28:07 -07:00
parent 5584f6adcf
commit 6c0bbfac4a
15 changed files with 289 additions and 136 deletions

View file

@ -1,17 +1,17 @@
#include "libc/assert.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/sysconf.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
@ -342,8 +342,8 @@ static int sys_trim(mstate m, size_t pad) {
size_t newsize = sp->size - extra;
(void)newsize; /* placate people compiling -Wunused-variable */
/* Prefer mremap, fall back to munmap */
if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) ||
(CALL_MUNMAP(sp->base + newsize, extra) == 0)) {
if (CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL ||
(!extra || !CALL_MUNMAP(sp->base + newsize, extra))) {
released = extra;
}
}

View file

@ -948,6 +948,14 @@ static int LuaUnixOpen(lua_State *L) {
luaL_optinteger(L, 2, O_RDONLY), luaL_optinteger(L, 3, 0)));
}
// unix.tmpfd()
// ├─→ fd:int
// └─→ nil, unix.Errno
static int LuaUnixTmpfd(lua_State *L) {
int olderr = errno;
return SysretInteger(L, "tmpfd", olderr, tmpfd());
}
// unix.close(fd:int)
// ├─→ true
// └─→ nil, unix.Errno
@ -2640,6 +2648,7 @@ static const luaL_Reg kLuaUnix[] = {
{"sync", LuaUnixSync}, // flushes files and disks
{"syslog", LuaUnixSyslog}, // logs to system log
{"tiocgwinsz", LuaUnixTiocgwinsz}, // pseudoteletypewriter dimensions
{"tmpfd", LuaUnixTmpfd}, // create anonymous file
{"truncate", LuaUnixTruncate}, // shrink or extend file medium
{"umask", LuaUnixUmask}, // set default file mask
{"unlink", LuaUnixUnlink}, // remove file

View file

@ -1753,7 +1753,7 @@ unveil_variable (const struct variable *var)
char *val, *tok, *state, *start;
if (!var) return 0;
start = val = xstrdup (variable_expand (var->value));
while (tok = strtok_r (start, " \t\r\n", &state))
while ((tok = strtok_r (start, " \t\r\n", &state)))
{
RETURN_ON_ERROR (Unveil (tok, "r"));
start = 0;
@ -1808,15 +1808,6 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
if (stack_limit.rlim_cur)
setrlimit (RLIMIT_STACK, &stack_limit);
/* For any redirected FD, dup2() it to the standard FD.
They are all marked close-on-exec already. */
if (fdin >= 0 && fdin != FD_STDIN)
EINTRLOOP (r, dup2 (fdin, FD_STDIN));
if (fdout != FD_STDOUT)
EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
if (fderr != FD_STDERR)
EINTRLOOP (r, dup2 (fderr, FD_STDERR));
g_strict = Vartoi (lookup_variable (STRING_SIZE_TUPLE (".STRICT")));
intptr_t loc = (intptr_t)child; /* we can cast if it's on the heap ;_; */
@ -1846,7 +1837,7 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
{
OSS (error, NILF, "%s: command not found on $PATH: %s",
argv[0], strerror (errno));
return -1;
_Exit (127);
}
}
@ -1955,7 +1946,7 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
{
OSS (error, NILF, "%s: touch target failed %s",
c->file->name, strerror (errno));
return -1;
_Exit (127);
}
DB (DB_JOBS, (_("Unveiling %s with permissions %s\n"),
c->file->name, "rwx"));
@ -1963,7 +1954,7 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
{
OSS (error, NILF, "%s: unveil target failed %s",
c->file->name, strerror (errno));
return -1;
_Exit (127);
}
}
@ -2001,16 +1992,20 @@ child_execute_job (struct childbase *child, int good_stdin, char **argv)
}
}
/* For any redirected FD, dup2() it to the standard FD.
They are all marked close-on-exec already. */
if (fdin >= 0 && fdin != FD_STDIN)
EINTRLOOP (r, dup2 (fdin, FD_STDIN));
if (fdout != FD_STDOUT)
EINTRLOOP (r, dup2 (fdout, FD_STDOUT));
if (fderr != FD_STDERR)
EINTRLOOP (r, dup2 (fderr, FD_STDERR));
/* Run the command. */
exec_command (argv, child->environment);
if (pid < 0)
OSS (error, NILF, "%s: exec_command failed: %s",
argv[0], strerror (r));
return pid;
OnError:
return -1;
_Exit (127);
}
@ -2029,7 +2024,7 @@ exec_command (char **argv, char **envp)
if(errno == ENOENT)
OSS (error, NILF, "%s: command doesn't exist: %s",
argv[0], strerror (errno));
else if(!g_strict && errno == ENOEXEC)
else if(errno == ENOEXEC)
{
/* The file was not a program. Try it as a shell script. */
const char *shell;
@ -2063,7 +2058,7 @@ exec_command (char **argv, char **envp)
OSS (error, NILF, "%s: execv failed: %s",
argv[0], strerror (errno));
_exit (127);
_Exit (127);
}

View file

@ -2082,6 +2082,20 @@ o/$(MODE)/third_party/python/pythontester.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
# if these files exist, then python will try to open them
o/$(MODE)/third_party/python/Lib/test/test_robotparser.py.runs \
o/$(MODE)/third_party/python/Lib/test/test_wsgiref.py.runs: private \
.UNVEIL += \
/etc/mime.types \
/etc/httpd/mime.types \
/etc/httpd/conf/mime.types \
/etc/apache/mime.types \
/etc/apache2/mime.types \
/usr/local/etc/httpd/conf/mime.types \
/usr/local/lib/netscape/mime.types \
/usr/local/etc/httpd/conf/mime.types \
/usr/local/etc/mime.types
o/$(MODE)/third_party/python/Lib/test/test_grammar.py.runs: \
o/$(MODE)/third_party/python/pythontester.com
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $< -m test.test_grammar $(PYTESTARGS)

View file

@ -133,62 +133,6 @@ static ZCONST char CannotSetTimestamps[] =
#ifndef SFX
#ifdef NO_DIR /* for AT&T 3B1 */
#define _opendir(path) fopen(path,"r")
#define _closedir(dir) fclose(dir)
typedef FILE DIR;
typedef struct zdir {
FILE *dirhandle;
struct dirent *entry;
} DIR
DIR *opendir OF((ZCONST char *dirspec));
void closedir OF((DIR *dirp));
struct dirent *readdir OF((DIR *dirp));
DIR *opendir(dirspec)
ZCONST char *dirspec;
{
DIR *dirp;
if ((dirp = malloc(sizeof(DIR)) != NULL) {
if ((dirp->dirhandle = fopen(dirspec, "r")) == NULL) {
free(dirp);
dirp = NULL;
}
}
return dirp;
}
void closedir(dirp)
DIR *dirp;
{
fclose(dirp->dirhandle);
free(dirp);
}
/*
* Apparently originally by Rich Salz.
* Cleaned up and modified by James W. Birdsall.
*/
struct dirent *readdir(dirp)
DIR *dirp;
{
if (dirp == NULL)
return NULL;
for (;;)
if (fread(&(dirp->entry), sizeof (struct dirent), 1,
dirp->dirhandle) == 0)
return (struct dirent *)NULL;
else if ((dirp->entry).d_ino)
return &(dirp->entry);
} /* end function readdir() */
#endif /* NO_DIR */
/**********************/
/* Function do_wild() */ /* for porting: dir separator; match(ignore_case) */