Changeset - efaa36850d87
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 17 years ago 2009-02-21 00:44:19
ohnobinki@ohnopublishing.net
most of openning a command is finished
2 files changed with 73 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/common/remoteio.c
Show inline comments
 
@@ -16,31 +16,95 @@
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "remoteio.h"
 

	
 
#include <unistd.h>
 
#include <sys/types.h>
 
#include <sys/wait.h>
 
#include <signal.h>
 
#include <malloc.h>
 

	
 
int remoteio_open(const char *spec, struct remoteio **rem)
 
{
 
  /* pipe used to write to child */
 
  int pipe_write[2];
 
  /* pipe used to read from child */
 
  int pipe_read[2];
 

	
 
  pid_t child;
 

	
 
  /* for wait(2) if needed */
 
  int childstatus; 
 

	
 
  /* create two pipes to facilitate communication with child */
 
  if(pipe(pipe_write))
 
    return 1;
 
  if(pipe(pipe_read));
 
  {
 
    close(pipe_write[0]);
 
    close(pipe_write[1]);
 
    return 1;
 
  }
 
  
 
  /* parent */
 
  child = fork();
 
  if(child == -1)
 
    {
 
      close(pipe_write[0]);
 
      close(pipe_write[1]);
 
      close(pipe_read[0]);
 
      close(pipe_read[1]);
 
      return 1;
 
    }
 
  if(child)
 
    {
 
      /* setup remoteio struct */
 
      (*rem) = malloc(sizeof(struct remoteio));
 
      if(!(*rem))
 
	{
 
	  /* we should tell the child we're dead - use wait and close our end of the pipes! */
 
	  close(pipe_write[1]);
 
	  close(pipe_read[0]);
 
	  /* we should probably pass of the wait() call to a thread that just does boring things like that. Especially for when the server tries to connect to other servers... */
 
	  /* maybe we should just kill the child */
 
	  kill(child, SIGTERM);
 
	  /* the waitpid(2) seems to indicate that only when the child is terminated will this wait return. */
 
	  waitpid(child, &childstatus, 0); 
 
	}
 
      (*rem)->pipe_write = pipe_write[1];
 
      (*rem)->pipe_read = pipe_read[0];
 
      (*rem)->child = child;
 
      
 
      return 0;
 
    }
 
  
 
  /* child */
 
  else
 
    {
 
      
 

	
 
      return 1; /* this line should never be reached because we exec*/
 
    }
 
}
 

	
 

	
 
size_t remoteio_read(struct remoteio *rem, void *buf, size_t len)
 
{
 
  
 
  return 0;
 
}
 

	
 
size_t remoteio_write(struct remoteio *rem, void *buf, size_t len)
 
{
 
  
 
  return 0;
 
}
 

	
 

	
 
int remoteio_close(struct remoteio *rem)
 
{
 
  
 
  return 0;
 
}
 

	
 

	
 
#endif
 

	
src/common/remoteio.h
Show inline comments
 
@@ -15,22 +15,26 @@
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef _DISTREN_REMOTEIO_H
 
#defien _DISTREN_REMOTEIO_H
 
#define _DISTREN_REMOTEIO_H
 

	
 
/*
 
  This file tries to abstract away getting a socket/fd that talks to a remote service
 
 */
 

	
 
#include <unistd.h>
 

	
 
struct remoteio
 
{
 
  int fd;
 
  int pipe_write;
 
  int pipe_read;
 
  
 
  pid_t child;
 
};
 

	
 
/* nonzsero return on error */
 
int remoteio_open(const char *spec, struct remoteio **rem);
 

	
 
/* blocks, returns 0 if len is 0 or on error */
0 comments (0 inline, 0 general)