Clean up Transport::execute() and callers.

- Ensure that the real exit code of the child program is retrieved
  using WEXITSTATUS().

- Centralise the handling of code 127, which means that the child
  shell process could not execute its child (ssh, rsync, or curl.)

Signed-off-by: Russell Steicke <russells@adelie.cx>
This commit is contained in:
Russell Steicke 2013-03-03 20:57:26 +08:00 committed by Paul Beckingham
parent 3dab5a1cb1
commit 1428a4135b
7 changed files with 41 additions and 33 deletions

View file

@ -30,6 +30,10 @@
#include <iostream>
#include <stdlib.h>
#include <util.h>
#include <string.h>
#include <errno.h>
#include <text.h>
#include <i18n.h>
#include <Transport.h>
#include <TransportSSH.h>
#include <TransportRSYNC.h>
@ -87,7 +91,17 @@ int Transport::execute()
it->append("\"");
}
}
return ::execute(_executable, _arguments);
int result = ::execute (_executable, _arguments);
int err;
switch (result) {
case 127:
throw format (STRING_TRANSPORT_NORUN, _executable);
case -1:
err = errno;
throw format (STRING_TRANSPORT_NOFORK, _executable, ::strerror(err));
default:
return result;
}
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -80,14 +80,8 @@ void TransportCurl::send(const std::string& source)
_arguments.push_back (_uri._protocol + "://" + _uri._host + "/" + _uri._path);
}
int result = execute();
if (result)
{
if (result == 127) // command not found
throw std::string (STRING_TRANSPORT_CURL_NORUN);
else
throw std::string (STRING_TRANSPORT_CURL_FAIL);
}
if (execute ())
throw std::string (STRING_TRANSPORT_CURL_FAIL);
}
////////////////////////////////////////////////////////////////////////////////
@ -148,14 +142,8 @@ void TransportCurl::recv(std::string target)
_arguments.insert (_arguments.end (), targetargs.begin (), targetargs.end ());
int result = execute();
if (result)
{
if (result == 127) // command not found
throw std::string (STRING_TRANSPORT_CURL_NORUN);
else
throw std::string (STRING_TRANSPORT_CURL_FAIL);
}
if (execute ())
throw std::string (STRING_TRANSPORT_CURL_FAIL);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -65,8 +65,8 @@ void TransportRSYNC::send(const std::string& source)
_arguments.push_back (_uri._host + "::" + _uri._path);
}
if (execute())
throw std::string (STRING_TRANSPORT_RSYNC_NORUN);
if (execute ())
throw std::string (STRING_TRANSPORT_RSYNC_FAIL);
}
////////////////////////////////////////////////////////////////////////////////
@ -95,8 +95,8 @@ void TransportRSYNC::recv(std::string target)
_arguments.push_back (target);
if (execute())
throw std::string (STRING_TRANSPORT_RSYNC_NORUN);
if (execute ())
throw std::string (STRING_TRANSPORT_RSYNC_FAIL);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -67,8 +67,8 @@ void TransportSSH::send(const std::string& source)
_arguments.push_back (_uri._host + ":" + escape (_uri._path, ' '));
}
if (execute())
throw std::string (STRING_TRANSPORT_SSH_NORUN);
if (execute ())
throw std::string (STRING_TRANSPORT_SSH_FAIL);
}
////////////////////////////////////////////////////////////////////////////////
@ -100,8 +100,8 @@ void TransportSSH::recv(std::string target)
_arguments.push_back (target);
if (execute())
throw std::string (STRING_TRANSPORT_SSH_NORUN);
if (execute ())
throw std::string (STRING_TRANSPORT_SSH_FAIL);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -837,15 +837,16 @@
#define STRING_TEXT_AMBIGUOUS "Ambiguous {1} '{2}' - could be either of "
// Transport
#define STRING_TRANSPORT_NORUN "Could not run '{1}'. Is it installed, and available in $PATH?"
#define STRING_TRANSPORT_NOFORK "Could not run '{1}': {2}. Are you out of system resources?"
#define STRING_TRANSPORT_URI_NODIR "The uri '{1}' does not appear to be a directory."
#define STRING_TRANSPORT_CURL_URI "When using the 'curl' protocol, the uri must contain a hostname."
#define STRING_TRANSPORT_CURL_WILDCD "When using the 'curl' protocol, wildcards are not supported."
#define STRING_TRANSPORT_CURL_NORUN "Could not run curl. Is it installed, and available in $PATH?"
#define STRING_TRANSPORT_CURL_FAIL "Curl failed, see output above."
#define STRING_TRANSPORT_RSYNC_URI "When using the 'rsync' protocol, the uri must contain a hostname."
#define STRING_TRANSPORT_RSYNC_NORUN "Could not run rsync. Is it installed, and available in $PATH?"
#define STRING_TRANSPORT_RSYNC_FAIL "rsync failed, see output above."
#define STRING_TRANSPORT_SSH_URI "When using the 'ssh' protocol, the uri must contain a hostname."
#define STRING_TRANSPORT_SSH_NORUN "Could not run ssh. Is it installed, and available in $PATH?"
#define STRING_TRANSPORT_SSH_FAIL "ssh failed, see output above."
// Uri
#define STRING_URI_QUOTES "Could not parse uri '{1}', wrong usage of single quotes."

