Add option to run process jobmodules in chroot.

This commit is contained in:
Teo Mrnjavac 2014-08-12 14:26:10 +02:00
parent 27a10fd08b
commit 7a3ce363b3
8 changed files with 142 additions and 30 deletions

View File

@ -18,6 +18,10 @@
#include "ProcessJob.h" #include "ProcessJob.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include <QDir>
#include <QProcess> #include <QProcess>
namespace Calamares { namespace Calamares {
@ -25,11 +29,13 @@ namespace Calamares {
ProcessJob::ProcessJob( const QString& command, ProcessJob::ProcessJob( const QString& command,
const QString& workingPath, const QString& workingPath,
bool runInChroot,
int secondsTimeout, int secondsTimeout,
QObject* parent ) QObject* parent )
: Job( parent ) : Job( parent )
, m_command( command ) , m_command( command )
, m_workingPath( workingPath ) , m_workingPath( workingPath )
, m_runInChroot( runInChroot )
, m_timeoutSec( secondsTimeout ) , m_timeoutSec( secondsTimeout )
{} {}
@ -49,42 +55,109 @@ ProcessJob::prettyName() const
JobResult JobResult
ProcessJob::exec() ProcessJob::exec()
{ {
QProcess p; int ec = 0;
p.setProcessChannelMode( QProcess::MergedChannels ); QString output;
p.setWorkingDirectory( m_workingPath ); if ( m_runInChroot )
p.start( m_command ); ec = CalamaresUtils::chrootOutput( m_command,
output,
m_workingPath,
QString(),
m_timeoutSec );
else
ec = callOutput( m_command,
output,
m_workingPath,
QString(),
m_timeoutSec );
// It's ok to block here because JobQueue runs this method in a separate thread. if ( ec == 0 )
if ( !p.waitForStarted() ) return JobResult::ok();
if ( ec == -1 ) //Crash!
return JobResult::error( tr( "External command crashed" ),
tr( "Command %1 crashed.\nOutput:\n%2" )
.arg( m_command )
.arg( output ) );
if ( ec == -2 )
return JobResult::error( tr( "External command failed to start" ), return JobResult::error( tr( "External command failed to start" ),
tr( "Command %1 failed to start." ) tr( "Command %1 failed to start." )
.arg( m_command ) ); .arg( m_command ) );
if ( !p.waitForFinished( m_timeoutSec * 1000/*msec*/ ) ) if ( ec == -3 )
return JobResult::error( tr( "External command failed to finish" ), return JobResult::error( tr( "Internal error when starting command" ),
tr( "Command %1 failed to finish in %2s." ) tr( "Bad parameters for process job call." ) );
.arg( m_command )
.arg( m_timeoutSec ) );
if ( p.exitStatus() == QProcess::NormalExit && p.exitCode() == 0 ) if ( ec == -4 )
{ return JobResult::error( tr( "External command failed to finish" ),
return JobResult::ok(); tr( "Command %1 failed to finish in %2s.\nOutput:\n%3" )
}
else if ( p.exitStatus() == QProcess::CrashExit )
{
return JobResult::error( tr( "External command crashed" ),
tr( "Command %1 crashed.\nOutput:\n%2" )
.arg( m_command ) .arg( m_command )
.arg( QString::fromLocal8Bit( p.readAll() ) ) ); .arg( m_timeoutSec )
} .arg( output ) );
else //NormalExit with non-zero exit code
{ //Any other exit code
return JobResult::error( tr( "External command finished with errors" ), return JobResult::error( tr( "External command finished with errors" ),
tr( "Command %1 finished with exit code %2.\nOutput:\n%3" ) tr( "Command %1 finished with exit code %2.\nOutput:\n%3" )
.arg( m_command ) .arg( m_command )
.arg( p.exitCode() ) .arg( ec )
.arg( QString::fromLocal8Bit( p.readAll() ) ) ); .arg( output ) );
}
} }
int
ProcessJob::callOutput( const QString& command,
QString& output,
const QString& workingPath,
const QString& stdInput,
int timeoutSec )
{
output.clear();
QProcess process;
process.setProgram( command );
process.setProcessChannelMode( QProcess::MergedChannels );
if ( !workingPath.isEmpty() )
{
if ( QDir( workingPath ).exists() )
process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
else
cLog() << "Invalid working directory:" << workingPath;
return -3;
}
cLog() << "Running" << command;
process.start();
if ( !process.waitForStarted() )
{
cLog() << "Process failed to start" << process.error();
return -2;
}
if ( !stdInput.isEmpty() )
{
process.write( stdInput.toLocal8Bit() );
process.closeWriteChannel();
}
if ( !process.waitForFinished( timeoutSec ? ( timeoutSec * 1000 ) : -1 ) )
{
cLog() << "Timed out. output so far:";
cLog() << process.readAllStandardOutput();
return -4;
}
output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() );
if ( process.exitStatus() == QProcess::CrashExit )
{
cLog() << "Process crashed";
return -1;
}
cLog() << "Finished. Exit code:" << process.exitCode();
return process.exitCode();
}
} // namespace Calamares } // namespace Calamares

View File

@ -29,6 +29,7 @@ class ProcessJob : public Job
public: public:
explicit ProcessJob( const QString& command, explicit ProcessJob( const QString& command,
const QString& workingPath, const QString& workingPath,
bool runInChroot = false,
int secondsTimeout = 30, int secondsTimeout = 30,
QObject* parent = nullptr ); QObject* parent = nullptr );
virtual ~ProcessJob(); virtual ~ProcessJob();
@ -37,8 +38,14 @@ public:
JobResult exec() override; JobResult exec() override;
private: private:
int callOutput( const QString& command,
QString& output,
const QString& workingPath = QString(),
const QString& stdInput = QString(),
int timeoutSec = 0 );
QString m_command; QString m_command;
QString m_workingPath; QString m_workingPath;
bool m_runInChroot;
int m_timeoutSec; int m_timeoutSec;
}; };

