From b4eb585941580539dad0e44c8a77895fe234c09c Mon Sep 17 00:00:00 2001
From: John Estabrook <jestabro@vyos.io>
Date: Tue, 23 Feb 2021 14:58:10 -0600
Subject: configd: T3302: redirect script stdout/err to file on boot

---
 src/services/vyos-configd | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

(limited to 'src/services')

diff --git a/src/services/vyos-configd b/src/services/vyos-configd
index 3bd516463..7aea77f5a 100755
--- a/src/services/vyos-configd
+++ b/src/services/vyos-configd
@@ -34,6 +34,8 @@ from vyos import ConfigError
 
 CFG_GROUP = 'vyattacfg'
 
+script_stdout_log = '/tmp/vyos-configd-script-stdout'
+
 debug = True
 
 logger = logging.getLogger(__name__)
@@ -60,7 +62,7 @@ configd_env_unset_file = os.path.join(directories['data'], 'vyos-configd-env-uns
 # sourced on entering config session
 configd_env_file = '/etc/default/vyos-configd-env'
 
-session_tty = None
+session_out = None
 
 def key_name_from_file_name(f):
     return os.path.splitext(f)[0]
@@ -109,7 +111,7 @@ include_set = {key_name_from_file_name(f) for f in filenames if f in include}
 def run_script(script, config) -> int:
     config.set_level([])
     try:
-        with open(session_tty, 'w') as f, redirect_stdout(f):
+        with open(session_out, 'a') as f, redirect_stdout(f):
             with redirect_stderr(f):
                 c = script.get_config(config)
                 script.verify(c)
@@ -117,7 +119,7 @@ def run_script(script, config) -> int:
                 script.apply(c)
     except ConfigError as e:
         logger.critical(e)
-        with open(session_tty, 'w') as f, redirect_stdout(f):
+        with open(session_out, 'a') as f, redirect_stdout(f):
             print(f"{e}\n")
         return R_ERROR_COMMIT
     except Exception as e:
@@ -127,7 +129,7 @@ def run_script(script, config) -> int:
     return R_SUCCESS
 
 def initialization(socket):
-    global session_tty
+    global session_out
     # Reset config strings:
     active_string = ''
     session_string = ''
@@ -155,9 +157,13 @@ def initialization(socket):
 
     logger.debug(f"config session pid is {pid_string}")
     try:
-        session_tty = os.readlink(f"/proc/{pid_string}/fd/1")
+        session_out = os.readlink(f"/proc/{pid_string}/fd/1")
     except FileNotFoundError:
-        session_tty = None
+        session_out = None
+
+    # if not a 'live' session, for example on boot, write to file
+    if '/dev/pts' not in session_out:
+        session_out = script_stdout_log
 
     try:
         configsource = ConfigSourceString(running_config_text=active_string,
-- 
cgit v1.2.3