kprocess.h
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef __kprocess_h__ 00021 #define __kprocess_h__ 00022 00023 #include <sys/types.h> // for pid_t 00024 #include <sys/wait.h> 00025 #include <signal.h> 00026 #include <unistd.h> 00027 #include <tqvaluelist.h> 00028 #include <tqcstring.h> 00029 #include <tqobject.h> 00030 #include "kdelibs_export.h" 00031 00032 class TQSocketNotifier; 00033 class KProcessPrivate; 00034 00035 #ifdef Q_OS_UNIX 00036 #include <kpty.h> 00037 #else 00038 class KPty; 00039 #endif 00040 00130 class KDECORE_EXPORT KProcess : public TQObject 00131 { 00132 Q_OBJECT 00133 00134 public: 00135 00157 enum Communication { 00158 NoCommunication = 0, 00159 Stdin = 1, Stdout = 2, Stderr = 4, 00160 AllOutput = 6, All = 7, 00161 NoRead = 8, 00162 CTtyOnly = NoRead, 00163 MergedStderr = 16 00164 }; 00165 00169 enum RunMode { 00174 DontCare, 00178 NotifyOnExit, 00182 Block, 00187 OwnGroup 00188 }; 00189 00194 KProcess( TQObject* parent, const char *name = 0 ); 00195 // KDE4 merge with the above 00199 KProcess(); 00200 00209 virtual ~KProcess(); 00210 00222 bool setExecutable(const TQString& proc) KDE_DEPRECATED; 00223 00224 00238 KProcess &operator<<(const TQString& arg); 00242 KProcess &operator<<(const char * arg); 00248 KProcess &operator<<(const TQCString & arg); 00249 00256 KProcess &operator<<(const TQStringList& args); 00257 00262 void clearArguments(); 00263 00290 virtual bool start(RunMode runmode = NotifyOnExit, 00291 Communication comm = NoCommunication); 00292 00299 virtual bool kill(int signo = SIGTERM); 00300 00305 bool isRunning() const; 00306 00317 pid_t pid() const; 00318 00323 KDE_DEPRECATED pid_t getPid() const { return pid(); } 00324 00328 void suspend(); 00329 00333 void resume(); 00334 00343 bool wait(int timeout = -1); 00344 00351 bool normalExit() const; 00352 00361 bool signalled() const; 00362 00372 bool coreDumped() const; 00373 00380 int exitStatus() const; 00381 00390 int exitSignal() const; 00391 00422 bool writeStdin(const char *buffer, int buflen); 00423 00430 bool closeStdin(); 00431 00439 bool closeStdout(); 00440 00448 bool closeStderr(); 00449 00458 bool closePty(); 00459 00466 void closeAll(); 00467 00472 const TQValueList<TQCString> &args() /* const */ { return arguments; } 00473 00483 void setRunPrivileged(bool keepPrivileges); 00484 00490 bool runPrivileged() const; 00491 00498 void setEnvironment(const TQString &name, const TQString &value); 00499 00506 void setWorkingDirectory(const TQString &dir); 00507 00524 void setUseShell(bool useShell, const char *shell = 0); 00525 00535 static TQString quote(const TQString &arg); 00536 00544 void detach(); 00545 00546 #ifdef Q_OS_UNIX 00547 00558 void setUsePty(Communication comm, bool addUtmp); 00559 00567 KPty *pty() const; 00568 #endif 00569 00573 enum { PrioLowest = 20, PrioLow = 10, PrioLower = 5, PrioNormal = 0, 00574 PrioHigher = -5, PrioHigh = -10, PrioHighest = -19 }; 00575 00582 bool setPriority(int prio); 00583 00584 signals: 00591 void processExited(KProcess *proc); 00592 00593 00612 void receivedStdout(KProcess *proc, char *buffer, int buflen); 00613 00632 void receivedStdout(int fd, int &len); // KDE4: change, broken API 00633 00634 00649 void receivedStderr(KProcess *proc, char *buffer, int buflen); 00650 00657 void wroteStdin(KProcess *proc); 00658 00659 00660 protected slots: 00661 00667 void slotChildOutput(int fdno); 00668 00674 void slotChildError(int fdno); 00675 00682 void slotSendData(int dummy); // KDE 4: remove dummy 00683 00684 protected: 00685 00690 void setupEnvironment(); 00691 00696 TQValueList<TQCString> arguments; 00701 RunMode run_mode; 00708 bool runs; 00709 00717 pid_t pid_; 00718 00726 int status; 00727 00728 00734 bool keepPrivs; 00735 00748 virtual int setupCommunication(Communication comm); 00749 00762 virtual int commSetupDoneP(); 00763 00769 virtual int commSetupDoneC(); 00770 00771 00778 virtual void processHasExited(int state); 00779 00805 virtual void commClose(); 00806 00807 /* KDE 4 - commClose will be changed to perform cleanup only in all cases * 00808 * If @p notfd is -1, all data immediately available from the 00809 * communication links should be processed. 00810 * If @p notfd is not -1, the communication links should be monitored 00811 * for data until the file handle @p notfd becomes ready for reading. 00812 */ 00813 // virtual void commDrain(int notfd); 00814 00820 void setBinaryExecutable(const char *filename); 00821 00825 int out[2]; 00829 int in[2]; 00833 int err[2]; 00834 00838 TQSocketNotifier *innot; 00842 TQSocketNotifier *outnot; 00846 TQSocketNotifier *errnot; 00847 00852 Communication communication; 00853 00859 int childOutput(int fdno); 00860 00866 int childError(int fdno); 00867 00871 const char *input_data; 00875 int input_sent; 00879 int input_total; 00880 00885 friend class KProcessController; 00886 00887 protected: 00888 virtual void virtual_hook( int id, void* data ); 00889 private: 00890 KProcessPrivate *d; 00891 }; 00892 00893 class KShellProcessPrivate; 00894 00904 class KDECORE_EXPORT KShellProcess: public KProcess 00905 { 00906 Q_OBJECT 00907 00908 public: 00909 00915 KShellProcess(const char *shellname=0); 00916 00920 ~KShellProcess(); 00921 00922 virtual bool start(RunMode runmode = NotifyOnExit, 00923 Communication comm = NoCommunication); 00924 00925 static TQString quote(const TQString &arg); 00926 00927 private: 00928 TQCString shell; 00929 00930 protected: 00931 virtual void virtual_hook( int id, void* data ); 00932 private: 00933 KShellProcessPrivate *d; 00934 }; 00935 00936 00937 00938 #endif 00939