[Git][NTPsec/ntpsec][master] Make write to logs thread-safe.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Mar 26 15:45:33 UTC 2019



Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
885a1a2d by Eric S. Raymond at 2019-03-26T15:45:13Z
Make write to logs thread-safe.

- - - - -


1 changed file:

- libntp/msyslog.c


Changes:

=====================================
libntp/msyslog.c
=====================================
@@ -154,11 +154,23 @@ addto_syslog(
 	}
 
 	if (log_to_file) {
+	 	/*
+		 * Thread-safe write, the de-facto way.  It's not
+		 * actually guaranteed by standards that a write of
+		 * PIPE_BUF chars or less is atomic anywhere but on a
+		 * pipe.  In ancient times this was 512 and happened to
+		 * be equal to the usual size of a hardware disk sector,
+		 * which was what really bounded atomicity. The actual
+		 * answer under POSIX is SSIZE_MAX, which is far larger
+		 * than we want or need to allocate here.
+		 */
+		char buf[PIPE_BUF];
+		buf[0] = '\0';
 		if (msyslog_include_timestamp)
-			fprintf(syslog_file, "%s ", human_time);
-		fprintf(syslog_file, "%s[%d]: %s%s", prog, pid, msg,
-			nl_or_empty);
-		fflush(syslog_file);
+			snprintf(buf, sizeof(buf), "%s ", human_time);
+		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
+			 "%s[%d]: %s%s", prog, pid, msg, nl_or_empty);
+		IGNORE(write(fileno(syslog_file), buf, strlen(buf)));
 	}
 }
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/885a1a2d6eba65294e5a070c9f924d71c09dbf4e

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/885a1a2d6eba65294e5a070c9f924d71c09dbf4e
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190326/15d09d77/attachment-0001.html>


More information about the vc mailing list