git: 3854dd52a3d3 - main - syslogd: Add closelogfiles() function

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 28 Sep 2023 15:52:42 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=3854dd52a3d3ff4a7a298fba10c96c8988fb814d

commit 3854dd52a3d3ff4a7a298fba10c96c8988fb814d
Author:     Jake Freeland <jfree@FreeBSD.org>
AuthorDate: 2023-09-01 02:51:00 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-09-28 15:51:53 +0000

    syslogd: Add closelogfiles() function
    
    The closelogfiles() function completely disassembles the global filed
    list by freeing all filed components and removing them from the list.
    
    Reviewed by:    markj
    MFC after:      3 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D41388
---
 usr.sbin/syslogd/syslogd.c | 96 ++++++++++++++++++++++++----------------------
 1 file changed, 51 insertions(+), 45 deletions(-)

diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c
index 659b71385087..d317afa73844 100644
--- a/usr.sbin/syslogd/syslogd.c
+++ b/usr.sbin/syslogd/syslogd.c
@@ -2520,6 +2520,54 @@ readconfigfile(const char *path)
 	}
 }
 
+/*
+ * Close all open log files.
+ */
+static void
+closelogfiles(void)
+{
+	struct filed *f;
+
+	while (!STAILQ_EMPTY(&fhead)) {
+		f = STAILQ_FIRST(&fhead);
+		STAILQ_REMOVE_HEAD(&fhead, next);
+
+		/* flush any pending output */
+		if (f->f_prevcount)
+			fprintlog_successive(f, 0);
+
+		switch (f->f_type) {
+		case F_FILE:
+		case F_FORW:
+		case F_CONSOLE:
+		case F_TTY:
+		case F_PIPE:
+			close_filed(f);
+			break;
+		default:
+			break;
+		}
+
+		free(f->f_program);
+		free(f->f_host);
+		if (f->f_prop_filter) {
+			switch (f->f_prop_filter->cmp_type) {
+			case FILT_CMP_REGEX:
+				regfree(f->f_prop_filter->pflt_re);
+				free(f->f_prop_filter->pflt_re);
+				break;
+			case FILT_CMP_CONTAINS:
+			case FILT_CMP_EQUAL:
+			case FILT_CMP_STARTS:
+				free(f->f_prop_filter->pflt_strval);
+				break;
+			}
+			free(f->f_prop_filter);
+		}
+		free(f);
+	}
+}
+
 /*
  *  INIT -- Initialize syslogd from configuration table
  */
@@ -2527,7 +2575,6 @@ static void
 init(bool reload)
 {
 	int i;
-	struct filed *f;
 	char *p;
 	char oldLocalHostName[MAXHOSTNAMELEN];
 	char hostMsg[2*MAXHOSTNAMELEN+40];
@@ -2570,56 +2617,15 @@ init(bool reload)
 		unsetenv("TZ");
 	}
 
-	/*
-	 *  Close all open log files.
-	 */
 	Initialized = false;
-	while (!STAILQ_EMPTY(&fhead)) {
-		f = STAILQ_FIRST(&fhead);
-		STAILQ_REMOVE_HEAD(&fhead, next);
-
-		/* flush any pending output */
-		if (f->f_prevcount)
-			fprintlog_successive(f, 0);
-
-		switch (f->f_type) {
-		case F_FILE:
-		case F_FORW:
-		case F_CONSOLE:
-		case F_TTY:
-			close_filed(f);
-			break;
-		case F_PIPE:
-			close_filed(f);
-			break;
-		default:
-			break;
-		}
-
-		free(f->f_program);
-		free(f->f_host);
-		if (f->f_prop_filter) {
-			switch (f->f_prop_filter->cmp_type) {
-			case FILT_CMP_REGEX:
-				regfree(f->f_prop_filter->pflt_re);
-				free(f->f_prop_filter->pflt_re);
-				break;
-			case FILT_CMP_CONTAINS:
-			case FILT_CMP_EQUAL:
-			case FILT_CMP_STARTS:
-				free(f->f_prop_filter->pflt_strval);
-				break;
-			}
-			free(f->f_prop_filter);
-		}
-		free(f);
-	}
-
+	closelogfiles();
 	readconfigfile(ConfFile);
 	Initialized = true;
 
 	if (Debug) {
+		struct filed *f;
 		int port;
+
 		STAILQ_FOREACH(f, &fhead, next) {
 			for (i = 0; i <= LOG_NFACILITIES; i++)
 				if (f->f_pmask[i] == INTERNAL_NOPRI)