git: 765274354040 - main - devd: Warn for deprecated 'kern' system type

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 02 Dec 2022 18:08:51 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=7652743540401f0b4dd080fc635031d1822295e8

commit 7652743540401f0b4dd080fc635031d1822295e8
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-12-02 17:47:22 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-12-02 17:48:02 +0000

    devd: Warn for deprecated 'kern' system type
    
    One year ago, I deprecated 'kern' in favor of 'kernel' for the system
    name for some power events. I'm about to remove it from the kernel, but
    realized there's been no warning generated for users. Preserve POLA by
    converting on the fly here and issuing a warning for 14.x, and an fatal
    error after we branch 15. Make compiling it an error on 16 to remove
    the gross hack after we branch.
    
    Sponsored by:           Netflix
    Reviewed by:            bapt
    Differential Revision:  https://reviews.freebsd.org/D37584
---
 sbin/devd/devd.cc      | 34 ++++++++++++++++++++++++++++++++++
 sys/kern/kern_devctl.c |  2 --
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/sbin/devd/devd.cc b/sbin/devd/devd.cc
index a1b6ee479921..abce58a1e659 100644
--- a/sbin/devd/devd.cc
+++ b/sbin/devd/devd.cc
@@ -173,6 +173,8 @@ delete_and_clear(vector<T *> &v)
 
 static config cfg;
 
+static const char *curr_cf = NULL;
+
 event_proc::event_proc() : _prio(-1)
 {
 	_epsvec.reserve(4);
@@ -452,17 +454,28 @@ config::reset(void)
 	delete_and_clear(_notify_list);
 }
 
+/*
+ * Called recursively as new files are included, so current stack of old names
+ * saved in each instance of 'old' on the call stack. Called single threaded
+ * so global varaibles curr_cf and lineno (and all of yacc's parser state)
+ * are safe to access w/o a lock.
+ */
 void
 config::parse_one_file(const char *fn)
 {
+	const char *old;
+
 	devdlog(LOG_DEBUG, "Parsing %s\n", fn);
 	yyin = fopen(fn, "r");
+	old = curr_cf;
+	curr_cf = fn;
 	if (yyin == NULL)
 		err(1, "Cannot open config file %s", fn);
 	lineno = 1;
 	if (yyparse() != 0)
 		errx(1, "Cannot parse %s at line %d", fn, lineno);
 	fclose(yyin);
+	curr_cf = old;
 }
 
 void
@@ -1194,6 +1207,27 @@ new_action(const char *cmd)
 eps *
 new_match(const char *var, const char *re)
 {
+	/*
+	 * In FreeBSD 14, we changed the system=kern to system=kernel for the
+	 * resume message to match all the other 'kernel' messages. Generate a
+	 * warning for the life of 14.x that we've 'fixed' the file on the fly,
+	 * but make it a fatal error in 15.x and newer.
+	 */
+	if (strcmp(var, "kern") == 0) {
+#if __FreeBSD_version < 1500000
+		devdlog(LOG_WARNING,
+		    "Changing deprecated system='kern' to new name 'kernel' in %s line %d.",
+		    curr_cf, lineno);
+		free(const_cast<char *>(var));
+		var = strdup("kernel");
+#elif  __FreeBSD_version < 1600000
+		errx(1, "Encountered deprecated system=\"kern\" rule in %s line %d",
+		    curr_cf, lineno);
+#else
+#error "Remove this gross hack"
+#endif
+	}
+
 	eps *e = new match(cfg, var, re);
 	free(const_cast<char *>(var));
 	free(const_cast<char *>(re));
diff --git a/sys/kern/kern_devctl.c b/sys/kern/kern_devctl.c
index 414a2b74cbc7..d40559bba8f1 100644
--- a/sys/kern/kern_devctl.c
+++ b/sys/kern/kern_devctl.c
@@ -568,5 +568,3 @@ devctl_safe_quote_sb(struct sbuf *sb, const char *src)
 		sbuf_putc(sb, *src++);
 	}
 }
-
-