shithub: ircs

Download patch

ref: 30d35379c291756e560de78b033c9fc0f3d443b7
parent: c5df4bd968e0c643de952eac7287497094334b62
author: kemal <[email protected]>
date: Sun Oct 3 05:17:29 EDT 2021

ircs: do logsend in one write, simplify ircsend

when 2 logsends happen at the same time, as logsend
writes the timestamp and the newline in seperate
writes, the writes can mixup and messages get print
incorrectly.

this bug can be triggered by running rc scripts
that sends irc commands right after ircs boots.
as ircs is busy logging the motd in the time period
that we send the command, 2 logsends will occur at
the same time.

doing it in one eiowrite fixes the issue.

--- a/main.c
+++ b/main.c
@@ -231,16 +231,10 @@
 static void
 ircsend(char *msg)
 {
-	long len;
-
-	len = strlen(msg);
-	if(debug) fprint(2, "ircsend: %s", msg);
-	if(len > 0){
-		eiowrite(outio, ircfd, msg, len);
-		if(msg[len-1] != '\n'){
-			if(debug) fprint(2, "\n");
-			eiowrite(outio, ircfd, "\r\n", 2);
-		}
+	if(debug) fprint(2, "ircsend: %s\n", msg);
+	if(*msg != 0){
+		eiowrite(outio, ircfd, msg, strlen(msg));
+		eiowrite(outio, ircfd, "\r\n", 2);
 	}
 }
 
@@ -247,11 +241,10 @@
 static void
 logsend(char *msg, long time, Log *log)
 {
-	long len, n;
 	char buf[Bufsize];
+	int n;
 
-	len = strlen(msg);
-	if(len > 0 && log->fd > 0){
+	if(*msg != 0 && log->fd > 0){
 		if(timestamps){
 			if(rawlog)
 				n = snprint(buf, sizeof(buf),
@@ -259,12 +252,10 @@
 			else
 				n = ircfmttime(buf, sizeof(buf),
 					time, &log->mday);
-			eiowrite(logio, log->fd, buf, n);
-		}
-		n = len;
-		eiowrite(logio, log->fd, msg, n);
-		if(msg[n-1] != '\n')
-			eiowrite(logio, log->fd, "\n", 1);
+		}else
+			n = 0;
+		n += snprint(buf+n, sizeof(buf)-n, "%s\n", msg);
+		eiowrite(logio, log->fd, buf, n);
 	}
 }