• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kfsprocess.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfsprocess.h"
21
22#include <config.h>
23
24#include <kdebug.h>
25//#include <kuser.h>
26
27#include <QtCore/QMap>
28#include <QtCore/QFile>
29#include <QtCore/QSocketNotifier>
30
31#include <unistd.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <errno.h>
35#include <sys/wait.h>
36
37KfsProcessController *KfsProcessController::s_instance = 0;
38int KfsProcessController::s_refCount = 0;
39
40void KfsProcessController::ref()
41{
42 if ( !s_refCount ) {
43 s_instance = new KfsProcessController;
44 setupHandlers();
45 }
46 s_refCount++;
47}
48
49void KfsProcessController::deref()
50{
51 s_refCount--;
52 if( !s_refCount ) {
53 resetHandlers();
54 delete s_instance;
55 s_instance = 0;
56 }
57}
58
59KfsProcessController* KfsProcessController::instance()
60{
61 return s_instance;
62}
63
64KfsProcessController::KfsProcessController()
65{
66 if( pipe( m_fd ) )
67 {
68 perror( "pipe" );
69 abort();
70 }
71
72 fcntl( m_fd[0], F_SETFL, O_NONBLOCK ); // in case slotDoHousekeeping is called without polling first
73 fcntl( m_fd[1], F_SETFL, O_NONBLOCK ); // in case it fills up
74 fcntl( m_fd[0], F_SETFD, FD_CLOEXEC );
75 fcntl( m_fd[1], F_SETFD, FD_CLOEXEC );
76
77 m_notifier = new QSocketNotifier( m_fd[0], QSocketNotifier::Read );
78 m_notifier->setEnabled( true );
79 QObject::connect( m_notifier, SIGNAL(activated(int)),
80 SLOT(slotDoHousekeeping()));
81}
82
83KfsProcessController::~KfsProcessController()
84{
85#ifndef Q_OS_MAC
86/* not sure why, but this is causing lockups */
87 close( m_fd[0] );
88 close( m_fd[1] );
89#else
90#warning FIXME: why does close() freeze up destruction?
91#endif
92}
93
94
95extern "C" {
96static void theReaper( int num )
97{
98 KfsProcessController::theSigCHLDHandler( num );
99}
100}
101
102struct sigaction KfsProcessController::s_oldChildHandlerData;
103bool KfsProcessController::s_handlerSet = false;
104
105void KfsProcessController::setupHandlers()
106{
107 if( s_handlerSet )
108 return;
109 s_handlerSet = true;
110
111 struct sigaction act;
112 sigemptyset( &act.sa_mask );
113
114 act.sa_handler = SIG_IGN;
115 act.sa_flags = 0;
116 sigaction( SIGPIPE, &act, 0L );
117
118 act.sa_handler = theReaper;
119 act.sa_flags = SA_NOCLDSTOP;
120 // CC: take care of SunOS which automatically restarts interrupted system
121 // calls (and thus does not have SA_RESTART)
122#ifdef SA_RESTART
123 act.sa_flags |= SA_RESTART;
124#endif
125 sigaction( SIGCHLD, &act, &s_oldChildHandlerData );
126
127 sigaddset( &act.sa_mask, SIGCHLD );
128 // Make sure we don't block this signal. gdb tends to do that :-(
129 sigprocmask( SIG_UNBLOCK, &act.sa_mask, 0 );
130}
131
132void KfsProcessController::resetHandlers()
133{
134 if( !s_handlerSet )
135 return;
136 s_handlerSet = false;
137
138 sigset_t mask, omask;
139 sigemptyset( &mask );
140 sigaddset( &mask, SIGCHLD );
141 sigprocmask( SIG_BLOCK, &mask, &omask );
142
143 struct sigaction act;
144 sigaction( SIGCHLD, &s_oldChildHandlerData, &act );
145 if (act.sa_handler != theReaper) {
146 sigaction( SIGCHLD, &act, 0 );
147 s_handlerSet = true;
148 }
149
150 sigprocmask( SIG_SETMASK, &omask, 0 );
151 // there should be no problem with SIGPIPE staying SIG_IGN
152}
153
154// the pipe is needed to sync the child reaping with our event processing,
155// as otherwise there are race conditions, locking requirements, and things
156// generally get harder
157void KfsProcessController::theSigCHLDHandler( int arg )
158{
159 int saved_errno = errno;
160
161 char dummy = 0;
162 ::write( instance()->m_fd[1], &dummy, 1 );
163
164 if ( s_oldChildHandlerData.sa_handler != SIG_IGN &&
165 s_oldChildHandlerData.sa_handler != SIG_DFL ) {
166 s_oldChildHandlerData.sa_handler( arg ); // call the old handler
167 }
168
169 errno = saved_errno;
170}
171
172void KfsProcessController::slotDoHousekeeping()
173{
174 char dummy[16]; // somewhat bigger - just in case several have queued up
175 ::read( m_fd[0], dummy, sizeof(dummy) );
176
177 again:
178 QList<KfsProcess*>::iterator it( m_kProcessList.begin() );
179 QList<KfsProcess*>::iterator eit( m_kProcessList.end() );
180 while( it != eit )
181 {
182 KfsProcess *prc = *it;
183 if( prc->runs && waitpid( prc->pid_, 0, WNOHANG ) > 0 )
184 {
185 prc->processHasExited();
186 // the callback can nuke the whole process list and even 'this'
187 if (!instance())
188 return;
189 goto again;
190 }
191 ++it;
192 }
193 QList<int>::iterator uit( m_unixProcessList.begin() );
194 QList<int>::iterator ueit( m_unixProcessList.end() );
195 while( uit != ueit )
196 {
197 if( waitpid( *uit, 0, WNOHANG ) > 0 )
198 {
199 uit = m_unixProcessList.erase( uit );
200 deref(); // counterpart to addProcess, can invalidate 'this'
201 } else
202 ++uit;
203 }
204}
205
206void KfsProcessController::addKProcess( KfsProcess* p )
207{
208 m_kProcessList.append( p );
209}
210
211void KfsProcessController::removeKProcess( KfsProcess* p )
212{
213 m_kProcessList.removeAll( p );
214}
215
216void KfsProcessController::addProcess( int pid )
217{
218 m_unixProcessList.append( pid );
219 ref(); // make sure we stay around when the KfsProcess goes away
220}
221
223// public member functions //
225
226KfsProcess::KfsProcess( QObject* parent )
227 : QObject( parent ),
228 runs(false),
229 pid_(0)
230{
231 KfsProcessController::ref();
232 KfsProcessController::instance()->addKProcess(this);
233}
234
235KfsProcess::~KfsProcess()
236{
237 KfsProcessController::instance()->removeKProcess(this);
238 KfsProcessController::deref();
239}
240
241void KfsProcess::detach()
242{
243 if (runs) {
244 KfsProcessController::instance()->addProcess(pid_);
245 runs = false;
246 pid_ = 0;
247 }
248}
249
250KfsProcess &KfsProcess::operator<<(const char* arg)
251{
252 arguments.append(QByteArray(arg));
253 return *this;
254}
255
256KfsProcess &KfsProcess::operator<<(const QString& arg)
257{
258 arguments.append(QFile::encodeName(arg));
259 return *this;
260}
261
262bool KfsProcess::start()
263{
264 if (runs) {
265 kDebug(175) << "Attempted to start an already running process";
266 return false;
267 }
268
269 uint n = arguments.count();
270 if (n == 0) {
271 kDebug(175) << "Attempted to start a process without arguments";
272 return false;
273 }
274 char **arglist = static_cast<char **>(malloc( (n + 1) * sizeof(char *)));
275 for (uint i = 0; i < n; i++)
276 arglist[i] = arguments[i].data();
277 arglist[n] = 0;
278
279 int fd[2];
280 if (pipe(fd))
281 fd[0] = fd[1] = -1; // Pipe failed.. continue
282
283 pid_ = fork();
284 if (pid_ == 0) {
285 // The child process
286
287 close(fd[0]);
288 // Closing of fd[1] indicates that the execvp() succeeded!
289 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
290
291 // reset all signal handlers
292 struct sigaction act;
293 sigemptyset(&act.sa_mask);
294 act.sa_handler = SIG_DFL;
295 act.sa_flags = 0;
296 for (int sig = 1; sig < NSIG; sig++)
297 sigaction(sig, &act, 0L);
298
299 setsid();
300 execvp(arglist[0], arglist);
301
302 char resultByte = 1;
303 write(fd[1], &resultByte, 1);
304 _exit(-1);
305 } else if (pid_ == -1) {
306 // forking failed
307
308 pid_ = 0;
309 free(arglist);
310 return false;
311 }
312 // the parent continues here
313 free(arglist);
314
315 // Check whether client could be started.
316 close(fd[1]);
317 for(;;)
318 {
319 char resultByte;
320 int n = ::read(fd[0], &resultByte, 1);
321 if (n == 1)
322 {
323 // exec() failed
324 close(fd[0]);
325 waitpid(pid_, 0, 0);
326 pid_ = 0;
327 return false;
328 }
329 if (n == -1)
330 {
331 if (errno == EINTR)
332 continue; // Ignore
333 }
334 break; // success
335 }
336 close(fd[0]);
337
338 runs = true;
339 return true;
340}
341
342void KfsProcess::processHasExited()
343{
344 // only successfully run processes ever get here
345 runs = false;
346 emit processExited();
347}
348
349#include "kfsprocess.moc"
KfsProcessController
Definition: kfsprocess.h:33
KfsProcessController::theSigCHLDHandler
static void theSigCHLDHandler(int signal)
Definition: kfsprocess.cpp:157
KfsProcess
Definition: kfsprocess.h:67
KfsProcess::start
bool start()
Definition: kfsprocess.cpp:262
KfsProcess::operator<<
KfsProcess & operator<<(const QString &arg)
Definition: kfsprocess.cpp:256
KfsProcess::detach
void detach()
Definition: kfsprocess.cpp:241
KfsProcess::~KfsProcess
~KfsProcess()
Definition: kfsprocess.cpp:235
KfsProcess::KfsProcess
KfsProcess(QObject *parent=0L)
Definition: kfsprocess.cpp:226
KfsProcess::processExited
void processExited()
QList
QObject
kDebug
#define kDebug
mask
#define mask
perror
QDebug perror(QDebug s, KDebugTag)
kdebug.h
theReaper
static void theReaper(int num)
Definition: kfsprocess.cpp:96
kfsprocess.h
close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal