From 4b9284a8d6a2af80c809a35799bbeba2cc1af363 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Thu, 14 Mar 2019 14:32:59 +0000 Subject: [PATCH] add option to suppress debug output --- ChangeLog | 8 +++++ EcProcess.h | 9 +++++ EcProcess.m | 98 ++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index 807fd5c..0435adb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2019-03-02 Richard Frith-Macdonald + + * EcProces.h: + * EcProces.m: + Add KillDebugOutput option to configure a process to avoid logging + on systems where logged information should not be available for + security reasons. This option is not overidable from the Console. + 2019-02-15 Richard Frith-Macdonald * EcControl.m: If we get an alarm clear before we have reported the diff --git a/EcProcess.h b/EcProcess.h index ef3bff2..beea0d4 100644 --- a/EcProcess.h +++ b/EcProcess.h @@ -251,6 +251,15 @@ extern NSString* cmdVersion(NSString *ver); * The default (EcKeepStandardError set to NO) is to merge the * standard error logging with the debug logging. * + * EcKillDebugOutput + * + * This boolean value determines whether debug output (including anything + * written to the standard error output if that is merged with debug) + * should be discarded (sent to the null device).
+ * This setting cannot e controlled from the Console command line.
+ * The default (EcKillDebugOutput set to NO) is to write debug output to + * file. + *
* EcTransient * * This boolean option is used to specify that the program diff --git a/EcProcess.m b/EcProcess.m index 6172758..b6ecf81 100644 --- a/EcProcess.m +++ b/EcProcess.m @@ -2009,6 +2009,31 @@ static NSString *noFiles = @"No log files to archive"; [EcDefaultRegistration defaultsChanged: cmdDefs]; + /* Update debug output kill status if necessary. + */ + if ([cmdDefs boolForKey: @"KillDebugOutput"] != cmdKillDebug) + { + NSFileHandle *hdl; + + [ecLock lock]; + hdl = [cmdLogMap objectForKey: cmdDebugName]; + if (hdl != nil) + { + if (cmdKillDebug == NO) + { + NSString *msg; + + msg = cmdLogFormat(LT_WARNING, + @"Logging suppressed by KillDebugOutput=YES"); + [hdl writeData: [msg dataUsingEncoding: NSUTF8StringEncoding]]; + } + [self ecLogEnd: cmdDebugName to: nil]; + } + [ecLock unlock]; + cmdKillDebug = (NO == cmdKillDebug ? YES : NO); + [self cmdLogFile: cmdDebugName]; + } + enumerator = [cmdDebugKnown keyEnumerator]; while (nil != (mode = [enumerator nextObject])) { @@ -2246,54 +2271,55 @@ static NSString *noFiles = @"No log files to archive"; hdl = [cmdLogMap objectForKey: name]; if (nil == hdl) { - NSFileManager *mgr = [NSFileManager defaultManager]; - NSString *path; - - path = [cmdLogsDir(nil) stringByAppendingPathComponent: name]; - /* Archive any old left-over file. */ [self _moveLog: name to: nil]; - /* - * Create the file if necessary, and open it for updating. - */ - if ([mgr isWritableFileAtPath: path] == NO - && [mgr createFileAtPath: path contents: nil attributes: nil] == NO) + if (YES == cmdKillDebug && [name isEqual: cmdDebugName] == YES) { - NSLog(@"File '%@' is not writable and can't be created", path); + /* Output is killed so we don't need to create the file + * and can simply write to /dev/null + */ + hdl = [NSFileHandle fileHandleWithNullDevice]; } else { - hdl = [NSFileHandle fileHandleForUpdatingAtPath: path]; - if (hdl == nil) + NSFileManager *mgr = [NSFileManager defaultManager]; + NSString *path; + + path = [cmdLogsDir(nil) stringByAppendingPathComponent: name]; + + /* Create the file if necessary, and open it for updating. + */ + if ([mgr isWritableFileAtPath: path] == NO + && [mgr createFileAtPath: path contents: nil attributes: nil] == NO) { - if (status != nil) - { - NSLog(@"%@", status); - } - NSLog(@"Unable to log to %@", path); + NSLog(@"File '%@' is not writable and can't be created", path); } else { - [hdl seekToEndOfFile]; + hdl = [NSFileHandle fileHandleForUpdatingAtPath: path]; + if (hdl == nil) + { + if (status != nil) + { + NSLog(@"%@", status); + } + NSLog(@"Unable to log to %@", path); + } + else + { + [hdl seekToEndOfFile]; + } } } + if (hdl == nil) { - [self ecUnLock]; + [self ecUnLock]; return nil; } - if (YES == cmdKillDebug && [name isEqual: cmdDebugName] == YES) - { - const char *msg = "Logging suppressed by KillDebugOutput=YES\n"; - - [hdl writeData: [NSData dataWithBytes: msg length: strlen(msg)]]; - [hdl closeFile]; - hdl = [NSFileHandle fileHandleWithNullDevice]; - } - /* As a special case, if this is the default debug file * we must set it up to write to stderr. */ @@ -4026,7 +4052,12 @@ NSLog(@"Ignored attempt to set timer interval to %g ... using 10.0", interval); if ([key isEqualToString: ecControlKey]) { [self cmdPrintf: @"%@ can only be set on startup.\n", key]; - val = nil; + return; + } + else if ([key isEqualToString: @"KillDebugOutput"]) + { + [self cmdPrintf: @"%@ can not be overridden.\n", key]; + return; } else { @@ -4040,7 +4071,12 @@ NSLog(@"Ignored attempt to set timer interval to %g ... using 10.0", interval); if ([key isEqualToString: ecControlKey]) { [self cmdPrintf: @"%@ can only be set on startup.\n", key]; - val = nil; + return; + } + else if ([key isEqualToString: @"KillDebugOutput"]) + { + [self cmdPrintf: @"%@ can not be overridden.\n", key]; + return; } else if ([msg count] == 4) {