summaryrefslogtreecommitdiff
authormickeyl <mickeyl>2004-01-13 19:51:42 (UTC)
committer mickeyl <mickeyl>2004-01-13 19:51:42 (UTC)
commite117e8427cd8bcd0ab1a74abdc5cd4ab12654194 (patch) (unidiff)
treef013f4f92cceefd20fb519f7e9a4d23f00fadac5
parent81b48fa5be4806e3afa64a0d1fa254fbdf9b7315 (diff)
downloadopie-e117e8427cd8bcd0ab1a74abdc5cd4ab12654194.zip
opie-e117e8427cd8bcd0ab1a74abdc5cd4ab12654194.tar.gz
opie-e117e8427cd8bcd0ab1a74abdc5cd4ab12654194.tar.bz2
- add the static method int OProcess::processPID(const QString&)
- add an example program for dealing with OProcess. This should be enhanced...
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/examples/opiecore/opiecore.pro2
-rw-r--r--libopie2/examples/opiecore/oprocessdemo/.cvsignore6
-rw-r--r--libopie2/examples/opiecore/oprocessdemo/oprocessdemo.cpp11
-rw-r--r--libopie2/examples/opiecore/oprocessdemo/oprocessdemo.pro12
-rw-r--r--libopie2/opiecore/oprocess.cpp30
-rw-r--r--libopie2/opiecore/oprocess.h5
6 files changed, 65 insertions, 1 deletions
diff --git a/libopie2/examples/opiecore/opiecore.pro b/libopie2/examples/opiecore/opiecore.pro
index 8f3aedc..ec14be0 100644
--- a/libopie2/examples/opiecore/opiecore.pro
+++ b/libopie2/examples/opiecore/opiecore.pro
@@ -1,3 +1,3 @@
1TEMPLATE = subdirs 1TEMPLATE = subdirs
2unix:SUBDIRS = odebugdemo oconfigdemo oglobalsettingsdemo 2unix:SUBDIRS = odebugdemo oconfigdemo oglobalsettingsdemo oprocessdemo
3 3
diff --git a/libopie2/examples/opiecore/oprocessdemo/.cvsignore b/libopie2/examples/opiecore/oprocessdemo/.cvsignore
new file mode 100644
index 0000000..8f7300c
--- a/dev/null
+++ b/libopie2/examples/opiecore/oprocessdemo/.cvsignore
@@ -0,0 +1,6 @@
1Makefile*
2moc*
3*moc
4*.o
5~*
6
diff --git a/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.cpp b/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.cpp
new file mode 100644
index 0000000..0abf53e
--- a/dev/null
+++ b/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.cpp
@@ -0,0 +1,11 @@
1#include <opie2/oprocess.h>
2#include <iostream.h>
3
4int main( int argc, char** argv )
5{
6 printf( "my own PID seems to be '%d'\n", OProcess::processPID( "oprocessdemo" ) );
7 printf( "the PID of process 'Mickey' seems to be '%d'\n\n", OProcess::processPID( "Mickey" ) );
8
9 return 0;
10}
11
diff --git a/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.pro b/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.pro
new file mode 100644
index 0000000..72dac7f
--- a/dev/null
+++ b/libopie2/examples/opiecore/oprocessdemo/oprocessdemo.pro
@@ -0,0 +1,12 @@
1TEMPLATE = app
2CONFIG = qt warn_on debug
3HEADERS =
4SOURCES = oprocessdemo.cpp
5INCLUDEPATH += $(OPIEDIR)/include
6DEPENDPATH += $(OPIEDIR)/include
7LIBS += -lopiecore2
8TARGET = oprocessdemo
9
10include ( $(OPIEDIR)/include.pro )
11
12
diff --git a/libopie2/opiecore/oprocess.cpp b/libopie2/opiecore/oprocess.cpp
index f1a5f3b..5cfcf32 100644
--- a/libopie2/opiecore/oprocess.cpp
+++ b/libopie2/opiecore/oprocess.cpp
@@ -23,36 +23,38 @@ _;:,     .>    :=|. This program is free software; you can
23-.   .:....=;==+<; You should have received a copy of the GNU 23-.   .:....=;==+<; You should have received a copy of the GNU
24 -_. . .   )=.  = Library General Public License along with 24 -_. . .   )=.  = Library General Public License along with
25   --        :-=` this library; see the file COPYING.LIB. 25   --        :-=` this library; see the file COPYING.LIB.
26 If not, write to the Free Software Foundation, 26 If not, write to the Free Software Foundation,
27 Inc., 59 Temple Place - Suite 330, 27 Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. 28 Boston, MA 02111-1307, USA.
29*/ 29*/
30 30
31#include "oprocctrl.h" 31#include "oprocctrl.h"
32 32
33/* OPIE */ 33/* OPIE */
34#include <opie2/oprocess.h> 34#include <opie2/oprocess.h>
35 35
36/* QT */ 36/* QT */
37 37
38#include <qapplication.h> 38#include <qapplication.h>
39#include <qdir.h>
39#include <qfile.h> 40#include <qfile.h>
40#include <qmap.h> 41#include <qmap.h>
41#include <qregexp.h> 42#include <qregexp.h>
42#include <qsocketnotifier.h> 43#include <qsocketnotifier.h>
44#include <qtextstream.h>
43 45
44/* STD */ 46/* STD */
45#include <errno.h> 47#include <errno.h>
46#include <fcntl.h> 48#include <fcntl.h>
47#include <pwd.h> 49#include <pwd.h>
48#include <stdlib.h> 50#include <stdlib.h>
49#include <signal.h> 51#include <signal.h>
50#include <stdio.h> 52#include <stdio.h>
51#include <string.h> 53#include <string.h>
52#include <sys/time.h> 54#include <sys/time.h>
53#include <sys/types.h> 55#include <sys/types.h>
54#include <sys/stat.h> 56#include <sys/stat.h>
55#include <sys/socket.h> 57#include <sys/socket.h>
56#include <unistd.h> 58#include <unistd.h>
57#ifdef HAVE_SYS_SELECT_H 59#ifdef HAVE_SYS_SELECT_H
58#include <sys/select.h> 60#include <sys/select.h>
@@ -900,16 +902,44 @@ bool OProcess::isExecutable( const QCString &filename )
900 ( S_ISSOCK( fileinfo.st_mode ) ) || 902 ( S_ISSOCK( fileinfo.st_mode ) ) ||
901#endif 903#endif
902 ( S_ISFIFO( fileinfo.st_mode ) ) || 904 ( S_ISFIFO( fileinfo.st_mode ) ) ||
903 ( S_ISDIR( fileinfo.st_mode ) ) ) 905 ( S_ISDIR( fileinfo.st_mode ) ) )
904 { 906 {
905 return false; 907 return false;
906 } 908 }
907 909
908 // CC: now check for permission to execute the file 910 // CC: now check for permission to execute the file
909 if ( access( filename.data(), X_OK ) != 0 ) 911 if ( access( filename.data(), X_OK ) != 0 )
910 return false; 912 return false;
911 913
912 // CC: we've passed all the tests... 914 // CC: we've passed all the tests...
913 return true; 915 return true;
914} 916}
915 917
918int OProcess::processPID( const QString& process )
919{
920 QString line;
921 QDir d = QDir( "/proc" );
922 QStringList dirs = d.entryList( QDir::Dirs );
923 QStringList::Iterator it;
924 for ( it = dirs.begin(); it != dirs.end(); ++it )
925 {
926 //qDebug( "next entry: %s", (const char*) *it );
927 QFile file( "/proc/"+*it+"/cmdline" );
928 file.open( IO_ReadOnly );
929 if ( !file.isOpen() ) continue;
930 QTextStream t( &file );
931 line = t.readLine();
932 //qDebug( "cmdline = %s", (const char*) line );
933 if ( line.contains( process ) ) break; //FIXME: That may find also other process, if the name is not long enough ;)
934 }
935 if ( line.contains( process ) )
936 {
937 //qDebug( "found process id #%d", (*it).toInt() );
938 return (*it).toInt();
939 }
940 else
941 {
942 //qDebug( "process '%s' not found", (const char*) process );
943 return -1;
944 }
945}
diff --git a/libopie2/opiecore/oprocess.h b/libopie2/opiecore/oprocess.h
index 352485b..1a2472d 100644
--- a/libopie2/opiecore/oprocess.h
+++ b/libopie2/opiecore/oprocess.h
@@ -448,32 +448,37 @@ public:
448 * This function can be used to quote an argument string such that 448 * This function can be used to quote an argument string such that
449 * the shell processes it properly. This is e. g. necessary for 449 * the shell processes it properly. This is e. g. necessary for
450 * user-provided file names which may contain spaces or quotes. 450 * user-provided file names which may contain spaces or quotes.
451 * It also prevents expansion of wild cards and environment variables. 451 * It also prevents expansion of wild cards and environment variables.
452 */ 452 */
453 static QString quote( const QString &arg ); 453 static QString quote( const QString &arg );
454 454
455 /** 455 /**
456 * Detaches OProcess from child process. All communication is closed. 456 * Detaches OProcess from child process. All communication is closed.
457 * No exit notification is emitted any more for the child process. 457 * No exit notification is emitted any more for the child process.
458 * Deleting the OProcess will no longer kill the child process. 458 * Deleting the OProcess will no longer kill the child process.
459 * Note that the current process remains the parent process of the 459 * Note that the current process remains the parent process of the
460 * child process. 460 * child process.
461 */ 461 */
462 void detach(); 462 void detach();
463 463
464 /**
465 * @return the PID of @a process, or -1 if the process is not running
466 */
467 static int processPID( const QString& process );
468
464signals: 469signals:
465 470
466 /** 471 /**
467 * Emitted after the process has terminated when 472 * Emitted after the process has terminated when
468 * the process was run in the @p NotifyOnExit (==default option to 473 * the process was run in the @p NotifyOnExit (==default option to
469 * @ref start()) or the @ref Block mode. 474 * @ref start()) or the @ref Block mode.
470 **/ 475 **/
471 void processExited( OProcess *proc ); 476 void processExited( OProcess *proc );
472 477
473 478
474 /** 479 /**
475 * Emitted, when output from the child process has 480 * Emitted, when output from the child process has
476 * been received on stdout. 481 * been received on stdout.
477 * 482 *
478 * To actually get 483 * To actually get
479 * these signals, the respective communication link (stdout/stderr) 484 * these signals, the respective communication link (stdout/stderr)