[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [2986] Now the ForkServer uses a SocketPair to communi
From: |
Giuseppe Scrivano |
Subject: |
[myserver-commit] [2986] Now the ForkServer uses a SocketPair to communicate with the main process. |
Date: |
Sat, 31 Jan 2009 17:02:58 +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
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [2986] Now the ForkServer uses a SocketPair to communicate with the main process.,
Giuseppe Scrivano <=