Changeset - 7b322e60270c
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 17 years ago 2009-03-16 23:11:35
ohnobinki@ohnopublishing.net
a few polishes to execio

maybe we're ready to start working on:
common configfile setup
and then remoteio, which uses the configfile
2 files changed with 33 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/common/execio.c
Show inline comments
 
@@ -23,12 +23,13 @@
 
#include <sys/types.h>
 
#include <sys/wait.h>
 
#include <signal.h>
 
#include <malloc.h>
 
#include <fcntl.h>
 
#include <stdio.h>
 
#include <errno.h>
 

	
 
int execio_open(struct execio **rem, const char *progname, char *const argv[])
 
{
 
  /* pipe used to write to child */
 
  int pipe_write[2];
 
  /* pipe used to read from child */
 
@@ -179,18 +180,36 @@ int execio_read(struct execio *eio, void
 

	
 
  return 0;
 
}
 

	
 
int execio_write(struct execio *eio, void *buf, size_t len, size_t *bytesread)
 
{
 
  /*
 
    TODO: errno handling
 
   */
 
  errno = 0;
 
  (*bytesread) = write(eio->pipe_write, buf, len);
 
  if(!*bytesread)
 
    {
 
      switch(errno)
 
	{
 
	case EPIPE:
 
	  /* 
 
	     the program closed the pipe (died)
 
	  */
 
	fprintf(stderr, "execio_write: the child program closed its stdin pipe\n");
 
	eio->state = EXECIO_STATE_EOF;
 
	break;
 
	
 
	default:
 
	  fprintf(stderr, "execio_write: unhandled error writing to an fd: \n");
 
	  perror("write");
 
    eio->state = EXECIO_STATE_ERROR;
 
	  break;
 
	  
 
	}
 
      
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 

	
 
enum execio_state execio_state(struct execio *eio)
 
@@ -205,12 +224,16 @@ int execio_close(struct execio *eio)
 

	
 
  close(eio->pipe_read);
 
  close(eio->pipe_write);
 

	
 
  /* maybe we should just kill the child */
 
  kill(eio->child, SIGTERM);
 
  /* the waitpid(2) seems to indicate that only when the child is terminated will this wait return. */
 
  /* 
 
     the waitpid(2) seems to indicate that only when the child is terminated will this wait return. 
 
     This are of code will probably need improving - the ability to seng SIGKILL after a timeout? So we'll output a debug line before running waitpid
 
  */
 
  fprintf(stderr, "execio_close: running waitpid\n");
 
  waitpid(eio->child, &childstatus, 0);
 
  
 
  return 0;
 
}
 

	
src/common/execio.h
Show inline comments
 
@@ -54,13 +54,18 @@ int execio_open(struct execio **rem, con
 
   doesn't block,
 
   returns 0 on success, 1 on failure
 
*/
 
int execio_read(struct execio *eio, void *buf, size_t len, size_t *bytesread);
 
int execio_write(struct execio *eio, void *buf, size_t len, size_t *bytesread);
 

	
 
/*
 
  use this function to determine if the using program should keep trying to read/write
 
 */
 
enum execio_state execio_state(struct execio *eio);
 

	
 
/* nonzero return on error */
 
/* 
 
   nonzero return on error 
 
*/
 
int execio_close(struct execio *eio);
 

	
 
#endif
 

	
0 comments (0 inline, 0 general)