View file

@ -853,15 +853,16 @@
#define STRING_TEXT_AMBIGUOUS "Ambiguo {1} '{2}' - puede ser "
// Transport
#define STRING_TRANSPORT_NORUN "Could not run '{1}'. Is it installed, and available in $PATH?"
#define STRING_TRANSPORT_NOFORK "Could not run '{1}': {2}. Are you out of system resources?"
#define STRING_TRANSPORT_URI_NODIR "El uri '{1}' no parece ser un directorio."
#define STRING_TRANSPORT_CURL_URI "Cuando se usa el protocolo 'curl' el uri debe contener un nombre de máquina."
#define STRING_TRANSPORT_CURL_WILDCD "Cuando se usa el protocolo 'curl' no están soportados los comodines."
#define STRING_TRANSPORT_CURL_NORUN "No se pudo lanzar curl. ¿Está instalado y disponible en $PATH?"
#define STRING_TRANSPORT_CURL_FAIL "Curl falló, consulte los mensajes precedentes."
#define STRING_TRANSPORT_RSYNC_URI "Cuando se usa el protocolo 'rsync' el uri debe contener un nombre de máquina."
#define STRING_TRANSPORT_RSYNC_NORUN "No se pudo lanzar rsync. ¿Está instalado y disponible en $PATH?"
#define STRING_TRANSPORT_RSYNC_FAIL "rsync failed, see output above."
#define STRING_TRANSPORT_SSH_URI "Cuando se usa el protocolo 'ssh' el uri debe contener un nombre de máquina."
#define STRING_TRANSPORT_SSH_NORUN "No se pudo lanzar ssh. ¿Está instalado y disponible en $PATH?"
#define STRING_TRANSPORT_SSH_FAIL "ssh failed, see output above."
// Uri
#define STRING_URI_QUOTES "No se pudo interpretar el uri '{1}', uso erróneo de comillas simples."

View file

@ -417,7 +417,7 @@ int execute(const std::string& executable, std::vector<std::string> arguments)
cmdline += " " + (std::string)*it;
}
context.debug ("Executing: " + std::string(shell) + " " + std::string(opt) + " " + cmdline);
context.debug ("Executing: " + std::string(shell) + " " + std::string(opt) + " " + cmdline);
pid_t child_pid = fork();
@ -435,6 +435,10 @@ int execute(const std::string& executable, std::vector<std::string> arguments)
exit(ret);
}
else if (child_pid == -1)
{
return -1;
}
else
{
// this is done by the parent process
@ -445,7 +449,7 @@ int execute(const std::string& executable, std::vector<std::string> arguments)
if (pid == -1)
return -1;
else
return child_status;
return WEXITSTATUS (child_status);
}
}