summaryrefslogtreecommitdiff
path: root/core/launcher/applauncher.cpp
blob: 7000346f33bb8cdbeccf063f25475469a9a6d4fa (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#ifndef QTOPIA_INTERNAL_PRELOADACCESS
#define QTOPIA_INTERNAL_PRELOADACCESS
#endif
#ifndef QTOPIA_INTERNAL_FILEOPERATIONS
#define QTOPIA_INTERNAL_FILEOPERATIONS
#endif
#ifndef QTOPIA_PROGRAM_MONITOR
#define QTOPIA_PROGRAM_MONITOR
#endif

#include "applauncher.h"
#include "documentlist.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oglobal.h>
#include <qtopia/qcopenvelope_qws.h>
#include <qtopia/qpeapplication.h>
using namespace Opie::Core;

/* QT */
#include <qtimer.h>
#include <qwindowsystem_qws.h>
#include <qmessagebox.h>
#include <qfileinfo.h>

/* STD */
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>

const int AppLauncher::RAISE_TIMEOUT_MS = 5000;

//---------------------------------------------------------------------------

static AppLauncher* appLauncherPtr;

const int appStopEventID = 1290;

class AppStoppedEvent : public QCustomEvent
{
public:
    AppStoppedEvent(int pid, int status)
	: QCustomEvent( appStopEventID ), mPid(pid), mStatus(status) { }

    int pid() { return mPid; }
    int status() { return mStatus; }

private:
    int mPid, mStatus;
};

AppLauncher::AppLauncher(QObject *parent, const char *name)
    : QObject(parent, name), qlPid(0), qlReady(FALSE),
      appKillerBox(0)
{
    connect(qwsServer, SIGNAL(newChannel(const QString&)), this, SLOT(newQcopChannel(const QString&)));
    connect(qwsServer, SIGNAL(removedChannel(const QString&)), this, SLOT(removedQcopChannel(const QString&)));
    QCopChannel* channel = new QCopChannel( "QPE/System", this );
    connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
	     this, SLOT(received(const QCString&,const QByteArray&)) );

    channel = new QCopChannel( "QPE/Server", this );
    connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
	     this, SLOT(received(const QCString&,const QByteArray&)) );

#ifndef Q_OS_WIN32
    signal(SIGCHLD, signalHandler);
#else
    runningAppsProc.setAutoDelete( TRUE );
#endif
    QString tmp = qApp->argv()[0];
    int pos = tmp.findRev('/');
    if ( pos > -1 )
	tmp = tmp.mid(++pos);
    runningApps[::getpid()] = tmp;

    appLauncherPtr = this;

    QTimer::singleShot( 1000, this, SLOT(createQuickLauncher()) );
}

AppLauncher::~AppLauncher()
{
    appLauncherPtr = 0;
#ifndef Q_OS_WIN32
    signal(SIGCHLD, SIG_DFL);
#endif
    if ( qlPid ) {
	int status;
	::kill( qlPid, SIGTERM );
	waitpid( qlPid, &status, 0 );
    }
}

/*  We use the QCopChannel of the app as an indicator of when it has been launched
    so that we can disable the busy indicators */
void AppLauncher::newQcopChannel(const QString& channelName)
{
//  odebug << "channel " << channelName.data() << " added" << oendl;
    QString prefix("QPE/Application/");
    if (channelName.startsWith(prefix)) {
	{
	    QCopEnvelope e("QPE/System", "newChannel(QString)");
	    e << channelName;
	}
	QString appName = channelName.mid(prefix.length());
	if ( appName != "quicklauncher" ) {
	    emit connected( appName );
	    QCopEnvelope e("QPE/System", "notBusy(QString)");
	    e << appName;
	}
    } else if (channelName.startsWith("QPE/QuickLauncher-")) {
    odebug << "Registered " << channelName << "" << oendl;
	int pid = channelName.mid(18).toInt();
	if (pid == qlPid)
	    qlReady = TRUE;
    }
}

void AppLauncher::removedQcopChannel(const QString& channelName)
{
    if (channelName.startsWith("QPE/Application/")) {
	QCopEnvelope e("QPE/System", "removedChannel(QString)");
	e << channelName;
    }
}

void AppLauncher::received(const QCString& msg, const QByteArray& data)
{
    QDataStream stream( data, IO_ReadOnly );
    if ( msg == "execute(QString)" ) {
	QString t;
	stream >> t;
	if ( !executeBuiltin( t, QString::null ) )
	    execute(t, QString::null);
    } else if ( msg == "execute(QString,QString)" ) {
	QString t,d;
	stream >> t >> d;
	if ( !executeBuiltin( t, d ) )
	    execute( t, d );
    } else if ( msg == "processQCop(QString)" ) { // from QPE/Server
	QString t;
	stream >> t;
	if ( !executeBuiltin( t, QString::null ) )
	    execute( t, QString::null, TRUE);
    } else if ( msg == "raise(QString)" ) {
	QString appName;
	stream >> appName;

	if ( !executeBuiltin( appName, QString::null ) ) {
	    if ( !waitingHeartbeat.contains( appName ) && appKillerName != appName ) {
        //odebug << "Raising: " << appName << "" << oendl;
		QCString channel = "QPE/Application/";
		channel += appName.latin1();

		// Need to lock it to avoid race conditions with QPEApplication::processQCopFile
		QFile f("/tmp/qcop-msg-" + appName);
		if ( f.open(IO_WriteOnly | IO_Append) ) {
#ifndef Q_OS_WIN32
		    flock(f.handle(), LOCK_EX);
#endif
		    QDataStream ds(&f);
		    QByteArray b;
		    QDataStream bstream(b, IO_WriteOnly);
		    ds << channel << QCString("raise()") << b;
		    f.flush();
#ifndef Q_OS_WIN32
		    flock(f.handle(), LOCK_UN);
#endif
		    f.close();
		}
		bool alreadyRunning = isRunning( appName );
		if ( execute(appName, QString::null) ) {
		    int id = startTimer(RAISE_TIMEOUT_MS + alreadyRunning?2000:0);
		    waitingHeartbeat.insert( appName, id );
		}
	    }
	}
    } else if ( msg == "sendRunningApps()" ) {
	QStringList apps;
	QMap<int,QString>::Iterator it;
        for( it = runningApps.begin(); it != runningApps.end(); ++it )
	    apps.append( *it );
	QCopEnvelope e( "QPE/Desktop", "runningApps(QStringList)" );
	e << apps;
    } else if ( msg == "appRaised(QString)" ) {
	QString appName;
	stream >> appName;
    odebug << "Got a heartbeat from " << appName << "" << oendl;
	QMap<QString,int>::Iterator it = waitingHeartbeat.find(appName);
	if ( it != waitingHeartbeat.end() ) {
	    killTimer( *it );
	    waitingHeartbeat.remove(it);
	}
	// Check to make sure we're not waiting on user input...
	if ( appKillerBox && appName == appKillerName ) {
	    // If we are, we kill the dialog box, and the code waiting on the result
	    // will clean us up (basically the user said "no").
	    delete appKillerBox;
	    appKillerBox = 0;
	    appKillerName = QString::null;
	}
    }
}

void AppLauncher::signalHandler(int)
{
#ifndef Q_OS_WIN32
    int status;
    pid_t pid = waitpid(-1, &status, WNOHANG);
/*    if (pid == 0 || &status == 0 ) {
    odebug << "hmm, could not get return value from signal" << oendl;
    }
*/
    QApplication::postEvent(appLauncherPtr, new AppStoppedEvent(pid, status) );
#else
    odebug << "Unhandled signal see by AppLauncher::signalHandler(int)" << oendl;
#endif
}

bool AppLauncher::event(QEvent *e)
{
    if ( e->type() == appStopEventID ) {
	AppStoppedEvent *ae = (AppStoppedEvent *) e;
	sigStopped(ae->pid(), ae->status() );
	return TRUE;
    }

    return QObject::event(e);
}

void AppLauncher::timerEvent( QTimerEvent *e )
{
    int id = e->timerId();
    QMap<QString,int>::Iterator it;
    for ( it = waitingHeartbeat.begin(); it != waitingHeartbeat.end(); ++it ) {
	if ( *it == id ) {
	    if ( appKillerBox ) // we're already dealing with one
		return;

	    appKillerName = it.key();
	    killTimer( id );
	    waitingHeartbeat.remove( it );

        // odebug << "Checking in on " << appKillerName << "" << oendl;

	    // We store this incase the application responds while we're
	    // waiting for user input so we know not to delete ourselves.
	    appKillerBox = new QMessageBox(tr("Application Problem"),
		    tr("<p>%1 is not responding.</p>").arg(appKillerName) +
		    tr("<p>Would you like to force the application to exit?</p>"),
		    QMessageBox::Warning, QMessageBox::Yes,
		    QMessageBox::No | QMessageBox::Default,
		    QMessageBox::NoButton);
	    if (appKillerBox->exec() == QMessageBox::Yes) {
        // odebug << "Killing the app!!! Bwuhahahaha!" << oendl;
		int pid = pidForName(appKillerName);
		if ( pid > 0 )
		    kill( pid );
	    }
	    appKillerName = QString::null;
	    delete appKillerBox;
	    appKillerBox = 0;
	    return;
	}
    }

    QObject::timerEvent( e );
}

#ifndef Q_OS_WIN32
void AppLauncher::sigStopped(int sigPid, int sigStatus)
{
    int exitStatus = 0;

    bool crashed = WIFSIGNALED(sigStatus);
    if ( !crashed ) {
	if ( WIFEXITED(sigStatus) )
	    exitStatus = WEXITSTATUS(sigStatus);
    } else {
	exitStatus  = WTERMSIG(sigStatus);
    }

    QMap<int,QString>::Iterator it = runningApps.find( sigPid );
    if ( it == runningApps.end() ) {
	if ( sigPid == qlPid ) {
        odebug << "quicklauncher stopped" << oendl;
	    qlPid = 0;
	    qlReady = FALSE;
	    QFile::remove("/tmp/qcop-msg-quicklauncher" );
	    QTimer::singleShot( 2000, this, SLOT(createQuickLauncher()) );
	}
/*
	if ( sigPid == -1 )
        odebug << "non-qtopia application exited (disregarded)" << oendl;
	else
        odebug << "==== no pid matching " << sigPid << " in list, definite bug" << oendl;
*/
	return;
    }
    QString appName = *it;
    runningApps.remove(it);

    QMap<QString,int>::Iterator hbit = waitingHeartbeat.find(appName);
    if ( hbit != waitingHeartbeat.end() ) {
	killTimer( *hbit );
	waitingHeartbeat.remove( hbit );
    }
    if ( appName == appKillerName ) {
	appKillerName = QString::null;
	delete appKillerBox;
	appKillerBox = 0;
    }

    /* we must disable preload for an app that crashes as the system logic relies on preloaded apps
       actually being loaded.  If eg. the crash happened in the constructor, we can't automatically reload
       the app (withouth some timeout value for eg. 3 tries (which I think is a bad solution)
    */
    bool preloadDisabled = FALSE;
    if ( !DocumentList::appLnkSet ) return;
    const AppLnk* app = DocumentList::appLnkSet->findExec( appName );
    if ( !app ) return; // QCop messages processed to slow?
    if ( crashed && app->isPreloaded() ) {
	Config cfg("Launcher");
	cfg.setGroup("Preload");
	QStringList apps = cfg.readListEntry("Apps",',');
	QString exe = app->exec();
	apps.remove(exe);
	cfg.writeEntry("Apps",apps,',');
	preloadDisabled = TRUE;
    }

    // clean up
    if ( exitStatus ) {
	QCopEnvelope e("QPE/System", "notBusy(QString)");
	e << app->exec();
    }
/*
    // debug info
    for (it = runningApps.begin(); it != runningApps.end(); ++it) {
    odebug << "running according to internal list: " << (*it).data() << ", with pid " << it.key() << "" << oendl;
    }
*/

#ifdef QTOPIA_PROGRAM_MONITOR
    if ( crashed ) {
	QString sig;
	switch( exitStatus ) {
	    case SIGABRT: sig = "SIGABRT"; break;
	    case SIGALRM: sig = "SIGALRM"; break;
	    case SIGBUS: sig = "SIGBUS"; break;
	    case SIGFPE: sig = "SIGFPE"; break;
	    case SIGHUP: sig = "SIGHUP"; break;
	    case SIGILL: sig = "SIGILL"; break;
	    case SIGKILL: sig = "SIGKILL"; break;
	    case SIGPIPE: sig = "SIGPIPE"; break;
	    case SIGQUIT: sig = "SIGQUIT"; break;
	    case SIGSEGV: sig = "SIGSEGV"; break;
	    case SIGTERM: sig = "SIGTERM"; break;
	    case SIGTRAP: sig = "SIGTRAP"; break;
	    default: sig = QString("Unkown %1").arg(exitStatus);
	}
	if ( preloadDisabled )
	    sig += tr("<qt><p>Fast loading has been disabled for this application.  Tap and hold the application icon to reenable it.</qt>");

	QString str = tr("<qt><b>%1</b> was terminated due to signal code %2</qt>").arg( app->name() ).arg( sig );
	QMessageBox::information(0, tr("Application terminated"), str );
    } else {
	if ( exitStatus == 255 ) {  //could not find app (because global returns -1)
	    QMessageBox::information(0, tr("Application not found"), tr("<qt>Could not locate application <b>%1</b></qt>").arg( app->exec() ) );
	} else  {
	    QFileInfo fi(QString::fromLatin1("/tmp/qcop-msg-") + appName);
	    if ( fi.exists() && fi.size() ) {
		emit terminated(sigPid, appName);
                owarn << "Re executing obmitted for " << appName << "" << oendl;
//		execute( appName, QString::null );
		return;
	    }
	}
    }

#endif

    emit terminated(sigPid, appName);
}
#else
void AppLauncher::sigStopped(int sigPid, int sigStatus)
{
    odebug << "Unhandled signal : AppLauncher::sigStopped(int sigPid, int sigStatus)" << oendl;
}
#endif // Q_OS_WIN32

