Skip to content

Commit

Permalink
configd: improve error handling while configd is either not active or…
Browse files Browse the repository at this point in the history
… not functional.

- reconnecting the socket stream_socket_client() is safe while not executing commands
- if configd dies during communication, we should log and retun an empty response. The caller should handle operation, since you can't be sure restarting the action is a safe operation.

closes #3744
  • Loading branch information
AdSchellevis committed Oct 10, 2019
1 parent 65212fc commit 817be51
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/opnsense/mvc/app/library/OPNsense/Core/Backend.php
Expand Up @@ -80,21 +80,24 @@ public function configdRun($event, $detach = false, $timeout = 120, $connect_tim

// wait until socket exist for a maximum of $connect_timeout
$timeout_wait = $connect_timeout;
while (!file_exists($this->configdSocket)) {
while (
!file_exists($this->configdSocket) ||
($stream = @stream_socket_client('unix://'.$this->configdSocket, $errorNumber, $errorMessage, $poll_timeout)) === false
) {
sleep(1);
$timeout_wait -= 1;
if ($timeout_wait <= 0) {
$this->getLogger()->error("failed waiting for configd (doesn't seem to be running)");
if (file_exists($this->configdSocket)) {
$this->getLogger()->error("Failed to connect to configd socket: $errorMessage while executing " . $event);
return null;
} else {
$this->getLogger()->error("failed waiting for configd (doesn't seem to be running)");
}
return null;
}
}

$resp = '';
$stream = @stream_socket_client('unix://'.$this->configdSocket, $errorNumber, $errorMessage, $poll_timeout);
if ($stream === false) {
$this->getLogger()->error("Failed to connect to configd socket: $errorMessage while executing " . $event);
return null;
}

stream_set_timeout($stream, $poll_timeout);
// send command
Expand All @@ -118,6 +121,9 @@ public function configdRun($event, $detach = false, $timeout = 120, $connect_tim
if ((time() - $starttime) > $timeout) {
$this->getLogger()->error("Timeout (".$timeout.") executing : ".$event);
return null;
} elseif (feof($stream)) {
$this->getLogger()->error("Configd disconnected while executing : ".$event);
return null;
}
}

Expand Down

0 comments on commit 817be51

Please sign in to comment.