Add syscalls to Blinkenlights and fix bugs

This commit is contained in:
Justine Tunney 2022-05-13 13:31:21 -07:00
parent f6df29cc3d
commit 578cb21591
25 changed files with 187 additions and 108 deletions

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
@ -30,5 +31,6 @@ relegated void __start_fatal(const char *file, int line) {
__restore_tty();
kprintf("%r%serror%s:%s:%d:%s%s: ", !__nocolor ? "\e[J\e[30;101m" : "",
!__nocolor ? "\e[94;49m" : "", file, line,
program_invocation_short_name, !__nocolor ? "\e[0m" : "");
firstnonnull(program_invocation_short_name, "unknown"),
!__nocolor ? "\e[0m" : "");
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/log/bsd.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
@ -23,8 +24,9 @@
#include "libc/stdio/stdio.h"
wontreturn void(verr)(int eval, const char *fmt, va_list va) {
fprintf(stderr, "%s: %s%s%s[%m]: ", program_invocation_name, RED2, "ERROR",
RESET);
fprintf(stderr,
"%s: %s%s%s[%m]: ", firstnonnull(program_invocation_name, "unknown"),
RED2, "ERROR", RESET);
if (fmt) (vfprintf)(stderr, fmt, va);
fprintf(stderr, "\n");
exit(eval);

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
#include "libc/calls/struct/stat.h"
@ -96,7 +97,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
vflogf_ts.tv_nsec = nsec;
localtime_r(&secs, &tm);
strcpy(iso8601(buf32, &tm), issamesecond ? "+" : ".");
prog = basename(program_invocation_name);
prog = basename(firstnonnull(program_invocation_name, "unknown"));
bufmode = f->bufmode;
if (bufmode == _IOLBF) f->bufmode = _IOFBF;
if ((fprintf)(f, "%r%c%s%06ld:%s:%d:%.*s:%d] ", "FEWIVDNT"[level & 7], buf32,

View file

@ -18,8 +18,13 @@
*/
#include "libc/macros.internal.h"
// Supplies argv[0] the GNU way.
.initbss 300,_init_program_invocation_name
// Supplies argv[0] the GNU way.
//
// If argv[0] isn't supplied, this value will be null.
//
// @see program_invocation_short_name
// @see GetProgramExecutableName()
program_invocation_name:
.quad 0
.endobj program_invocation_name,globl

View file

@ -344,6 +344,7 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
return MAP_FAILED;
}
f |= MAP_STACK_openbsd;
needguard = true;
} else if (IsLinux()) {
// by default MAP_GROWSDOWN will auto-allocate 10mb of pages. it's
// supposed to stop growing if an adjacent allocation exists, to

View file

@ -36,7 +36,8 @@ void __paginate(int fd, const char *s) {
((args[0] = commandv("less", progpath, sizeof(progpath))) ||
(args[0] = commandv("more", progpath, sizeof(progpath))))) {
snprintf(tmppath, sizeof(tmppath), "%s%s-%s-%d.txt", kTmpPath,
program_invocation_short_name, "paginate", getpid());
firstnonnull(program_invocation_short_name, "unknown"), "paginate",
getpid());
if ((tfd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) != -1) {
write(tfd, s, strlen(s));
close(tfd);

View file

@ -155,15 +155,18 @@
6666555555555544444444443333333333222222222211111111110000000000
3210987654321098765432109876543210987654321098765432109876543210*/
#define PAGE_V /* */ 0b000000001
#define PAGE_RW /* */ 0b000000010
#define PAGE_U /* */ 0b000000100
#define PAGE_4KB /* */ 0b010000000
#define PAGE_2MB /* */ 0b110000000
#define PAGE_1GB /* */ 0b110000000
#define PAGE_TA 0x00007ffffffff000
#define PAGE_PA2 0x00007fffffe00000
#define PAGE_XD 0x8000000000000000
#define PAGE_V /* */ 0b000000000001
#define PAGE_RW /* */ 0b000000000010
#define PAGE_U /* */ 0b000000000100
#define PAGE_4KB /* */ 0b000010000000
#define PAGE_2MB /* */ 0b000110000000
#define PAGE_1GB /* */ 0b000110000000
#define PAGE_IGN1 /* */ 0b111000000000
#define PAGE_RSRV /* blinkenlights reservation */ 0b001000000000
#define PAGE_GROD /* blinkenlights MAP_GROWSDOWN */ 0b010000000000
#define PAGE_TA 0x00007ffffffff000
#define PAGE_PA2 0x00007fffffe00000
#define PAGE_XD 0x8000000000000000
#if !(__ASSEMBLER__ + __LINKER__ + 0)

View file

@ -18,8 +18,13 @@
*/
#include "libc/macros.internal.h"
// Supplies basename(argv[0]) The GNU Way.
.initbss 400,_init_program_invocation_short_name
// Supplies basename(argv[0]) The GNU Way.
//
// If argv[0] isn't supplied, this value will be null.
//
// @see GetProgramExecutableName()
// @see program_invocation_name
program_invocation_short_name:
.quad 0
.endobj program_invocation_short_name,globl

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/dce.h"
@ -288,7 +289,7 @@ void openlog(const char *ident, int opt, int facility) {
__initlog();
}
if (!ident) {
ident = program_invocation_short_name;
ident = firstnonnull(program_invocation_short_name, "unknown");
}
tprecode8to16(log_ident, ARRAYLEN(log_ident), ident);
log_opt = opt;

View file

@ -36,7 +36,8 @@ FILE *tmpfile(void) {
tmp = firstnonnull(getenv("TMPDIR"), kTmpPath);
sep = !isempty(tmp) && !endswith(tmp, "/") ? "/" : "";
if ((snprintf)(tpl, PATH_MAX, "%s%stmp.%s.XXXXXX", tmp, sep,
program_invocation_short_name) <= PATH_MAX) {
firstnonnull(program_invocation_short_name, "unknown")) <=
PATH_MAX) {
if ((fd = mkostemps(tpl, 0, 0)) != -1) {
return fdopen(fd, "w+");
}

View file

@ -18,6 +18,7 @@
*/
#include "libc/bits/bits.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
#include "libc/str/str.h"
@ -26,7 +27,7 @@ static char g_strsignal[12];
/**
* Returns string describing signal code.
*
* This returns SIGUNKNOWN for 0 which is the empty value. Textual names
* This returns SIGZERO for 0 which is the empty value. Textual names
* should be available for signals 1 through 32. Signals in the range 33
* and 128 are returned as a `SIG%03d` string. Everything else is SIGWUT
*
@ -34,24 +35,36 @@ static char g_strsignal[12];
* @return pointer to static memory that mutates on subsequent calls
* @see sigaction()
*/
char *strsignal(int sig) {
noasan noinstrument char *strsignal(int sig) {
char *p;
const char *s;
strcpy(g_strsignal, "SIG");
p = g_strsignal;
p[0] = 'S';
p[1] = 'I';
p[2] = 'G';
p[3] = 0;
if (sig) {
if ((s = GetMagnumStr(kSignalNames, sig))) {
strcpy(g_strsignal + 3, s);
return g_strsignal;
__stpcpy(p + 3, s);
return p;
}
}
if (!sig) {
strcpy(g_strsignal + 3, "UNKNOWN");
p[3] = 'Z';
p[4] = 'E';
p[5] = 'R';
p[6] = 'O';
p[7] = 0;
} else if (1 <= sig && sig <= 128) {
g_strsignal[3] = '0' + sig / 100;
g_strsignal[4] = '0' + sig / 10 % 10;
g_strsignal[5] = '0' + sig % 10;
g_strsignal[6] = 0;
p[3] = '0' + sig / 100;
p[4] = '0' + sig / 10 % 10;
p[5] = '0' + sig % 10;
p[6] = 0;
} else {
strcpy(g_strsignal + 3, "WUT");
p[3] = 'W';
p[4] = 'U';
p[5] = 'T';
p[6] = 0;
}
return g_strsignal;
}

View file

@ -88,8 +88,8 @@ testonly void testlib_showerror_(int line, const char *wantcode,
}
kprintf("\t%s%s%s\n"
"\t%s%s @ %s%s\n",
SUBTLE, strerror(e), RESET, SUBTLE, program_invocation_name, hostname,
RESET);
SUBTLE, strerror(e), RESET, SUBTLE,
firstnonnull(program_invocation_name, "unknown"), hostname, RESET);
free_s(&FREED_want);
free_s(&FREED_got);
++g_testlib_failed;

View file

@ -35,6 +35,7 @@
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/vendor.internal.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.internal.h"
@ -71,7 +72,7 @@ static bool runbenchmarks_;
void PrintUsage(int rc, FILE *f) {
fputs("Usage: ", f);
fputs(program_invocation_name, f);
fputs(firstnonnull(program_invocation_name, "unknown"), f);
fputs(USAGE, f);
exit(rc);
}
@ -102,17 +103,26 @@ static void EmptySignalMask(void) {
}
static void FixIrregularFds(void) {
int i;
int i, fd;
struct pollfd pfds[64];
for (i = 0; i < 3; ++i) {
if (fcntl(0, F_GETFL) == -1) {
CHECK_EQ(0, open("/dev/null", O_RDWR));
if (fcntl(i, F_GETFL) == -1) {
errno = 0;
fd = open("/dev/null", O_RDWR);
CHECK_NE(-1, fd);
if (fd != i) {
close(fd);
}
}
}
for (i = 0; i < ARRAYLEN(pfds); ++i) {
pfds[i].fd = i + 3;
pfds[i].events = POLLIN;
}
if (IsGenuineCosmo()) {
// TODO(jart): Fix Blinkenlights poll() / close()
return;
}
if (poll(pfds, ARRAYLEN(pfds), 0) != -1) {
for (i = 0; i < ARRAYLEN(pfds); ++i) {
if (pfds[i].revents & POLLNVAL) continue;