[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdesupport/qca
From:       Justin Karneges <infiniti () affinix ! com>
Date:       2007-05-31 21:32:30
Message-ID: 1180647150.939258.28554.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 670251 by infiniti:

control over pipe inheritability


 M  +4 -8      include/QtCrypto/qpipe.h  
 M  +54 -16    src/support/qpipe.cpp  


--- trunk/kdesupport/qca/include/QtCrypto/qpipe.h #670250:670251
@@ -79,9 +79,7 @@
 	void enable();                         // enables usage (read/write) of the pipe
 	void close();                          // close the pipe
 	void release();                        // let go of the pipe but don't close
-#ifdef Q_OS_WIN
-	bool winDupHandle();                   // DuplicateHandle()
-#endif
+	bool setInheritable(bool enabled);     // note: on windows, this operation changes \
the id  
 	int bytesAvailable() const;            // bytes available to read
 	int read(char *data, int maxsize);     // return number read, 0 = EOF, -1 = error
@@ -116,7 +114,7 @@
 	QPipeDevice::Type type() const;
 	bool isValid() const;
 	Q_PIPE_ID id() const;
-	QString idAsString() const;
+	int idAsInt() const;
 
 	void take(Q_PIPE_ID id, QPipeDevice::Type t);
 #ifdef QPIPE_SECURE
@@ -125,9 +123,7 @@
 	void enable();
 	void close();
 	void release();
-#ifdef Q_OS_WIN
-	bool winDupHandle();
-#endif
+	bool setInheritable(bool enabled);
 
 	void finalize(); // do an immediate read, and invalidate
 	void finalizeAndRelease(); // same as above, but don't close pipe
@@ -163,7 +159,7 @@
 	Private *d;
 };
 
-// creates a full pipe (two pipe ends)
+// creates a full pipe (two pipe ends), non-inheritable
 class QCA_EXPORT QPipe
 {
 public:
--- trunk/kdesupport/qca/src/support/qpipe.cpp #670250:670251
@@ -99,6 +99,33 @@
 #endif
 }
 
+// on windows, the pipe is closed and the new pipe is returned in newPipe
+static bool pipe_set_inheritable(Q_PIPE_ID pipe, bool b, Q_PIPE_ID *newPipe = 0)
+{
+#ifdef Q_OS_WIN
+	// windows is required to accept a new pipe id
+	if(!newPipe)
+		return false;
+	HANDLE h;
+	if(!DuplicateHandle(GetCurrentProcess(), pipe, GetCurrentProcess(), &h, 0, b, \
DUPLICATE_SAME_ACCESS)) +		return false;
+	*newPipe = h;
+	return true;
+#endif
+#ifdef Q_OS_UNIX
+	if(newPipe)
+		*newPipe = pipe;
+	int flags = fcntl(pipe, F_GETFD);
+	if(!b)
+		flags |= FD_CLOEXEC;
+	else
+		flags &= ~FD_CLOEXEC;
+	if(fcntl(pipe, F_SETFD, flags) == -1)
+		return false;
+	return true;
+#endif
+}
+
 // returns number of bytes available
 static int pipe_read_avail(Q_PIPE_ID pipe)
 {
@@ -1154,19 +1181,25 @@
 	d->reset();
 }
 
+bool QPipeDevice::setInheritable(bool enabled)
+{
 #ifdef Q_OS_WIN
-bool QPipeDevice::winDupHandle()
-{
-	HANDLE h;
-	if(!DuplicateHandle(GetCurrentProcess(), d->pipe, GetCurrentProcess(), &h, 0, \
false, DUPLICATE_SAME_ACCESS)) +	Q_PIPE_ID newPipe;
+	if(!pipe_set_inheritable(d->pipe, enabled, &newPipe))
 		return false;
-
-	Type t = d->type;
-	d->reset();
-	d->setup(h, t);
+	d->pipe = newPipe;
+#ifdef USE_POLL
+	if(d->pipeReader)
+		d->pipeReader->pipe = d->pipe;
+	if(d->pipeWriter)
+		d->pipeWriter->pipe = d->pipe;
+#endif
 	return true;
+#endif
+#ifdef Q_OS_UNIX
+	return pipe_set_inheritable(d->pipe, enabled, 0);
+#endif
 }
-#endif
 
 int QPipeDevice::bytesAvailable() const
 {
@@ -1840,9 +1873,9 @@
 	return d->pipe.id();
 }
 
-QString QPipeEnd::idAsString() const
+int QPipeEnd::idAsInt() const
 {
-	return QString::number(d->pipe.idAsInt());
+	return d->pipe.idAsInt();
 }
 
 void QPipeEnd::take(Q_PIPE_ID id, QPipeDevice::Type t)
@@ -1900,12 +1933,10 @@
 	d->reset(ResetSession);
 }
 
-#ifdef Q_OS_WIN
-bool QPipeEnd::winDupHandle()
+bool QPipeEnd::setInheritable(bool enabled)
 {
-	return d->pipe.winDupHandle();
+	return d->pipe.setInheritable(enabled);
 }
-#endif
 
 void QPipeEnd::finalize()
 {
@@ -2034,7 +2065,7 @@
 	SECURITY_ATTRIBUTES secAttr;
 	memset(&secAttr, 0, sizeof secAttr);
 	secAttr.nLength = sizeof secAttr;
-	secAttr.bInheritHandle = true;
+	secAttr.bInheritHandle = false;
 
 	HANDLE r, w;
 	if(!CreatePipe(&r, &w, &secAttr, 0))
@@ -2047,6 +2078,13 @@
 	int p[2];
 	if(pipe(p) == -1)
 		return false;
+	if(!pipe_set_inheritable(p[0], false, 0) ||
+		!pipe_set_inheritable(p[1], false, 0))
+	{
+		close(p[0]);
+		close(p[1]);
+		return false;
+	}
 	i.take(p[0], QPipeDevice::Read);
 	o.take(p[1], QPipeDevice::Write);
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic