1/*
2 This file is part of the KDE libraries
3
4 Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22
23#include "kptyprocess.h"
24#include "kprocess_p.h"
25
26#include <kuser.h>
27#include <kptydevice.h>
28
29#include <stdlib.h>
30#include <unistd.h>
31
32//////////////////
33// private data //
34//////////////////
35
36struct KPtyProcessPrivate : KProcessPrivate {
37 KPtyProcessPrivate() :
38 ptyChannels(KPtyProcess::NoChannels),
39 addUtmp(false)
40 {
41 }
42
43 void _k_onStateChanged(QProcess::ProcessState newState)
44 {
45 if (newState == QProcess::NotRunning && addUtmp)
46 pty->logout();
47 }
48
49 KPtyDevice *pty;
50 KPtyProcess::PtyChannels ptyChannels;
51 bool addUtmp : 1;
52};
53
54KPtyProcess::KPtyProcess(QObject *parent) :
55 KProcess(new KPtyProcessPrivate, parent)
56{
57 Q_D(KPtyProcess);
58
59 d->pty = new KPtyDevice(this);
60 d->pty->open();
61 connect(this, SIGNAL(stateChanged(QProcess::ProcessState)),
62 SLOT(_k_onStateChanged(QProcess::ProcessState)));
63}
64
65KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) :
66 KProcess(new KPtyProcessPrivate, parent)
67{
68 Q_D(KPtyProcess);
69
70 d->pty = new KPtyDevice(this);
71 d->pty->open(ptyMasterFd);
72 connect(this, SIGNAL(stateChanged(QProcess::ProcessState)),
73 SLOT(_k_onStateChanged(QProcess::ProcessState)));
74}
75
76KPtyProcess::~KPtyProcess()
77{
78 Q_D(KPtyProcess);
79
80 if (state() != QProcess::NotRunning && d->addUtmp) {
81 d->pty->logout();
82 disconnect(SIGNAL(stateChanged(QProcess::ProcessState)),
83 this, SLOT(_k_onStateChanged(QProcess::ProcessState)));
84 }
85 delete d->pty;
86}
87
88void KPtyProcess::setPtyChannels(PtyChannels channels)
89{
90 Q_D(KPtyProcess);
91
92 d->ptyChannels = channels;
93}
94
95KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const
96{
97 Q_D(const KPtyProcess);
98
99 return d->ptyChannels;
100}
101
102void KPtyProcess::setUseUtmp(bool value)
103{
104 Q_D(KPtyProcess);
105
106 d->addUtmp = value;
107}
108
109bool KPtyProcess::isUseUtmp() const
110{
111 Q_D(const KPtyProcess);
112
113 return d->addUtmp;
114}
115
116KPtyDevice *KPtyProcess::pty() const
117{
118 Q_D(const KPtyProcess);
119
120 return d->pty;
121}
122
123void KPtyProcess::setupChildProcess()
124{
125 Q_D(KPtyProcess);
126
127 d->pty->setCTty();
128 if (d->addUtmp)
129 d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY"));
130 if (d->ptyChannels & StdinChannel)
131 dup2(d->pty->slaveFd(), 0);
132 if (d->ptyChannels & StdoutChannel)
133 dup2(d->pty->slaveFd(), 1);
134 if (d->ptyChannels & StderrChannel)
135 dup2(d->pty->slaveFd(), 2);
136
137 KProcess::setupChildProcess();
138}
139
140#include "kptyprocess.moc"
141