1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
gnupgprocessbase.cpp
This file is part of libkleopatra, the KDE keymanagement library
Copyright (c) 2004 Klar�lvdalens Datakonsult AB
Libkleopatra is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
Libkleopatra 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
TQt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include "gnupgprocessbase.h"
#include <kdebug.h>
#include <kurl.h>
#include <tqsocketnotifier.h>
#include <tqtextcodec.h>
#include <tqstringlist.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
struct Kleo::GnuPGProcessBase::Private {
Private() : usetStatusFD( false ), statnot( 0 ) {
statusFD[0] = statusFD[1] = -1;
}
bool usetStatusFD;
int statusFD[2];
TQSocketNotifier * statnot;
TQCString statusBuffer;
};
Kleo::GnuPGProcessBase::GnuPGProcessBase( TQObject * parent, const char * name )
: KProcess( parent, name )
{
d = new Private();
}
Kleo::GnuPGProcessBase::~GnuPGProcessBase() {
delete d; d = 0;
}
void Kleo::GnuPGProcessBase::setUsetStatusFD( bool use ) {
assert( d );
d->usetStatusFD = use;
}
bool Kleo::GnuPGProcessBase::start( RunMode runmode, Communication comm ) {
if ( d->usetStatusFD ) {
// set up the status-fd. This should be in setupCommunication(),
// but then it's too late: we need the fd of the pipe to pass it
// as argument to the --status-fd option:
// PENDING(marc) find out why KProcess uses both pipe() and socketpair()...
if ( ::pipe( d->statusFD ) < 0 ) {
kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::start: pipe(2) failed: " << perror << endl;
return false;
}
::fcntl( d->statusFD[0], F_SETFD, FD_CLOEXEC );
::fcntl( d->statusFD[1], F_SETFD, FD_CLOEXEC );
if ( !arguments.empty() ) {
TQValueList<TQCString>::iterator it = arguments.begin();
++it;
arguments.insert( it, "--status-fd" );
char buf[25];
sprintf( buf, "%d", d->statusFD[1] );
arguments.insert( it, buf );
arguments.insert( it, "--no-tty" );
//arguments.insert( it, "--enable-progress-filter" ); // gpgsm doesn't know this
}
}
return KProcess::start( runmode, comm );
}
int Kleo::GnuPGProcessBase::setupCommunication( Communication comm ) {
if ( int ok = KProcess::setupCommunication( comm ) )
return ok;
if ( d->usetStatusFD ) {
// base class impl returned error, so close our fd's, too
::close( d->statusFD[0] );
::close( d->statusFD[1] );
d->statusFD[0] = d->statusFD[1] = -1;
}
return 0; // Error
}
int Kleo::GnuPGProcessBase::commSetupDoneP() {
if ( d->usetStatusFD ) {
::close( d->statusFD[1] ); // close the input end of the pipe, we're the reader
d->statnot = new TQSocketNotifier( d->statusFD[0], TQSocketNotifier::Read, this );
connect( d->statnot, TQT_SIGNAL(activated(int)), TQT_SLOT(slotChildStatus(int)) );
}
return KProcess::commSetupDoneP();
}
int Kleo::GnuPGProcessBase::commSetupDoneC() {
if ( d->usetStatusFD )
::fcntl( d->statusFD[1], F_SETFD, 0 );
return KProcess::commSetupDoneC();
}
void Kleo::GnuPGProcessBase::slotChildStatus( int fd ) {
if ( !childStatus(fd) )
closetStatus();
}
bool Kleo::GnuPGProcessBase::closetStatus() {
if ( !d->usetStatusFD )
return false;
d->usetStatusFD = false;
delete d->statnot; d->statnot = 0;
::close( d->statusFD[0] ); d->statusFD[0] = -1;
return true;
}
int Kleo::GnuPGProcessBase::childStatus( int fd ) {
char buf[1024];
const int len = ::read( fd, buf, sizeof(buf)-1 );
if ( len > 0 ) {
buf[len] = 0;
d->statusBuffer += buf;
parsetStatusOutput();
}
return len;
}
static TQString fromHexEscapedUtf8( const TQCString & str ) {
return KURL::decode_string( str.data(), 106 /* utf-8 */ );
}
void Kleo::GnuPGProcessBase::parsetStatusOutput() {
static const char startToken[] = "[GNUPG:] ";
static const int startTokenLen = sizeof startToken / sizeof *startToken - 1;
int lineStart = 0;
for ( int lineEnd = d->statusBuffer.find( '\n' ) ; lineEnd >= 0 ; lineEnd = d->statusBuffer.find( '\n', lineStart = lineEnd+1 ) ) {
// get next line:
const TQCString line = d->statusBuffer.mid( lineStart, lineEnd - lineStart ).stripWhiteSpace();
if ( line.isEmpty() )
continue;
// check status token
if ( line.left( startTokenLen ) != startToken ) {
kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line doesn't begin with \""
<< startToken << "\"" << endl;
continue;
}
// remove status token:
const TQCString command = line.mid( startTokenLen ).simplifyWhiteSpace() + ' ';
if ( command == " " ) {
kdDebug( 5150 ) << "Kleo::GnuPGProcessBase::childStatus: status-fd protocol error: line without content." << endl;
continue;
}
// split into base and args
TQString cmd;
TQStringList args;
int tagStart = 0;
for ( int tagEnd = command.find( ' ' ) ; tagEnd >= 0 ; tagEnd = command.find( ' ', tagStart = tagEnd+1 ) ) {
const TQCString tag = command.mid( tagStart, tagEnd - tagStart );
if ( cmd.isNull() )
cmd = fromHexEscapedUtf8( tag );
else
args.push_back( fromHexEscapedUtf8( tag ) );
}
emit status( this, cmd, args );
}
d->statusBuffer = d->statusBuffer.mid( lineStart );
}
void Kleo::GnuPGProcessBase::virtual_hook( int id, void * data ) {
KProcess::virtual_hook( id, data );
}
#include "gnupgprocessbase.moc"
|