bool AppLauncher::isRunning(const QString &app)
{
    for (QMap<int,QString>::ConstIterator it = runningApps.begin(); it != runningApps.end(); ++it) {
	if ( *it == app ) {
#ifdef Q_OS_UNIX
	    pid_t t = ::__getpgid( it.key() );
	    if ( t == -1 ) {
        odebug << "appLauncher bug, " << app.data() << " believed running, but pid " << it.key() << " is not existing" << oendl;
		runningApps.remove( it.key() );
		return FALSE;
	    }
#endif
	    return TRUE;
	}
    }

    return FALSE;
}

bool AppLauncher::executeBuiltin(const QString &c, const QString &document)
{
    Global::Command* builtin = OGlobal::builtinCommands();
    QGuardedPtr<QWidget> *running = OGlobal::builtinRunning();

    // Attempt to execute the app using a builtin class for the app
    if (builtin) {
	for (int i = 0; builtin[i].file; i++) {
	    if ( builtin[i].file == c ) {
		if ( running[i] ) {
		    if ( !document.isNull() && builtin[i].documentary )
			Global::setDocument(running[i], document);
		    running[i]->raise();
		    running[i]->show();
		    running[i]->setActiveWindow();
		} else {
		    running[i] = builtin[i].func( builtin[i].maximized );
		}
#ifndef QT_NO_COP
		QCopEnvelope e("QPE/System", "notBusy(QString)" );
		e << c; // that was quick ;-)
#endif
		return TRUE;
	    }
	}
    }

    // Convert the command line in to a list of arguments
    QStringList list = QStringList::split(QRegExp("  *"),c);
    QString ap=list[0];

    if ( ap == "suspend" ) { // No tr
	QWSServer::processKeyEvent( 0xffff, Qt::Key_F34, FALSE, TRUE, FALSE );
	return TRUE;
    }

    return FALSE;
}

