From MAILER-DAEMON Sat Jan 10 10:39:57 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LLfwL-0005Vy-5H
	for mharc-myserver-commit@gnu.org; Sat, 10 Jan 2009 10:39:57 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LLfwJ-0005T6-0e
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 10:39:55 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LLfwI-0005SR-Ev
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 10:39:54 -0500
Received: from [199.232.76.173] (port=43935 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LLfwI-0005SL-CB
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 10:39:54 -0500
Received: from savannah.gnu.org ([199.232.41.3]:49956 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LLfwI-0000oi-3R
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 10:39:54 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LLfwH-0000We-Cu
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 15:39:53 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LLfwH-0000Wa-5V
	for myserver-commit@gnu.org; Sat, 10 Jan 2009 15:39:53 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LLfwH-0000Wa-5V@cvs.savannah.gnu.org>
Date: Sat, 10 Jan 2009 15:39:53 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2981] Documentation for the `PROCESS_SERVER'
	directive.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 10 Jan 2009 15:39:55 -0000

Revision: 2981
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2981
Author:   gscrivano
Date:     2009-01-10 15:39:52 +0000 (Sat, 10 Jan 2009)

Log Message:
-----------
Documentation for the `PROCESS_SERVER' directive.

Modified Paths:
--------------
    trunk/myserver/documentation/process_security.texi

Modified: trunk/myserver/documentation/process_security.texi
===================================================================
--- trunk/myserver/documentation/process_security.texi	2008-12-26 22:30:20 UTC (rev 2980)
+++ trunk/myserver/documentation/process_security.texi	2009-01-10 15:39:52 UTC (rev 2981)
@@ -37,3 +37,34 @@
 active.
 
 It is enabled passing the @code{-f} switch to the myserver process.
+
+@section FastCGI/SCGI servers
+By default, FastCGI and SCGI servers are lazily initialized by
+MyServer on the first request to a resource that is handled trought
+these protocols.  In this case MyServer will initialize it using a
+random TCP port.
+There may be cases where a different configuration is needed and it is
+done using the @code{PROCESS_SERVER} directive inside the
+@file{myserver.xml} file.
+
+A @code{PROCESS_SERVER} entry makes it possible to access a specific
+FastCGI/SCGI configuration inside MIME types by its name.
+
+
+@example
+<PROCESS_SERVER>
+        <NAME>/opt/bin/fastcgi_server</NAME>
+        <DOMAIN>fastcgi</DOMAIN>
+        <HOST>localhost</HOST>
+        <PORT>2010</PORT>
+        <LOCAL>yes</LOCAL>
+</PROCESS_SERVER>
+@end example
+
+In the previous example a FastCGI server
+@code{/opt/bin/fastcgi_server} is created.  The @code{domain} must be
+"fastcgi" or "scgi", depending on the protocol to use.
+@code{LOCAL} specifies if the server is handled by myserver or it is
+already running, thus myserver will simply access and use it.  In this
+case @code{name} can be anything without any specific meaning.
+





From MAILER-DAEMON Fri Jan 16 16:27:48 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LNwEG-0005ON-Bl
	for mharc-myserver-commit@gnu.org; Fri, 16 Jan 2009 16:27:48 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LNwEE-0005NX-H6
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 16:27:46 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LNwED-0005Mx-NY
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 16:27:46 -0500
Received: from [199.232.76.173] (port=39094 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LNwED-0005Mq-JZ
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 16:27:45 -0500
Received: from savannah.gnu.org ([199.232.41.3]:33724 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LNwED-0007R0-A8
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 16:27:45 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LNwEC-0004xC-89
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 21:27:44 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LNwEB-0004wO-IP
	for myserver-commit@gnu.org; Fri, 16 Jan 2009 21:27:43 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LNwEB-0004wO-IP@cvs.savannah.gnu.org>
Date: Fri, 16 Jan 2009 21:27:43 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2982] Fixed segfault in the tests suite.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 16 Jan 2009 21:27:46 -0000

Revision: 2982
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2982
Author:   gscrivano
Date:     2009-01-16 21:27:42 +0000 (Fri, 16 Jan 2009)

Log Message:
-----------
Fixed segfault in the tests suite.

Modified Paths:
--------------
    trunk/myserver/tests/test_xml_validator.cpp

Modified: trunk/myserver/tests/test_xml_validator.cpp
===================================================================
--- trunk/myserver/tests/test_xml_validator.cpp	2009-01-10 15:39:52 UTC (rev 2981)
+++ trunk/myserver/tests/test_xml_validator.cpp	2009-01-16 21:27:42 UTC (rev 2982)
@@ -46,6 +46,10 @@
   {
     string val("value");
     SecurityToken secToken;
+    secToken.setResource (&val);
+    secToken.setResource (&val);
+    secToken.setDirectory (&val);
+    secToken.setSysDirectory (&val);
     CPPUNIT_ASSERT_EQUAL(xmlValidator->getPermissionMaskImpl(&secToken, NULL, NULL), 0);
  
   }
@@ -53,6 +57,11 @@
   void testGetPermissionMask()
   {
     SecurityToken secToken;
+    string val("value");
+    secToken.setResource (&val);
+    secToken.setDirectory (&val);
+    secToken.setSysDirectory (&val);
+
     CPPUNIT_ASSERT_EQUAL (xmlValidator->getPermissionMask (&secToken, (SecurityDomain**) NULL, NULL), 0);
     CPPUNIT_ASSERT_EQUAL (xmlValidator->getPermissionMask (&secToken, (list<SecurityDomain*>*) NULL, NULL), 0);
   }





From MAILER-DAEMON Mon Jan 19 16:50:50 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LP21C-0003E2-5w
	for mharc-myserver-commit@gnu.org; Mon, 19 Jan 2009 16:50:50 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LP21A-0003DY-Mh
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 16:50:48 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LP219-0003D7-Av
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 16:50:47 -0500
Received: from [199.232.76.173] (port=53602 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LP219-0003D0-3r
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 16:50:47 -0500
Received: from savannah.gnu.org ([199.232.41.3]:55089 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LP218-0004rI-KG
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 16:50:46 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LP218-0006IF-1I
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 21:50:46 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LP217-0006IB-Na
	for myserver-commit@gnu.org; Mon, 19 Jan 2009 21:50:45 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LP217-0006IB-Na@cvs.savannah.gnu.org>
Date: Mon, 19 Jan 2009 21:50:45 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2983] Added new `SocketPair' class.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 19 Jan 2009 21:50:49 -0000

Revision: 2983
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2983
Author:   gscrivano
Date:     2009-01-19 21:50:45 +0000 (Mon, 19 Jan 2009)

Log Message:
-----------
Added new `SocketPair' class.

Modified Paths:
--------------
    trunk/myserver/configure.in
    trunk/myserver/include/base/Makefile.am
    trunk/myserver/src/base/Makefile.am
    trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/include/base/socket_pair/
    trunk/myserver/include/base/socket_pair/Makefile.am
    trunk/myserver/include/base/socket_pair/socket_pair.h
    trunk/myserver/src/base/socket_pair/
    trunk/myserver/src/base/socket_pair/Makefile.am
    trunk/myserver/src/base/socket_pair/socket_pair.cpp
    trunk/myserver/tests/test_socket_pair.cpp

Modified: trunk/myserver/configure.in
===================================================================
--- trunk/myserver/configure.in	2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/configure.in	2009-01-19 21:50:45 UTC (rev 2983)
@@ -2,7 +2,7 @@
 dnl
 dnl GNU MyServer
 dnl
-dnl Copyright (C) 2002-2008 Free Software Foundation, Inc.
+dnl Copyright (C) 2002-2009 Free Software Foundation, Inc.
 dnl This program is free software; you can redistribute it and/or modify
 dnl it under the terms of the GNU General Public License as published by
 dnl the Free Software Foundation; either version 3 of the License, or
@@ -365,6 +365,7 @@
     include/base/sync/Makefile
     include/base/safetime/Makefile
     include/base/socket/Makefile
+    include/base/socket_pair/Makefile
     include/base/hash_map/Makefile
     include/base/dynamic_lib/Makefile
     include/base/process/Makefile
@@ -414,6 +415,7 @@
     src/base/sync/Makefile
     src/base/safetime/Makefile
     src/base/socket/Makefile
+    src/base/socket_pair/Makefile
     src/base/hash_map/Makefile
     src/base/dynamic_lib/Makefile
     src/base/process/Makefile

Modified: trunk/myserver/include/base/Makefile.am
===================================================================
--- trunk/myserver/include/base/Makefile.am	2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/include/base/Makefile.am	2009-01-19 21:50:45 UTC (rev 2983)
@@ -1,4 +1,4 @@
 baseincludedir=$(includedir)/myserver/include/base
 baseinclude_HEADERS = utility.h
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 mem_buff multicast pipe process regex safetime socket ssl string sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 mem_buff multicast pipe process regex safetime socket socket_pair ssl string sync thread xml
 

Added: trunk/myserver/include/base/socket_pair/Makefile.am
===================================================================
--- trunk/myserver/include/base/socket_pair/Makefile.am	                        (rev 0)
+++ trunk/myserver/include/base/socket_pair/Makefile.am	2009-01-19 21:50:45 UTC (rev 2983)
@@ -0,0 +1,3 @@
+socket_pair_includedir=$(includedir)/myserver/include/base/socket_pair
+socket_pair_include_HEADERS = socket_pair.h
+SUBDIRS =

Added: trunk/myserver/include/base/socket_pair/socket_pair.h
===================================================================
--- trunk/myserver/include/base/socket_pair/socket_pair.h	                        (rev 0)
+++ trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-19 21:50:45 UTC (rev 2983)
@@ -0,0 +1,45 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program 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 3 of the License, or
+(at your option) any later version.
+
+This program 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef PIPE_H
+#define PIPE_H
+
+#include "stdafx.h"
+#include <include/filter/stream.h>
+#include <string>
+
+using namespace std;
+
+
+class SocketPair : public Stream
+{
+public:
+	SocketPair ();
+	int create ();
+	FileHandle getFirstHandle ();
+	FileHandle getSecondHandle ();
+	void inverted (SocketPair&);
+  virtual int read (char* buffer, u_long len, u_long *nbr);
+  virtual int write (const char* buffer, u_long len, u_long *nbw);
+	virtual int close ();
+	void closeFirstHandle ();
+	void closeSecondHandle ();
+private:
+	FileHandle handles[2];
+};
+#endif

Modified: trunk/myserver/src/base/Makefile.am
===================================================================
--- trunk/myserver/src/base/Makefile.am	2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/src/base/Makefile.am	2009-01-19 21:50:45 UTC (rev 2983)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libbase.a
 libbase_a_SOURCES = utility.cpp
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 mem_buff multicast pipe process regex safetime socket ssl string sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 mem_buff multicast pipe process regex safetime socket socket_pair ssl string sync thread xml
 INCLUDES = $(all_includes)
 

Added: trunk/myserver/src/base/socket_pair/Makefile.am
===================================================================
--- trunk/myserver/src/base/socket_pair/Makefile.am	                        (rev 0)
+++ trunk/myserver/src/base/socket_pair/Makefile.am	2009-01-19 21:50:45 UTC (rev 2983)
@@ -0,0 +1,5 @@
+lib_LIBRARIES = libsocket_pair.a
+libsocket_pair_a_SOURCES = socket_pair.cpp
+SUBDIRS =
+INCLUDES = $(all_includes)
+

Added: trunk/myserver/src/base/socket_pair/socket_pair.cpp
===================================================================
--- trunk/myserver/src/base/socket_pair/socket_pair.cpp	                        (rev 0)
+++ trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-19 21:50:45 UTC (rev 2983)
@@ -0,0 +1,208 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program 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 3 of the License, or
+(at your option) any later version.
+
+This program 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, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include "stdafx.h"
+#include <include/base/utility.h>
+#include <include/base/socket_pair/socket_pair.h>
+
+#ifdef NOT_WIN
+extern "C" {
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+}
+#else
+#include <windows.h>
+#endif
+
+#include <string>
+#include <sstream>
+
+using namespace std;
+
+
+
+SocketPair::SocketPair ()
+{
+
+}
+
+/*!
+ *Create a Socket pair.
+ *\return 0 on success.
+ */
+int SocketPair::create ()
+{
+#ifdef WIN32
+#define LOCAL_SOCKETPAIR_AF AF_INET
+#else
+#define LOCAL_SOCKETPAIR_AF AF_UNIX
+#endif
+
+  int af = LOCAL_SOCKETPAIR_AF;
+  int type = SOCK_STREAM;
+  int protocol = 0;
+
+#ifndef WIN32
+  return socketpair (af, type, protocol, (int*)handles);
+#else
+  struct sockaddr_in addr;
+  SOCKET listener;
+  int e;
+  int addrlen = sizeof(addr);
+  DWORD flags = 0;
+  
+  if (handles == 0)
+    return -1;
+
+  handles[0] = handles[1] = INVALID_SOCKET;
+  listener = socket (AF_INET, type, 0);
+
+  if (listener == INVALID_SOCKET)
+    return -1;
+
+  memset(&addr, 0, sizeof (addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl (0x7f000001);
+  addr.sin_port = 0;
+
+  e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
+  if (e == SOCKET_ERROR)
+    {
+      close(listener);
+      return -1;
+    }
+  
+  e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
+  if (e == SOCKET_ERROR)
+    {
+      close (listener);
+      return -1;
+    }
+  
+  do
+    {
+      if (listen (listener, 1) == SOCKET_ERROR)
+        break;
+      if ((handles[0] = socket (AF_INET, type, 0)) == INVALID_SOCKET)
+        break;
+      if (connect(handles[0], (const struct sockaddr*) &addr, sizeof (addr)) == SOCKET_ERROR)
+        break;
+      if ((handles[1] = accept (listener, NULL, NULL)) == INVALID_SOCKET)
+        break;
+    
+      close (listener);
+      return 0;
+    } while (0);
+  
+  close (listener);
+  close (handles[0]);
+  close (handles[1]);
+  
+  return -1;
+#endif
+
+  return 0;
+}
+
+/*!
+ *Get the first handle used by the socket pair.
+ */
+FileHandle SocketPair::getFirstHandle ()
+{
+  return handles[0];
+}
+
+/*!
+ *Get the second handle used by the socket pair.
+ */
+FileHandle SocketPair::getSecondHandle ()
+{
+  return handles[1];
+}
+
+
+/*!
+ *Invert the current socket pair on the passed argument.
+ *The first handle will be the second handle on the inverted SocketPair
+ *and the second handle will be the first one.
+ *\param inverted It will be the inverted SocketPair.
+ */
+void SocketPair::inverted (SocketPair& inverted)
+{
+  inverted.handles[0] = handles[1];
+  inverted.handles[1] = handles[0];
+}
+
+/*!
+ *Read using the first handle.
+ *\see Stream#read.
+ */
+int SocketPair::read (char* buffer, u_long len, u_long *nbr)
+{
+  Socket sock (handles[0]);
+  return sock.read (buffer, len, nbr);
+
+}
+
+/*!
+ *Write using the first handle.
+ *\see Stream#write.
+ */
+int SocketPair::write (const char* buffer, u_long len, u_long *nbw)
+{
+  Socket sock (handles[0]);
+  return sock.write (buffer, len, nbw);
+}
+
+/*!
+ *Close both handles.
+ */
+int SocketPair::close ()
+{
+  closeFirstHandle ();
+  closeSecondHandle ();
+
+  return 0;
+}
+
+/*!
+ *Close the first handle.
+ */
+void SocketPair::closeFirstHandle ()
+{
+  Socket sock (handles[0]);
+  sock.close ();
+}
+
+/*!
+ *Close the second handle.
+ */
+void SocketPair::closeSecondHandle ()
+{
+  Socket sock (handles[1]);
+  sock.close ();
+}

Modified: trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
===================================================================
--- trunk/myserver/src/connections_scheduler/connections_scheduler.cpp	2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/src/connections_scheduler/connections_scheduler.cpp	2009-01-19 21:50:45 UTC (rev 2983)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 This program 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 3 of the License, or
@@ -18,7 +18,8 @@
 #include <include/connections_scheduler/connections_scheduler.h>
 #include <include/server/server.h>
 
-///////////////////////////////////////////////////////////////////////////
+
+//////FIXME: Use the SocketPair class//////////////////////////
 #ifdef WIN32
 #define socket_t intptr_t
 #include <windows.h>

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am	2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/tests/Makefile.am	2009-01-19 21:50:45 UTC (rev 2983)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp test_security_domain.cpp test_validator.cpp test_auth_domain.cpp test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp test_cached_file.cpp test_files_utility.cpp test_http_request.cpp test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp test_http_response.cpp test_multicast.cpp test_security_cache.cpp test_security_token.cpp test_utility.cpp  test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp test_security_domain.cpp test_validator.cpp test_auth_domain.cpp test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp test_cached_file.cpp test_files_utility.cpp test_http_request.cpp test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp test_http_response.cpp test_multicast.cpp test_security_cache.cpp test_security_token.cpp test_socket_pair.cpp test_utility.cpp  test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) $(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(SSL_LIB) $(ZLIB_LIB) $(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_socket_pair.cpp
===================================================================
--- trunk/myserver/tests/test_socket_pair.cpp	                        (rev 0)
+++ trunk/myserver/tests/test_socket_pair.cpp	2009-01-19 21:50:45 UTC (rev 2983)
@@ -0,0 +1,98 @@
+/*
+ MyServer
+ Copyright (C) 2009 Free Software Foundation, Inc.
+ This program 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 3 of the License, or
+ (at your option) any later version.
+ 
+ This program 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <include/base/socket_pair/socket_pair.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <string.h>
+
+class TestSocketPair : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestSocketPair );
+  CPPUNIT_TEST( testCreateClose );
+  CPPUNIT_TEST( testInverted );
+  CPPUNIT_TEST( testWriteRead );
+  CPPUNIT_TEST_SUITE_END();
+  
+public:
+
+  void setUp() 
+  {
+  }
+
+  void tearDown()
+  {
+  }
+
+  void testInverted ()
+  {
+    SocketPair sp;
+    SocketPair inverted;
+    sp.create ();
+    sp.inverted (inverted);
+
+    CPPUNIT_ASSERT_EQUAL (sp.getFirstHandle (), inverted.getSecondHandle ());
+    CPPUNIT_ASSERT_EQUAL (sp.getSecondHandle (), inverted.getFirstHandle ());
+
+    sp.close ();
+  }
+
+  void testCreateClose ()
+  {
+    SocketPair sp;
+    int ret = sp.create ();
+
+    CPPUNIT_ASSERT_EQUAL (ret, 0);
+
+    ret = sp.close ();
+
+    CPPUNIT_ASSERT_EQUAL (ret, 0);
+  }
+
+  void testWriteRead ()
+  {
+    char inBuffer[] = "Hello World!";
+    char outBuffer[256];
+    u_long nbw, nbr;
+    int ret;
+    SocketPair writeSock;
+    SocketPair readSock;
+
+    writeSock.create ();
+    writeSock.inverted (readSock);
+
+
+    ret = writeSock.write (inBuffer, strlen (inBuffer) + 1, &nbw);
+
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+
+    ret = readSock.read (outBuffer, 256, &nbr);
+    
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+
+
+    CPPUNIT_ASSERT_EQUAL(strcmp (inBuffer, outBuffer), 0);
+
+    writeSock.close ();
+  }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestSocketPair );





From MAILER-DAEMON Tue Jan 20 16:13:58 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LPNv3-000619-SW
	for mharc-myserver-commit@gnu.org; Tue, 20 Jan 2009 16:13:57 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LPNv1-0005zU-SF
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 16:13:55 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LPNv1-0005yU-1q
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 16:13:55 -0500
Received: from [199.232.76.173] (port=38981 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LPNv0-0005yD-Nb
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 16:13:54 -0500
Received: from savannah.gnu.org ([199.232.41.3]:35806 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LPNv0-0007bY-FK
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 16:13:54 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LPNuz-0008Io-WE
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 21:13:54 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LPNuz-0008Ii-Lh
	for myserver-commit@gnu.org; Tue, 20 Jan 2009 21:13:53 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LPNuz-0008Ii-Lh@cvs.savannah.gnu.org>
Date: Tue, 20 Jan 2009 21:13:53 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2984] `ConnectionsScheduler' uses `SocketPair'.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 20 Jan 2009 21:13:56 -0000

Revision: 2984
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2984
Author:   gscrivano
Date:     2009-01-20 21:13:52 +0000 (Tue, 20 Jan 2009)

Log Message:
-----------
`ConnectionsScheduler' uses `SocketPair'.

Modified Paths:
--------------
    trunk/myserver/include/base/socket_pair/socket_pair.h
    trunk/myserver/include/connections_scheduler/connections_scheduler.h
    trunk/myserver/src/base/socket_pair/socket_pair.cpp
    trunk/myserver/src/connections_scheduler/connections_scheduler.cpp

Modified: trunk/myserver/include/base/socket_pair/socket_pair.h
===================================================================
--- trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-19 21:50:45 UTC (rev 2983)
+++ trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-20 21:13:52 UTC (rev 2984)
@@ -16,8 +16,8 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifndef PIPE_H
-#define PIPE_H
+#ifndef SOCKET_PAIR_H
+#define SOCKET_PAIR_H
 
 #include "stdafx.h"
 #include <include/filter/stream.h>
@@ -39,6 +39,8 @@
 	virtual int close ();
 	void closeFirstHandle ();
 	void closeSecondHandle ();
+  void setNonBlocking (bool blocking);
+	virtual u_long bytesToRead();
 private:
 	FileHandle handles[2];
 };

Modified: trunk/myserver/include/connections_scheduler/connections_scheduler.h
===================================================================
--- trunk/myserver/include/connections_scheduler/connections_scheduler.h	2009-01-19 21:50:45 UTC (rev 2983)
+++ trunk/myserver/include/connections_scheduler/connections_scheduler.h	2009-01-20 21:13:52 UTC (rev 2984)
@@ -1,7 +1,7 @@
 /* -*- mode: c++ -*- */
 /*
 MyServer
-Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 This program 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 3 of the License, or
@@ -27,6 +27,7 @@
 #include <include/base/sync/semaphore.h>
 #include <include/base/hash_map/hash_map.h>
 #include <include/base/thread/thread.h>
+#include <include/base/socket_pair/socket_pair.h>
 
 #include <list>
 #include <queue>
@@ -69,10 +70,10 @@
     bool terminated; 
     bool terminate;
     Mutex* mutex;
-    FileHandle fd[2];
     event loopEvent;
     Server* server;
     ConnectionsScheduler* scheduler;
+    SocketPair socketPair;
   };
 
   ConnectionsScheduler(Server* server = NULL);

Modified: trunk/myserver/src/base/socket_pair/socket_pair.cpp
===================================================================
--- trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-19 21:50:45 UTC (rev 2983)
+++ trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-20 21:13:52 UTC (rev 2984)
@@ -206,3 +206,25 @@
   Socket sock (handles[1]);
   sock.close ();
 }
+
+/*!
+ *Configure the server socket blocking or not blocking.
+ *\param blocking The new blocking status.
+ */
+void SocketPair::setNonBlocking (bool notBlocking)
+{
+   Socket sock0 (handles[0]);
+   sock0.setNonBlocking (notBlocking);
+
+   Socket sock1 (handles[1]);
+   sock1.setNonBlocking (notBlocking);
+}
+
+/*!
+ *Return how many bytes can be read on the first socket.
+ */
+u_long SocketPair::bytesToRead()
+{
+  Socket sock (handles[0]);
+  return sock.bytesToRead ();
+}

Modified: trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
===================================================================
--- trunk/myserver/src/connections_scheduler/connections_scheduler.cpp	2009-01-19 21:50:45 UTC (rev 2983)
+++ trunk/myserver/src/connections_scheduler/connections_scheduler.cpp	2009-01-20 21:13:52 UTC (rev 2984)
@@ -19,92 +19,7 @@
 #include <include/server/server.h>
 
 
-//////FIXME: Use the SocketPair class//////////////////////////
 #ifdef WIN32
-#define socket_t intptr_t
-#include <windows.h>
-#else
-#define socket_t int
-#endif
-
-static int
-make_socket_nonblocking(FileHandle fd)
-{
-#ifdef WIN32
-  unsigned long nonblocking = 1;
-  ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking);
-#else
-  return fcntl(fd, F_SETFL, O_NONBLOCK);
-#endif
-  return 0;
-}
-
-static int
-create_socketpair(int af, int type, int protocol, FileHandle socks[2])
-{
-#ifndef WIN32
-  return socketpair(af, type, protocol, (int*)socks);
-#else
-  struct sockaddr_in addr;
-  SOCKET listener;
-  int e;
-  int addrlen = sizeof(addr);
-  DWORD flags = 0;
-  
-  if (socks == 0)
-    return -1;
-
-  socks[0] = socks[1] = INVALID_SOCKET;
-  listener = socket(AF_INET, type, 0);
-
-  if (listener == INVALID_SOCKET)
-    return -1;
-
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family = AF_INET;
-  addr.sin_addr.s_addr = htonl(0x7f000001);
-  addr.sin_port = 0;
-
-  e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
-  if (e == SOCKET_ERROR)
-  {
-    close(listener);
-    return -1;
-  }
-  
-  e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
-  if (e == SOCKET_ERROR)
-  {
-    close(listener);
-    return -1;
-  }
-  
-  do
-  {
-    if (listen(listener, 1) == SOCKET_ERROR)
-      break;
-    if ((socks[0] = socket(AF_INET, type, 0)) == INVALID_SOCKET)
-      break;
-    if (connect(socks[0], (const struct sockaddr*) &addr, sizeof(addr)) == SOCKET_ERROR)
-      break;
-    if ((socks[1] = accept(listener, NULL, NULL)) == INVALID_SOCKET)
-      break;
-    
-    close(listener);
-    return 0;
-  } while (0);
-  
-  close(listener);
-  close(socks[0]);
-  close(socks[1]);
-  
-  return -1;
-  #endif
-}
-
-///////////////////////////////////////////////////////////////////////////
-
-#ifdef WIN32
 static unsigned int __stdcall dispatcher(void* p)
 #else
 static void* dispatcher(void* p)
@@ -182,16 +97,16 @@
 static void eventLoopHandler(int fd, short event, void *arg)
 {
   ConnectionsScheduler::DispatcherArg *da = (ConnectionsScheduler::DispatcherArg*)arg;
- 
-  if(event == EV_READ || event == EV_TIMEOUT)
+  u_long nbr;
+  if (event == EV_READ || event == EV_TIMEOUT)
   {
-    Socket sock((FileHandle) da->fd[0]);
+    Socket sock ((FileHandle) da->socketPair.getFirstHandle ());
 
-    while(sock.bytesToRead())
+    while (da->socketPair.bytesToRead())
     {
       char cmd;
-      sock.recv(&cmd, 1, 0);
-      if(cmd == 'c')
+      da->socketPair.read (&cmd, 1, &nbr);
+      if (cmd == 'c')
       {
         /*
          *Schedule a new connection.
@@ -204,43 +119,43 @@
         ConnectionPtr c;
         timeval tv = {10, 0};
 
-        sock.recv((char*)&handle, sizeof(FileHandle), 0);
-        sock.recv((char*)&c, sizeof(ConnectionPtr), 0);
-        sock.recv((char*)&tv, sizeof(timeval), 0);
+        da->socketPair.read ((char*)&handle, sizeof(FileHandle), &nbr);
+        da->socketPair.read ((char*)&c, sizeof(ConnectionPtr), &nbr);
+        da->socketPair.read ((char*)&tv, sizeof(timeval), &nbr);
 
-        event_once((int) handle, EV_READ | EV_TIMEOUT, newDataHandler, da, &tv);
+        event_once ((int) handle, EV_READ | EV_TIMEOUT, newDataHandler, da, &tv);
       }
-      if(cmd == 'r')
+      if (cmd == 'r')
         return;
       /* Handle other cmd without do anything else.  */
     }
 
-    event_add(&(da->loopEvent), NULL);
+    event_add (&(da->loopEvent), NULL);
   }
 }
 
-static void listenerHandler(int fd, short event, void *arg)
+static void listenerHandler (int fd, short event, void *arg)
 {
   static timeval tv = {5, 0};
   ConnectionsScheduler::ListenerArg* s = (ConnectionsScheduler::ListenerArg*)arg;
 
-  if(event == EV_TIMEOUT)
+  if (event == EV_TIMEOUT)
   {
     event_add (&(s->ev), &tv);
   }
-  else if(event == EV_READ)
+  else if (event == EV_READ)
   {
     MYSERVER_SOCKADDRIN asockIn;
     int asockInLen = 0;
     Socket asock;
 
-    asockInLen = sizeof(sockaddr_in);
-    asock = s->serverSocket->accept(&asockIn, &asockInLen);
+    asockInLen = sizeof (sockaddr_in);
+    asock = s->serverSocket->accept (&asockIn, &asockInLen);
 
-    if(s->server &&
+    if (s->server &&
        asock.getHandle() != (FileHandle)INVALID_SOCKET)
     {
-      s->server->addConnection(asock, &asockIn);
+      s->server->addConnection (asock, &asockIn);
     }
 
     event_add (&(s->ev), &tv);
@@ -273,7 +188,7 @@
   event_add(&(arg->ev), &tv);
 
   u_long nbw;
-  Socket sock ((FileHandle)dispatcherArg.fd[1]);
+  Socket sock ((FileHandle)dispatcherArg.socketPair.getSecondHandle ());
 
   eventsSocketMutex.lock();
   sock.write("l", 1, &nbw);  
@@ -372,9 +287,9 @@
 #else
 #define LOCAL_SOCKETPAIR_AF AF_UNIX
 #endif
-  int err = create_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0,
-                              dispatcherArg.fd);
 
+  int err = dispatcherArg.socketPair.create ();
+
   if (err == -1)
   {
     if(server)
@@ -384,19 +299,18 @@
     return;
   }
 
-  make_socket_nonblocking(dispatcherArg.fd[0]);
-  make_socket_nonblocking(dispatcherArg.fd[1]);
+  dispatcherArg.socketPair.setNonBlocking (true);
 
-  event_set(&(dispatcherArg.loopEvent), dispatcherArg.fd[0], EV_READ | EV_TIMEOUT,
+  event_set (&(dispatcherArg.loopEvent), dispatcherArg.socketPair.getFirstHandle (), EV_READ | EV_TIMEOUT,
             eventLoopHandler, &dispatcherArg);
 
-  event_add(&(dispatcherArg.loopEvent), NULL);
+  event_add (&(dispatcherArg.loopEvent), NULL);
 
-  if(Thread::create(&dispatchedThreadId, dispatcher, &dispatcherArg))
+  if (Thread::create (&dispatchedThreadId, dispatcher, &dispatcherArg))
   {
-    if(server)
+    if (server)
     {
-      server->logWriteln("Error initializing dispatcher thread.", MYSERVER_LOG_MSG_ERROR);
+      server->logWriteln ("Error initializing dispatcher thread.", MYSERVER_LOG_MSG_ERROR);
     }
     dispatchedThreadId = 0;
   }
@@ -443,7 +357,7 @@
   int priority = c->getPriority();
 
   if(priority == -1 && c->host)
-      priority = c->host->getDefaultPriority();
+    priority = c->host->getDefaultPriority();
     
   priority = std::max(0, priority);
   priority = std::min(PRIORITY_CLASSES - 1, priority);
@@ -504,7 +418,7 @@
   if(lock)
   {
     u_long nbw;
-    Socket sock (dispatcherArg.fd[1]);
+    Socket sock (dispatcherArg.socketPair.getSecondHandle ());
 
     eventsSocketMutex.lock();
     sock.write("c", 1, &nbw);
@@ -578,8 +492,8 @@
     readySemaphore->unlock();
   }
 
-  Socket sockR (dispatcherArg.fd[0]);
-  Socket sockW (dispatcherArg.fd[1]);
+  Socket sockR (dispatcherArg.socketPair.getFirstHandle ());
+  Socket sockW (dispatcherArg.socketPair.getSecondHandle ());
 
   eventsSocketMutex.lock();
   sockW.write("r", 1, &nbw);
@@ -607,11 +521,7 @@
   
   eventsMutex.unlock();
 
-  sockR.shutdown(SD_BOTH);
-  sockW.shutdown(SD_BOTH);
-
-  sockR.close();
-  sockW.close();
+  dispatcherArg.socketPair.close ();
 }
 
 /*!





From MAILER-DAEMON Fri Jan 30 17:38:40 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LT20W-0003Sy-0q
	for mharc-myserver-commit@gnu.org; Fri, 30 Jan 2009 17:38:40 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LT20U-0003Sd-Rn
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 17:38:38 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LT20U-0003SA-BK
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 17:38:38 -0500
Received: from [199.232.76.173] (port=35158 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LT20U-0003S3-8B
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 17:38:38 -0500
Received: from savannah.gnu.org ([199.232.41.3]:55151 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LT20T-0003a3-V2
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 17:38:38 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LT20T-0008E9-Dx
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 22:38:37 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LT20T-0008E5-5w
	for myserver-commit@gnu.org; Fri, 30 Jan 2009 22:38:37 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LT20T-0008E5-5w@cvs.savannah.gnu.org>
Date: Fri, 30 Jan 2009 22:38:37 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2985] SocketPair can now transmit file
	descriptors.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Jan 2009 22:38:39 -0000

Revision: 2985
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2985
Author:   gscrivano
Date:     2009-01-30 22:38:36 +0000 (Fri, 30 Jan 2009)

Log Message:
-----------
SocketPair can now transmit file descriptors.

Modified Paths:
--------------
    trunk/myserver/include/base/process/fork_server.h
    trunk/myserver/include/base/socket_pair/socket_pair.h
    trunk/myserver/src/base/socket_pair/socket_pair.cpp

Modified: trunk/myserver/include/base/process/fork_server.h
===================================================================
--- trunk/myserver/include/base/process/fork_server.h	2009-01-20 21:13:52 UTC (rev 2984)
+++ trunk/myserver/include/base/process/fork_server.h	2009-01-30 22:38:36 UTC (rev 2985)
@@ -53,6 +53,7 @@
   u_short getPort (){return port;}
   bool isInitialized (){return initialized;}
   int generateListenerSocket (Socket &socket, u_short *port);
+
  private:
   Mutex serverLock;
   u_short port;

Modified: trunk/myserver/include/base/socket_pair/socket_pair.h
===================================================================
--- trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-20 21:13:52 UTC (rev 2984)
+++ trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-30 22:38:36 UTC (rev 2985)
@@ -41,6 +41,9 @@
 	void closeSecondHandle ();
   void setNonBlocking (bool blocking);
 	virtual u_long bytesToRead();
+
+  int readHandle (FileHandle*);
+  int writeHandle (FileHandle);
 private:
 	FileHandle handles[2];
 };

Modified: trunk/myserver/src/base/socket_pair/socket_pair.cpp
===================================================================
--- trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-20 21:13:52 UTC (rev 2984)
+++ trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-30 22:38:36 UTC (rev 2985)
@@ -33,6 +33,9 @@
 #include <string.h>
 #include <math.h>
 #include <time.h>
+
+#include <sys/socket.h>
+#include <sys/uio.h>
 }
 #else
 #include <windows.h>
@@ -228,3 +231,69 @@
   Socket sock (handles[0]);
   return sock.bytesToRead ();
 }
+
+/*!
+ *Read a file handle on the socket pair.
+ *\param fd The file descriptor to read.
+ *\return 0 on success.
+ */
+int SocketPair::readHandle (FileHandle* fd)
+{
+#ifdef WIN32
+  return -1;
+#else
+  struct msghdr mh;
+  struct cmsghdr cmh[2];
+  struct iovec iov;
+  int ret;
+  char tbuf[4];
+  mh.msg_name = 0;
+  mh.msg_namelen = 0;
+  mh.msg_iov = &iov;
+  mh.msg_iovlen = 1;
+  mh.msg_control = (caddr_t)&cmh[0];
+  mh.msg_controllen = sizeof (cmh[0]) * 2;
+  iov.iov_base = tbuf;
+  iov.iov_len = 4;
+  cmh[0].cmsg_len = sizeof (cmh[0]) + sizeof(int);
+  ret = recvmsg (handles[0], &mh, 0);
+  
+  if (ret < 0)
+    return ret;
+
+  *fd = *(FileHandle *)&cmh[1];
+
+  return 0;
+#endif
+}
+
+/*!
+ *Transmit a file descriptor on the socket.
+ *\param fd The file descriptor to transmit.
+ *\return 0 on success.
+ */
+int SocketPair::writeHandle (FileHandle fd)
+{
+#ifdef WIN32
+  return -1;
+#else
+  struct msghdr mh;
+  struct cmsghdr cmh[2];
+  struct iovec iov;
+  memset (&mh,0,sizeof (mh));
+  mh.msg_name = 0;
+  mh.msg_namelen = 0;
+  mh.msg_iov = &iov;
+  mh.msg_iovlen = 1;
+  mh.msg_control = (caddr_t)&cmh[0];
+  mh.msg_controllen = sizeof (cmh[0]) + sizeof (int);
+  mh.msg_flags = 0;
+  iov.iov_base = NULL;
+  iov.iov_len = (strlen ((const char*)iov.iov_base) + 1);
+  cmh[0].cmsg_level = SOL_SOCKET;
+  cmh[0].cmsg_type = SCM_RIGHTS;
+  cmh[0].cmsg_len = sizeof(cmh[0]) + sizeof(int);
+  *(int *)&cmh[1] = fd;
+  return sendmsg (handles[1], &mh, 0);
+#endif
+}





From MAILER-DAEMON Sat Jan 31 12:03:03 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LTJFG-00064G-Ec
	for mharc-myserver-commit@gnu.org; Sat, 31 Jan 2009 12:03:02 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LTJFF-00063A-FR
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:03:01 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LTJFD-000612-NW
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:03:00 -0500
Received: from [199.232.76.173] (port=54391 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LTJFD-00060Y-Fl
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:02:59 -0500
Received: from savannah.gnu.org ([199.232.41.3]:55342 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LTJFC-0003pP-QK
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:02:59 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LTJFC-0004hz-Bq
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 17:02:58 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LTJFC-0004hv-04
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 17:02:58 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LTJFC-0004hv-04@cvs.savannah.gnu.org>
Date: Sat, 31 Jan 2009 17:02:58 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2986]
 Now the ForkServer uses a SocketPair to communicate with the main process.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 31 Jan 2009 17:03:01 -0000

Revision: 2986
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2986
Author:   gscrivano
Date:     2009-01-31 17:02:57 +0000 (Sat, 31 Jan 2009)

Log Message:
-----------
Now the ForkServer uses a SocketPair to communicate with the main process.

Modified Paths:
--------------
    trunk/myserver/include/base/process/fork_server.h
    trunk/myserver/include/base/socket_pair/socket_pair.h
    trunk/myserver/src/base/process/fork_server.cpp
    trunk/myserver/src/base/process/process.cpp
    trunk/myserver/src/base/process/process_server_manager.cpp
    trunk/myserver/src/base/socket_pair/socket_pair.cpp
    trunk/myserver/tests/test_fork_server.cpp

Modified: trunk/myserver/include/base/process/fork_server.h
===================================================================
--- trunk/myserver/include/base/process/fork_server.h	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/include/base/process/fork_server.h	2009-01-31 17:02:57 UTC (rev 2986)
@@ -1,6 +1,6 @@
 /*
   MyServer
-  Copyright (C) 2008 Free Software Foundation, Inc.
+  Copyright (C) 2008, 2009 Free Software Foundation, Inc.
   This program 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 3 of the License, or
@@ -17,6 +17,7 @@
 
 
 #include <unistd.h>
+#include <include/base/socket_pair/socket_pair.h>
 #include <include/base/socket/socket.h>
 #include <include/base/sync/mutex.h>
 
@@ -28,9 +29,10 @@
 class ForkServer
 {
  public:
-  const static int FLAG_USE_OUT = 1;
-  const static int FLAG_USE_IN = 2;
-  const static int FLAG_STDIN_SOCKET = 4;
+  const static int FLAG_USE_IN = 1;
+  const static int FLAG_USE_OUT = 2;
+  const static int FLAG_USE_ERR = 4;
+  const static int FLAG_STDIN_SOCKET = 8;
 
   ForkServer () {initialized = false; serverLock.init ();}
   ~ForkServer () {serverLock.destroy ();}
@@ -38,23 +40,28 @@
   void killServer ();
   int startForkServer ();
 
-  int writeInt (Socket *socket, int num);
-  int writeString (Socket *socket, const char* str, int len);
-  int readInt (Socket *sock, int *dest);
-  int readString (Socket *sock, char **out);
+  int writeInt (SocketPair *socket, int num);
+  int writeString (SocketPair *socket, const char* str, int len);
+  int readInt (SocketPair *sock, int *dest);
+  int readString (SocketPair *sock, char **out);
 
-  int handleRequest (Socket sin, Socket sout, Socket *serverSock);
-  int forkServerLoop (Socket *socket);
+  int writeFd (SocketPair *socket, FileHandle fd);
+  int readFd (SocketPair *sock, FileHandle *fd);
 
-  int getConnection (Socket *socket, Socket *socket2);
-  int executeProcess (StartProcInfo *spi, Socket *sin, Socket *sout, 
-                      int flags, int *pid, int *port);
+  int handleRequest (SocketPair *serverSock);
+  int forkServerLoop (SocketPair *socket);
 
+  int executeProcess (StartProcInfo *spi, int flags,
+                      int *pid, int *port);
+
   u_short getPort (){return port;}
   bool isInitialized (){return initialized;}
   int generateListenerSocket (Socket &socket, u_short *port);
 
  private:
+
+  SocketPair socket;
+
   Mutex serverLock;
   u_short port;
   bool initialized;

Modified: trunk/myserver/include/base/socket_pair/socket_pair.h
===================================================================
--- trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/include/base/socket_pair/socket_pair.h	2009-01-31 17:02:57 UTC (rev 2986)
@@ -21,12 +21,13 @@
 
 #include "stdafx.h"
 #include <include/filter/stream.h>
+#include <include/base/socket/socket.h>
 #include <string>
 
 using namespace std;
 
 
-class SocketPair : public Stream
+class SocketPair : public Socket
 {
 public:
 	SocketPair ();
@@ -34,17 +35,15 @@
 	FileHandle getFirstHandle ();
 	FileHandle getSecondHandle ();
 	void inverted (SocketPair&);
-  virtual int read (char* buffer, u_long len, u_long *nbr);
-  virtual int write (const char* buffer, u_long len, u_long *nbw);
 	virtual int close ();
 	void closeFirstHandle ();
 	void closeSecondHandle ();
   void setNonBlocking (bool blocking);
-	virtual u_long bytesToRead();
 
   int readHandle (FileHandle*);
   int writeHandle (FileHandle);
-private:
+
+protected:
 	FileHandle handles[2];
 };
 #endif

Modified: trunk/myserver/src/base/process/fork_server.cpp
===================================================================
--- trunk/myserver/src/base/process/fork_server.cpp	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/src/base/process/fork_server.cpp	2009-01-31 17:02:57 UTC (rev 2986)
@@ -1,6 +1,6 @@
 /*
   MyServer
-  Copyright (C) 2008 Free Software Foundation, Inc.
+  Copyright (C) 2008, 2009 Free Software Foundation, Inc.
   This program 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 3 of the License, or
@@ -18,6 +18,7 @@
 
 #include <include/base/process/fork_server.h>
 #include <include/base/process/process.h>
+#include <include/base/socket_pair/socket_pair.h>
 #include <include/base/pipe/pipe.h>
 
 #ifdef NOT_WIN
@@ -25,11 +26,38 @@
 #endif
 
 /*!
+ *Read a file descriptor from the specified socket.
+ *\param socket Socket to use.
+ *\param num Integer to write.
+ */
+int ForkServer::writeFd (SocketPair *socket, FileHandle fd)
+{
+
+  if (socket->writeHandle (fd))
+    return 1;
+
+  return 0;
+}
+
+/*!
+ *Write a file descriptor on the specified socket.
+ *\param socket Socket to use.
+ *\param num Integer to write.
+ */
+int ForkServer::readFd (SocketPair *socket, FileHandle *fd)
+{
+  if (socket->readHandle (fd))
+    return 1;
+
+  return 0;
+}
+
+/*!
  *Write an integer on the specified socket.
  *\param socket Socket to use.
  *\param num Integer to write.
  */
-int ForkServer::writeInt (Socket *socket, int num)
+int ForkServer::writeInt (SocketPair *socket, int num)
 {
   u_long nbw;
 
@@ -47,7 +75,7 @@
  *\param str string to write.
  *\param len string length.
  */
-int ForkServer::writeString (Socket *socket, const char* str, int len)
+int ForkServer::writeString (SocketPair *socket, const char* str, int len)
 {
   u_long nbw;
 
@@ -70,7 +98,7 @@
  *\param dest integer where write
  *\return 0 on success.
  */
-int ForkServer::readInt (Socket *sock, int *dest)
+int ForkServer::readInt (SocketPair *sock, int *dest)
 {
   u_long nbr;
   
@@ -91,7 +119,7 @@
  *\param out destination buffer pointer.
  *\return 0 on success.
  */
-int ForkServer::readString (Socket *sock, char **out)
+int ForkServer::readString (SocketPair *sock, char **out)
 {
   int len;
   u_long nbr;
@@ -101,16 +129,11 @@
       return -1;
     }
 
-  *out = new char[len];
+  *out = new char[len + 1];
+  (*out)[len] = '\0';
 
-  if (!len)
+  if (len && (sock->read (*out, len, &nbr) || nbr < len))
     {
-      *out = NULL;
-      return 0;
-    }
-
-  if (sock->read (*out, len, &nbr) || nbr < len)
-    {
       delete [] *out;
       return -1;      
     }
@@ -121,59 +144,42 @@
 /*!
  *Handle a request on the socket.
  */
-int ForkServer::handleRequest (Socket sin, Socket sout, Socket *serverSock)
+int ForkServer::handleRequest (SocketPair *serverSock)
 {
 #ifdef NOT_WIN
-  int ret, flags, stdIn, stdOut, stdErr, gid, uid;
+  int ret, flags, stdIn = -1, stdOut = -1, stdErr = -1;
+  int gid, uid;
+  int stdInPort = 0;
   char *exec;
   char *cwd;
   char *arg;
   char *env;
 
-  if (readInt (&sin, &flags) ||
-      readInt (&sin, &stdIn) ||
-      readInt (&sin, &stdOut) ||
-      readInt (&sin, &stdErr) ||
-      readInt (&sin, &gid) ||
-      readInt (&sin, &uid))
-    {
-      return -1;     
-    }
-   
-  if (readString (&sin, &exec))
-    {
-      return -1;
-    }
+  readInt (serverSock, &flags);
 
-  if (readString (&sin, &cwd))
-    {
-      delete [] exec;
-      return -1;
-    }
+  if (flags & FLAG_USE_IN)
+    readFd (serverSock, &stdIn);
 
-  if (readString (&sin, &arg))
-    {
-      delete [] exec;
-      delete [] cwd;
-      return -1;
-    }
-  string argS (arg);
+  if (flags & FLAG_USE_OUT)
+    readFd (serverSock, &stdOut);
   
+  if (flags & FLAG_USE_ERR)
+    readFd (serverSock, &stdErr);
 
-  if (readString (&sin, &env))
-    {
-      delete [] exec;
-      delete [] cwd;
-      delete [] arg;
-      return -1;
-    }
+  readInt (serverSock, &gid);
+  readInt (serverSock, &uid);
+
+  readString (serverSock, &exec);
+  readString (serverSock, &cwd);
+
+  readString (serverSock, &arg);
+ 
+  string argS (arg);
   
-  FileHandle stdHandles[3] = {flags & FLAG_USE_IN ? sin.getHandle () : -1,
-                              flags & FLAG_USE_OUT ? sout.getHandle () : -1,
-                              -1};
 
+  readString (serverSock, &env);
+
   Socket socketIn;
-  int stdInPort = 0;
 
   if (flags & FLAG_STDIN_SOCKET)
     {
@@ -184,124 +190,59 @@
           delete [] exec;
           delete [] cwd;
           delete [] arg;
+          writeInt (serverSock, -1);
+          writeInt (serverSock, -1);
           return -1;
         }
       stdInPort = (int) stdInPortS;
 
-      stdHandles[0] = socketIn.getHandle ();
-      stdHandles[1] = stdHandles[2] = (FileHandle) -1;
+      stdIn = socketIn.getHandle ();
+      stdOut = stdErr = (FileHandle) -1;
     }
 
   StartProcInfo spi;
 
   spi.envString = (env && env[0]) ? env : NULL;
 
-  spi.stdIn = stdHandles[0];
-  spi.stdOut = stdHandles[1];
-  spi.stdError = stdHandles[2];
+  spi.stdIn = stdIn;
+  spi.stdOut = stdOut;
+  spi.stdError = stdErr;
 
   spi.cmd.assign (exec);
   spi.arg.assign (arg);
   spi.cwd.assign (cwd);
 
-  Pipe syncPipe;
-  syncPipe.create ();
-  /* spi.cmdLine is used only under Windows and 
-   * the fork server doesn't work there.  */
-  ret = fork ();
+  Process pi;
+  int pid = pi.exec (&spi, false);
 
-  if (ret)
-    {
-      u_long nbw;
-      writeInt (&sout, ret);
-      writeInt (&sout, stdInPort);
+  writeInt (serverSock, pid);
+  writeInt (serverSock, stdInPort);
 
-      /* Synchronize with the child process.  It avoids that the
-       * child process starts to write on `sout' before the process
-       * information are sent back.  */
-      syncPipe.write ("1", 1, &nbw);
-      syncPipe.close ();
-    }
-
-  /* Code already present in process.cpp, refactoring needed.  */
-  if (ret == 0) // child
-    {
-      u_long nbr;
-      char syncB;
-      const char *envp[100];
-      const char *args[100];
-
-      /* The parent process sent an ack when the child can start 
-       * its execution.  */
-      syncPipe.read (&syncB, 1, &nbr);
-      syncPipe.close ();
-
-      /* Close the fork server descriptor in the child process.  */
-      serverSock->close ();
-
-      if (gid)
-        Process::setgid (gid);
-
-      if (uid)
-        Process::setuid (uid);
-     
-      if (Process::generateArgList (args, spi.cmd.c_str (), spi.arg))
-        exit (1);
-
-      if (Process::generateEnvString (envp, (char*) spi.envString))
-        exit (1);
-      
-      if (spi.cwd.length ())
-        {
-          ret = chdir ((const char*)(spi.cwd.c_str()));
-          if (ret == -1)
-            exit (1);
-        }
-      if ((long)spi.stdIn == -1)
-      {
-        spi.stdIn = (FileHandle)open ("/dev/null", O_RDONLY);
-      }
-
-      if ((long)spi.stdIn == -1 || flags & FLAG_STDIN_SOCKET)
-        sin.close ();
-
-      if ((long)spi.stdOut == -1)
-      {
-        spi.stdOut = (FileHandle)open ("/dev/null", O_WRONLY);
-        sout.close ();
-      }
-
-      ret = close(0);
-      if (ret == -1)
-        exit (1);
-      ret = dup2(spi.stdIn, 0);
-      if (ret == -1)
-        exit (1);
-      ret = close(spi.stdIn);
-      if (ret == -1)
-        exit (1);
-      ret = close (1);
-      if (ret == -1)
-        exit (1);
-    ret = dup2(spi.stdOut, 1);
-    if (ret == -1)
-      exit (1);
-    ret = close (spi.stdOut);
-    if (ret == -1)
-      exit (1);
-
-    execve ((const char*)args[0], 
-            (char* const*)args, (char* const*) envp);
-    exit (0);
-
-  }
-
   delete [] exec;
   delete [] cwd;
   delete [] arg;
   delete [] env;
 
-  return ret == -1 ? -1 : 0;
+  if (flags & FLAG_USE_IN)
+    {
+      Socket s0 (stdIn);
+      s0.close ();
+    }
+
+  if (flags & FLAG_USE_OUT)
+    {
+      Socket s1 (stdOut);
+      s1.close ();
+    }
+  
+  if (flags & FLAG_USE_ERR)
+    {
+      Socket s0 (stdIn);
+      s0.close ();
+    }
+
+
+  return 0;
 #endif
   return 0;
 }
@@ -313,7 +254,7 @@
  *\param socket Socket where wait for new connections.
  *\return 0 on success.
  */
-int ForkServer::forkServerLoop (Socket *socket)
+int ForkServer::forkServerLoop (SocketPair *socket)
 {
 #ifdef NOT_WIN
   for (;;)
@@ -325,38 +266,18 @@
           MYSERVER_SOCKADDR_STORAGE sockaddr;
           int len = sizeof (sockaddr);
           
-          if (!socket->dataOnRead(5, 0))
+          if (socket->read (&command, 1, &nbr))
             continue;
-          
-          Socket sin = socket->accept (&sockaddr, &len);
-          Socket sout = socket->accept (&sockaddr, &len);
-          
-          //if (sin.getHandle () == -1 || sout.getHandle ())
-          //  {
-          //    continue;
-          //  }
-          
-          if (sin.read (&command, 1, &nbr))
-            {
-              sin.shutdown (2);
-              sin.close ();
-              sout.shutdown (2);
-              sout.close ();
-              continue;
-            }
-        
+
           switch (command)
             {
             case 'e': //exit process
               exit (0);
               return 0;
             case 'r':
-              if (handleRequest (sin, sout, socket))
+              if (handleRequest (socket))
                 {
-                  sin.shutdown (2);
-                  sin.close ();
-                  sout.shutdown (2);
-                  sout.close ();
+                  continue;
                 }
             }
         }
@@ -373,79 +294,59 @@
 }
 
 /*!
- *Get a connection to the fork server on the specified sockets.
- *
- *\param sin Socket where obtain the input connection.
- *\param sout Socket where obtain the output connection.
- *\return 0 on success.
- */
-int ForkServer::getConnection (Socket *sin, Socket *sout)
-{
-  int len = sizeof(sockaddr_in);
-  MYSERVER_SOCKADDR_STORAGE sockaddr = { 0 };
-  ((sockaddr_in*)(&sockaddr))->sin_family = AF_INET;
-  ((sockaddr_in*)(&sockaddr))->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
-  ((sockaddr_in*)(&sockaddr))->sin_port = htons (port);
-
-  sin->socket (AF_INET, SOCK_STREAM, 0);
-  sout->socket (AF_INET, SOCK_STREAM, 0);
-  
-  serverLock.lock ();
-
-  int ret = sin->connect ((MYSERVER_SOCKADDR*)&sockaddr, len);
-  ret |= sout->connect ((MYSERVER_SOCKADDR*)&sockaddr, len) ;
-
-  serverLock.unlock ();
-
-  return ret;
-}
-
-
-/*!
  *Execute a process using the fork server.
  *\param spi New process information.
- *\param sin Socket to use for the process stdin.
- *\param sout Socket to use for the process stdout.
  *\param flags Flags.
  *\param pid The new process ID.
  *\param port if FLAG_STDIN_SOCKET was specified.
  */
-int ForkServer::executeProcess (StartProcInfo *spi, Socket *sin, Socket *sout, 
+int ForkServer::executeProcess (StartProcInfo *spi, 
                                 int flags, int *pid, int *port)
 {
   u_long nbw;
   int len = 0;
   const char * env = (const char *) spi->envString;
 
-  if (getConnection (sin, sout))
+  serverLock.lock ();
+
+  try
     {
-      return 1;
+      socket.write ("r", 1, &nbw);
+      
+      writeInt (&socket, flags);
+      
+      if (flags & FLAG_USE_IN)
+        writeFd (&socket, spi->stdIn);
+      
+      if (flags & FLAG_USE_OUT)
+        writeFd (&socket, spi->stdOut);
+      
+      if (flags & FLAG_USE_ERR)
+        writeFd (&socket, spi->stdError);
+      
+      writeInt (&socket, spi->gid);
+      writeInt (&socket, spi->uid);
+      
+      writeString (&socket, spi->cmd.c_str (), spi->cmd.length ());
+      writeString (&socket, spi->cwd.c_str (), spi->cwd.length ());
+      writeString (&socket, spi->arg.c_str (), spi->arg.length ());
+      
+      if (env)
+        for (len = 0; env[len] != '\0' || env[len + 1] != '\0' ; len++);
+      
+      writeString (&socket, env, len);
+      
+      readInt (&socket, pid);
+      readInt (&socket, port);
     }
-
-  sin->write ("r", 1, &nbw);
-
-  writeInt (sin, flags);
-  writeInt (sin, spi->stdIn);
-  writeInt (sin, spi->stdOut);
-  writeInt (sin, spi->stdError);
-
-  writeInt (sin, spi->gid);
-  writeInt (sin, spi->uid);
-
-  writeString (sin, spi->cmd.c_str (), spi->cmd.length () + 1);
-  writeString (sin, spi->cwd.c_str (), spi->cwd.length () + 1);
-  writeString (sin, spi->arg.c_str (), spi->arg.length () + 1);
-
-  if (env)
+  catch (exception &e)
     {
-      for (len = 0; env[len] != '\0' || env[len + 1] != '\0' ; len++);
+      serverLock.unlock ();
+      throw e;
     }
 
-  writeString (sin, env, len + 1);
+  serverLock.unlock ();
 
-  readInt (sout, pid);
-  readInt (sout, port);
-
   return 0;
 }
 
@@ -454,20 +355,17 @@
  */
 void ForkServer::killServer ()
 {
-  Socket sin;
-  Socket sout;
   u_long nbw;
 
-  if (getConnection (&sin, &sout))
-    {
-      return;
-    }
+  serverLock.lock ();
 
-  sin.write ("e", 1, &nbw);
-  sin.shutdown (2);
-  sout.shutdown (2);
-  sin.close ();
-  sout.close ();
+  socket.write ("e", 1, &nbw);
+
+  serverLock.unlock ();
+
+  serverLock.destroy ();
+
+  socket.close ();
 }
                   
 /*!
@@ -478,26 +376,25 @@
 int ForkServer::startForkServer ()
 {
 #ifdef NOT_WIN
-  Socket socket;
+  socket.create ();
+  serverLock.init ();
+  SocketPair inverted;
+  socket.inverted (inverted);
 
-  if (generateListenerSocket (socket, &port))
-    return 1;
-
-  if (socket.listen (2))
-    return -1;
-
   switch (fork ())
     {
     case -1:
       return -1;
     case 0:
       initialized = true;
-      forkServerLoop (&socket);
+      socket.closeFirstHandle ();
+      forkServerLoop (&inverted);
+      exit (1);
       break;
 
     default:
+      socket.closeSecondHandle ();
       initialized = true;
-      socket.close ();
       break;
     }  
 #endif

Modified: trunk/myserver/src/base/process/process.cpp
===================================================================
--- trunk/myserver/src/base/process/process.cpp	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/src/base/process/process.cpp	2009-01-31 17:02:57 UTC (rev 2986)
@@ -233,20 +233,29 @@
           if (ret == -1)
             exit (1);
         }
-
-      // If stdOut is -1, pipe to /dev/null
+ 
       if ((long)spi->stdOut == -1)
         spi->stdOut = (FileHandle)open ("/dev/null", O_WRONLY);
+
+      if ((long)spi->stdError == -1)
+        spi->stdError = (FileHandle)open ("/dev/null", O_WRONLY);
+
       // map stdio to files
       ret = close(0); // close stdin
       if (ret == -1)
         exit (1);
-      ret = dup2(spi->stdIn, 0);
+
+
+      if (spi->stdIn != -1)
+        {
+          ret = dup2(spi->stdIn, 0);
+          if (ret == -1)
+            exit (1);
+          ret = close(spi->stdIn);
+        }
+
       if (ret == -1)
         exit (1);
-      ret = close(spi->stdIn);
-      if (ret == -1)
-        exit (1);
       ret = close (1); // close stdout
       if (ret == -1)
         exit (1);

Modified: trunk/myserver/src/base/process/process_server_manager.cpp
===================================================================
--- trunk/myserver/src/base/process/process_server_manager.cpp	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/src/base/process/process_server_manager.cpp	2009-01-31 17:02:57 UTC (rev 2986)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 This program 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 3 of the License, or
@@ -437,11 +437,8 @@
 
   if (Process::getForkServer ()->isInitialized ())
     {
-      Socket forkSockIn, forkSockOut;
       int ret, port, pid;
       ret = Process::getForkServer ()->executeProcess (&spi, 
-                                                       &forkSockIn,
-                                                       &forkSockOut,
                                                        ForkServer::FLAG_STDIN_SOCKET, 
                                                        &pid,
                                                        &port);

Modified: trunk/myserver/src/base/socket_pair/socket_pair.cpp
===================================================================
--- trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/src/base/socket_pair/socket_pair.cpp	2009-01-31 17:02:57 UTC (rev 2986)
@@ -70,7 +70,13 @@
   int protocol = 0;
 
 #ifndef WIN32
-  return socketpair (af, type, protocol, (int*)handles);
+  int ret = socketpair (af, type, protocol, (int*)handles);
+
+  if (ret == 0)
+    socketHandle = handles[0];
+
+  return ret;
+
 #else
   struct sockaddr_in addr;
   SOCKET listener;
@@ -117,6 +123,8 @@
       if ((handles[1] = accept (listener, NULL, NULL)) == INVALID_SOCKET)
         break;
     
+      socketHandle = handles[0];
+
       close (listener);
       return 0;
     } while (0);
@@ -158,30 +166,11 @@
 {
   inverted.handles[0] = handles[1];
   inverted.handles[1] = handles[0];
-}
 
-/*!
- *Read using the first handle.
- *\see Stream#read.
- */
-int SocketPair::read (char* buffer, u_long len, u_long *nbr)
-{
-  Socket sock (handles[0]);
-  return sock.read (buffer, len, nbr);
-
+  inverted.socketHandle = handles[1];
 }
 
 /*!
- *Write using the first handle.
- *\see Stream#write.
- */
-int SocketPair::write (const char* buffer, u_long len, u_long *nbw)
-{
-  Socket sock (handles[0]);
-  return sock.write (buffer, len, nbw);
-}
-
-/*!
  *Close both handles.
  */
 int SocketPair::close ()
@@ -224,15 +213,6 @@
 }
 
 /*!
- *Return how many bytes can be read on the first socket.
- */
-u_long SocketPair::bytesToRead()
-{
-  Socket sock (handles[0]);
-  return sock.bytesToRead ();
-}
-
-/*!
  *Read a file handle on the socket pair.
  *\param fd The file descriptor to read.
  *\return 0 on success.
@@ -257,7 +237,7 @@
   iov.iov_len = 4;
   cmh[0].cmsg_len = sizeof (cmh[0]) + sizeof(int);
   ret = recvmsg (handles[0], &mh, 0);
-  
+
   if (ret < 0)
     return ret;
 
@@ -280,6 +260,7 @@
   struct msghdr mh;
   struct cmsghdr cmh[2];
   struct iovec iov;
+  char tbuf[4];
   memset (&mh,0,sizeof (mh));
   mh.msg_name = 0;
   mh.msg_namelen = 0;
@@ -288,12 +269,12 @@
   mh.msg_control = (caddr_t)&cmh[0];
   mh.msg_controllen = sizeof (cmh[0]) + sizeof (int);
   mh.msg_flags = 0;
-  iov.iov_base = NULL;
-  iov.iov_len = (strlen ((const char*)iov.iov_base) + 1);
+  iov.iov_base = tbuf;
+  iov.iov_len = 4;
   cmh[0].cmsg_level = SOL_SOCKET;
   cmh[0].cmsg_type = SCM_RIGHTS;
   cmh[0].cmsg_len = sizeof(cmh[0]) + sizeof(int);
   *(int *)&cmh[1] = fd;
-  return sendmsg (handles[1], &mh, 0);
+  return sendmsg (handles[0], &mh, 0);
 #endif
 }

Modified: trunk/myserver/tests/test_fork_server.cpp
===================================================================
--- trunk/myserver/tests/test_fork_server.cpp	2009-01-30 22:38:36 UTC (rev 2985)
+++ trunk/myserver/tests/test_fork_server.cpp	2009-01-31 17:02:57 UTC (rev 2986)
@@ -21,6 +21,7 @@
 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <cppunit/ui/text/TestRunner.h>
 #include <cppunit/extensions/HelperMacros.h>
+#include <include/base/pipe/pipe.h>
 
 #include <exception>
 #include <string.h>
@@ -30,8 +31,8 @@
 class TestForkServer : public CppUnit::TestFixture
 {
   CPPUNIT_TEST_SUITE( TestForkServer );
+  CPPUNIT_TEST( testExecuteProcess );
   CPPUNIT_TEST (testStartKillLoop);
-  CPPUNIT_TEST( testExecuteProcess );
   CPPUNIT_TEST_SUITE_END();
 
   ForkServer *fs;
@@ -58,8 +59,6 @@
 
     CPPUNIT_ASSERT_EQUAL (fs->isInitialized (), true);
 
-    CPPUNIT_ASSERT (fs->getPort ());
-
     fs->killServer ();
 #endif
   }
@@ -72,33 +71,35 @@
         int pid = 0;
         int port = 0;
         StartProcInfo spi;
-        Socket sin, sout;
         char buffer [32];
         const char *msg = "ForkServer";
         u_long nbr;
         int ret = fs->startForkServer ();
-        CPPUNIT_ASSERT_EQUAL (ret, 0);
 
-        spi.stdIn = spi.stdOut = spi.stdError = -1;
+        Pipe pipe;
+        pipe.create();
     
+        spi.stdIn = spi.stdError = -1;
+        spi.stdOut =  pipe.getWriteHandle();
+
         spi.cmd.assign ("/bin/echo");
         spi.arg.assign (msg);
         spi.cwd.assign ("");
+        spi.envString = NULL;
 
-        ret = fs->executeProcess (&spi, &sin, &sout, ForkServer::FLAG_USE_OUT, &pid, &port);
-        sin.shutdown(2);
-        sin.close ();
+        ret = fs->executeProcess (&spi, ForkServer::FLAG_USE_OUT, &pid, &port);
 
         CPPUNIT_ASSERT_EQUAL (ret, 0);
-    
-        ret = sout.read (buffer, 32, &nbr);
+        pipe.closeWrite ();
 
-        sout.close();
+        ret = pipe.read (buffer, 32, &nbr);
 
         CPPUNIT_ASSERT_EQUAL (ret, 0);
 
         CPPUNIT_ASSERT_EQUAL (strncmp (buffer, msg, strlen (msg)), 0);
 
+        pipe.closeRead ();
+
         fs->killServer ();
 #endif
       }





From MAILER-DAEMON Sat Jan 31 12:55:34 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LTK46-0002iU-3e
	for mharc-myserver-commit@gnu.org; Sat, 31 Jan 2009 12:55:34 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LTK44-0002he-6i
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:55:32 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LTK43-0002hJ-Mu
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:55:31 -0500
Received: from [199.232.76.173] (port=35393 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LTK43-0002hA-EQ
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:55:31 -0500
Received: from savannah.gnu.org ([199.232.41.3]:56379 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LTK43-00088c-0e
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 12:55:31 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LTK42-0007Uk-8Y
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 17:55:30 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LTK41-0007Uf-TI
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 17:55:30 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LTK41-0007Uf-TI@cvs.savannah.gnu.org>
Date: Sat, 31 Jan 2009 17:55:29 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2987]
 Added possibility to specify a list of descriptors to close after the fork
 .
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 31 Jan 2009 17:55:32 -0000

Revision: 2987
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2987
Author:   gscrivano
Date:     2009-01-31 17:55:29 +0000 (Sat, 31 Jan 2009)

Log Message:
-----------
Added possibility to specify a list of descriptors to close after the fork.

Modified Paths:
--------------
    trunk/myserver/include/base/process/process.h
    trunk/myserver/src/base/process/fork_server.cpp
    trunk/myserver/src/base/process/process.cpp
    trunk/myserver/tests/test_fork_server.cpp

Modified: trunk/myserver/include/base/process/process.h
===================================================================
--- trunk/myserver/include/base/process/process.h	2009-01-31 17:02:57 UTC (rev 2986)
+++ trunk/myserver/include/base/process/process.h	2009-01-31 17:55:29 UTC (rev 2987)
@@ -33,6 +33,13 @@
  */
 struct StartProcInfo
 {
+  StartProcInfo ()
+  {
+    gid = uid = 0;
+    envString = NULL;
+    handlesToClose = NULL;
+  }
+
 	/*! STDIN file for new process.  */
 	FileHandle stdIn;	
 	
@@ -57,11 +64,9 @@
 
 	void *envString;
 
-  StartProcInfo ()
-  {
-    gid = uid = 0;
-    envString = NULL;
-  }
+  /*! Pointer to a NULL terminated array of 
+   *  file pointers to close.  */
+  FileHandle *handlesToClose; 
 };
 
 class Process

Modified: trunk/myserver/src/base/process/fork_server.cpp
===================================================================
--- trunk/myserver/src/base/process/fork_server.cpp	2009-01-31 17:02:57 UTC (rev 2986)
+++ trunk/myserver/src/base/process/fork_server.cpp	2009-01-31 17:55:29 UTC (rev 2987)
@@ -211,6 +211,7 @@
   spi.cmd.assign (exec);
   spi.arg.assign (arg);
   spi.cwd.assign (cwd);
+  spi.handlesToClose = (FileHandle[]){serverSock->getFirstHandle (), NULL};
 
   Process pi;
   int pid = pi.exec (&spi, false);

Modified: trunk/myserver/src/base/process/process.cpp
===================================================================
--- trunk/myserver/src/base/process/process.cpp	2009-01-31 17:02:57 UTC (rev 2986)
+++ trunk/myserver/src/base/process/process.cpp	2009-01-31 17:55:29 UTC (rev 2987)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 This program 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 3 of the License, or
@@ -218,8 +218,8 @@
 
       if (spi->uid)
         Process::setuid (spi->uid);
-      
 
+
       if (generateArgList (args, spi->cmd.c_str (), spi->arg))
         exit (1);
 
@@ -229,8 +229,7 @@
       // change to working dir
       if (spi->cwd.length ())
         {
-          ret = chdir ((const char*)(spi->cwd.c_str()));
-          if (ret == -1)
+          if (chdir ((const char*)(spi->cwd.c_str())) == -1)
             exit (1);
         }
  
@@ -241,37 +240,46 @@
         spi->stdError = (FileHandle)open ("/dev/null", O_WRONLY);
 
       // map stdio to files
-      ret = close(0); // close stdin
-      if (ret == -1)
+      if (close(0) == -1) // close stdin
         exit (1);
 
-
       if (spi->stdIn != -1)
         {
-          ret = dup2(spi->stdIn, 0);
-          if (ret == -1)
+          if (dup2 (spi->stdIn, 0) == -1)
             exit (1);
-          ret = close(spi->stdIn);
+          
+          if (close (spi->stdIn) == -1)
+            exit (1);
         }
 
-      if (ret == -1)
+      if (close (1) == -1) // close stdout
         exit (1);
-      ret = close (1); // close stdout
-      if (ret == -1)
+
+      if (dup2 (spi->stdOut, 1) == -1)
         exit (1);
-    ret = dup2(spi->stdOut, 1);
-    if (ret == -1)
-      exit (1);
-    ret = close (spi->stdOut);
-    if (ret == -1)
-      exit (1);
-    //close(2); // close stderr
-    //dup2((int)spi->stdError, 2);
-    // Run the script
-    ret = execve ((const char*)args[0], 
-                 (char* const*)args, (char* const*) envp);
-    exit (0);
 
+      if (close (2) == -1) // close stderr
+        exit (1);
+
+      if (dup2 (spi->stdError, 1) == -1)
+        exit (1);
+      
+      if (spi->handlesToClose)
+        {
+          FileHandle* h = spi->handlesToClose;
+          while (*h)
+            {
+              if (close (*h) == -1)
+                exit (1);
+              h++;
+            }
+        }
+      // Run the script
+      execve ((const char*)args[0], 
+              (char* const*)args, (char* const*) envp);
+
+    exit (1);
+
   }
 
   if (waitEnd)

Modified: trunk/myserver/tests/test_fork_server.cpp
===================================================================
--- trunk/myserver/tests/test_fork_server.cpp	2009-01-31 17:02:57 UTC (rev 2986)
+++ trunk/myserver/tests/test_fork_server.cpp	2009-01-31 17:55:29 UTC (rev 2987)
@@ -1,6 +1,6 @@
 /*
  MyServer
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  This program 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 3 of the License, or





From MAILER-DAEMON Sat Jan 31 14:05:43 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LTL9z-0001Wn-6p
	for mharc-myserver-commit@gnu.org; Sat, 31 Jan 2009 14:05:43 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LTL9x-0001Wh-AY
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 14:05:41 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LTL9u-0001WV-MG
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 14:05:41 -0500
Received: from [199.232.76.173] (port=49042 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LTL9u-0001WS-Ix
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 14:05:38 -0500
Received: from savannah.gnu.org ([199.232.41.3]:41139 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LTL9u-0006h4-4L
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 14:05:38 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LTL9t-0004R8-LZ
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 19:05:37 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LTL9t-0004R3-DP
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 19:05:37 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LTL9t-0004R3-DP@cvs.savannah.gnu.org>
Date: Sat, 31 Jan 2009 19:05:37 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2988] When available use the ForkServer to
	execute CGI scripts.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 31 Jan 2009 19:05:41 -0000

Revision: 2988
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2988
Author:   gscrivano
Date:     2009-01-31 19:05:36 +0000 (Sat, 31 Jan 2009)

Log Message:
-----------
When available use the ForkServer to execute CGI scripts.

Modified Paths:
--------------
    trunk/myserver/include/base/process/fork_server.h
    trunk/myserver/src/base/process/fork_server.cpp
    trunk/myserver/src/http_handler/cgi/cgi.cpp

Modified: trunk/myserver/include/base/process/fork_server.h
===================================================================
--- trunk/myserver/include/base/process/fork_server.h	2009-01-31 17:55:29 UTC (rev 2987)
+++ trunk/myserver/include/base/process/fork_server.h	2009-01-31 19:05:36 UTC (rev 2988)
@@ -52,7 +52,8 @@
   int forkServerLoop (SocketPair *socket);
 
   int executeProcess (StartProcInfo *spi, int flags,
-                      int *pid, int *port);
+                      int *pid, int *port, 
+                      bool waitEnd = false);
 
   u_short getPort (){return port;}
   bool isInitialized (){return initialized;}

Modified: trunk/myserver/src/base/process/fork_server.cpp
===================================================================
--- trunk/myserver/src/base/process/fork_server.cpp	2009-01-31 17:55:29 UTC (rev 2987)
+++ trunk/myserver/src/base/process/fork_server.cpp	2009-01-31 19:05:36 UTC (rev 2988)
@@ -23,6 +23,7 @@
 
 #ifdef NOT_WIN
 #include <unistd.h>
+#include <sys/wait.h>
 #endif
 
 /*!
@@ -32,7 +33,6 @@
  */
 int ForkServer::writeFd (SocketPair *socket, FileHandle fd)
 {
-
   if (socket->writeHandle (fd))
     return 1;
 
@@ -208,6 +208,9 @@
   spi.stdOut = stdOut;
   spi.stdError = stdErr;
 
+  spi.uid = uid;
+  spi.gid = gid;
+
   spi.cmd.assign (exec);
   spi.arg.assign (arg);
   spi.cwd.assign (cwd);
@@ -225,24 +228,14 @@
   delete [] env;
 
   if (flags & FLAG_USE_IN)
-    {
-      Socket s0 (stdIn);
-      s0.close ();
-    }
+    close (stdIn);
 
   if (flags & FLAG_USE_OUT)
-    {
-      Socket s1 (stdOut);
-      s1.close ();
-    }
+    close (stdOut);
   
   if (flags & FLAG_USE_ERR)
-    {
-      Socket s0 (stdIn);
-      s0.close ();
-    }
+    close (stdErr);
 
-
   return 0;
 #endif
   return 0;
@@ -300,10 +293,18 @@
  *\param flags Flags.
  *\param pid The new process ID.
  *\param port if FLAG_STDIN_SOCKET was specified.
+ *\param waitEnd If true `executeProcess' will wait until
+ *the process terminates.
  */
 int ForkServer::executeProcess (StartProcInfo *spi, 
-                                int flags, int *pid, int *port)
+                                int flags, 
+                                int *pid, 
+                                int *port,
+                                bool waitEnd)
 {
+#ifdef WIN32
+  return 0;
+#else
   u_long nbw;
   int len = 0;
   const char * env = (const char *) spi->envString;
@@ -348,7 +349,14 @@
 
   serverLock.unlock ();
 
+  if (waitEnd)
+    {
+      return waitpid (*pid, NULL, 0);
+    }
+  
   return 0;
+#endif
+
 }
 
 /*!

Modified: trunk/myserver/src/http_handler/cgi/cgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/cgi/cgi.cpp	2009-01-31 17:55:29 UTC (rev 2987)
+++ trunk/myserver/src/http_handler/cgi/cgi.cpp	2009-01-31 19:05:36 UTC (rev 2988)
@@ -269,7 +269,24 @@
 
   /* Execute the CGI process. */
   {
-    if( cgiProc.exec (&spi) == -1)
+    int ret;
+    if (Process::getForkServer ()->isInitialized ())
+      {
+        int pid;
+        int port;
+
+        ret = Process::getForkServer ()->executeProcess (&spi, 
+                                                         ForkServer::FLAG_USE_IN | 
+                                                         ForkServer::FLAG_USE_OUT | 
+                                                         ForkServer::FLAG_USE_ERR, 
+                                                         &pid, 
+                                                         &port);
+        cgiProc.setPid (pid);
+      }
+    else
+      ret = cgiProc.exec (&spi);
+
+    if( ret == -1)
     {
       stdInFile.close();
       stdOutFile.close();
@@ -277,7 +294,7 @@
                                        ("Cgi: Error in the CGI execution");
       chain.clearAllFilters(); 
       return td->http->raiseHTTPError(500);
-    }
+      }
     /* Close the write stream of the pipe on the server.  */
     stdOutFile.closeWrite();  
   }





From MAILER-DAEMON Sat Jan 31 18:00:31 2009
Received: from mailman by lists.gnu.org with archive (Exim 4.43)
	id 1LTOpD-0006J3-8j
	for mharc-myserver-commit@gnu.org; Sat, 31 Jan 2009 18:00:31 -0500
Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43)
	id 1LTOpA-0006I6-Ea
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 18:00:28 -0500
Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43)
	id 1LTOp9-0006Hr-DF
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 18:00:27 -0500
Received: from [199.232.76.173] (port=44564 helo=monty-python.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1LTOp9-0006Ho-5G
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 18:00:27 -0500
Received: from savannah.gnu.org ([199.232.41.3]:41426 helo=sv.gnu.org)
	by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32)
	(Exim 4.60) (envelope-from <INVALID.NOREPLY@gnu.org>)
	id 1LTOp8-00029W-Nn
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 18:00:26 -0500
Received: from cvs.savannah.gnu.org ([199.232.41.69])
	by sv.gnu.org with esmtp (Exim 4.63)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1LTOp7-0007Vq-Lr
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 23:00:25 +0000
Received: from gscrivano by cvs.savannah.gnu.org with local (Exim 4.63)
	(envelope-from <gscrivano@savannah.gnu.org>) id 1LTOp7-0007Vl-9W
	for myserver-commit@gnu.org; Sat, 31 Jan 2009 23:00:25 +0000
MIME-Version: 1.0
Errors-To: gscrivano
To: myserver-commit@gnu.org
X-Mailer: SVN::Notify 2.64: http://search.cpan.org/dist/SVN-Notify/
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Giuseppe Scrivano <gscrivano@gnu.org>
Message-Id: <E1LTOp7-0007Vl-9W@cvs.savannah.gnu.org>
Date: Sat, 31 Jan 2009 23:00:25 +0000
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2)
Subject: [myserver-commit] [2989] Support other control protocol commands.
X-BeenThere: myserver-commit@gnu.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: myserver-commit.gnu.org
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/myserver-commit>
List-Post: <mailto:myserver-commit@gnu.org>
List-Help: <mailto:myserver-commit-request@gnu.org?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/myserver-commit>,
	<mailto:myserver-commit-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 31 Jan 2009 23:00:29 -0000

Revision: 2989
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2989
Author:   gscrivano
Date:     2009-01-31 23:00:24 +0000 (Sat, 31 Jan 2009)

Log Message:
-----------
Support other control protocol commands.

Modified Paths:
--------------
    trunk/misc/el_control_client/myserver-control.el

Modified: trunk/misc/el_control_client/myserver-control.el
===================================================================
--- trunk/misc/el_control_client/myserver-control.el	2009-01-31 19:05:36 UTC (rev 2988)
+++ trunk/misc/el_control_client/myserver-control.el	2009-01-31 23:00:24 UTC (rev 2989)
@@ -1,5 +1,5 @@
 ;; MyServer
-;; Copyright (C) 2008 Free Software Foundation, Inc.
+;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
 ;; This program 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 3 of the License, or
@@ -24,6 +24,10 @@
 ;;
 ;;(myserver-control-end my-proc)
 
+(defvar myserver-control-user "ADMIN")
+(defvar myserver-control-password "ADMIN")
+(defvar myserver-control-connection "Keep-Alive")
+
 (defun myserver-control-init (server port)
   "Initialize a connection to the specified SERVER using the PORT."
   (when (get-buffer "myserver") (kill-buffer "myserver"))
@@ -33,38 +37,53 @@
                                        port)))
     (starttls-negotiate proc)
     proc))
-    
 
+
 (defun myserver-control-send-message (proc msg)
   "Send the message MSG to the specified PROC and
    return the response header lines."
   (process-send-string proc msg)
-  
+      
   (with-current-buffer "myserver"
-    (let ((i 3) 
+    (let ((i 3)
           (np 0)
           (start 0)
           (curr nil)
+          (len 0)
           (res '()))
       (while (and (> i 0)
                   (not (search-forward "\r\n\r\n" nil t)))
+        (goto-char (point-max))
         (myserver-control-wait-for-data my-proc 2)
         (setq i (1- i))
-        (goto-char (point-min))  )
-      
+        (goto-char (point-min)))
+
+      (set-buffer "myserver")
       (goto-char (point-min))
-      (setq res (append res (list (buffer-substring 2 (- (search-forward "\r\n" nil t) 2)))))
-      
+      (setq res (list (buffer-substring 2 (- (search-forward "\r\n" nil t) 2))))
+
       (while (progn
                (setq start (1+ (point)))
                (setq np (- (search-forward "\r" nil t) 1))
                (setq curr (buffer-substring start np))
                (> (- np start) 4))
         
-        (setq res (append res (list curr)))) 
-      res
-      )))
+        (setq res (append res (list curr))))
+      
+      (setq res (mapcar 'split-string res))
+      (setq len (string-to-number (cadr (assoc "LEN" res))))
 
+      (setq i 3)
+      (while (and (> i 0)
+                  (< len (- (buffer-size) np)))
+        (goto-char (point-max))
+        (myserver-control-wait-for-data my-proc 2)
+        (setq i (1- i)))
+
+
+      (list res (buffer-substring (+ np 2) (+ 1 (buffer-size)))))))
+
+
 (defun myserver-control-wait-for-data (proc sec)
   "Read data from the process using a timeout of SEC seconds."
   (accept-process-output proc sec 0))
@@ -74,4 +93,23 @@
   (delete-process proc))
 
 
+(defun myserver-control-show-connections (proc)
+  (let ((r (myserver-control-send-message proc (concat "/SHOWCONNECTIONS CONTROL/1.0 \r\n"
+                                                       "/AUTH " myserver-control-user ":" myserver-control-password "\r\n"
+                                                       "/CONNECTION " myserver-control-connection "\r\n\r\n"))))
+    
+    (mapcar (lambda (x) (split-string x " - ")) (split-string (cadr r) "\r\n"))))
+
+
+(defun myserver-control-reboot (proc)
+  (myserver-control-send-message proc (concat "/REBOOT CONTROL/1.0 \r\n"
+                                              "/AUTH " myserver-control-user ":" myserver-control-password "\r\n"
+
+                                              "/CONNECTION " myserver-control-connection "\r\n\r\n")))
+(defun myserver-control-version (proc)
+  (cadr (myserver-control-send-message proc (concat "/VERSION CONTROL/1.0 \r\n"
+                                                    "/AUTH " myserver-control-user ":" myserver-control-password "\r\n"
+                                                    "/CONNECTION " myserver-control-connection "\r\n\r\n"))))
+
+
 (provide 'myserver-control)





