/* This file is part of the KDE project Copyright (C) 2000-2002 Alexander Neundorf This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "program.h" #include #include #include #include #include #include #include #include #include #include Program::Program(const TQStringList &args) :m_pid(0) ,mArgs(args) ,mStarted(false) { } Program::~Program() { if (m_pid!=0) { ::close(mStdin[0]); ::close(mStdout[0]); ::close(mStderr[0]); ::close(mStdin[1]); ::close(mStdout[1]); ::close(mStderr[1]); int s(0); //::wait(&s); ::waitpid(m_pid,&s,0); this->kill(); ::waitpid(m_pid,&s,WNOHANG); }; } bool Program::start() { if (mStarted) return false; if (pipe(mStdout)==-1) return false; if (pipe(mStdin )==-1) return false; if (pipe(mStderr )==-1) return false; int notificationPipe[2]; if (pipe(notificationPipe )==-1) return false; m_pid=fork(); if (m_pid>0) { //parent ::close(mStdin[0]); ::close(mStdout[1]); ::close(mStderr[1]); ::close(notificationPipe[1]); mStarted=true; fd_set notifSet; FD_ZERO(¬ifSet); FD_SET(notificationPipe[0],¬ifSet); struct timeval tv; //wait up to five seconds kdDebug(7101)<<"**** waiting for notification"<0) return false; }; kdDebug(7101)<<"**** waiting for notification: succeeded"<maxFD) maxFD=stderrFD(); /*fd_set writeFDs; FD_ZERO(&writeFDs); FD_SET(stdinFD(),&writeFDs); if (stdinFD()>maxFD) maxFD=stdinFD();*/ maxFD++; int result=::select(maxFD,&readFDs,/*&writeFDs*/0,0,&tv); if (result>0) { stdoutReceived=FD_ISSET(stdoutFD(),&readFDs); stderrReceived=FD_ISSET(stderrFD(),&readFDs); //stdinWaiting=(FD_ISSET(stdinFD(),&writeFDs)); }; return result; } int Program::kill() { if (m_pid==0) return -1; return ::kill(m_pid, SIGTERM); //::kill(m_pid, SIGKILL); }