bool AppLauncher::execute(const QString &c, const QString &docParam, bool noRaise)
{
    owarn << "AppLauncher::execute '" <<  c << "' '" <<  docParam << "'" << oendl;
    // Convert the command line in to a list of arguments
    QStringList list = QStringList::split(QRegExp("  *"),c);
    QStringList arglist = QStringList::split(QRegExp("  *"),docParam);
    for ( QStringList::Iterator it = arglist.begin(); it != arglist.end(); ++it )
        list.append( *it );

    QString appName = list[0];
    if ( isRunning(appName) ) {
	QCString channel = "QPE/Application/";
	channel += appName.latin1();

	// Need to lock it to avoid race conditions with QPEApplication::processQCopFile
        QFile f(QString::fromLatin1("/tmp/qcop-msg-") + appName);
	if ( !noRaise && f.open(IO_WriteOnly | IO_Append) ) {
#ifndef Q_OS_WIN32
	    flock(f.handle(), LOCK_EX);
#endif

	    QDataStream ds(&f);
	    QByteArray b;
	    QDataStream bstream(b, IO_WriteOnly);
	    if ( !f.size() ) {
		ds << channel << QCString("raise()") << b;
		if ( !waitingHeartbeat.contains( appName ) && appKillerName != appName ) {
		    int id = startTimer(RAISE_TIMEOUT_MS);
		    waitingHeartbeat.insert( appName, id );
		}
	    }
	    if ( !docParam.isEmpty() ) {
		bstream << docParam;
		ds << channel << QCString("setDocument(QString)") << b;
	    }

	    f.flush();
#ifndef Q_OS_WIN32
	    flock(f.handle(), LOCK_UN);
#endif
	    f.close();
	}
	if ( QCopChannel::isRegistered(channel) ) // avoid unnecessary warnings
	    QCopChannel::send(channel,"QPEProcessQCop()");

	return TRUE;
    }

#ifdef QT_NO_QWS_MULTIPROCESS
    QMessageBox::warning( 0, tr("Error"), tr("<qt>Could not find the application %1</qt>").arg(c),
	tr("OK"), 0, 0, 0, 1 );
#else

    QStrList slist;
    unsigned j;
    for ( j = 0; j < list.count(); j++ )
	slist.append( list[j].utf8() );

    const char **args = new const char *[slist.count() + 1];
    for ( j = 0; j < slist.count(); j++ )
	args[j] = slist.at(j);
    args[j] = NULL;

#ifndef Q_OS_WIN32
#ifdef Q_OS_MACX
    if ( qlPid && qlReady && QFile::exists( QPEApplication::qpeDir()+"plugins/application/lib"+args[0] + ".dylib" ) ) {
#else
    if ( qlPid && qlReady && QFile::exists( QPEApplication::qpeDir()+"plugins/application/lib"+args[0] + ".so" ) ) {
#endif /* Q_OS_MACX */
    odebug << "Quick launching: " << args[0] << "" << oendl;
	if ( getuid() == 0 )
	    setpriority( PRIO_PROCESS, qlPid, 0 );
	QCString qlch("QPE/QuickLauncher-");
	qlch += QString::number(qlPid);
	QCopEnvelope env( qlch, "execute(QStrList)" );
	env << slist;
	runningApps[qlPid] = QString(args[0]);
	emit launched(qlPid, QString(args[0]));
	QCopEnvelope e("QPE/System", "busy()");
	qlPid = 0;
	qlReady = FALSE;
	QTimer::singleShot( getuid() == 0 ? 800 : 1500, this, SLOT(createQuickLauncher()) );
    } else {
	int pid = ::vfork();
	if ( !pid ) {
	    for ( int fd = 3; fd < 100; fd++ )
		::close( fd );
	    ::setpgid( ::getpid(), ::getppid() );
	    // Try bindir first, so that foo/bar works too
	    ::execv( QPEApplication::qpeDir()+"bin/"+args[0], (char * const *)args );
	    ::execvp( args[0], (char * const *)args );
	    _exit( -1 );
	}

	runningApps[pid] = QString(args[0]);
	emit launched(pid, QString(args[0]));
	QCopEnvelope e("QPE/System", "busy()");
    }
#else
    QProcess *proc = new QProcess(this);
    if (proc){
	for (int i=0; i < slist.count(); i++)
	    proc->addArgument(args[i]);
	connect(proc, SIGNAL(processExited()), this, SLOT(processExited()));
	if (!proc->start()){
        odebug << "Unable to start application " << args[0] << "" << oendl;
	}else{
	    PROCESS_INFORMATION *procInfo = (PROCESS_INFORMATION *)proc->processIdentifier();
	    if (procInfo){
		DWORD pid = procInfo->dwProcessId;
		runningApps[pid] = QString(args[0]);
		runningAppsProc.append(proc);
		emit launched(pid, QString(args[0]));
		QCopEnvelope e("QPE/System", "busy()");
	    }else{
        odebug << "Unable to read process inforation #1 for " << args[0] << "" << oendl;
	    }
	}
    }else{
    odebug << "Unable to create process for application " << args[0] << "" << oendl;
	return FALSE;
    }
#endif
#endif //QT_NO_QWS_MULTIPROCESS

    delete [] args;
    return TRUE;
}

void AppLauncher::kill( int pid )
{
#ifndef Q_OS_WIN32
    ::kill( pid, SIGTERM );
#else
    for ( QProcess *proc = runningAppsProc.first(); proc; proc = runningAppsProc.next() ) {
	if ( proc->processIdentifier() == pid ) {
	    proc->kill();
	    break;
	}
    }
#endif
}

int AppLauncher::pidForName( const QString &appName )
{
    int pid = -1;

    QMap<int, QString>::Iterator it;
    for (it = runningApps.begin(); it!= runningApps.end(); ++it) {
	if (*it == appName) {
	    pid = it.key();
	    break;
	}
    }

    return pid;
}

void AppLauncher::createQuickLauncher()
{
    static bool disabled = FALSE;
    if (disabled)
        return;

    qlReady = FALSE;
    qlPid = ::vfork();
    if ( !qlPid ) {
	char **args = new char *[2];
	args[0] = "quicklauncher";
	args[1] = 0;
	for ( int fd = 3; fd < 100; fd++ )
	    ::close( fd );
	::setpgid( ::getpid(), ::getppid() );
	// Try bindir first, so that foo/bar works too
        /*
         * LD_BIND_NOW will change the behaviour of ld.so and dlopen
         * RTLD_LAZY will be made RTLD_NOW which leads to problem
         * with miscompiled libraries... if LD_BIND_NOW is set.. there
         * is no way back.. We will wait for numbers from TT to see
         * if using LD_BIND_NOW is worth it - zecke
         */
//	setenv( "LD_BIND_NOW", "1", 1 );
	::execv( QPEApplication::qpeDir()+"bin/quicklauncher", args );
	::execvp( "quicklauncher", args );
        delete []args;
        disabled = TRUE;
	_exit( -1 );
    } else if ( qlPid == -1 ) {
	qlPid = 0;
    } else {
	if ( getuid() == 0 )
	    setpriority( PRIO_PROCESS, qlPid, 19 );
    }
}

// Used only by Win32
void AppLauncher::processExited()
{
#ifdef Q_OS_WIN32
    odebug << "AppLauncher::processExited()" << oendl;
    bool found = FALSE;
    QProcess *proc = (QProcess *) sender();
    if (!proc){
    odebug << "Interanl error NULL proc" << oendl;
	return;
    }

    QString appName = proc->arguments()[0];
    odebug << "Removing application " << appName << "" << oendl;
    runningAppsProc.remove(proc);

    QMap<QString,int>::Iterator hbit = waitingHeartbeat.find(appName);
    if ( hbit != waitingHeartbeat.end() ) {
	killTimer( *hbit );
	waitingHeartbeat.remove( hbit );
    }
    if ( appName == appKillerName ) {
	appKillerName = QString::null;
	delete appKillerBox;
	appKillerBox = 0;
    }

    // Search for the app to find its PID
    QMap<int, QString>::Iterator it;
    for (it = runningApps.begin(); it!= runningApps.end(); ++it){
	if (it.data() == appName){
	    found = TRUE;
	    break;
	}
    }

    if (found){
	emit terminated(it.key(), it.data());
	runningApps.remove(it.key());
    }else{
    odebug << "Internal error application " << appName << " not listed as running" << oendl;
    }

#endif
}