Skip to content

Commit

Permalink
Use npth
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed May 7, 2024
1 parent a9ecf31 commit d6879ef
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 543 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "coroutine"]
path = multitasking/coro
url = https://github.com/clibs/coro.git
16 changes: 6 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ CFLAGS += $(call if_coverage,--coverage)
# TODO: Remove those warnings only for generated files
CFLAGS += -Wno-implicit-fallthrough -Wno-unused-const-variable -Wno-sign-compare -Wno-unused-variable -Wno-unused-parameter
TARGET_ARCH := -mtune=native -march=native
LDLIBS :=
LDLIBS := -lnpth

LDFLAGS ?= -flto
LDFLAGS += $(call if_coverage,--coverage)

# Project configuration ########################################################
RL_C := wireprotocol.c frontend.c header.c body.c attrs.c frame.c
RL_C := wireprotocol.c header.c body.c attrs.c frame.c
RL_O := $(RL_C:.c=.o)
CFILES := main.c log.c utils.c serialization.c autoresponder.c sighandler.c controller.c ucspi.c s6notify.c
CFILES := main.c log.c utils.c serialization.c ucspi.c
OFILES := $(RL_O) $(CFILES:.c=.o)
UT_C := ut.c serialization.c log.c utils.c contextring.c
UT_O := $(UT_C:.c=.o) header.o body.o frame.o attrs.o
Expand All @@ -46,9 +46,6 @@ NORMAL_O := $(RL_O) ut.o log.o
ALL_C := $(CFILES) $(UT_C)
ALL_PLIST := $(ALL_C:.c=.plist)

LIBCOMULTI_C := coro.c contextring.c eventloop.c suspendables.c
LIBCOMULTI_O := $(LIBCOMULTI_C:.c=.o)

HOSTNAME := $(shell hostname -f)
BUILD_DIR ?= build

Expand All @@ -69,7 +66,7 @@ clang-analyze: $(ALL_PLIST)
tcp-server: frob | d5.txt
s6-tcpserver -vd -b2 0.0.0.0 5002 s6-tcpserver-access -t200 -v3 -rp -B "Welcome!\r\n" $(realpath $<) 1000 $|
tcp-client: frob | d5.txt
s6-tcpclient -rv localhost 5002 rlwrap $(realpath $<) 1000 $| 2>&1 | s6-tai64n | s6-tai64nlocal
s6-tcpclient -rv localhost 5002 $(realpath $<) 1000 $|
tls-server: frob server.cer server.key | d5.txt
env -i PATH=/bin CERTFILE=$(word 2,$^) KEYFILE=$(word 3,$^) s6-tlsserver 0.0.0.0 6666 $(realpath $<) -f $(word 1,$^)
tls-client: frob server.cer | d5.txt
Expand All @@ -87,7 +84,7 @@ dir-%: | $(BUILD_DIR)/

