summaryrefslogtreecommitdiff
path: root/library
authorsandman <sandman>2002-12-17 00:29:22 (UTC)
committer sandman <sandman>2002-12-17 00:29:22 (UTC)
commit2d39ff65b27c1a680a56c1b70b534e3cb767e08f (patch) (unidiff)
tree09e5cc3f7157681fe51fe83bb54ebbaa548d8c64 /library
parent70090722d240bed8c390281e072c9bcfc5ba7782 (diff)
downloadopie-2d39ff65b27c1a680a56c1b70b534e3cb767e08f.zip
opie-2d39ff65b27c1a680a56c1b70b534e3cb767e08f.tar.gz
opie-2d39ff65b27c1a680a56c1b70b534e3cb767e08f.tar.bz2
replaced the simple fork and execv method of starting applications with a
more sophisticated method (mostly taken from K/OProcess, because we can't use libopie in libqpe). We can detect now, if apps can not be started.
Diffstat (limited to 'library') (more/less context) (ignore whitespace changes)
-rw-r--r--library/global.cpp65
1 files changed, 54 insertions, 11 deletions
diff --git a/library/global.cpp b/library/global.cpp
index d6ba84f..68a3a75 100644
--- a/library/global.cpp
+++ b/library/global.cpp
@@ -1,140 +1,141 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#define QTOPIA_INTERNAL_LANGLIST 20#define QTOPIA_INTERNAL_LANGLIST
21#include <qpe/qpedebug.h> 21#include <qpe/qpedebug.h>
22#include <qpe/global.h> 22#include <qpe/global.h>
23#include <qpe/qdawg.h> 23#include <qpe/qdawg.h>
24#include <qpe/qpeapplication.h> 24#include <qpe/qpeapplication.h>
25#include <qpe/resource.h> 25#include <qpe/resource.h>
26#include <qpe/storage.h> 26#include <qpe/storage.h>
27#include <qpe/applnk.h> 27#include <qpe/applnk.h>
28#include <qpe/qcopenvelope_qws.h> 28#include <qpe/qcopenvelope_qws.h>
29 29
30#include <qfile.h> 30#include <qfile.h>
31#include <qlabel.h> 31#include <qlabel.h>
32#include <qtimer.h> 32#include <qtimer.h>
33#include <qmap.h> 33#include <qmap.h>
34#include <qdict.h> 34#include <qdict.h>
35#include <qdir.h> 35#include <qdir.h>
36#include <qmessagebox.h> 36#include <qmessagebox.h>
37#include <qregexp.h> 37#include <qregexp.h>
38 38
39#include <stdlib.h> 39#include <stdlib.h>
40#include <sys/stat.h> 40#include <sys/stat.h>
41#include <sys/wait.h> 41#include <sys/wait.h>
42#include <sys/types.h> 42#include <sys/types.h>
43#include <fcntl.h> 43#include <fcntl.h>
44#include <unistd.h> 44#include <unistd.h>
45#include <errno.h>
45 46
46#include <qwindowsystem_qws.h> // for qwsServer 47#include <qwindowsystem_qws.h> // for qwsServer
47#include <qdatetime.h> 48#include <qdatetime.h>
48 49
49#include <qfile.h> 50#include <qfile.h>
50 51
51namespace { 52namespace {
52 // checks if the storage should be searched 53 // checks if the storage should be searched
53 bool checkStorage(const QString &path ){ // this is a small Config replacement cause config is too limited -zecke 54 bool checkStorage(const QString &path ){ // this is a small Config replacement cause config is too limited -zecke
54 QFile file(path ); 55 QFile file(path );
55 if(!file.open(IO_ReadOnly ) ) 56 if(!file.open(IO_ReadOnly ) )
56 return true; 57 return true;
57 58
58 QByteArray array = file.readAll(); 59 QByteArray array = file.readAll();
59 QStringList list = QStringList::split('\n', QString( array ) ); 60 QStringList list = QStringList::split('\n', QString( array ) );
60 for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ){ 61 for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ){
61 if( (*it).startsWith("autocheck = 0" ) ){ 62 if( (*it).startsWith("autocheck = 0" ) ){
62 return false; 63 return false;
63 }else if( (*it).startsWith("autocheck = 1" ) ){ 64 }else if( (*it).startsWith("autocheck = 1" ) ){
64 return true; 65 return true;
65 } 66 }
66 } 67 }
67 return true; 68 return true;
68 } 69 }
69} 70}
70 71
71//#include "quickexec_p.h" 72//#include "quickexec_p.h"
72 73
73class Emitter : public QObject { 74class Emitter : public QObject {
74 Q_OBJECT 75 Q_OBJECT
75public: 76public:
76 Emitter( QWidget* receiver, const QString& document ) 77 Emitter( QWidget* receiver, const QString& document )
77 { 78 {
78 connect(this, SIGNAL(setDocument(const QString&)), 79 connect(this, SIGNAL(setDocument(const QString&)),
79 receiver, SLOT(setDocument(const QString&))); 80 receiver, SLOT(setDocument(const QString&)));
80 emit setDocument(document); 81 emit setDocument(document);
81 disconnect(this, SIGNAL(setDocument(const QString&)), 82 disconnect(this, SIGNAL(setDocument(const QString&)),
82 receiver, SLOT(setDocument(const QString&))); 83 receiver, SLOT(setDocument(const QString&)));
83 } 84 }
84 85
85signals: 86signals:
86 void setDocument(const QString&); 87 void setDocument(const QString&);
87}; 88};
88 89
89 90
90class StartingAppList : public QObject { 91class StartingAppList : public QObject {
91 Q_OBJECT 92 Q_OBJECT
92public: 93public:
93 static void add( const QString& name ); 94 static void add( const QString& name );
94 static bool isStarting( const QString name ); 95 static bool isStarting( const QString name );
95private slots: 96private slots:
96 void handleNewChannel( const QString &); 97 void handleNewChannel( const QString &);
97private: 98private:
98 StartingAppList( QObject *parent=0, const char* name=0 ) ; 99 StartingAppList( QObject *parent=0, const char* name=0 ) ;
99 100
100 QDict<QTime> dict; 101 QDict<QTime> dict;
101 static StartingAppList *appl; 102 static StartingAppList *appl;
102}; 103};
103 104
104StartingAppList* StartingAppList::appl = 0; 105StartingAppList* StartingAppList::appl = 0;
105 106
106StartingAppList::StartingAppList( QObject *parent, const char* name ) 107StartingAppList::StartingAppList( QObject *parent, const char* name )
107 :QObject( parent, name ) 108 :QObject( parent, name )
108{ 109{
109#if QT_VERSION >= 232 && defined(QWS) 110#if QT_VERSION >= 232 && defined(QWS)
110 connect( qwsServer, SIGNAL( newChannel(const QString&)), 111 connect( qwsServer, SIGNAL( newChannel(const QString&)),
111 this, SLOT( handleNewChannel(const QString&)) ); 112 this, SLOT( handleNewChannel(const QString&)) );
112 #endif 113 #endif
113 dict.setAutoDelete( TRUE ); 114 dict.setAutoDelete( TRUE );
114} 115}
115 116
116void StartingAppList::add( const QString& name ) 117void StartingAppList::add( const QString& name )
117{ 118{
118#if QT_VERSION >= 232 && !defined(QT_NO_COP) 119#if QT_VERSION >= 232 && !defined(QT_NO_COP)
119 if ( !appl ) 120 if ( !appl )
120 appl = new StartingAppList; 121 appl = new StartingAppList;
121 QTime *t = new QTime; 122 QTime *t = new QTime;
122 t->start(); 123 t->start();
123 appl->dict.insert( "QPE/Application/" + name, t ); 124 appl->dict.insert( "QPE/Application/" + name, t );
124#endif 125#endif
125} 126}
126 127
127bool StartingAppList::isStarting( const QString name ) 128bool StartingAppList::isStarting( const QString name )
128{ 129{
129#if QT_VERSION >= 232 && !defined(QT_NO_COP) 130#if QT_VERSION >= 232 && !defined(QT_NO_COP)
130 if ( appl ) { 131 if ( appl ) {
131 QTime *t = appl->dict.find( "QPE/Application/" + name ); 132 QTime *t = appl->dict.find( "QPE/Application/" + name );
132 if ( !t ) 133 if ( !t )
133 return FALSE; 134 return FALSE;
134 if ( t->elapsed() > 10000 ) { 135 if ( t->elapsed() > 10000 ) {
135 // timeout in case of crash or something 136 // timeout in case of crash or something
136 appl->dict.remove( "QPE/Application/" + name ); 137 appl->dict.remove( "QPE/Application/" + name );
137 return FALSE; 138 return FALSE;
138 } 139 }
139 return TRUE; 140 return TRUE;
140 } 141 }
@@ -510,204 +511,246 @@ void Global::setDocument( QWidget* receiver, const QString& document )
510 Emitter emitter(receiver,document); 511 Emitter emitter(receiver,document);
511} 512}
512 513
513/*! 514/*!
514 \internal 515 \internal
515*/ 516*/
516bool Global::terminateBuiltin( const QString& n ) 517bool Global::terminateBuiltin( const QString& n )
517{ 518{
518 if (!builtin) 519 if (!builtin)
519 return FALSE; 520 return FALSE;
520 for (int i = 0; builtin[i].file; i++) { 521 for (int i = 0; builtin[i].file; i++) {
521 if ( builtin[i].file == n ) { 522 if ( builtin[i].file == n ) {
522 delete running[i]; 523 delete running[i];
523 return TRUE; 524 return TRUE;
524 } 525 }
525 } 526 }
526 return FALSE; 527 return FALSE;
527} 528}
528 529
529/*! 530/*!
530 \internal 531 \internal
531*/ 532*/
532void Global::terminate( const AppLnk* app ) 533void Global::terminate( const AppLnk* app )
533{ 534{
534 //if ( terminateBuiltin(app->exec()) ) return; // maybe? haven't tried this 535 //if ( terminateBuiltin(app->exec()) ) return; // maybe? haven't tried this
535 536
536#ifndef QT_NO_COP 537#ifndef QT_NO_COP
537 QCString channel = "QPE/Application/" + app->exec().utf8(); 538 QCString channel = "QPE/Application/" + app->exec().utf8();
538 if ( QCopChannel::isRegistered(channel) ) { 539 if ( QCopChannel::isRegistered(channel) ) {
539 QCopEnvelope e(channel, "quit()"); 540 QCopEnvelope e(channel, "quit()");
540 } 541 }
541#endif 542#endif
542} 543}
543 544
544/*! 545/*!
545 Low-level function to run command \a c. 546 Low-level function to run command \a c.
546 547
547 \warning Do not use this function. Use execute instead. 548 \warning Do not use this function. Use execute instead.
548 549
549 \sa execute() 550 \sa execute()
550*/ 551*/
551void Global::invoke(const QString &c) 552void Global::invoke(const QString &c)
552{ 553{
553 // Convert the command line in to a list of arguments 554 // Convert the command line in to a list of arguments
554 QStringList list = QStringList::split(QRegExp(" *"),c); 555 QStringList list = QStringList::split(QRegExp(" *"),c);
555 556
556#if !defined(QT_NO_COP) 557#if !defined(QT_NO_COP)
557 QString ap=list[0]; 558 QString ap=list[0];
558 // see if the application is already running 559 // see if the application is already running
559 // XXX should lock file /tmp/qcop-msg-ap 560 // XXX should lock file /tmp/qcop-msg-ap
560 if ( QCopChannel::isRegistered( ("QPE/Application/" + ap).latin1() ) ) { 561 if ( QCopChannel::isRegistered( ("QPE/Application/" + ap).latin1() ) ) {
561 // If the channel is already register, the app is already running, so show it. 562 // If the channel is already register, the app is already running, so show it.
562 { QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "raise()" ); } 563 { QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "raise()" ); }
563 564
564 QCopEnvelope e("QPE/System", "notBusy(QString)" ); 565 QCopEnvelope e("QPE/System", "notBusy(QString)" );
565 e << ap; 566 e << ap;
566 return; 567 return;
567 } 568 }
568 // XXX should unlock file /tmp/qcop-msg-ap 569 // XXX should unlock file /tmp/qcop-msg-ap
569 //see if it is being started 570 //see if it is being started
570 if ( StartingAppList::isStarting( ap ) ) { 571 if ( StartingAppList::isStarting( ap ) ) {
571 QCopEnvelope e("QPE/System", "notBusy(QString)" ); 572 QCopEnvelope e("QPE/System", "notBusy(QString)" );
572 e << ap; 573 e << ap;
573 return; 574 return;
574 } 575 }
575 576
576#endif 577#endif
577 578
578#ifdef QT_NO_QWS_MULTIPROCESS 579#ifdef QT_NO_QWS_MULTIPROCESS
579 QMessageBox::warning( 0, "Error", "Could not find the application " + c, "Ok", 0, 0, 0, 1 ); 580 QMessageBox::warning( 0, "Error", "Could not find the application " + c, "Ok", 0, 0, 0, 1 );
580#else 581#else
581 582
582 QStrList slist; 583 QStrList slist;
583 unsigned int j; 584 unsigned int j;
584 for ( j = 0; j < list.count(); j++ ) 585 for ( j = 0; j < list.count(); j++ )
585 slist.append( list[j].utf8() ); 586 slist.append( list[j].utf8() );
586 587
587 const char **args = new (const char *)[slist.count() + 1]; 588 const char **args = new (const char *)[slist.count() + 1];
588 for ( j = 0; j < slist.count(); j++ ) 589 for ( j = 0; j < slist.count(); j++ )
589 args[j] = slist.at(j); 590 args[j] = slist.at(j);
590 args[j] = NULL; 591 args[j] = NULL;
591 592
592#if !defined(QT_NO_COP) 593#if !defined(QT_NO_COP)
593 // an attempt to show a wait... 594 // an attempt to show a wait...
594 // more logic should be used, but this will be fine for the moment... 595 // more logic should be used, but this will be fine for the moment...
595 QCopEnvelope ( "QPE/System", "busy()" ); 596 QCopEnvelope ( "QPE/System", "busy()" );
596#endif 597#endif
597 598
598#ifdef HAVE_QUICKEXEC 599#ifdef HAVE_QUICKEXEC
599 QString libexe = qpeDir()+"/binlib/lib"+args[0] + ".so"; 600 QString libexe = qpeDir()+"/binlib/lib"+args[0] + ".so";
600 qDebug("libfile = %s", libexe.latin1() ); 601 qDebug("libfile = %s", libexe.latin1() );
601 if ( QFile::exists( libexe ) ) { 602 if ( QFile::exists( libexe ) ) {
602 qDebug("calling quickexec %s", libexe.latin1() ); 603 qDebug("calling quickexec %s", libexe.latin1() );
603 quickexecv( libexe.utf8().data(), (const char **)args ); 604 quickexecv( libexe.utf8().data(), (const char **)args );
604 } else 605 } else
605#endif 606#endif
606 { 607 {
607 if ( !::vfork() ) { 608 bool success = false;
608 for ( int fd = 3; fd < 100; fd++ ) 609 int pfd [2];
609 ::close( fd ); 610 if ( ::pipe ( pfd ) < 0 )
610 ::setpgid( ::getpid(), ::getppid() ); 611 pfd [0] = pfd [1] = -1;
611 // Try bindir first, so that foo/bar works too 612
612 ::execv( qpeDir()+"/bin/"+args[0], (char * const *)args ); 613 pid_t pid = ::fork ( );
613 ::execvp( args[0], (char * const *)args ); 614
614 _exit( -1 ); 615 if ( pid == 0 ) { // child
615 } 616 for ( int fd = 3; fd < 100; fd++ ) {
617 if ( fd != pfd [1] )
618 ::close ( fd );
619 }
620 ::setpgid ( ::getpid ( ), ::getppid ( ));
621
622 // Closing of fd[1] indicates that the execvp succeeded!
623 if ( pfd [1] >= 0 )
624 ::fcntl ( pfd [1], F_SETFD, FD_CLOEXEC );
625
626 // Try bindir first, so that foo/bar works too
627 ::execv ( qpeDir ( ) + "/bin/" + args [0], (char * const *) args );
628 ::execvp ( args [0], (char * const *) args );
629
630 char resultByte = 1;
631 if ( pfd [1] >= 0 )
632 ::write ( pfd [1], &resultByte, 1 );
633 ::_exit ( -1 );
634 }
635 else if ( pid > 0 ) {
636 success = true;
637
638 if ( pfd [1] >= 0 )
639 ::close ( pfd [1] );
640 if ( pfd [0] >= 0 ) {
641 while ( true ) {
642 char resultByte;
643 int n = ::read ( pfd [0], &resultByte, 1 );
644 if ( n == 1 ) {
645 success = false;
646 break;
647 }
648 if (( n == -1 ) && (( errno == ECHILD ) || ( errno == EINTR )))
649 continue;
650
651 break; // success
652 }
653 ::close ( pfd [0] );
654 }
655 }
656 if ( success )
657 StartingAppList::add( list[0] );
658 else
659 QMessageBox::warning( 0, "Error", "Could not start the application " + c, "Ok", 0, 0, 0, 1 );
616 } 660 }
617 StartingAppList::add( list[0] );
618#endif //QT_NO_QWS_MULTIPROCESS 661#endif //QT_NO_QWS_MULTIPROCESS
619} 662}
620 663
621 664
622/*! 665/*!
623 Executes the application identfied by \a c, passing \a 666 Executes the application identfied by \a c, passing \a
624 document if it isn't null. 667 document if it isn't null.
625 668
626 Note that a better approach might be to send a QCop message to the 669 Note that a better approach might be to send a QCop message to the
627 application's QPE/Application/\e{appname} channel. 670 application's QPE/Application/\e{appname} channel.
628*/ 671*/
629void Global::execute( const QString &c, const QString& document ) 672void Global::execute( const QString &c, const QString& document )
630{ 673{
631 if ( qApp->type() != QApplication::GuiServer ) { 674 if ( qApp->type() != QApplication::GuiServer ) {
632 // ask the server to do the work 675 // ask the server to do the work
633#if !defined(QT_NO_COP) 676#if !defined(QT_NO_COP)
634 if ( document.isNull() ) { 677 if ( document.isNull() ) {
635 QCopEnvelope e( "QPE/System", "execute(QString)" ); 678 QCopEnvelope e( "QPE/System", "execute(QString)" );
636 e << c; 679 e << c;
637 } else { 680 } else {
638 QCopEnvelope e( "QPE/System", "execute(QString,QString)" ); 681 QCopEnvelope e( "QPE/System", "execute(QString,QString)" );
639 e << c << document; 682 e << c << document;
640 } 683 }
641#endif 684#endif
642 return; 685 return;
643 } 686 }
644 687
645 // Attempt to execute the app using a builtin class for the app first 688 // Attempt to execute the app using a builtin class for the app first
646 // else try and find it in the bin directory 689 // else try and find it in the bin directory
647 if (builtin) { 690 if (builtin) {
648 for (int i = 0; builtin[i].file; i++) { 691 for (int i = 0; builtin[i].file; i++) {
649 if ( builtin[i].file == c ) { 692 if ( builtin[i].file == c ) {
650 if ( running[i] ) { 693 if ( running[i] ) {
651 if ( !document.isNull() && builtin[i].documentary ) 694 if ( !document.isNull() && builtin[i].documentary )
652 setDocument(running[i], document); 695 setDocument(running[i], document);
653 running[i]->raise(); 696 running[i]->raise();
654 running[i]->show(); 697 running[i]->show();
655 running[i]->setActiveWindow(); 698 running[i]->setActiveWindow();
656 } else { 699 } else {
657 running[i] = builtin[i].func( builtin[i].maximized ); 700 running[i] = builtin[i].func( builtin[i].maximized );
658 } 701 }
659#ifndef QT_NO_COP 702#ifndef QT_NO_COP
660 QCopEnvelope e("QPE/System", "notBusy(QString)" ); 703 QCopEnvelope e("QPE/System", "notBusy(QString)" );
661 e << c; // that was quick ;-) 704 e << c; // that was quick ;-)
662#endif 705#endif
663 return; 706 return;
664 } 707 }
665 } 708 }
666 } 709 }
667 710
668 //Global::invoke(c, document); 711 //Global::invoke(c, document);
669 712
670 // Convert the command line in to a list of arguments 713 // Convert the command line in to a list of arguments
671 QStringList list = QStringList::split(QRegExp(" *"),c); 714 QStringList list = QStringList::split(QRegExp(" *"),c);
672 715
673#if !defined(QT_NO_COP) 716#if !defined(QT_NO_COP)
674 QString ap=list[0]; 717 QString ap=list[0];
675 718
676 qDebug("executing %s", ap.latin1() ); 719 qDebug("executing %s", ap.latin1() );
677 720
678 /* if need be, sending a qcop message will result in an invoke, see 721 /* if need be, sending a qcop message will result in an invoke, see
679 preceeding function */ 722 preceeding function */
680 invoke( ap ); 723 invoke( ap );
681 //{ QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "raise()" ); } 724 //{ QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "raise()" ); }
682 if ( !document.isEmpty() ) { 725 if ( !document.isEmpty() ) {
683 QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "setDocument(QString)" ); 726 QCopEnvelope env( ("QPE/Application/" + ap).latin1(), "setDocument(QString)" );
684 env << document; 727 env << document;
685 } 728 }
686#endif 729#endif
687} 730}
688 731
689/*! 732/*!
690 Returns the string \a s with the characters '\', '"', and '$' quoted 733 Returns the string \a s with the characters '\', '"', and '$' quoted
691 by a preceeding '\'. 734 by a preceeding '\'.
692 735
693 \sa stringQuote() 736 \sa stringQuote()
694*/ 737*/
695QString Global::shellQuote(const QString& s) 738QString Global::shellQuote(const QString& s)
696{ 739{
697 QString r="\""; 740 QString r="\"";
698 for (int i=0; i<(int)s.length(); i++) { 741 for (int i=0; i<(int)s.length(); i++) {
699 char c = s[i].latin1(); 742 char c = s[i].latin1();
700 switch (c) { 743 switch (c) {
701 case '\\': case '"': case '$': 744 case '\\': case '"': case '$':
702 r+="\\"; 745 r+="\\";
703 } 746 }
704 r += s[i]; 747 r += s[i];
705 } 748 }
706 r += "\""; 749 r += "\"";
707 return r; 750 return r;
708} 751}
709 752
710/*! 753/*!
711 Returns the string \a s with the characters '\' and '"' quoted by a 754 Returns the string \a s with the characters '\' and '"' quoted by a
712 preceeding '\'. 755 preceeding '\'.
713 756