Changeset - 510b082d50ae
[Not reviewed]
default
0 3 0
Nathan Brink (binki) - 17 years ago 2009-02-23 21:38:50
ohnobinki@ohnopublishing.net
improvements to comments, calling execio_open, and verbosity of execio.c
3 files changed with 14 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/client/distren.c
Show inline comments
 
@@ -10,35 +10,39 @@
 

	
 
  DistRen 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 Affero General Public License for more details.
 

	
 
  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 <stdio.h> /* sprintf, printf */
 
#include <stdlib.h> /* malloc, free */
 

	
 
#include "options.h"
 
#include "execio.h"
 

	
 
int main(int argc, char *argv[])
 
{
 
  struct execio *testrem;
 
  char *execargv[] = 
 
    {
 
      "ssh",
 
      "protofusion.org",
 
      "echo",
 
      "hello from protofusion.org"
 
      "sh",
 
      "-c",
 
      "\"echo hello from ${HOSTNAME} 1>&2\"",
 
      (char *)NULL
 
    };
 

	
 
  fprintf(stderr, "testing execio (It shouldn't work) :-)\n");
 
  
 
  fprintf(stderr, "execio_open returns %d\n", execio_open(&testrem, "ssh", execargv));
 

	
 
  return 0;
 
};
 

	
 
/* this function should probably not exist. asprintf should be used instead of sprintf */
 
size_t intstrlen(size_t theint)
 
{
src/common/execio.c
Show inline comments
 
@@ -31,24 +31,25 @@ int execio_open(struct execio **rem, con
 
  /* 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;
 
  
 
  int counter;
 
  int counter2;
 
  int maxfds;
 

	
 
  /* 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;
 
    }
 
  
 
  
 
@@ -102,33 +103,34 @@ int execio_open(struct execio **rem, con
 
      dup2(pipe_write[0], 1);
 
      /* 
 
	 close all other file descriptors. We want to keep 0, 1, and 2 open. We don't know that the last open() or pipe() always gives the highest fd number. However, I will assume that it does. Maybe this is a bad idea:
 
       */
 
      counter = pipe_write[0];
 
      if(counter < pipe_write[1])
 
	counter = pipe_write[1];
 
      if(counter < pipe_read[0])
 
	counter = pipe_read[0];
 
      if(counter < pipe_read[1])
 
	counter = pipe_read[1];
 
      counter2 = 0;
 
      maxfds = counter;
 
      while(counter > 2)
 
	{
 
	  if(!close(counter))
 
	    counter2 ++; /* record how many descriptors we still had open :-) */
 
	  counter --;
 
	}
 
      
 
      /* stderr is the only stream we haven't confiscated atm - just for fun - I will confiscate it later, though, to support parsing error messages */
 
      fprintf(stderr, "closed %d fds before execing \"%s\"\n", counter2, progname);
 
      fprintf(stderr, "closed %d/%d fds before execing \"%s\"\n", counter2, maxfds, progname);
 

	
 
      /*
 
	now exec: execvp uses interpreter to find the file to exec
 
       */
 
      execvp(progname, argv);
 

	
 
      return 1; /* this line should never be reached because we exec -- unless if the exec returns something bad. Then we'd have to tell execio over the pipe about that somehow... */
 
    }
 
}
 

	
 

	
 
size_t execio_read(struct execio *eio, void *buf, size_t len)
src/common/execio.h
Show inline comments
 
@@ -32,25 +32,29 @@ enum execio_state
 
    EXECIO_STATE_EOF
 
  };
 
  
 

	
 
struct execio
 
{
 
  int pipe_write;
 
  int pipe_read;
 
  
 
  pid_t child;
 
};
 

	
 
/* nonzsero return on error */
 
/*
 
  runs progname with the arguments in argv. argv must be null terminated!!!!!!!!!
 

	
 
  returns nonzsero return on error 
 
*/
 
int execio_open(struct execio **rem, const char *progname, char *const argv[]);
 

	
 
/* 
 
   blocks, 
 
   returns 0 if len is 0. Otherwise, only returns 0 on error/EOF: use execio_state() to determine
 
*/
 
size_t execio_read(struct execio *eio, void *buf, size_t len);
 
size_t execio_write(struct execio *eio, void *buf, size_t len);
 

	
 
enum execio_state execio_state(struct execio *eio);
 

	
 
/* nonzero return on error */
0 comments (0 inline, 0 general)