commit 723bee54d360957eec85ac2eef9ebd5516db3ade from: Omar Polo date: Tue Mar 09 05:49:19 2021 UTC add timeout for connection and some s/die/_exit Crashing is some situation is not a good idea. If the other process is dead, it has probably already crashed, so crashing again would overwrite the core file, and in every case it doesn't tell us much. It's more interesting to see why the first process crashed in the first place. commit - b0003f5861f022ca86f3b254ebb0c3f2fdadfe4e commit + 723bee54d360957eec85ac2eef9ebd5516db3ade blob - b9d7b8287a090c303ff1b6a8df20342f17075afb blob + a1c257215f7f6c1e9d123d4250f34403415a94e1 --- gemini.c +++ gemini.c @@ -67,6 +67,9 @@ static void handle_proceed(struct imsg*, size_t); static void handle_stop(struct imsg*, size_t); static void handle_quit(struct imsg*, size_t); +/* TODO: making this customizable */ +struct timeval timeout_for_handshake = { 5, 0 }; + static imsg_handlerfn *handlers[] = { [IMSG_GET] = handle_get, [IMSG_CERT_STATUS] = handle_cert_status, @@ -222,6 +225,11 @@ do_handshake(int fd, short ev, void *d) struct req *req = d; const char *hash; + if (ev == EV_TIMEOUT) { + close_with_err(req, "Timeout loading page"); + return; + } + switch (tls_handshake(req->ctx)) { case TLS_WANT_POLLIN: yield_r(req, do_handshake, NULL); @@ -345,6 +353,9 @@ parse_reply(struct req *req) req->buf, len); imsg_flush(ibuf); + if (code != 20) + close_conn(0, 0, req); + return; err: @@ -417,7 +428,7 @@ handle_get(struct imsg *imsg, size_t datalen) } TAILQ_INSERT_HEAD(&reqhead, req, reqs); - yield_w(req, do_handshake, NULL); + yield_w(req, do_handshake, &timeout_for_handshake); return; err: @@ -478,15 +489,15 @@ dispatch_imsg(int fd, short ev, void *d) if ((n = imsg_read(ibuf)) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) return; - die(); + _exit(1); } if (n == 0) - die(); + _exit(1); for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) - die(); + _exit(1); if (n == 0) return; datalen = imsg.hdr.len - IMSG_HEADER_SIZE;