Skip to content

Commit

Permalink
Compile on GNU Hurd
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed May 3, 2024
1 parent 50b5988 commit 18c08e8
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 86 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CPPFLAGS_cc = $(CPPFLAGS_gcc)
CPPFLAGS ?= $(CPPFLAGS_$(CC))
CPPFLAGS += -I$(PROJECT_DIR) -I$(PROJECT_DIR)multitasking
CPPFLAGS += -D_GNU_SOURCE
CPPFLAGS += -DCORO_UCONTEXT
#CPPFLAGS += -DNDEBUG
# Disable all logs
#CPPFLAGS += -DNO_LOGS_ON_STDERR
Expand All @@ -30,15 +31,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 := -lpthread

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

# Project configuration ########################################################
RL_C := wireprotocol.c frontend.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 autoresponder.c controller.c ucspi.c s6notify.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 @@ -57,9 +58,10 @@ vpath %.rl $(PROJECT_DIR)fsm
vpath %.c $(PROJECT_DIR) $(addprefix $(PROJECT_DIR),multitasking multitasking/coro)
vpath %.t $(PROJECT_DIR)
vpath %.txt $(PROJECT_DIR)
vpath %.tcl $(PROJECT_DIR)

# Public targets ###############################################################
all: frob ut
all: dir-pipe-client dir-ut
index: tags cscope.out
test: test-unit test-functional test-random
clean:
Expand All @@ -70,7 +72,9 @@ 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 ./$< 1000 $| 2>&1 | s6-tai64n | s6-tai64nlocal
s6-tcpclient -rv localhost 5002 ./$< 1000 $|
pipe-client: frob | d5.txt gent1.tcl
$(word 2,$|) | ./$< 1000 $(word 1,$|)
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 Down
15 changes: 7 additions & 8 deletions autoresponder.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ int autoresponder(const struct autoresponder_args* const args) {
},
.parameters = load_d5_from_file(args->d5_path)
};
const unsigned char* p;
char buf[1024];
const unsigned char* p = buf;
ssize_t bytes;
unsigned char rsp_buf[1024];
/*
while ((bytes = sus_borrow(args->in, (void**)&p)) >= 0) {
//LOGDXP(char tmp[4*bytes], "Received %zd bytes: %s", bytes, PRETTY(p, p + bytes, tmp));
LOGDX("reading on %d, writting to %d", args->in, args->out);
while ((bytes = sio_read(args->in, buf, sizeof buf)) >= 0) {
p = buf;
LOGDXP(char tmp[4*bytes], "Received %zd bytes: %s", bytes, PRETTY(p, p + bytes, tmp));
if (bytes > 1) {
const struct frob_msg msg = xparse_message(bytes - 1, p + 1);
const struct frob_msg response = {
Expand All @@ -87,13 +89,10 @@ int autoresponder(const struct autoresponder_args* const args) {
if (w < 0) {
LOGEX("Message skipped");
} else {
sus_write(args->out, rsp_buf, w);
sio_write(args->out, rsp_buf, w);
LOGDXP(char tmp[4*w], "← % 4zd %s", sizeof w, PRETTY(rsp_buf, rsp_buf + w, tmp));
}
}
//LOGDX("Returning buffer");
sus_return(args->in, p, bytes);
}
*/
return -1;
}
6 changes: 5 additions & 1 deletion frob.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ static_assert(sizeof (struct frob_msg) % 16 == 0, "Message shall fit into 16-byt

struct fsm_frontend_foreign_args {
int cs;
const int infd;
const int foreign_out;
const int to_autoresponder;
};

struct fsm_frontend_internal_args {
Expand All @@ -299,6 +302,7 @@ struct fsm_frontend_timer_args {

struct fsm_wireformat_args {
const int infd;
const int outfd;
};

struct autoresponder_args {
Expand All @@ -317,7 +321,7 @@ struct s6_notify_args {
const int fd;
};

int fsm_wireformat(void*);
int 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
27 changes: 11 additions & 16 deletions fsm/frontend.rl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "log.h"
#include "utils.h"
#include <stdbool.h>
#include <sys/timerfd.h>
#include <unistd.h>

static int cs;
Expand Down Expand Up @@ -31,15 +30,15 @@ static int cs;
acknak = 0x15;
}
action Send {
if (sio_write(6, &acknak, 1) != 1) {
if (sio_write(a->foreign_out, &acknak, 1) != 1) {
LOGE("write");
fbreak;
}
LOGDXP(char tmp[4*1], "← % 4d %s", 1, PRETTY(&acknak, &acknak + 1, tmp));
}
action Process {
LOGDXP(char tmp[4*(pe-p)], "Lending %zd bytes: %s", pe - p, PRETTY((unsigned char*)p, (unsigned char*)pe, tmp));
//sus_lend(1, pe - p, (char*)p);// TODO: Remove this cast
LOGDXP(char tmp[4*(pe-p)], "to_autoresponder %zd bytes: %s", pe - p, PRETTY((unsigned char*)p, (unsigned char*)pe, tmp));
sio_write(a->to_autoresponder, p, pe - p);
}
action Forward {
if (sio_write(forwarded_fd, p, pe - p) != pe - p) {
Expand Down Expand Up @@ -67,7 +66,7 @@ static bool is_idempotent(const char* const msg) {
}
*/

static int fsm_exec(const char* p, const char* const pe) {
static int fsm_exec(struct fsm_frontend_foreign_args* const a, const char* p, const char* const pe) {
unsigned char acknak;
%% write exec;
return -1;
Expand All @@ -76,25 +75,21 @@ static int fsm_exec(const char* p, const char* const pe) {
__attribute__((constructor))
void fsm_frontend_init() {
(void)frontend_en_main, (void)frontend_error, (void)frontend_first_final;
set_nonblocking(6);
%% write init;
}

int fsm_frontend_foreign(struct fsm_frontend_foreign_args* const a) {
(void)a;
ssize_t bytes;
const char* p;
/*
while ((bytes = sus_borrow(0, (void**)&p)) >= 0) {
char buf[1024];
const char* p = buf;
while ((bytes = sio_read(a->infd, buf, sizeof buf)) > 0) {
LOGDX("Received %zd bytes", bytes);
const char* const pe = p + bytes;
fsm_exec(p, pe);
sus_return(0, p, bytes);
const char* const pe = (p = buf) + bytes;
fsm_exec(a, p, pe);
}
if (bytes < 0)
LOGE("Closing fronted");
sus_disable(1);
*/
LOGE("Closing fronted");
close(a->infd);
return -1;
}

Expand Down
13 changes: 6 additions & 7 deletions fsm/wireprotocol.rl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
}
action LRC_Check {
if (lrc != fc) {
//sus_lend(0, 1, "\x03");
LOGDX("@@@ NOK");
sio_write(args->outfd, "\x03", 1);
} else {
//sus_lend(0, fpc - start, buf);
LOGDX("@@@ OK");
sio_write(args->outfd, buf, fpc - start);
}
}
action Frame_Start {
Expand All @@ -33,24 +35,21 @@

%% write data;

int fsm_wireformat(void*) {
int 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(6);
while ((bytes = sio_read(6, buf, sizeof buf)) > 0) {
while ((bytes = sio_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;
}
if (bytes < 0)
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;
}
3 changes: 0 additions & 3 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ 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,
Expand Down
1 change: 1 addition & 0 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* PostScriptum ,Prefix,Level ,Method,Prologue,Epilogue,Message */
#define LOGDXP(P, ...) LOG( ,"D" ,DEBUG ,warnx ,P , ,##__VA_ARGS__)
#define LOGDP(P, ...) LOG( ,"D" ,DEBUG ,warn ,P , ,##__VA_ARGS__)
#define LOGDX(...) LOG( ,"D" ,DEBUG ,warnx , , ,##__VA_ARGS__)
#define LOGD(...) LOG( ,"D" ,DEBUG ,warn , , ,##__VA_ARGS__)
#define LOGIX(...) LOG( ,"I" ,INFO ,warnx , , ,##__VA_ARGS__)
Expand Down
11 changes: 6 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#include <fcntl.h>

static void adjust_rlimit(void) {
// This will force syscalls that allocate file descriptors to fail if it
Expand All @@ -24,16 +25,16 @@ int main(const int ac, const char* av[static const ac]) {
adjust_rlimit();
int fd_fo_main = STDOUT_FILENO, fd_fi_main = STDIN_FILENO;
ucsp_info_and_adjust_fds(&fd_fo_main, &fd_fi_main);
set_nonblocking(fd_fi_main);
set_nonblocking(fd_fo_main);
struct sus_registation_form tasks[] = {
sus_registration(fsm_wireformat, fd_fi_main),
sus_registration(fsm_frontend_foreign),
sus_registration(fsm_wireformat, .infd = fd_fi_main),
sus_registration(fsm_frontend_foreign, .foreign_out = fd_fo_main),
sus_registration(fsm_frontend_timer),
sus_registration(autoresponder, av[2], 1, fd_fo_main),
sus_registration(autoresponder, .d5_path = av[2], .out = fd_fo_main),
sus_registration(s6_notify, -1),
sus_registration(controller),
};
set_nonblocking(fd_fo_main);
set_nonblocking(fd_fi_main);
if (sig_runall(lengthof(tasks), &tasks) != 0)
EXITF("Can't start");
int ret = 0;
Expand Down
Loading

0 comments on commit 18c08e8

Please sign in to comment.