2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public
8
License version 2 or at your option version 3 as published by
9
the Free Software Foundation.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
//#include "SensorClient.h"
28
#include "SensorManager.h"
30
#include "SensorShellAgent.h"
32
using namespace KSGRD;
34
SensorShellAgent::SensorShellAgent( SensorManager *sm )
35
: SensorAgent( sm ), mDaemon( 0 )
39
SensorShellAgent::~SensorShellAgent()
42
mDaemon->write( "quit\n", sizeof( "quit\n" )-1 );
43
mDaemon->disconnect();
44
mDaemon->waitForFinished();
50
bool SensorShellAgent::start( const QString &host, const QString &shell,
51
const QString &command, int )
53
mDaemon = new KProcess();
54
mDaemon->setOutputChannelMode( KProcess::SeparateChannels );
60
connect( mDaemon, SIGNAL( error ( QProcess::ProcessError ) ),
61
SLOT( daemonError( QProcess::ProcessError ) ) );
62
connect( mDaemon, SIGNAL( finished ( int, QProcess::ExitStatus ) ),
63
SLOT( daemonExited( int, QProcess::ExitStatus ) ) );
64
connect( mDaemon, SIGNAL( readyReadStandardOutput() ),
66
connect( mDaemon, SIGNAL( readyReadStandardError() ),
67
SLOT( errMsgRcvd() ) );
69
if ( !command.isEmpty() ) {
70
*mDaemon << KShell::splitArgs(command);
73
*mDaemon << mShell << hostName() << "ksysguardd";
79
void SensorShellAgent::hostInfo( QString &shell, QString &command,
87
void SensorShellAgent::msgRcvd( )
89
QByteArray buffer = mDaemon->readAllStandardOutput();
90
mRetryCount = 3; //we received an answer, so reset our retry count back to 3
91
processAnswer( buffer.constData(), buffer.size());
94
void SensorShellAgent::errMsgRcvd( )
96
QByteArray buffer = mDaemon->readAllStandardOutput();
98
// Because we read the error buffer in chunks, we may not have a proper utf8 string.
99
// We should never get input over stderr anyway, so no need to worry too much about it.
100
// But if this is extended, we will need to handle this better
101
QString buf = QString::fromUtf8( buffer );
103
kDebug(1215) << "SensorShellAgent: Warning, received text over stderr!"
104
<< endl << buf << endl;
107
void SensorShellAgent::daemonExited( int exitCode, QProcess::ExitStatus exitStatus )
110
kDebug(1215) << "daemon exited, exit status " << exitStatus;
111
if ( mRetryCount-- <= 0 || (mDaemon->start(), !mDaemon->waitForStarted()) )
113
setDaemonOnLine( false );
114
if(sensorManager()) {
115
sensorManager()->disengage( this ); //delete ourselves
120
void SensorShellAgent::daemonError( QProcess::ProcessError errorStatus )
123
switch(errorStatus) {
124
case QProcess::FailedToStart:
125
kDebug(1215) << "failed to run" << mDaemon->program().join(" ");
126
error = i18n("Could not run daemon program '%1'.", mDaemon->program().join(" "));
128
case QProcess::Crashed:
129
case QProcess::Timedout:
130
case QProcess::WriteError:
131
case QProcess::ReadError:
133
error = i18n("The daemon program '%1' failed.", mDaemon->program().join(" "));
135
setReasonForOffline(error);
136
kDebug(1215) << "Error received " << error << "(" << errorStatus << ")";
137
setDaemonOnLine( false );
139
sensorManager()->disengage( this ); //delete ourselves
141
bool SensorShellAgent::writeMsg( const char *msg, int len )
143
//write returns -1 on error, in which case we should return false. true otherwise.
144
return mDaemon->write( msg, len ) != -1;
147
#include "SensorShellAgent.moc"