Skip to content

Commit

Permalink
Trying to use npth
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed May 11, 2024
1 parent a444a6f commit 25e677a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ LDFLAGS ?= -flto
LDFLAGS += $(call if_coverage,--coverage)

# Project configuration ########################################################
RL_C := wireprotocol.c header.c body.c attrs.c frame.c
RL_C := wireprotocol.c header.c body.c attrs.c frame.c frontend.c
RL_O := $(RL_C:.c=.o)
CFILES := main.c log.c utils.c serialization.c ucspi.c npthfix.c
OFILES := $(RL_O) $(CFILES:.c=.o)
Expand Down
5 changes: 3 additions & 2 deletions frob.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ static_assert(sizeof (struct frob_msg) % 16 == 0, "Message shall fit into 16-byt
#pragma once

struct fsm_frontend_foreign_args {
int in;
int cs;
};

Expand All @@ -300,7 +301,7 @@ struct fsm_frontend_timer_args {
};

struct fsm_wireformat_args {
const int infd;
const int in, out;
};

struct autoresponder_args {
Expand All @@ -320,7 +321,7 @@ struct s6_notify_args {
};

void* fsm_wireformat(const struct fsm_wireformat_args*);
int fsm_frontend_foreign(struct fsm_frontend_foreign_args*);
void* 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*);
int autoresponder(const struct autoresponder_args*);
Expand Down
24 changes: 11 additions & 13 deletions fsm/frontend.rl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "multitasking/sus.h"
#include "frob.h"
#include "log.h"
#include "utils.h"
#include <stdbool.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <npth.h>

static int cs;

Expand All @@ -31,18 +31,18 @@ static int cs;
acknak = 0x15;
}
action Send {
if (sus_write(6, &acknak, 1) != 1) {
LOGDXP(char tmp[4*1], "← % 4d %s", 1, PRETTY(&acknak, &acknak + 1, tmp));
if (npth_write(6, &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
//sus_lend(1, pe - p, (char*)p);// TODO: Remove this cast
}
action Forward {
if (sus_write(forwarded_fd, p, pe - p) != pe - p) {
if (npth_write(forwarded_fd, p, pe - p) != pe - p) {
LOGE("write");
fbreak;
}
Expand Down Expand Up @@ -76,24 +76,22 @@ 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* fsm_frontend_foreign(struct fsm_frontend_foreign_args* const a) {
(void)a;
ssize_t bytes;
char buf[1024];
const char* p;
while ((bytes = sus_borrow(0, (void**)&p)) >= 0) {
while ((bytes = npth_read(a->in, buf, sizeof buf)) >= 0) {
LOGDX("Received %zd bytes", bytes);
const char* const pe = p + bytes;
const char* const pe = (p = buf) + bytes;
fsm_exec(p, pe);
sus_return(0, p, bytes);
}
if (bytes < 0)
LOGE("Closing fronted");
sus_disable(1);
return -1;
return NULL;
}

int fsm_frontend_internal(struct fsm_frontend_internal_args* const a) {
Expand All @@ -114,7 +112,7 @@ int fsm_frontend_timer(struct fsm_frontend_timer_args* const a) {
const int fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
ssize_t bytes;
unsigned char buf[8];
while ((bytes = sus_read(fd, buf, sizeof buf)) > 0) {
while ((bytes = npth_read(fd, buf, sizeof buf)) > 0) {
const char* p = (char[]){0}, * const pe = p + 1;
fsm_exec(p, pe);
}
Expand Down
6 changes: 3 additions & 3 deletions fsm/wireprotocol.rl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
}
action LRC_Check {
if (lrc != fc) {
//sus_lend(0, 1, "\x03");
npth_write(args->out, "\x03", 1);
} else {
//sus_lend(0, fpc - start, buf);
npth_write(args->out, buf, fpc - start);
}
}
action Frame_Start {
Expand All @@ -41,7 +41,7 @@ void* fsm_wireformat(const struct fsm_wireformat_args* const args) {
int cs;
unsigned char* p = buf, * pe = p;
%% write init;
while ((bytes = npth_read(args->infd, buf, sizeof buf)) > 0) {
while ((bytes = npth_read(args->in, buf, sizeof buf)) > 0) {
pe = (p = buf) + bytes;
LOGDXP(char tmp[4*bytes], "→ % 4zd %s", bytes, PRETTY(p, pe, tmp));
%% write exec;
Expand Down
22 changes: 6 additions & 16 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
#include "log.h"
#include "utils.h"
#include "frob.h"
#include "npthfix.h"
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#include <npth.h>

typedef void* (* const npth_entry_t)(void*);

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

#define npth_define(Entry, ...) \
{ .entry = (npth_entry_t)Entry, .name = #Entry, .arg = &(struct Entry ## _args){ __VA_ARGS__ } }

static void adjust_rlimit(void) {
// This will force syscalls that allocate file descriptors to fail if it
Expand All @@ -35,10 +23,13 @@ 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);
int frontend_pipe[2];
xpipe(frontend_pipe);
struct ThreadBag thr[] = {
npth_define(fsm_wireformat, .infd = fd_fi_main)
npth_define(fsm_wireformat, .in = fd_fi_main, .out = frontend_pipe[1]),
npth_define(fsm_frontend_foreign, .in = frontend_pipe[0])
};
npth_init();
xnpth_init();
npth_sigev_init();
npth_sigev_add(SIGPWR);
npth_sigev_fini();
Expand All @@ -48,7 +39,6 @@ int main(const int ac, const char* av[static const ac]) {
}
for (;;) {
int sig;
LOGDX("Waiting for a signal...");
xnpth_sigwait(npth_sigev_sigmask(), &sig);
switch (sig) {
case SIGPWR:
Expand Down
8 changes: 8 additions & 0 deletions npthfix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "npthfix.h"

int npth_sigwait(const sigset_t *set, int *sig) {
npth_unprotect();
const int res = sigwait(set, sig);
npth_protect();
return res;
}
14 changes: 14 additions & 0 deletions npthfix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <npth.h>

#define npth_define(Entry, ...) \
{ .entry = (npth_entry_t)Entry, .name = #Entry, .arg = &(struct Entry ## _args){ __VA_ARGS__ } }

typedef void* (* const npth_entry_t)(void*);

struct ThreadBag {
npth_t handle;
const char* const name;
npth_entry_t entry;
void* const arg;
};
6 changes: 3 additions & 3 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include <fcntl.h>
#include <execinfo.h>

void set_nonblocking(const int fd) {
void set_blocking(const int fd) {
const int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
EXITF("fcntl F_GETFL");
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
EXITF("fcntl F_SETFL O_NONBLOCK");
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1)
EXITF("fcntl F_SETFL ~O_NONBLOCK");
}

input_t calculate_lrc(input_t* p, const input_t* const pe) {
Expand Down
4 changes: 3 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#define xnpth_setname_np(...) XCALL(npth_setname_np, __VA_ARGS__)
#define xnpth_create(...) XCALL(npth_create, __VA_ARGS__)
#define xnpth_sigwait(...) XCALL(npth_sigwait, __VA_ARGS__)
#define xnpth_init(...) XCALL(npth_init, __VA_ARGS__)
#define xfprintf(...) XCALL(fprintf, __VA_ARGS__)
#define xfputs(...) XCALL(fputs, __VA_ARGS__)
#define xfflush(...) XCALL(fflush, __VA_ARGS__)
Expand All @@ -78,6 +79,7 @@
#define xwrite(...) XCALL(write, __VA_ARGS__)
#define xfclose(...) XCALL(fclose, __VA_ARGS__)
#define xclose(...) XCALL(close, __VA_ARGS__)
#define xpipe(...) XCALL(pipe, __VA_ARGS__)

#ifdef NDEBUG
# define XCALL(Syscall, ...) syscall_exitf(#Syscall, Syscall(__VA_ARGS__))
Expand All @@ -102,7 +104,7 @@
typedef uint8_t input_t;


void set_nonblocking(int fd);
void set_blocking(int fd);

byte_t hex2nibble(char h);
byte_t unhex(const char h[static 2]);
Expand Down

0 comments on commit 25e677a

Please sign in to comment.