Crudely handle reconnecting after suspend

master
Curtis McEnroe 2018-12-01 22:05:37 -05:00
parent e3cff14e03
commit 5c17393d2b
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
5 changed files with 113 additions and 84 deletions

7
chat.h
View File

@ -44,6 +44,7 @@ void selfJoin(const char *join);
void eventWait(const char *argv[static 2]);
void eventPipe(const char *argv[static 2]);
void eventQuit(void);
noreturn void eventLoop(void);
struct Tag {
@ -103,14 +104,14 @@ void inputTab(void);
void ircInit(char *host, char *port, char *pass, char *webPass);
int ircConnect(void);
void ircDisconnect(const char *quit);
void ircRead(void);
bool ircRead(void);
void ircWrite(const char *ptr, size_t len);
void ircFmt(const char *format, ...) __attribute__((format(printf, 1, 2)));
void uiInit(void);
void uiShow(void);
void uiHide(void);
void uiExit(void);
noreturn void uiExit(void);
void uiDraw(void);
void uiRead(void);
void uiPrompt(void);

144
event.c
View File

@ -19,6 +19,7 @@
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
@ -30,10 +31,19 @@
#include "chat.h"
static struct {
bool quit;
bool wait;
bool pipe;
int fd;
} spawn;
bool susp;
int irc;
int pipe;
} event = {
.irc = -1,
.pipe = -1,
};
void eventQuit(void) {
event.quit = true;
}
void eventWait(const char *argv[static 2]) {
uiHide();
@ -43,11 +53,27 @@ void eventWait(const char *argv[static 2]) {
execvp(argv[0], (char *const *)argv);
err(EX_CONFIG, "%s", argv[0]);
}
spawn.wait = true;
event.wait = true;
}
static void childWait(void) {
uiShow();
int status;
pid_t pid = wait(&status);
if (pid < 0) err(EX_OSERR, "wait");
if (WIFEXITED(status) && WEXITSTATUS(status)) {
uiFmt(TagStatus, UIHot, "event: exit %d", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
uiFmt(
TagStatus, UIHot,
"event: signal %s", strsignal(WTERMSIG(status))
);
}
event.wait = false;
}
void eventPipe(const char *argv[static 2]) {
if (spawn.pipe) {
if (event.pipe > 0) {
uiLog(TagStatus, UIHot, L"event: existing pipe");
return;
}
@ -70,42 +96,23 @@ void eventPipe(const char *argv[static 2]) {
}
close(rw[1]);
spawn.fd = rw[0];
spawn.pipe = true;
event.pipe = rw[0];
}
static void pipeRead(void) {
char buf[256];
ssize_t len = read(spawn.fd, buf, sizeof(buf) - 1);
ssize_t len = read(event.pipe, buf, sizeof(buf) - 1);
if (len < 0) err(EX_IOERR, "read");
if (len) {
buf[len] = '\0';
len = strcspn(buf, "\n");
uiFmt(TagStatus, UIHot, "event: %.*s", (int)len, buf);
buf[strcspn(buf, "\n")] = '\0';
uiFmt(TagStatus, UIHot, "event: %s", buf);
} else {
close(spawn.fd);
spawn.pipe = false;
close(event.pipe);
event.pipe = -1;
}
}
static void handleChild(void) {
int status;
pid_t pid = wait(&status);
if (pid < 0) err(EX_OSERR, "wait");
if (WIFEXITED(status) && WEXITSTATUS(status)) {
uiFmt(TagStatus, UIHot, "event: exit %d", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
uiFmt(TagStatus, UIHot, "event: signal %d", WTERMSIG(status));
}
spawn.wait = false;
}
static void handleInterrupt(void) {
input(TagStatus, "/quit");
uiExit();
exit(EX_OK);
}
static sig_atomic_t sig[NSIG];
static void handler(int n) {
sig[n] = 1;
@ -114,43 +121,47 @@ static void handler(int n) {
noreturn void eventLoop(void) {
sigset_t mask;
sigemptyset(&mask);
struct sigaction sa = {
struct sigaction action = {
.sa_handler = handler,
.sa_mask = mask,
.sa_flags = SA_RESTART,
.sa_flags = SA_RESTART | SA_NOCLDSTOP,
};
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
struct sigaction curses;
sigaction(SIGWINCH, &sa, &curses);
sigaction(SIGWINCH, &action, &curses);
assert(!(curses.sa_flags & SA_SIGINFO));
int irc = ircConnect();
struct pollfd fds[3] = {
{ irc, POLLIN, 0 },
{ STDIN_FILENO, POLLIN, 0 },
{ -1, POLLIN, 0 },
};
event.irc = ircConnect();
for (;;) {
if (sig[SIGCHLD]) handleChild();
if (sig[SIGINT]) handleInterrupt();
if (sig[SIGCHLD]) childWait();
if (sig[SIGINT]) {
signal(SIGINT, SIG_DFL);
ircFmt("QUIT :Goodbye\r\n");
event.quit = true;
}
if (sig[SIGTSTP]) {
signal(SIGTSTP, SIG_DFL);
ircFmt("QUIT :zzz\r\n");
event.susp = true;
}
if (sig[SIGWINCH]) {
curses.sa_handler(SIGWINCH);
uiRead();
uiDraw();
}
sig[SIGCHLD] = 0;
sig[SIGINT] = 0;
sig[SIGWINCH] = 0;
sig[SIGCHLD] = sig[SIGINT] = sig[SIGTSTP] = sig[SIGWINCH] = 0;
nfds_t nfds = 2;
if (spawn.wait) nfds = 1;
if (spawn.pipe) {
fds[2].fd = spawn.fd;
nfds = 3;
}
nfds_t nfds = 0;
struct pollfd fds[3] = {
{ .events = POLLIN },
{ .events = POLLIN },
{ .events = POLLIN },
};
if (!event.wait) fds[nfds++].fd = STDIN_FILENO;
if (event.irc > 0) fds[nfds++].fd = event.irc;
if (event.pipe > 0) fds[nfds++].fd = event.pipe;
int ready = poll(fds, nfds, -1);
if (ready < 0) {
@ -158,10 +169,25 @@ noreturn void eventLoop(void) {
err(EX_IOERR, "poll");
}
if (fds[0].revents) ircRead();
if (nfds > 1 && fds[1].revents) uiRead();
if (nfds > 2 && fds[2].revents) pipeRead();
if (nfds > 1) uiDraw();
for (nfds_t i = 0; i < nfds; ++i) {
if (!fds[i].revents) continue;
if (fds[i].fd == STDIN_FILENO) uiRead();
if (fds[i].fd == event.pipe) pipeRead();
if (fds[i].fd == event.irc) {
if (ircRead()) continue;
event.irc = -1;
// TODO: Handle unintended disconnects.
if (event.quit) uiExit();
if (event.susp) {
uiHide();
raise(SIGTSTP);
sigaction(SIGTSTP, &action, NULL);
uiShow();
event.irc = ircConnect();
event.susp = false;
}
}
}
uiDraw();
}
}

View File

@ -110,6 +110,7 @@ static void inputQuit(struct Tag tag, char *params) {
} else {
ircFmt("QUIT :Goodbye\r\n");
}
eventQuit();
}
static void inputURL(struct Tag tag, char *params) {

37
irc.c
View File

@ -19,6 +19,7 @@
#include <netdb.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -103,27 +104,16 @@ int ircConnect(void) {
);
}
/// FIXME
if (self.user[0] == '~') selfUser(&self.user[1]);
if (irc.pass) ircFmt("PASS :%s\r\n", irc.pass);
ircFmt(
"NICK %s\r\n"
"USER %s 0 * :%s\r\n",
self.nick, self.user, self.nick
);
ircFmt("NICK %s\r\n", self.nick);
ircFmt("USER %s 0 * :%s\r\n", self.user, self.nick);
return irc.sock;
}
void ircDisconnect(const char *quit) {
// TODO: Wait for response, send quit to UI.
ircFmt("QUIT :%s\r\n", quit);
int error = tls_close(irc.client);
if (error) errx(EX_IOERR, "tls_close: %s", tls_error(irc.client));
error = close(irc.sock);
if (error) err(EX_IOERR, "close");
}
void ircWrite(const char *ptr, size_t len) {
while (len) {
ssize_t ret = tls_write(irc.client, ptr, len);
@ -151,15 +141,23 @@ void ircFmt(const char *format, ...) {
free(buf);
}
void ircRead(void) {
static void disconnect(void) {
int error = tls_close(irc.client);
if (error) errx(EX_IOERR, "tls_close: %s", tls_error(irc.client));
error = close(irc.sock);
if (error) err(EX_IOERR, "close");
}
bool ircRead(void) {
static char buf[4096];
static size_t len;
ssize_t read = tls_read(irc.client, &buf[len], sizeof(buf) - len);
if (read < 0) errx(EX_IOERR, "tls_read: %s", tls_error(irc.client));
if (!read) {
uiExit();
exit(EX_OK);
disconnect();
len = 0;
return false;
}
len += read;
@ -178,4 +176,5 @@ void ircRead(void) {
len -= line - buf;
memmove(buf, line, len);
return true;
}

8
ui.c
View File

@ -22,6 +22,7 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <sysexits.h>
#include <wchar.h>
@ -81,9 +82,10 @@ static struct {
struct View *view;
} ui;
static void uiShow(void) {
void uiShow(void) {
ui.hide = false;
termMode(TermFocus, true);
uiDraw();
}
void uiHide(void) {
@ -109,12 +111,13 @@ void uiInit(void) {
uiViewTag(TagStatus);
}
void uiExit(void) {
noreturn void uiExit(void) {
uiHide();
printf(
"This program is AGPLv3 Free Software!\n"
"The source is available at <" SOURCE_URL ">.\n"
);
exit(EX_OK);
}
static int lastLine(void) {
@ -557,7 +560,6 @@ void uiPrompt(void) {
}
void uiRead(void) {
uiShow();
int ret;
wint_t ch;
while (ERR != (ret = wget_wch(ui.input, &ch))) {