• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • server
processcontrol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org> *
3  * Copyright (C) 2008-2011 by Sebastian Trueg <trueg@kde.org> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Library General Public License as *
7  * published by the Free Software Foundation; either version 2 of the *
8  * License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Library General Public *
16  * License along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "processcontrol.h"
22 
23 #include <QtCore/QDebug>
24 #include <QtCore/QTimer>
25 #include <QtCore/QCoreApplication>
26 
27 #include <KDebug>
28 
29 ProcessControl::ProcessControl( QObject *parent )
30  : QObject( parent ), mFailedToStart( false ), mCrashCount( 0 )
31 {
32  connect( &mProcess, SIGNAL( error( QProcess::ProcessError ) ),
33  this, SLOT( slotError( QProcess::ProcessError ) ) );
34  connect( &mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ),
35  this, SLOT( slotFinished( int, QProcess::ExitStatus ) ) );
36  mProcess.setProcessChannelMode( QProcess::ForwardedChannels );
37 }
38 
39 ProcessControl::~ProcessControl()
40 {
41  mProcess.disconnect(this);
42  terminate(true);
43 }
44 
45 void ProcessControl::start( const QString &application, const QStringList &arguments, CrashPolicy policy, int maxCrash )
46 {
47  mFailedToStart = false;
48 
49  mApplication = application;
50  mArguments = arguments;
51  mPolicy = policy;
52  mCrashCount = maxCrash;
53 
54  start();
55 }
56 
57 void ProcessControl::setCrashPolicy( CrashPolicy policy )
58 {
59  mPolicy = policy;
60 }
61 
62 void ProcessControl::slotError( QProcess::ProcessError error )
63 {
64  switch ( error ) {
65  case QProcess::Crashed:
66  // do nothing, we'll respawn in slotFinished
67  break;
68  case QProcess::FailedToStart:
69  default:
70  mFailedToStart = true;
71  break;
72  }
73 
74  qDebug( "ProcessControl: Application '%s' stopped unexpected (%s)",
75  qPrintable( mApplication ), qPrintable( mProcess.errorString() ) );
76 }
77 
78 void ProcessControl::slotFinished( int exitCode, QProcess::ExitStatus exitStatus )
79 {
80  // the process went down -> inform clients
81  emit finished(false);
82 
83  // Since Nepomuk services are KApplications and, thus, use DrKonqi QProcess does not
84  // see a crash as an actual CrashExit but as a normal exit with an exit code != 0
85  if ( exitStatus == QProcess::CrashExit ||
86  exitCode != 0 ) {
87  if ( mPolicy == RestartOnCrash ) {
88  // don't try to start an unstartable application
89  if ( !mFailedToStart && --mCrashCount >= 0 ) {
90  qDebug( "Application '%s' crashed! %d restarts left.", qPrintable( commandLine() ), mCrashCount );
91  start();
92  }
93  else {
94  if ( mFailedToStart ) {
95  qDebug( "Application '%s' failed to start!", qPrintable( commandLine() ) );
96  }
97  else {
98  qDebug( "Application '%s' crashed to often. Giving up!", qPrintable( commandLine() ) );
99  }
100  }
101  }
102  else {
103  qDebug( "Application '%s' crashed. No restart!", qPrintable( commandLine() ) );
104  }
105  }
106  else {
107  qDebug( "Application '%s' exited normally...", qPrintable( commandLine() ) );
108  }
109 }
110 
111 void ProcessControl::start()
112 {
113  mProcess.start( mApplication, mArguments );
114 }
115 
116 bool ProcessControl::isRunning() const
117 {
118  return mProcess.state() != QProcess::NotRunning;
119 }
120 
121 QString ProcessControl::commandLine() const
122 {
123  return mApplication + QLatin1String(" ") + mArguments.join(QLatin1String(" "));
124 }
125 
126 void ProcessControl::terminate( bool waitAndKill )
127 {
128  if(isRunning()) {
129  mProcess.terminate();
130  // kill if not stopped after timeout
131  if(waitAndKill ||
132  QCoreApplication::instance()->closingDown()) {
133  if(!mProcess.waitForFinished(20000)) {
134  mProcess.kill();
135  }
136  }
137  else {
138  QTimer::singleShot(20000, &mProcess, SLOT(kill()));
139  }
140  }
141 }
142 
143 bool ProcessControl::waitForStarted()
144 {
145  return mProcess.waitForStarted();
146 }
147 
148 #include "processcontrol.moc"
ProcessControl::terminate
void terminate(bool waitAndKill=false)
Sends the term signal to the process.
Definition: processcontrol.cpp:126
ProcessControl::ProcessControl
ProcessControl(QObject *parent=0)
Creates a new process control.
Definition: processcontrol.cpp:29
ProcessControl::isRunning
bool isRunning() const
Definition: processcontrol.cpp:116
QObject
ProcessControl::RestartOnCrash
Definition: processcontrol.h:47
ProcessControl::setCrashPolicy
void setCrashPolicy(CrashPolicy policy)
Sets the crash policy.
Definition: processcontrol.cpp:57
ProcessControl::start
void start(const QString &application, const QStringList &arguments=QStringList(), CrashPolicy policy=RestartOnCrash, int maxCrashes=5)
Starts the application with the given list of arguments.
Definition: processcontrol.cpp:45
ProcessControl::~ProcessControl
~ProcessControl()
Destroys the process control.
Definition: processcontrol.cpp:39
ProcessControl::finished
void finished(bool clean)
ProcessControl::CrashPolicy
CrashPolicy
Theses enums describe the behaviour when the observed application crashed.
Definition: processcontrol.h:44
start
void start()
Definition: ontologydownloadjob.cpp:13
ProcessControl::waitForStarted
bool waitForStarted()
Definition: processcontrol.cpp:143
processcontrol.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal