From ef9ced7f9cff69646856adc5331f3d04a66cbe00 Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@nsc.liu.se>
Date: Fri, 16 Aug 2019 22:18:45 +0200
Subject: [PATCH] Better Upstart conflict handling for serial getty.

Previously, on RHEL-6, we always disabled the automatic starting
of getty on serial port consoles, to avoid conflicts with explicit
configuration of the same port.  This had the disadvantage that even
if logins were not explicit configured for, e.g, ttyS17, and you
booted the system with console=ttyS17, you would not get a login
prompt there.  That is probably not what most users would want.

We here make use of the 'stop on' event handling in Upstart job
definitions, to force the automatic getty job to be stopped, if an
explicit getty job for that port is started.  This assumes that such
jobs are named "serial-$DEV", as the console::serial::login definition
names them.

Signed-off-by: Thomas Bellman <bellman@lysator.liu.se>
---
 manifests/serial/login.pp | 47 ++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/manifests/serial/login.pp b/manifests/serial/login.pp
index 6ca779c..7ce676d 100644
--- a/manifests/serial/login.pp
+++ b/manifests/serial/login.pp
@@ -119,7 +119,7 @@ define console::serial::login::rhel_6(
 
     if ($ensure == 'enabled')
     {
-	include console::serial::login::rhel_6::disable_auto_serial
+	include console::serial::login::rhel_6::conflict_auto_serial
 
 	$lines = [
 	    'start on stopped rc RUNLEVEL=[2345]',
@@ -136,17 +136,6 @@ define console::serial::login::rhel_6(
 		notify => Exec["console::serial::login::rhel_6::${name}"];
 	}
 	exec {
-	    "console::serial::login::rhel_6::autoserial-stop::${name}":
-		command => shellquote(
-		    '/sbin/initctl', 'stop', 'serial', "DEV=${name}"),
-		onlyif => sprintf(
-		    '%s | /bin/grep -q "start/running"',
-		    shellquote('/sbin/initctl', 'status', 'serial',
-			       "DEV=${name}")),
-		path => '/sbin:/usr/sbin:/bin:/usr/bin',
-		before => Class[
-		    'console::serial::login::rhel_6::disable_auto_serial'];
-
 	    "console::serial::login::rhel_6::${name}":
 		command => shellquote('/sbin/initctl', 'start', $svcname),
 		unless => sprintf(
@@ -179,14 +168,36 @@ define console::serial::login::rhel_6(
 
 # Internal helper class
 # The default /etc/init/serial.conf upstart job runs an agetty process on the
-# primary console, if it is a serial port.  That clashes with an agetty job
-# configured explicitly on that port.  Disable this automatic running of
-# agetty, to avoid this.
+# primary console, if it is a serial port.  That would clash with an agetty
+# job configured explicitly on that port.  This class causes that automatic
+# agetty job to be stopped if an explicit job for the console port is started.
 #
-class console::serial::login::rhel_6::disable_auto_serial
+class console::serial::login::rhel_6::conflict_auto_serial
 {
+    # This is a bit of an abuse of disable_file.  Problem is that older
+    # versions of console::serial::login::rhel_6 disabled serial.conf
+    # entirely to avoid the conflict, by renaming the file to .DISABLED.
+    # Here we un-disable any such file by "disabling" it to its original
+    # name...
     disable_file {
-	'/etc/init/serial.conf':
-	    renameto => '%D/%F.DISABLED';
+	'/etc/init/serial.conf.DISABLED':
+	    renameto => '%D/serial.conf',
+	    before => Regexp_replace_lines['console::serial::login::rhel_6::conflict_auto_serial'],
+	    notify => Exec['console::serial::login::rhel_6::conflict_auto_serial::reload_init'];
+    }
+
+    regexp_replace_lines {
+	'console::serial::login::rhel_6::conflict_auto_serial':
+	    file => '/etc/init/serial.conf',
+	    pattern => '^stop on .*',
+	    replacement => '\& or starting serial-$DEV',
+	    skip => '.* starting serial-\$DEV.*',
+	    notify => Exec['console::serial::login::rhel_6::conflict_auto_serial::reload_init'];
+    }
+
+    exec {
+	'console::serial::login::rhel_6::conflict_auto_serial::reload_init':
+	    command => '/sbin/initctl reload-configuration',
+	    refreshonly => true;
     }
 }
-- 
GitLab