diff options
Diffstat (limited to 'src/stls/stls_run.c')
-rw-r--r-- | src/stls/stls_run.c | 132 |
1 files changed, 40 insertions, 92 deletions
diff --git a/src/stls/stls_run.c b/src/stls/stls_run.c index 11a7234..e6ab609 100644 --- a/src/stls/stls_run.c +++ b/src/stls/stls_run.c @@ -1,20 +1,18 @@ /* ISC license. */ -#include <skalibs/nonposix.h> #include <sys/uio.h> -#include <sys/socket.h> #include <errno.h> -#include <signal.h> +#include <unistd.h> + #include <tls.h> -#include <skalibs/allreadwrite.h> + #include <skalibs/error.h> #include <skalibs/buffer.h> -#include <skalibs/sig.h> -#include <skalibs/selfpipe.h> #include <skalibs/strerr2.h> #include <skalibs/tai.h> #include <skalibs/iopause.h> #include <skalibs/djbunix.h> + #include <s6-networking/stls.h> typedef struct tlsbuf_s tlsbuf_t, *tlsbuf_t_ref ; @@ -109,50 +107,24 @@ static void send_closenotify (struct tls *ctx, int const *fds) static void closeit (struct tls *ctx, int *fds, int brutal) { - if (brutal) shutdown(fds[3], SHUT_WR) ; + if (brutal) fd_shutdown(fds[3], 1) ; else if (fds[2] >= 0) send_closenotify(ctx, fds) ; fd_close(fds[3]) ; fds[3] = -1 ; } -static void handle_signals (pid_t pid, int *e) -{ - for (;;) switch (selfpipe_read()) - { - case -1 : strerr_diefu1sys(111, "read selfpipe") ; - case 0 : return ; - case SIGCHLD : - { - int wstat ; - if (wait_pid_nohang(pid, &wstat) == pid) - { - *e = wstat ; - return ; - } - } - } -} - -int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint32_t options, tain_t const *tto) +void stls_run (struct tls *ctx, int *fds, tain_t const *tto, uint32_t options, unsigned int verbosity) { tlsbuf_t b[2] = { { .blockedonother = 0 }, { .blockedonother = 0 } } ; - iopause_fd x[5] = { { .fd = fds[4], .events = IOPAUSE_READ } } ; + iopause_fd x[4] ; unsigned int xindex[4] ; - unsigned int i = 0 ; - int e = -1 ; - - for (; i < 2 ; i++) - { - buffer_init(&b[i].b, i ? &buffer_write : &buffer_read, fds[i], b[i].buf, STLS_BUFSIZE) ; - if (ndelay_on(fds[2+i]) < 0) strerr_diefu1sys(111, "set fds non-blocking") ; - } - if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; - tain_now_g() ; + buffer_init(&b[0].b, &buffer_read, fds[0], b[0].buf, STLS_BUFSIZE) ; + buffer_init(&b[1].b, &buffer_write, fds[1], b[1].buf, STLS_BUFSIZE) ; for (;;) { tain_t deadline ; - unsigned int xlen = 1 ; + unsigned int j = 0 ; int r ; tain_add_g(&deadline, fds[0] >= 0 && fds[2] >= 0 && buffer_isempty(&b[0].b) && buffer_isempty(&b[1].b) ? tto : &tain_infinite_relative) ; @@ -162,68 +134,54 @@ int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint if (fds[0] >= 0 && buffer_isreadable(&b[0].b)) { - x[xlen].fd = fds[0] ; - x[xlen].events = IOPAUSE_READ ; - xindex[0] = xlen++ ; + x[j].fd = fds[0] ; + x[j].events = IOPAUSE_READ ; + xindex[0] = j++ ; } - else xindex[0] = 5 ; + else xindex[0] = 4 ; if (fds[1] >= 0 && buffer_iswritable(&b[1].b)) { - x[xlen].fd = fds[1] ; - x[xlen].events = IOPAUSE_WRITE ; - xindex[1] = xlen++ ; + x[j].fd = fds[1] ; + x[j].events = IOPAUSE_WRITE ; + xindex[1] = j++ ; } - else xindex[1] = 5 ; + else xindex[1] = 4 ; if (fds[2] >= 0 && !b[1].blockedonother && buffer_isreadable(&b[1].b)) { - x[xlen].fd = fds[2] ; - x[xlen].events = IOPAUSE_READ ; - xindex[2] = xlen++ ; + x[j].fd = fds[2] ; + x[j].events = IOPAUSE_READ ; + xindex[2] = j++ ; } - else xindex[2] = 5 ; + else xindex[2] = 4 ; if (fds[3] >= 0 && !b[0].blockedonother && buffer_iswritable(&b[0].b)) { - x[xlen].fd = fds[3] ; - x[xlen].events = IOPAUSE_WRITE ; - xindex[3] = xlen++ ; + x[j].fd = fds[3] ; + x[j].events = IOPAUSE_WRITE ; + xindex[3] = j++ ; } - else xindex[3] = 5 ; + else xindex[3] = 4 ; - if (xlen == 1) break ; + if (xindex[0] == 4 && xindex[1] == 4 && xindex[3] == 4) break ; /* poll() */ - r = iopause_g(x, xlen, &deadline) ; + r = iopause_g(x, j, &deadline) ; if (r < 0) strerr_diefu1sys(111, "iopause") ; - else if (!r) - { - fd_close(fds[0]) ; fds[0] = -1 ; - closeit(ctx, fds, options & 1) ; - if (e >= 0) break ; - continue ; - } + else if (!r) break ; - while (xlen--) - if (x[xlen].revents & IOPAUSE_EXCEPT) - x[xlen].revents |= IOPAUSE_READ | IOPAUSE_WRITE ; + while (j--) + if (x[j].revents & IOPAUSE_EXCEPT) + x[j].revents |= IOPAUSE_READ | IOPAUSE_WRITE ; - /* Signal */ - - if (x[0].revents & IOPAUSE_READ) - { - handle_signals(pid, &e) ; - if (e >= 0 && xindex[0] == 5 && xindex[3] == 5) break ; - } - /* Flush to local */ - if (xindex[1] < 5 && x[xindex[1]].revents & IOPAUSE_WRITE) + if (xindex[1] < 4 && x[xindex[1]].revents & IOPAUSE_WRITE) { r = buffer_flush(&b[1].b) ; if (!r && !error_isagain(errno)) @@ -231,7 +189,7 @@ int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint strerr_warnwu1sys("write to application") ; if (fds[2] >= 0) { - if (options & 1) shutdown(fds[2], SHUT_RD) ; + if (options & 1) fd_shutdown(fds[2], 0) ; fd_close(fds[2]) ; fds[2] = -1 ; xindex[2] = 4 ; } @@ -246,25 +204,23 @@ int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint /* Flush to remote */ - if (xindex[3] < 5 && x[xindex[3]].revents & IOPAUSE_WRITE) + if (xindex[3] < 4 && x[xindex[3]].revents & IOPAUSE_WRITE) { r = buffer_tls_flush(ctx, b) ; if (r < 0) { strerr_warnwu2x("write to peer: ", tls_error(ctx)) ; fd_close(fds[0]) ; fds[0] = -1 ; + xindex[0] = 4 ; } if (r && fds[0] < 0) - { closeit(ctx, fds, options & 1) ; - if (e >= 0) break ; - } } /* Fill from local */ - if (xindex[0] < 5 && x[xindex[0]].revents & IOPAUSE_READ) + if (xindex[0] < 4 && x[xindex[0]].revents & IOPAUSE_READ) { r = sanitize_read(buffer_fill(&b[0].b)) ; if (r < 0) @@ -272,23 +228,20 @@ int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint if (errno != EPIPE) strerr_warnwu1sys("read from application") ; fd_close(fds[0]) ; fds[0] = -1 ; if (buffer_isempty(&b[0].b)) - { closeit(ctx, fds, options & 1) ; - if (e >= 0) break ; - } } } /* Fill from remote */ - if (xindex[2] < 5 && x[xindex[2]].revents & IOPAUSE_READ) + if (xindex[2] < 4 && x[xindex[2]].revents & IOPAUSE_READ) { r = buffer_tls_fill(ctx, b) ; if (r < 0) { if (r == -1) strerr_warnwu2x("read from peer: ", tls_error(ctx)) ; - if (options & 1) shutdown(fds[2], SHUT_RD) ; + if (options & 1) fd_shutdown(fds[2], 0) ; /* XXX: We need a way to detect when we've received a close_notify, because then we need to trigger a write and then shut the engine @@ -305,10 +258,5 @@ int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint } } } - - if (fds[1] >= 0) fd_close(fds[1]) ; - if (fds[0] >= 0) fd_close(fds[0]) ; - if (fds[3] >= 0) fd_close(fds[3]) ; - if (fds[2] >= 0) fd_close(fds[2]) ; - return e ; + _exit(0) ; } |