mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-03-31 08:50:07 +02:00
🐛 Prevent crashing with RealCommand
This addresses some potential crash issues with RealCommand. The PHPInstallation health check has been updated to accommodate the potential error strings, namely: - PHPMON_COMMAND_UNCAUGHT_SIGNAL - PHPMON_FILE_HANDLE_READ_FAILURE
This commit is contained in:
@@ -15,6 +15,8 @@ public class RealCommand: CommandProtocol {
|
||||
withStandardError: Bool
|
||||
) -> String {
|
||||
let task = Process()
|
||||
var output = ""
|
||||
|
||||
task.launchPath = path
|
||||
task.arguments = arguments
|
||||
|
||||
@@ -26,10 +28,28 @@ public class RealCommand: CommandProtocol {
|
||||
}
|
||||
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output: String = String.init(data: data, encoding: String.Encoding.utf8)!
|
||||
defer {
|
||||
try? pipe.fileHandleForReading.close()
|
||||
}
|
||||
|
||||
// Handle termination
|
||||
if task.terminationReason == .uncaughtSignal {
|
||||
Log.err("The command `\(path) w/ args: \(arguments)` likely crashed. Returning UNCAUGHT_SIGNAL.")
|
||||
return "PHPMON_COMMAND_UNCAUGHT_SIGNAL"
|
||||
}
|
||||
|
||||
// Try reading from file handle and close it
|
||||
if let data = try? pipe.fileHandleForReading.readToEnd() {
|
||||
if let string = String(data: data, encoding: .utf8) {
|
||||
output = string
|
||||
} else {
|
||||
return "PHPMON_FILE_HANDLE_READ_FAILURE"
|
||||
}
|
||||
}
|
||||
|
||||
// Trim newline output if necessary
|
||||
if trimNewlines {
|
||||
return output.components(separatedBy: .newlines)
|
||||
.filter({ !$0.isEmpty })
|
||||
|
||||
@@ -94,11 +94,23 @@ class PhpInstallation {
|
||||
withStandardError: true
|
||||
).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
// The PHP executable did not return any output
|
||||
if testCommand.isEmpty || testCommand.contains("HANDLE_READ_FAILURE") {
|
||||
Log.err("No output. PHP \(self.versionNumber.short) is not healthy!")
|
||||
self.isHealthy = false
|
||||
}
|
||||
|
||||
// The PHP executable crashed with an uncaught signal when we tried to run this
|
||||
if testCommand.contains("UNCAUGHT_SIGNAL") {
|
||||
Log.err("Uncaught signal, PHP \(self.versionNumber.short) is not healthy!")
|
||||
self.isHealthy = false
|
||||
}
|
||||
|
||||
// If the "dyld: Library not loaded" issue pops up, we have an unhealthy PHP installation
|
||||
// and we will need to reinstall this version of PHP via Homebrew.
|
||||
if testCommand.contains("Library not loaded") && testCommand.contains("dyld") {
|
||||
Log.err("dyld error, PHP \(self.versionNumber.short) is not healthy!")
|
||||
self.isHealthy = false
|
||||
Log.err("The PHP installation of \(self.versionNumber.short) is not healthy!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user