[hackers] [ii][PATCH] Always clean before exit

From: Petr Vaněk <arkamar_AT_atlas.cz>
Date: Thu, 29 Sep 2022 10:40:48 +0200

This improves situations when ii exits unexpectedly, due to a ping
timeout or closed remote connection, for example. If this happens, all
"in" FIFOs are removed before exit in similar fashion to regular exit
issued with "/q" command.
---
 ii.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/ii.c b/ii.c
index 940b2fb..a7dfb1f 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -54,6 +54,7 @@ static int       channel_open(Channel *);
 static void      channel_print(Channel *, const char *);
 static int       channel_reopen(Channel *);
 static void      channel_rm(Channel *);
+static void      clean(void);
 static void      create_dirtree(const char *);
 static void      create_filepath(char *, size_t, const char *, const char *, const char *);
 static void      die(const char *, ...);
_AT_@ -92,6 +93,8 @@ die(const char *fmt, ...)
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
 	va_end(ap);
+
+	clean();
 	exit(1);
 }
 
_AT_@ -170,6 +173,20 @@ channel_normalize_name(char *s)
 	*p = '\0';
 }
 
+static void
+clean(void)
+{
+	Channel *c, *tmp;
+
+	if (channelmaster)
+		channel_leave(channelmaster);
+
+	for (c = channels; c; c = tmp) {
+		tmp = c->next;
+		channel_leave(c);
+	}
+}
+
 static void
 create_filepath(char *filepath, size_t len, const char *path,
 	const char *channel, const char *suffix)
_AT_@ -757,6 +774,7 @@ run(int ircfd, const char *host)
 		} else if (r == 0) {
 			if (time(NULL) - last_response >= PING_TIMEOUT) {
 				channel_print(channelmaster, "-!- ii shutting down: ping timeout");
+				clean();
 				exit(2); /* status code 2 for timeout */
 			}
 			ewritestr(ircfd, ping_msg);
_AT_@ -777,7 +795,6 @@ run(int ircfd, const char *host)
 int
 main(int argc, char *argv[])
 {
-	Channel *c, *tmp;
 	struct passwd *spw;
 	const char *key = NULL, *fullname = NULL, *host = "";
 	const char *uds = NULL, *service = "6667";
_AT_@ -843,13 +860,7 @@ main(int argc, char *argv[])
 	loginuser(ircfd, host, fullname && *fullname ? fullname : nick);
 	setup();
 	run(ircfd, host);
-	if (channelmaster)
-		channel_leave(channelmaster);
-
-	for (c = channels; c; c = tmp) {
-		tmp = c->next;
-		channel_leave(c);
-	}
+	clean();
 
 	return 0;
 }
-- 
2.35.1
Received on Thu Sep 29 2022 - 10:40:48 CEST

This archive was generated by hypermail 2.3.0 : Thu Sep 29 2022 - 11:12:42 CEST