summaryrefslogtreecommitdiffstats
path: root/kmrml/kmrml/server/watcher.cpp
blob: 60b37c48b5334a25013220ebc2724970c0f90e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* This file is part of the KDE project
   Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <dcopclient.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "watcher.h"

using namespace KMrml;

Watcher::Watcher( const TQCString& name )
    : KDEDModule( name )
{
    m_daemons.setAutoDelete( true );

    // safety, for clients that die without unregistering
    KApplication::dcopClient()->setNotifications( true );
    connect( KApplication::dcopClient(),
             TQT_SIGNAL( applicationRemoved( const TQCString& )),
             TQT_SLOT( slotAppUnregistered( const TQCString& )));
}

Watcher::~Watcher()
{
    KApplication::dcopClient()->setNotifications( false );
}

bool Watcher::requireDaemon( const TQCString& clientAppId,
                             const TQString& daemonKey,
                             const TQString& commandline,
                             uint timeout /* seconds */,
                             int restartOnFailure )
{
    if ( !KApplication::dcopClient()->isApplicationRegistered( clientAppId ) )
        kdWarning() << "Watcher::requireDaemon: " << daemonKey
                    << ": Client AppID is not registered with DCOP: "
                    << clientAppId << endl;

    DaemonData *daemon = m_daemons.find( daemonKey );

    if ( daemon )
    {
        if ( !daemon->apps.find( clientAppId ) )
            daemon->apps.append( clientAppId );

        // timeout, commandline and restart values are: first come, first serve
        return true; // process already running, all fine
    }

    else // start daemon
    {
        daemon = new DaemonData( daemonKey, commandline,
                                 timeout, restartOnFailure );
        m_daemons.insert( daemonKey, daemon );
        daemon->apps.append( clientAppId );

#if KDE_VERSION >= 306
        daemon->process = new KProcess();
        daemon->process->setUseShell( true );
#else
        daemon->process = new KShellProcess();
#endif
        daemon->process->setEnvironment( "LC_ALL", "C" );
        daemon->process->setEnvironment( "LANG", "C" );
        daemon->process->setEnvironment( "LANGUAGE", "C" );
        *daemon->process << commandline;
        connect( daemon->process, TQT_SIGNAL( processExited( KProcess * ) ),
                 TQT_SLOT( slotProcExited( KProcess * )));
        return startDaemon( daemon );
    }
}

void Watcher::unrequireDaemon( const TQCString& clientAppId,
                               const TQString& daemonKey )
{
    unrequireDaemon( m_daemons.find( daemonKey ), clientAppId );
}

void Watcher::unrequireDaemon( DaemonData *daemon,
                               const TQCString& clientAppId )
{
    if ( daemon )
    {
        daemon->apps.remove( clientAppId.data() );
        if ( daemon->apps.isEmpty() )
        {
            if ( !daemon->timer )
            {
                daemon->timer = new TQTimer();
                connect( daemon->timer, TQT_SIGNAL( timeout() ),
                         TQT_SLOT( slotTimeout() ));
            }
            daemon->timer->start( daemon->timeout * 1000, true );
        }
    }
    else
        kdWarning() << "Watcher::unrequireDaemon: daemon unknown. client: "
                    << clientAppId << endl;
}

TQStringList Watcher::runningDaemons() const
{
    TQStringList result;
    TQDictIterator<DaemonData> it( m_daemons );
    for ( ; it.current(); ++it )
        result.append( it.current()->commandline );

    return result;
}

void Watcher::slotProcExited( KProcess *proc )
{
    DaemonData *daemon = findDaemonFromProcess( proc );

    if ( proc->normalExit() )
    {
        emitExited( daemon );
        return;
    }

    if ( daemon )
    {
        if ( --daemon->restartOnFailure <= 0 )
        {
            if ( KMessageBox::questionYesNo( 0L,
                             i18n("<qt>The server with the command line"
                                  "<br>%1<br>"
                                  "is not available anymore. Do you want to "
                                  "restart it?" ).tqarg( daemon->commandline ),
                                            i18n("Service Failure"), i18n("Restart Server"), i18n("Do Not Restart") )
                 == KMessageBox::Yes )
            {
                daemon->restartOnFailure = 1;
            }
        }

        if ( daemon->restartOnFailure > 0 )
        {
            startDaemon( daemon );
            return;
        }
    }

    emitFailure( daemon );
}

bool Watcher::startDaemon( DaemonData *daemon )
{
    if ( daemon->process->start( KProcess::NotifyOnExit ) )
        return true;

    else
    {
        if ( KMessageBox::questionYesNo( 0L,
                      i18n("Unable to start the server with the "
                           "command line"
                           "<br>%1<br>"
                          "Try again?").tqarg( daemon->commandline ),
                                         i18n("Service Failure"), i18n("Try Again"), i18n("Do Not Try") )
             == KMessageBox::Yes )
        {
            return startDaemon( daemon );
        }
    }

    return false;
}

void Watcher::slotTimeout()
{
    TQTimer *timer = static_cast<TQTimer*>( TQT_TQOBJECT( const_cast<TQT_BASE_OBJECT_NAME*>(sender()) ) );
    DaemonData *daemon = findDaemonFromTimer( timer );
    if ( daemon )
    {
        if ( daemon->apps.isEmpty() )
        {
            // the daemon and KProcess might get deleted by killing the
            // KProcess (through slotProcExited()), so don't dereference
            // daemon after proc->kill()
            TQString key = daemon->daemonKey;

            // noone registered during the timeout, so kill the daemon
            if ( !daemon->process->kill() )
                daemon->process->kill( SIGKILL );

            m_daemons.remove( key );
        }
    }
}

DaemonData * Watcher::findDaemonFromProcess( KProcess *proc )
{
    DaemonData *daemon;
    TQDictIterator<DaemonData> it( m_daemons );
    for ( ; (daemon = it.current()); ++it )
    {
        if ( daemon->process == proc )
            return daemon;
    }

    return 0L;
}

DaemonData * Watcher::findDaemonFromTimer( TQTimer *timer )
{
    DaemonData *daemon;
    TQDictIterator<DaemonData> it( m_daemons );
    for ( ; (daemon = it.current()); ++it )
    {
        if ( daemon->timer == timer )
            return daemon;
    }

    return 0L;
}

void Watcher::slotAppUnregistered( const TQCString& appId )
{
    if ( m_daemons.isEmpty() )
        return;

    DaemonData *daemon;
    TQDictIterator<DaemonData> it( m_daemons );
    for ( ; (daemon = it.current()); ++it )
    {
        if ( daemon->apps.find( appId ) != -1 )
            unrequireDaemon( daemon, appId );
    }
}

void Watcher::emitExited( DaemonData *daemon )
{
    if ( daemon )
    {
        daemonExited( daemon->daemonKey,
                      daemon->process->pid(),
                      daemon->process->exitStatus() );

        m_daemons.remove( daemon->daemonKey );
    }
}

void Watcher::emitFailure( DaemonData *daemon )
{
    if ( daemon )
    {
        daemonDied( daemon->daemonKey, daemon->process->pid() );
        m_daemons.remove( daemon->daemonKey ); // deletes daemon + KProcess
    }
}

extern "C" {
    KDE_EXPORT KDEDModule *create_daemonwatcher(const TQCString & obj )
    {
        return new Watcher( obj );
    }
}


#include "watcher.moc"