View File

@ -51,6 +51,7 @@ chroot_call( const std::string& command,
int timeout ) int timeout )
{ {
return CalamaresUtils::chrootCall( QString::fromStdString( command ), return CalamaresUtils::chrootCall( QString::fromStdString( command ),
QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); timeout );
} }
@ -69,6 +70,7 @@ chroot_call( const bp::list& args,
} }
return CalamaresUtils::chrootCall( list, return CalamaresUtils::chrootCall( list,
QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); timeout );
} }
@ -112,6 +114,7 @@ check_chroot_output( const std::string& command,
QString output; QString output;
int ec = CalamaresUtils::chrootOutput( QString::fromStdString( command ), int ec = CalamaresUtils::chrootOutput( QString::fromStdString( command ),
output, output,
QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); timeout );
_handle_check_chroot_call_error( ec, QString::fromStdString( command ) ); _handle_check_chroot_call_error( ec, QString::fromStdString( command ) );
@ -134,6 +137,7 @@ check_chroot_output( const bp::list& args,
int ec = CalamaresUtils::chrootOutput( list, int ec = CalamaresUtils::chrootOutput( list,
output, output,
QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); timeout );
_handle_check_chroot_call_error( ec, list.join( ' ' ) ); _handle_check_chroot_call_error( ec, list.join( ' ' ) );

View File

@ -59,12 +59,14 @@ mount( const QString& devicePath,
int int
chrootCall( const QStringList& args, chrootCall( const QStringList& args,
const QString& workingPath,
const QString& stdInput, const QString& stdInput,
int timeoutSec ) int timeoutSec )
{ {
QString discard; QString discard;
return chrootOutput( args, return chrootOutput( args,
discard, discard,
workingPath,
stdInput, stdInput,
timeoutSec ); timeoutSec );
} }
@ -72,10 +74,12 @@ chrootCall( const QStringList& args,
int int
chrootCall( const QString& command, chrootCall( const QString& command,
const QString& workingPath,
const QString& stdInput, const QString& stdInput,
int timeoutSec ) int timeoutSec )
{ {
return chrootCall( QStringList() = { command }, return chrootCall( QStringList() = { command },
workingPath,
stdInput, stdInput,
timeoutSec ); timeoutSec );
} }
@ -84,6 +88,7 @@ chrootCall( const QString& command,
int int
chrootOutput( const QStringList& args, chrootOutput( const QStringList& args,
QString& output, QString& output,
const QString& workingPath,
const QString& stdInput, const QString& stdInput,
int timeoutSec ) int timeoutSec )
{ {
@ -115,6 +120,15 @@ chrootOutput( const QStringList& args,
process.setArguments( arguments ); process.setArguments( arguments );
process.setProcessChannelMode( QProcess::MergedChannels ); process.setProcessChannelMode( QProcess::MergedChannels );
if ( !workingPath.isEmpty() )
{
if ( QDir( workingPath ).exists() )
process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
else
cLog() << "Invalid working directory:" << workingPath;
return -3;
}
cLog() << "Running" << program << arguments; cLog() << "Running" << program << arguments;
process.start(); process.start();
if ( !process.waitForStarted() ) if ( !process.waitForStarted() )
@ -152,15 +166,16 @@ chrootOutput( const QStringList& args,
int int
chrootOutput( const QString& command, chrootOutput( const QString& command,
QString& output, QString& output,
const QString& workingPath,
const QString& stdInput, const QString& stdInput,
int timeoutSec ) int timeoutSec )
{ {
return chrootOutput( QStringList() = { command }, return chrootOutput( QStringList() = { command },
output, output,
workingPath,
stdInput, stdInput,
timeoutSec ); timeoutSec );
} }
} }

View File

@ -45,20 +45,24 @@ DLLEXPORT int mount( const QString& devicePath,
* -4 = QProcess timeout * -4 = QProcess timeout
*/ */
DLLEXPORT int chrootCall( const QStringList& args, DLLEXPORT int chrootCall( const QStringList& args,
const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 );
DLLEXPORT int chrootCall( const QString& command, DLLEXPORT int chrootCall( const QString& command,
const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 );
DLLEXPORT int chrootOutput( const QStringList& args, DLLEXPORT int chrootOutput( const QStringList& args,
QString& output, QString& output,
const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 );
DLLEXPORT int chrootOutput( const QString& command, DLLEXPORT int chrootOutput( const QString& command,
QString& output, QString& output,
const QString& workingPath = QString(),
const QString& stdInput = QString(), const QString& stdInput = QString(),
int timeoutSec = 0 ); int timeoutSec = 0 );
} }

View File

@ -49,6 +49,7 @@ ProcessJobModule::loadSelf()
m_job = Calamares::job_ptr( new ProcessJob( m_command, m_job = Calamares::job_ptr( new ProcessJob( m_command,
m_workingPath, m_workingPath,
m_runInChroot,
m_secondsTimeout ) ); m_secondsTimeout ) );
m_loaded = true; m_loaded = true;
} }
@ -78,6 +79,12 @@ ProcessJobModule::initFrom( const YAML::Node& node )
{ {
m_secondsTimeout = node[ "timeout" ].as< int >(); m_secondsTimeout = node[ "timeout" ].as< int >();
} }
m_runInChroot = false;
if ( node[ "chroot" ] )
{
m_runInChroot = node[ "chroot" ].as< bool >();
}
} }

View File

@ -45,6 +45,7 @@ private:
QString m_command; QString m_command;
QString m_workingPath; QString m_workingPath;
int m_secondsTimeout; int m_secondsTimeout;
bool m_runInChroot;
job_ptr m_job; job_ptr m_job;
}; };

View File

@ -4,5 +4,6 @@
type: "job" type: "job"
name: "dummyprocess" name: "dummyprocess"
interface: "process" interface: "process"
chroot: false
command: "/bin/sh -c \"touch ~/calamares-dummyprocess\"" command: "/bin/sh -c \"touch ~/calamares-dummyprocess\""
timeout: 5 timeout: 5