# Internal targets #############################################################
tags:
ctags --kinds-C=+p -R . /usr/include/{sys,}/*.h
ctags --kinds-C=+p -R . /usr/include/{sys,bits,}/*.h /usr/local/src/npth
test-random: frob
pv -Ss100M /dev/urandom | ./$< 1 2>/dev/null 1>/dev/null
test-unit: ut
Expand All @@ -109,12 +106,11 @@ mut: $(NORMAL_O) mutated
$(LINK.o) -o $@ $(NORMAL_O) $(MUTATED_O) $(LDLIBS)
mutated: CFLAGS += -fexperimental-new-pass-manager -fpass-plugin=/usr/local/lib/mull-ir-frontend-15 -grecord-command-line
mutated: $(MUTATED_O)
frob: $(OFILES) libcomulti.a
frob: $(OFILES)
$(LINK.o) -o $@ $^ $(LDLIBS)
objcopy --only-keep-debug $@ $@.debug
strip --strip-unneeded $@
objcopy --add-gnu-debuglink=$@.debug $@
libcomulti.a: libcomulti.a($(LIBCOMULTI_O))
%.c: %.rl
ragel -G2 -L -o $@ $<
%.s: %.c
Expand Down
2 changes: 1 addition & 1 deletion frob.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ struct s6_notify_args {
const int fd;
};

int fsm_wireformat(void*);
void* fsm_wireformat(const struct fsm_wireformat_args*);
int fsm_frontend_foreign(struct fsm_frontend_foreign_args*);
int fsm_frontend_internal(struct fsm_frontend_internal_args*);
int fsm_frontend_timer(struct fsm_frontend_timer_args*);
Expand Down
15 changes: 7 additions & 8 deletions fsm/wireprotocol.rl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "multitasking/sus.h"
#include "frob.h"
#include "utils.h"
#include "log.h"
#include <unistd.h>
#include <fcntl.h>
#include <npth.h>

%%{
machine wireformat;
Expand All @@ -18,9 +18,9 @@
}
action LRC_Check {
if (lrc != fc) {
sus_lend(0, 1, "\x03");
//sus_lend(0, 1, "\x03");
} else {
sus_lend(0, fpc - start, buf);
//sus_lend(0, fpc - start, buf);
}
}
action Frame_Start {
Expand All @@ -33,16 +33,15 @@

%% write data;

int fsm_wireformat(void*) {
void* fsm_wireformat(const struct fsm_wireformat_args* const args) {
unsigned char* start = NULL;
char lrc;
ssize_t bytes;
unsigned char buf[1024];
int cs;
unsigned char* p = buf, * pe = p;
%% write init;
set_nonblocking(7);
while ((bytes = sus_read(7, buf, sizeof buf)) > 0) {
while ((bytes = npth_read(args->infd, buf, sizeof buf)) > 0) {
pe = (p = buf) + bytes;
LOGDXP(char tmp[4*bytes], "→ % 4zd %s", bytes, PRETTY(p, pe, tmp));
%% write exec;
Expand All @@ -51,6 +50,6 @@ int fsm_wireformat(void*) {
LOGE("read");
close(7);
LOGIX("FSM state: current/entry/error/final %d/%d/%d/%d", cs, wireformat_en_main, wireformat_error, wireformat_first_final);
sus_disable(0);
return cs == wireformat_error ? -1 : 0;
//sus_disable(0);
return cs == wireformat_error ? NULL + 1 : NULL;
}
2 changes: 1 addition & 1 deletion gent1.tcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/tclsh

source ../testsuite/lib/frob.exp
puts [msg T2]
puts [msg T1]
after 10000
6 changes: 0 additions & 6 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ int g_log_level = LOG_DEBUG;
const char* g_errname;
#endif

void init_log(void) {
#ifdef NO_LOGS_ON_STDERR
xfclose(stderr);
#endif
}

char* to_printable(const unsigned char* const p, const unsigned char* const pe,
const size_t s, char b[static const s]) {
// TODO: Add support for regional characters, ie from 0x80 to 0xFF
Expand Down
56 changes: 37 additions & 19 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#include "log.h"
#include "utils.h"
#include "multitasking/sus.h"
#include "frob.h"
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#include <npth.h>

struct ThreadBag {
npth_t handle;
const char* const name;
void* (* const entry)(void*);
void* const arg;
};

int npth_sigwait(const sigset_t *set, int *sig) {
npth_unprotect();
const int res = sigwait(set, sig);
npth_protect();
return res;
}

static void adjust_rlimit(void) {
// This will force syscalls that allocate file descriptors to fail if it
Expand All @@ -18,29 +32,33 @@ static void adjust_rlimit(void) {
}

int main(const int ac, const char* av[static const ac]) {
init_log();
if (ac != 3)
return 1;
adjust_rlimit();
int fd_fo_main = STDOUT_FILENO, fd_fi_main = STDIN_FILENO;
ucsp_info_and_adjust_fds(&fd_fo_main, &fd_fi_main);
struct sus_registation_form tasks[] = {
sus_registration(fsm_wireformat, fd_fi_main),
sus_registration(fsm_frontend_foreign),
sus_registration(fsm_frontend_timer),
sus_registration(autoresponder, av[2], 1, fd_fo_main),
sus_registration(sighandler),
sus_registration(s6_notify, -1),
sus_registration(controller),
sus_registration(sus_ioloop, .timeout = atoi(av[1]))
LOGDX("%d %d", fd_fo_main, fd_fi_main);
struct ThreadBag thr[] = {
{ .entry = fsm_wireformat, .name = "wireprotocol", .arg = &(struct fsm_wireformat_args){fd_fi_main}}
};
if (sus_runall(lengthof(tasks), &tasks) != 0)
EXITF("Can't start");
int ret = 0;
for (size_t i = 0; i < lengthof(tasks); i++) {
LOGDX("task %zu returned % 2d (%s)", i, tasks[i].result, tasks[i].name);
if (tasks[i].result)
ret = 100;
npth_init();
npth_sigev_init();
npth_sigev_add(SIGINT);
npth_sigev_fini();
for (size_t i = 0; i < lengthof(thr); i++) {
xnpth_create(&thr[i].handle, NULL, thr[i].entry, thr[i].arg);
xnpth_setname_np(thr[i].handle, thr[i].name);
}
for (;;) {
int sig;
xnpth_sigwait(npth_sigev_sigmask(), &sig);
switch (sig) {
case SIGINT:
LOGD("got %s", strsignal(sig));
break;
default:
break;
}
}
return ret;
return 0;
}
43 changes: 0 additions & 43 deletions multitasking/contextring.c

This file was deleted.

13 changes: 0 additions & 13 deletions multitasking/contextring.h

This file was deleted.

1 change: 0 additions & 1 deletion multitasking/coro
Submodule coro deleted from 9c50c5
62 changes: 0 additions & 62 deletions multitasking/eventloop.c

This file was deleted.

17 changes: 0 additions & 17 deletions multitasking/eventloop.h

This file was deleted.

8 changes: 0 additions & 8 deletions multitasking/gdb.scm

This file was deleted.

Loading

0 comments on commit d6879ef

Please sign in to comment.