KPty
kptyprocess.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 00004 Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 00023 #include "kptyprocess.h" 00024 #include "kprocess_p.h" 00025 00026 #include <kuser.h> 00027 #include <kptydevice.h> 00028 00029 #include <stdlib.h> 00030 #include <unistd.h> 00031 00033 // private data // 00035 00036 struct KPtyProcessPrivate : KProcessPrivate { 00037 KPtyProcessPrivate() : 00038 ptyChannels(KPtyProcess::NoChannels), 00039 addUtmp(false) 00040 { 00041 } 00042 00043 void _k_onStateChanged(QProcess::ProcessState newState) 00044 { 00045 if (newState == QProcess::NotRunning && addUtmp) 00046 pty->logout(); 00047 } 00048 00049 KPtyDevice *pty; 00050 KPtyProcess::PtyChannels ptyChannels; 00051 bool addUtmp : 1; 00052 }; 00053 00054 KPtyProcess::KPtyProcess(QObject *parent) : 00055 KProcess(new KPtyProcessPrivate, parent) 00056 { 00057 Q_D(KPtyProcess); 00058 00059 d->pty = new KPtyDevice(this); 00060 d->pty->open(); 00061 connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), 00062 SLOT(_k_onStateChanged(QProcess::ProcessState))); 00063 } 00064 00065 KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) : 00066 KProcess(new KPtyProcessPrivate, parent) 00067 { 00068 Q_D(KPtyProcess); 00069 00070 d->pty = new KPtyDevice(this); 00071 d->pty->open(ptyMasterFd); 00072 connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), 00073 SLOT(_k_onStateChanged(QProcess::ProcessState))); 00074 } 00075 00076 KPtyProcess::~KPtyProcess() 00077 { 00078 Q_D(KPtyProcess); 00079 00080 if (state() != QProcess::NotRunning && d->addUtmp) { 00081 d->pty->logout(); 00082 disconnect(SIGNAL(stateChanged(QProcess::ProcessState)), 00083 this, SLOT(_k_onStateChanged(QProcess::ProcessState))); 00084 } 00085 delete d->pty; 00086 } 00087 00088 void KPtyProcess::setPtyChannels(PtyChannels channels) 00089 { 00090 Q_D(KPtyProcess); 00091 00092 d->ptyChannels = channels; 00093 } 00094 00095 KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const 00096 { 00097 Q_D(const KPtyProcess); 00098 00099 return d->ptyChannels; 00100 } 00101 00102 void KPtyProcess::setUseUtmp(bool value) 00103 { 00104 Q_D(KPtyProcess); 00105 00106 d->addUtmp = value; 00107 } 00108 00109 bool KPtyProcess::isUseUtmp() const 00110 { 00111 Q_D(const KPtyProcess); 00112 00113 return d->addUtmp; 00114 } 00115 00116 KPtyDevice *KPtyProcess::pty() const 00117 { 00118 Q_D(const KPtyProcess); 00119 00120 return d->pty; 00121 } 00122 00123 void KPtyProcess::setupChildProcess() 00124 { 00125 Q_D(KPtyProcess); 00126 00127 d->pty->setCTty(); 00128 if (d->addUtmp) 00129 d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY")); 00130 if (d->ptyChannels & StdinChannel) 00131 dup2(d->pty->slaveFd(), 0); 00132 if (d->ptyChannels & StdoutChannel) 00133 dup2(d->pty->slaveFd(), 1); 00134 if (d->ptyChannels & StderrChannel) 00135 dup2(d->pty->slaveFd(), 2); 00136 00137 KProcess::setupChildProcess(); 00138 } 00139 00140 #include "kptyprocess.moc"
KDE 4.6 API Reference